卷积神经网络进行Boston房价预测

您所在的位置:网站首页 基于keras的波士顿房价预测 卷积神经网络进行Boston房价预测

卷积神经网络进行Boston房价预测

2024-02-22 05:07| 来源: 网络整理| 查看: 265

Boston房价预测问题常采用多元线性回归,或者采用前馈神经网络,我这篇博客是采用卷积神经网络的,就是想看一下,卷积神经网络对此类问题的效果。卷积神经网络(Convolutional Neural Networks, CNN)是一类包含卷积计算且具有深度结构的前馈神经网络(Feedforward Neural Networks),是深度学习(deep learning)的代表算法之一 。卷积神经网络具有表征学习(representation learning)能力,能够按其阶层结构对输入信息进行平移不变分类(shift-invariant classification),因此也被称为“平移不变人工神经网络(Shift-Invariant Artificial Neural Networks, SIANN)”。卷积层模拟了人类的视感知,即局部感知功能。卷积层就是一种特征的提取方式。

一般处理图像的时候,优先考虑使用卷积神经网络,因为图像数据维度太高,没办法用全连接的神经网络,所以才想到使用卷积层。

那么卷积层和全连接层有什么区别呢?

conv只建模区域输入间的联系,只是利用小的区域信息得到更高层的抽象。然后通过多层的conv+pooling+fc建立全局关联。你可以把它理解为民主制度,就像“人大代表制度”,通过层层选出人大代表,得到最终决策。

而全连接层是对全局信息进行建模,考虑了全局关联,用全局信息得到更高层的抽象。

所以说,conv是fc的妥协,用为fc会导致网络的参数太多,内存不够,网络深度当然也不深,因为深度越大,会导致隐层的节点数也越多,非线性的表达能力也就越强。

下面我就试一下卷积神经网络的处理一维数据的能力。

代码如下:

