构建一个回归分析的实例模型,目标是实现线性回归模型:y = wx + b 。
import numpy as np
import matplotlib.pyplot as plt
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
### Building a regression model
# 建立计算图
## define a graph
g = tf.Graph()
## define the computation graph
with g.as_default():
## placeholders
tf.set_random_seed(123)
tf_x = tf.placeholder(shape=(None),
dtype=tf.float32,
name='tf_x')
print(tf_x)
tf_y = tf.placeholder(shape=(None),
dtype=tf.float32,
name='tf_y')
print(tf_y)
## define the variable (model parameters)
# tf.random_normal()函数用于从“服从指定正态分布的序列”中随机取出指定个数的值
# stddev: 正态分布的标准差,默认为1.0
weight = tf.Variable(
tf.random_normal(shape=(1, 1), stddev=0.25), name='weight')
bias = tf.Variable(0.0, name='bias')
## build the model
y_hat = tf.add(weight * tf_x, bias,
name='y_hat')
print(y_hat)
## compute the cost
cost = tf.reduce_mean(tf.square(tf_y - y_hat),
name='cost')
print(cost)
## train
optim = tf.train.GradientDescentOptimizer(
learning_rate=0.001)
train_op = optim.minimize(cost, name='train_op')
# 生成随机回归数据,并调用make_random_data函数来实现其可视化
np.random.seed(0)
def make_random_data():
# 从一个均匀分布[low,high)中随机采样,注意定义域是左闭右开,即包含low,不包含high
x = np.random.uniform(low=-2, high=4, size=200)
y = []
for t in x:
# np.random.normal()的意思是一个正态分布,normal这里是正态的意思。
# 如:numpy.random.normal(loc=0,scale=1e-2,size=shape) ,意义如下:
# 参数loc(float):正态分布的均值,对应着这个分布的中心。loc=0说明这一个以Y轴为对称轴的正态分布,
# 参数scale(float):正态分布的标准差,对应分布的宽度,scale越大,正态分布的曲线越矮胖,scale越小,曲线越高瘦。
# 参数size(int 或者整数元组):输出的值赋在shape里,默认为None。
r = np.random.normal(loc=0.0, scale=(0.5 + t * t / 3), size=None)
y.append(r)
return x, 1.726 * x - 0.84 + np.array(y)
x, y = make_random_data()
plt.plot(x, y, '^')
# plt.savefig('images/14_03.png', dpi=300)
plt.show()
# ### Executing objects in a TensorFlow graph using their names
## train/test splits:
x_train, y_train = x[:100], y[:100]
x_test, y_test = x[100:], y[100:]
## add saver to the graph
with g.as_default():
saver = tf.train.Saver()
## training the model
n_epochs = 500
training_costs = []
with tf.Session(graph=g) as sess:
## first, run the variables initializer
sess.run(tf.global_variables_initializer())
## train the model for n_epochs
for e in range(n_epochs):
c, _ = sess.run(['cost:0', 'train_op'],
feed_dict={'tf_x:0': x_train,
'tf_y:0': y_train})
training_costs.append(c)
if not e % 50:
print('Epoch %4d: %.4f' % (e, c))
# 执行完这个新语句后,将产生三个分别以.data、.index和.meta为扩展名的文件。
# TensorFlow用缓存协议(https://developers.google.com/protocol- buffers/,一种非语言的方式)
# 把结构化的数据序列化存储。
saver.save(sess, './trained-model')
plt.plot(training_costs)
# plt.savefig('images/14_04.png', dpi=300)
plt.show()
运行结果:
<tf.Variable ‘classifier/weights:0’ shape=(100, 2) dtype=float32_ref>
<tf.Variable ‘classifier/bias:0’ shape=(2,) dtype=float32_ref>
Tensor(“classifier/logits:0”, shape=(64, 2), dtype=float32)
<tf.Variable ‘classifier/weights:0’ shape=(100, 2) dtype=float32_ref>
<tf.Variable ‘classifier/bias:0’ shape=(2,) dtype=float32_ref>
Tensor(“classifier/logits_1:0”, shape=(64, 2), dtype=float32)
Tensor(“tf_x:0”, dtype=float32)
Tensor(“tf_y:0”, dtype=float32)
Tensor(“y_hat:0”, dtype=float32)
Tensor(“cost:0”, shape=(), dtype=float32)
Epoch 0: 10.0121
Epoch 50: 7.3338
Epoch 100: 6.0509
Epoch 150: 5.4111
Epoch 200: 5.0714
Epoch 250: 4.8749
Epoch 300: 4.7494
Epoch 350: 4.6613
Epoch 400: 4.5946
Epoch 450: 4.5414
运行结果图:
所生成的随机回归数据:
每次迭代后的训练成本: