导入相关的包和加载数据
import tensorflow as tf
import matplotlib.pylab as plt
import numpy as np
boston_house = tf.keras.datasets.boston_housing
(train_x, train_y), (test_x, test_y) = boston_house.load_data(test_split=0.1)
输入数据归一化
train_x = (train_x - train_x.min(axis=0)) / (train_x.max(axis=0) - train_x.min(axis=0))
test_x = (test_x - test_x.min(axis=0)) / (test_x.max(axis=0) - test_x.min(axis=0))
train_x = tf.cast(train_x, tf.float32)
test_x = tf.cast(test_x, tf.float32)
输出数据转换为列向量
train_y = train_y.reshape(-1, 1)
test_y = test_y.reshape(-1, 1)
初始化参数
w = tf.Variable(tf.random.truncated_normal([13, 1], stddev=0.01, seed=1))
b = tf.Variable(tf.random.truncated_normal([1, 1], stddev=0.01, seed=1))
loss_list = []
lr = 0.01
for i in range(50000):
with tf.GradientTape() as tape:
pred = tf.matmul(train_x, w) + b
loss = tf.reduce_mean(tf.square(train_y - pred))
loss_list.append(loss)
grad = tape.gradient(loss, [w, b])
w.assign_sub(lr * grad[0])
b.assign_sub(lr * grad[1])
if i % 1000 == 0:
print('i: %d, loss:%f' % (i, loss))
pre_test_y = tf.matmul(test_x, w) + b
pre_test_y = pre_test_y.numpy()
可视化
plt.figure("train_loss")
plt.title("train_loss")
plt.plot(loss_list, label='train_loss')
plt.legend()
plt.show()
plt.figure('predict')
plt.title("predict")
plt.xlabel("ground truth")
plt.ylabel("infer result")
x = np.arange(1, 50)
y = x
plt.plot(x, y)
plt.scatter(test_y, pre_test_y, color="green", label="Test")
plt.grid()
plt.legend()
plt.show()
迭代过程中轮次与Loss的数据如下:
i: 0, loss:584.958679 i: 1000, loss:33.152821 i: 2000, loss:26.689535 i: 3000, loss:24.490910 i: 4000, loss:23.541544 i: 5000, loss:23.036236 i: 6000, loss:22.716373 i: 7000, loss:22.488619 i: 8000, loss:22.315008 i: 9000, loss:22.177662 i: 10000, loss:22.066746 i: 11000, loss:21.976049 i: 12000, loss:21.901287 i: 13000, loss:21.839272 i: 14000, loss:21.787601 i: 15000, loss:21.744373 i: 16000, loss:21.708096 i: 17000, loss:21.677565 i: 18000, loss:21.651814 i: 19000, loss:21.630049 i: 20000, loss:21.611616 i: 21000, loss:21.595987 i: 22000, loss:21.582718 i: 23000, loss:21.571444 i: 24000, loss:21.561855 i: 25000, loss:21.553692 i: 26000, loss:21.546738 i: 27000, loss:21.540808 i: 28000, loss:21.535749 i: 29000, loss:21.531433 i: 30000, loss:21.527750 i: 31000, loss:21.524603 i: 32000, loss:21.521917 i: 33000, loss:21.519627 i: 34000, loss:21.517666 i: 35000, loss:21.515989 i: 36000, loss:21.514557 i: 37000, loss:21.513329 i: 38000, loss:21.512283 i: 39000, loss:21.511389 i: 40000, loss:21.510618 i: 41000, loss:21.509964 i: 42000, loss:21.509403 i: 43000, loss:21.508924 i: 44000, loss:21.508514 i: 45000, loss:21.508160 i: 46000, loss:21.507860 i: 47000, loss:21.507603 i: 48000, loss:21.507381 i: 49000, loss:21.507193 Process finished with exit code 0
可视化结果如下:
|