#2021.11.11 HIT ATCI LZH #一维卷积神经网络实现波士顿房价预测,数据集为boston房价与周边环境等因素,参照网上的例子 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt #加载boston房价数据集 boston = tf.contrib.learn.datasets.load_dataset('boston') X_train, Y_train = boston.data, boston.target #==============================对数据集的格式进行解析======================================= #boston数据集的X为506行,13列,分别为影响房价的诸多因素,Y为506行,1列,即房价 print('数据加载成功!') print('boston.type is',type(boston)) print('X_train.type =', type(X_train)) print('X_train.ndim =',X_train.ndim) print('X_train.shape =',X_train.shape) print('X_train.dtype =',X_train.dtype) print('X_train 的行数 m = {0}, 列数 n = {1}'.format(X_train.shape[0],X_train.shape[1])) print('Y_train.type =', type(Y_train)) print('Y_train.ndim =',Y_train.ndim) print('Y_train.shape =',Y_train.shape) print('Y_train 的行数 m = {0}, 列数 n = None'.format(Y_train.shape[0])) print('Y_train.dtype =',Y_train.dtype) #========================================================================================== #定义归一化函数 def normalize(X): mean = np.mean(X) #均值 std = np.std(X) #默认计算每一列的标准差 X = (X - mean)/std return X X_train = normalize(X_train) #对输入变量按列进行归一化 #==============================网络参数===================================================== #parameters超参数 learning_rate = 0.001 training_iters = 500 #训练次数 batch_size = 10 #批训练样本大小 display_step = 10 #打印训练结果的Iter的步长 #Network Parameters n_input = 13 #X_train的列数 #========================================================================================== #为训练数据申明占位符 x = tf.placeholder(tf.float32,[None,n_input]) y = tf.placeholder(tf.float32) #keep_prob = tf.placeholder(tf.float32) #keep_prob用于激活某些神经元,防止过拟合 #定义一个输入为x,权值为w,偏置为b,给定步幅的卷积层,激活函数是ReLu,padding设为SAMEM模式 def conv2d(x, w, b, strides=1): x = tf.nn.conv2d(x, w, strides = [1, strides, strides, 1], padding = 'SAME') x = tf.nn.bias_add(x,b) return tf.nn.relu(x) #定义一个输入是x的maxpool层,卷积核为ksize并且padding为SAME def maxpool2d(x, k=2): return tf.nn.max_pool(x, ksize = [1, k, k, 1],strides = [1, k, k, 1], padding = 'SAME') #定义卷积神经网络,其构成是两个卷积层,一个droup层,最后是输出层 def conv_net(x, weights, biases): #reshape the input picture x = tf.reshape(x, shape = [-1, 13, 1, 1])#将输入数据变为4-D张量 #First convolution layer conv1 = conv2d(x, weights['wc1'], biases['bc1']) fc1 = tf.reshape(conv1, [-1, weights['wd1'].get_shape().as_list()[0]]) #Fully connected layer fc1 = tf.add(tf.matmul(fc1, weights['wd1']),biases['bd1']) fc1 = tf.nn.relu(fc1) #Fully connected layer fc2 = tf.add(tf.matmul(fc1, weights['wd2']),biases['bd2']) fc2 = tf.nn.relu(fc2) #output the class prediction out = tf.add(tf.matmul(fc1,weights['out']),biases['out']) return out #定义网络层的权重和偏置,第一个conv层有一个5*5的卷积核,一个输入和32个输出。第二个 #conv层有1个5*5的卷积核,32个输入和64个输出。全连接层有1024个输入和10个输出对应于最后 #的数字数目。所有的权重和偏置用randon_normal分布完成初始化: weights = { #5*5 conv ,1 input, and 32 outputs 'wc1':tf.Variable(tf.random_normal([5, 5, 1, 32])), # fully connected, 13*32 inputs, and 128 outputs 'wd1':tf.Variable(tf.random_normal([13*32, 128])), 'wd2':tf.Variable(tf.random_normal([128, 128])), # 128 inputs, 10 outputs for class digits 'out':tf.Variable(tf.random_normal([128,1])) } biases = { 'bc1':tf.Variable(tf.random_normal([32])), 'bd1':tf.Variable(tf.random_normal([128])), 'bd2':tf.Variable(tf.random_normal([128])), 'out':tf.Variable(tf.random_normal([1])) } #建立一个给定权重和偏置的convnet。定义均方根误差的损失函数,并用Adam优化器进行损失最小化。 #优化后,计算精度: pred = conv_net(x, weights, biases) cost = tf.reduce_mean(tf.square(y - pred))#损失函数,均方误差 optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(cost) init_op = tf.global_variables_initializer() #启动计算图,并迭代train_iterats次,其中每次输入batch_size个数据进行优化,请注意,用从mnist数据集分离出的 #mnist.train数据进行训练,每进行display_step次迭代,会计算当前的精度,最后,在2048个测试图片上计算精度, #此时无dropout total = []#定义一个空列表,用于存储每一次Epoch的误差 with tf.Session() as sess: sess.run(init_op)#初始化变量 for i in range(training_iters): _, l = sess.run([optimizer, cost],feed_dict = {x:X_train, y:Y_train}) total.append(l) print('Epoch {0}: Loss {1}'.format(i, l)) #绘制损失函数 plt.figure(num=1) plt.title('loss curve') plt.xlabel('Epoch', color = 'red') plt.ylabel('loss', color = 'blue') plt.plot(total) plt.show()

输出:

Epoch 490: Loss 108.11799621582031 Epoch 491: Loss 108.05949401855469 Epoch 492: Loss 108.00202941894531 Epoch 493: Loss 107.94499969482422 Epoch 494: Loss 107.88826751708984 Epoch 495: Loss 107.83196258544922 Epoch 496: Loss 107.77583312988281 Epoch 497: Loss 107.71996307373047 Epoch 498: Loss 107.66434478759766 Epoch 499: Loss 107.60884857177734

 下面对网络结构进行修改,只保留全连接层,网络结构如下:

输入层:13 、隐层1: 128、隐层2:128、输出层:1

