实验6-使用TensorFlow完成线性回归

VMware虚拟机 Ubuntu20-LTS

python3.6

tensorflow1.15.0

keras2.3.1

运行截图:

 

 

代码:

 

复制代码
%matplotlib inline
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (14,8)

n_observations = 100
xs = np.linspace(-3, 3, n_observations)
ys = np.sin(xs) + np.random.uniform(-0.5, 0.5, n_observations)
plt.scatter(xs, ys)
plt.show()
X = tf.placeholder(tf.float32, name='X')
Y = tf.placeholder(tf.float32, name='Y')
#%%
W = tf.Variable(tf.random_normal([1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
#%%
Y_pred = tf.add(tf.multiply(X, W), b)
#%%
loss = tf.square(Y - Y_pred, name='loss')
#%%
learning_rate = 0.01
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
#%%
n_samples = xs.shape[0]
with tf.Session() as sess:
    # 记得初始化所有变量
    sess.run(tf.global_variables_initializer()) 
    
    writer = tf.summary.FileWriter('./graphs/linear_reg', sess.graph)
    
    # 训练模型
    for i in range(50):
        total_loss = 0
        for x, y in zip(xs, ys):
            # 通过feed_dic把数据灌进去
            _, l = sess.run([optimizer, loss], feed_dict={X: x, Y:y}) 
            total_loss += l
        if i%5 ==0:
            print('Epoch {0}: {1}'.format(i, total_loss/n_samples))

    # 关闭writer
    writer.close() 
    
    # 取出w和b的值
    W, b = sess.run([W, b]) 
print(W,b)
print("W:"+str(W[0]))
print("b:"+str(b[0]))
plt.plot(xs, ys, 'bo', label='Real data')
plt.plot(xs, xs * W + b, 'r', label='Predicted data')
plt.legend()
plt.show()
复制代码

 

posted @   lcz111  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示