介绍
代码介绍
from __future__ import absolute_import, division, print_function
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# 学习率
learning_rate = 0.01
# 迭代次数
training_steps = 1000
display_step = 50
# 训练数据
X = np.array([3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,
7.042,10.791,5.313,7.997,5.654,9.27,3.1])
Y = np.array([1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,
2.827,3.465,1.65,2.904,2.42,2.94,1.3])
# 取出数组X的长度
n_samples = X.shape[0]
# 随机初始化权重,偏置
W = tf.Variable(np.random.randn(), name="weight")
b = tf.Variable(np.random.randn(), name="bias")
# 线性回归(Wx+b)
def linear_regression(x):
return W * x + b
# 均方差
def mean_square(y_pred,y_true):
return tf.reduce_sum(tf.pow(y_pred - y_true, 2)) / (2 * n_samples)
# 随机梯度下降优化器
optimizer = tf.optimizers.SGD(learning_rate)
# 优化过程
def run_optimization():
# 将计算封装在GradientTape中以实现自动微分
with tf.GradientTape() as g:
pred = linear_regression(X)
loss = mean_square(pred, Y)
# 计算梯度
# print("loss is ", loss)
gradients = g.gradient(loss, [W, b])
# 按gradients更新 W 和 b
optimizer.apply_gradients(zip(gradients, [W, b]))
# 针对给定训练步骤数开始训练
for step in range(1, training_steps + 1):
# 运行优化以更新W和b值
run_optimization()
if step % display_step == 0:
pred = linear_regression(X)
loss = mean_square(pred, Y)
print("step: %i, loss: %f, W: %f, b: %f" % (step, loss, W.numpy(), b.numpy()))
# 绘制图
plt.plot(X, Y, 'ro', label='Original data')
plt.plot(X, np.array(W * X + b), label='Fitted line')
plt.legend()
plt.show()
- 另外一个版本是指定权重和偏置的:
y= x * 0.1 + 0.3
,权重0.1 偏置0.3
from __future__ import absolute_import, division, print_function
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
# 学习率
learning_rate = 0.05
# 迭代次数
training_steps = 1000
display_step = 20
# 训练数据
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.1 + 0.3 #权重0.1 偏置0.3
# 随机初始化权重,偏置
# 权重和偏置
Weights = tf.Variable(tf.random.uniform([1], -1.0, 1.0))
biases = tf.Variable(tf.zeros([1]))
# 随机梯度下降优化器
optimizer = tf.optimizers.SGD(learning_rate)
# 优化过程
def run_optimization():
# 将计算封装在GradientTape中以实现自动微分
with tf.GradientTape() as g:
# 预测值y
y = Weights * x_data + biases
# 损失函数
loss = tf.reduce_mean(tf.square(y-y_data))
# 计算梯度
# print("loss is ", loss)
gradients = g.gradient(loss, [Weights, biases])
# 按gradients更新 W 和 b
optimizer.apply_gradients(zip(gradients, [Weights, biases]))
# 针对给定训练步骤数开始训练
for step in range(1, training_steps + 1):
# 运行优化以更新W和b值
run_optimization()
if step % display_step == 0:
# 预测值y
y = Weights * x_data + biases
# 损失函数
loss = tf.reduce_mean(tf.square(y-y_data))
print("step: %i, loss: %f, W: %f, b: %f" % (step, loss, Weights.numpy(), biases.numpy()))
# 绘制图
plt.plot(x_data, y_data, 'ro', label='Original data')
plt.plot(x_data, np.array(Weights * x_data + biases), label='Fitted line')
plt.legend()
plt.show()
- 打印结果发现,权重基本向0.1,偏置基本向0.3靠拢
step: 20, loss: 0.031721, W: -0.491671, b: 0.560534
step: 40, loss: 0.021747, W: -0.396559, b: 0.561665
step: 60, loss: 0.016477, W: -0.331112, b: 0.530155
step: 80, loss: 0.012491, W: -0.275286, b: 0.500551
...
step: 920, loss: 0.000000, W: 0.098881, b: 0.300598
step: 940, loss: 0.000000, W: 0.099026, b: 0.300521
step: 960, loss: 0.000000, W: 0.099152, b: 0.300453
step: 980, loss: 0.000000, W: 0.099261, b: 0.300395
step: 1000, loss: 0.000000, W: 0.099357, b: 0.300344
其他