TensorFlow实战:线性回归模型入门

首先导入各种TensorFlow等工具及设置画图的大小及字体

登录后复制

import numpy as npimport tensorflow as tfimport matplotlib.pyplot as pltplt
.rcParams['figure.figsize'] = (10.0, 8.0)plt.rcParams['font.sans-serif'] = 'NSimSun,
Times New Roman'1.2.3.4.5.



生成用于进行线性回归的模型的数据

登录后复制

# 随机生成100个点,围绕在y=3x+5的直线周围num_points = 200vectors_set = []for i in range(num_points):
x1 = np.random.uniform(-10, 25)    y1 = x1 * 3 + 5 + np.random.normal(0.0, 5)    
vectors_set.append([x1, y1])# 生成一些样本x_data = [v[0] for v in 
vectors_set]y_data = [v[1] for v in vectors_set]
plt.scatter(x_data,y_data,c='r')plt.show()1.2.3.4.5.6.7.8.9.10.11.12.



生成的数据及画出的点像图如下:

使用TensorFlow实现简单的线性回归模型_Python



设置模型的原始数据,编写现行回归的训练模型代码,并使用梯度下降算法进行训练



登录后复制

W = tf.Variable(tf.random_uniform([1], -1.0, 1.0), name='W')b = tf.Variable(tf.zeros([1]), 
name='b')y = W * x_data + bloss = tf.reduce_mean(tf.square(y - y_data), name='loss')
optimizer = tf.train.GradientDescentOptimizer(0.005)train = 
optimizer.minimize(loss, name='train')sess = tf.Session()init = tf.
global_variables_initializer()sess.run(init)# 初始化的W和b是多少print ("W =", sess.run(W), 
"\tb =", sess.run(b), "\tloss =", sess.run(loss))# 执行20次训练for step in range(1000):    
sess.run(train)    # 输出训练好的W和b    if(step % 50 == 0):        
print ("W =", sess.run(W), "\tb =", sess.run(b), "\tloss =", 
sess.run(loss))print ("最终的结果 W =", sess.run(W), "\tb =", sess.run(b), 
"\tloss =", sess.run(loss))1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.



训练过程显示的参数结果如下:

W = [ 0.26262569]  b = [ 0.]  loss = 1366.89
W = [ 4.73117256]  b = [ 0.23248257]  loss = 386.389
W = [ 3.15473843]  b = [ 1.66317821]  loss = 32.05
W = [ 3.10857034]  b = [ 2.7120173]  loss = 27.6106
W = [ 3.07622719]  b = [ 3.44677091]  loss = 25.432
W = [ 3.05356979]  b = [ 3.96149445]  loss = 24.3628
W = [ 3.03769755]  b = [ 4.32207775]  loss = 23.8381
W = [ 3.02657843]  b = [ 4.57468128]  loss = 23.5806
W = [ 3.01878905]  b = [ 4.7516408]  loss = 23.4542
W = [ 3.01333213]  b = [ 4.87560892]  loss = 23.3922
W = [ 3.00950956]  b = [ 4.96245146]  loss = 23.3617
W = [ 3.00683141]  b = [ 5.02328825]  loss = 23.3468
W = [ 3.00495553]  b = [ 5.06590843]  loss = 23.3395
W = [ 3.00364113]  b = [ 5.09576511]  loss = 23.3359
W = [ 3.00272036]  b = [ 5.11667919]  loss = 23.3341
W = [ 3.00207567]  b = [ 5.13133097]  loss = 23.3332
W = [ 3.00162363]  b = [ 5.14159679]  loss = 23.3328
W = [ 3.00130725]  b = [ 5.14878702]  loss = 23.3326
W = [ 3.00108552]  b = [ 5.15382385]  loss = 23.3325
W = [ 3.00093007]  b = [ 5.15735531]  loss = 23.3324
W = [ 3.00082135]  b = [ 5.1598258]  loss = 23.3324
最终的结果 W = [ 3.00074625]  b = [ 5.16152811]  loss = 23.3324



登录后复制

plt.scatter(x_data,y_data,c='r')plt.plot(x_data,sess.run(W)*x_data+sess.run(b))plt.show()1.2.3.

使用TensorFlow实现简单的线性回归模型_数据_02



免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删

QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空