#2021.11.11 HIT ATCI LZH #FNN前馈神经网络实现波士顿房价预测,数据集为boston房价与周边环境等因素,参照网上的例子,输入层:13 、隐层1: 128、隐层2:128、输出层:1 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt #加载boston房价数据集 boston = tf.contrib.learn.datasets.load_dataset('boston') X_train, Y_train = boston.data, boston.target #==============================对数据集的格式进行解析======================================= #boston数据集的X为506行,13列,分别为影响房价的诸多因素,Y为506行,1列,即房价 print('数据加载成功!') print('boston.type is',type(boston)) print('X_train.type =', type(X_train)) print('X_train.ndim =',X_train.ndim) print('X_train.shape =',X_train.shape) print('X_train.dtype =',X_train.dtype) print('X_train 的行数 m = {0}, 列数 n = {1}'.format(X_train.shape[0],X_train.shape[1])) print('Y_train.type =', type(Y_train)) print('Y_train.ndim =',Y_train.ndim) print('Y_train.shape =',Y_train.shape) print('Y_train 的行数 m = {0}, 列数 n = None'.format(Y_train.shape[0])) print('Y_train.dtype =',Y_train.dtype) #========================================================================================== #定义归一化函数 def normalize(X): mean = np.mean(X) #均值 std = np.std(X) #默认计算每一列的标准差 X = (X - mean)/std return X X_train = normalize(X_train) #对输入变量按列进行归一化 #==============================网络参数===================================================== #parameters超参数 learning_rate = 0.001 training_iters = 500 #训练次数 batch_size = 10 #批训练样本大小 display_step = 10 #打印训练结果的Iter的步长 #Network Parameters n_input = 13 #X_train的列数 #========================================================================================== #为训练数据申明占位符 x = tf.placeholder(tf.float32,[None,n_input]) y = tf.placeholder(tf.float32) #keep_prob = tf.placeholder(tf.float32) #keep_prob用于激活某些神经元,防止过拟合 #定义一个输入为x,权值为w,偏置为b,给定步幅的卷积层,激活函数是ReLu,padding设为SAMEM模式 #定义卷积神经网络,其构成是两个卷积层,一个droup层,最后是输出层 def fnn_net(x, weights, biases): fc1 = tf.add(tf.matmul(x, weights['wd1']),biases['bd1']) fc1 = tf.nn.relu(fc1) #Fully connected layer fc2 = tf.add(tf.matmul(fc1, weights['wd2']),biases['bd2']) fc2 = tf.nn.relu(fc2) #output the class prediction out = tf.add(tf.matmul(fc1,weights['out']),biases['out']) return out #定义网络层的权重和偏置,第一个conv层有一个5*5的卷积核,一个输入和32个输出。第二个 #conv层有1个5*5的卷积核,32个输入和64个输出。全连接层有1024个输入和10个输出对应于最后 #的数字数目。所有的权重和偏置用randon_normal分布完成初始化: weights = { # fully connected, 13*32 inputs, and 128 outputs 'wd1':tf.Variable(tf.random_normal([13, 128])), 'wd2':tf.Variable(tf.random_normal([128, 128])), # 128 inputs, 1 outputs for class digits 'out':tf.Variable(tf.random_normal([128,1])) } biases = { 'bd1':tf.Variable(tf.random_normal([128])), 'bd2':tf.Variable(tf.random_normal([128])), 'out':tf.Variable(tf.random_normal([1])) } #建立一个给定权重和偏置的convnet。定义均方根误差的损失函数,并用Adam优化器进行损失最小化。 #优化后,计算精度: pred = fnn_net(x, weights, biases) cost = tf.reduce_mean(tf.square(y - pred))#损失函数,均方误差 optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(cost) init_op = tf.global_variables_initializer() #启动计算图,并迭代train_iterats次,其中每次输入batch_size个数据进行优化,请注意,用从mnist数据集分离出的 #mnist.train数据进行训练,每进行display_step次迭代,会计算当前的精度,最后,在2048个测试图片上计算精度, #此时无dropout total = []#定义一个空列表,用于存储每一次Epoch的误差 with tf.Session() as sess: sess.run(init_op)#初始化变量 for i in range(training_iters): _, l = sess.run([optimizer, cost],feed_dict = {x:X_train, y:Y_train}) total.append(l) print('Epoch {0}: Loss {1}'.format(i, l)) #绘制损失函数 plt.figure(num=1) plt.title('loss curve') plt.xlabel('Epoch', color = 'red') plt.ylabel('loss', color = 'blue') plt.plot(total) plt.show()

输出:

Epoch 490: Loss 86.8122787475586 Epoch 491: Loss 86.80827331542969 Epoch 492: Loss 86.80428314208984 Epoch 493: Loss 86.80030822753906 Epoch 494: Loss 86.79631805419922 Epoch 495: Loss 86.79234313964844 Epoch 496: Loss 86.78837585449219 Epoch 497: Loss 86.78440856933594 Epoch 498: Loss 86.78043365478516 Epoch 499: Loss 86.77648162841797

前几次的epoch,fnn就优于cnn,收敛后,fnn的Loss还是要优于cnn,通过这个简单的例子,我们也能看到,对于一维数据而言,cnn的优势并不明显。 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3