处理回归问题是人工智能的一项能力,这篇文章将使用tensorflow处理一个简单的多元线性回归问题:波士顿房价预测项目。
1.导入库
import tensorflow as tf from tensorflow import keras import numpy as np from sklearn import preprocessing import pandas as pd import matplotlib.pyplot as plt
其中pandas与matplotlib用于绘图
2.导入与处理数据
def deal_file(file): data_list_text = file.readlines() data_list = [] for i in data_list_text: data_list.append(list(map(float, i.split(',')))) data_list = np.asarray(data_list, float) return data_list
data=deal_file(open('in.txt')) label=deal_file(open('out.txt')) test=deal_file(open('test.txt')) testsplit=np.split(test,[0,13],axis=1) testx=testsplit[1] testy=testsplit[2] print('prompt: data load finished')
这里我使用的数据:仓库链接
3.对数据进行预处理
def scale(x): return preprocessing.StandardScaler().fit_transform(x) data=scale(data) label=scale(label) testx=scale(testx) testy=scale(testy)
4.建立神经网络模型
model=tf.keras.Sequential([ keras.layers.Dense(16,input_shape=([13]),activation='relu'), keras.layers.Dense(32,activation='relu'), keras.layers.Dense(1) ])
relu为回归模型常用的激活函数
model.summary()
Model: "sequential"
_____________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 16) 224
_____________________________________________________
dense_1 (Dense) (None, 32) 544
_____________________________________________________
dense_2 (Dense) (None, 1) 33
=================================================================
Total params: 801
Trainable params: 801
Non-trainable params: 0
_____________________________________________________
由于样本数量比较小,所以我们使用神经元数量相对较少的三层神经网络
5.设置回调
class PrintDot(keras.callbacks.Callback): def on_epoch_end(self, epoch, logs): if epoch % 100 == 0: print('') print('.', end='') early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=10)
适当设置回调可以防止过拟合,PrintDot可以简化神经网络在训练时的输出
6.训练模型
model.compile(optimizer=tf.keras.optimizers.RMSprop(0.001),loss='mse',metrics=['mae','mse']) history=model.fit( data,label,epochs=200, validation_split = 0.2, verbose=0, callbacks=[early_stop,PrintDot()] )
RMSProp算法是AdaGrad算法的一种改进,常用于回归模型,将训练结果存储在history中便于后续查看训练情况
7.训练过程分析
def plot_history(history): hist = pd.DataFrame(history.history) hist['epoch'] = history.epoch plt.figure() plt.xlabel('Epoch') plt.ylabel('Mean Abs Error') plt.plot(hist['epoch'], hist['mae'],label='Train Error') plt.plot(hist['epoch'], hist['val_mae'],label = 'Val Error') plt.ylim([0,1]) plt.legend() plt.show()
plot_history(history)

8.训练结果分析
predict=model.predict(testx).flatten()
plt.scatter(testy, predict) plt.xlabel('True Values') plt.ylabel('Predictions') plt.axis('equal') plt.axis('square') plt.xlim([0,plt.xlim()[1]]) plt.ylim([0,plt.ylim()[1]]) _ = plt.plot([-100, 100], [-100, 100])

error = predict - testy plt.hist(error) plt.xlabel("Prediction Error") _ = plt.ylabel("Count")

从图中可以看出,虽然有异常数据,但误差基本符合高斯分布