1. 程式人生 > >【TensorFlow】使用TensorFlow的Eager API實現線性迴歸

【TensorFlow】使用TensorFlow的Eager API實現線性迴歸

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

開啟eager模式

tf.enable_eager_execution()
tfe = tf.contrib.eager

引數

learning_rate = 0.01
training_epochs = 1000
display_step = 50

生成資料

X_train = 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_train = 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]) n_samples = X_train.shape[0]

定義模型

# 權值
W = tfe.Variable(np.random.randn())
b = tfe.Variable(np.random.randn())
# 線性迴歸模型
def linear_regression
(inputs): return inputs*W + b # MSE def MSE(model,inputs,labels): return tf.reduce_sum(tf.pow(model(inputs)-labels,2)) / (2*n_samples) # SGD optimizer = tf.train.GradientDescentOptimizer(learning_rate) # 返回給定函式梯度的“函式“,其引數與給定的函式相同 grad = tfe.implicit_gradients(MSE)

訓練模型

# 初始化loss
# print("Initial loss={:.9f}".format(MSE(linear_regression,X_train,Y_train)),"W=",W.numpy(),"b=",b.numpy())
# 訓練模型 for epoch in range(training_epochs): optimizer.apply_gradients(grad(linear_regression,X_train,Y_train)) if (epoch+1)%display_step==0: print("Epoch:", '%04d' % (epoch + 1), "loss=", "{:.9f}".format(MSE(linear_regression, X_train, Y_train)), "W=", W.numpy(), "b=", b.numpy())
Epoch: 0050 loss= 0.137683481 W= 0.39137775 b= -0.19191246
Epoch: 0100 loss= 0.130734965 W= 0.38314393 b= -0.13353844
Epoch: 0150 loss= 0.124581113 W= 0.3753953 b= -0.07860399
Epoch: 0200 loss= 0.119131088 W= 0.3681032 b= -0.026906287
Epoch: 0250 loss= 0.114304394 W= 0.36124077 b= 0.021745302
Epoch: 0300 loss= 0.110029764 W= 0.35478267 b= 0.06753029
Epoch: 0350 loss= 0.106243983 W= 0.34870508 b= 0.11061759
Epoch: 0400 loss= 0.102891207 W= 0.3429856 b= 0.15116611
Epoch: 0450 loss= 0.099921875 W= 0.33760312 b= 0.18932548
Epoch: 0500 loss= 0.097292140 W= 0.3325378 b= 0.2252365
Epoch: 0550 loss= 0.094963185 W= 0.3277709 b= 0.2590315
Epoch: 0600 loss= 0.092900597 W= 0.3232849 b= 0.29083526
Epoch: 0650 loss= 0.091073900 W= 0.3190632 b= 0.32076517
Epoch: 0700 loss= 0.089456134 W= 0.31509024 b= 0.34893158
Epoch: 0750 loss= 0.088023372 W= 0.3113514 b= 0.37543836
Epoch: 0800 loss= 0.086754479 W= 0.3078328 b= 0.40038347
Epoch: 0850 loss= 0.085630715 W= 0.3045216 b= 0.42385873
Epoch: 0900 loss= 0.084635474 W= 0.30140543 b= 0.44595078
Epoch: 0950 loss= 0.083754070 W= 0.2984729 b= 0.46674109
Epoch: 1000 loss= 0.082973465 W= 0.29571316 b= 0.4863064

繪製擬合曲線

plt.plot(X_train, Y_train, 'ro', label='Original data')
plt.plot(X_train, np.array(W * X_train + b), label='Fitted line')
plt.legend()
plt.show()

在這裡插入圖片描述