TensorFlow 实现多元线性回归

多元线性回归:y=w1*x1+w2*x2+w3*x3...wn*xn+bias

本文使用TensorFlow 2.0+ 训练多元线性回归模型,具体步骤:

1、随机生成训练数据和验证数据,其中训练数据中附加随机噪声

2、构建模型

3、编译、训练模型

4、绘制训练损失和验证损失

代码如下:

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential

print("tensorflow.version=" + tf.__version__)
# tensorflow.version=2.1.0

# 设置随机种子,确保多次执行代码,生成的随机数不变
# 消除数据变更的干扰,以便调参优化看效果
np.random.seed(0)

# 随机生成训练数据
trainX = np.random.random((1000, 5))
bias = 0.25
weights = []
for i in range(5):
    weights.append([i + 1])
# 附加噪声数据
noise = np.random.normal(0, 0.01, size=(1000, 1))
trainY = np.dot(trainX, weights) + bias + noise

# 随机生成验证数据
vaildX = np.random.random((200, 5))
vaildY = np.dot(vaildX, weights) + bias

print("trainX.shape=", trainX.shape,
      "trainY.shape=", trainY.shape,
      "validX.shape=", vaildX.shape,
      "vaildY.shape=", vaildY.shape, )
# trainX.shape= (1000, 5)
# trainY.shape= (1000, 1)
# validX.shape= (200, 5)
# vaildY.shape= (200, 1)

# 构建模型
model = Sequential()
model.add(Dense(units=1, input_dim=5, use_bias=True))
print(model.summary())
# Model: "sequential"
# _________________________________________________________________
# Layer (type)                 Output Shape              Param #
# =================================================================
# dense (Dense)                (None, 1)                 6
# =================================================================
# Total params: 6
# Trainable params: 6
# Non-trainable params: 0
# _________________________________________________________________
# None

# 编译、训练
model.compile(loss='mse', optimizer='SGD')
history = model.fit(trainX, trainY, batch_size=20, epochs=100, validation_data=(vaildX, vaildY))

W, B = model.layers[0].get_weights()
print("Weight=", W)
# Weight= [[0.9696301]
#  [1.9690223]
#  [2.9694655]
#  [3.973977 ]
#  [4.9692006]]
print("Bias=", B)
# Bias= [0.32691073]

# 绘制训练和验证损失
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(loss) + 1)

plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.xticks(np.arange(0, 100, step=5))
plt.plot(epochs, loss, 'r', label='Train Loss')
plt.plot(epochs, val_loss, 'b', label='Validate Loss')
plt.legend()
plt.show()

从图中可以看出,当Epochs 达到 20 以后,训练和验证损失都趋于稳定

因此在当前参数下,只需要训练20轮左右即可,后续训练带来的收益不高,性价比不高。

以上就是TensorFlow 用于训练多元线性回归基本步骤,后续可以通过以下方式进行优化模型:

(1)增加网络模型的层数

(2)选择合适的优化器,并调整其参数(如:学习率)





个人资料
时海
等级:8
文章:272篇
访问:16.0w
排名: 2
上一篇: 线程池
下一篇:科技周报20200907
标签: shape、loss、plt、tensorflow、np、面试题
隐藏