import tensorflow as tf
x = tf.constant([1,2,3,4,5,6], dtype=tf.float32, name='x')
y = tf.constant([3,4,7,8,11,14], dtype=tf.float32, name='y')
print(x,y)
w = tf.Variable(1.0, dtype=tf.float32, name='w')
b = tf.Variable(1.0, dtype=tf.float32, name='b')
print(w,b)
loss = tf.reduce_sum(tf.square(y-(w*x+b)))
print(loss)
opti = tf.train.GradientDescentOptimizer(0.005).minimize(loss)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
MSE=[]
for i in range(1000):
sess.run(opti)
MSE.append(sess.run(loss))
if i%50==0:
print((sess.run(w),sess.run(b)))
import matplotlib.pyplot as plt
plt.figure(1)
plt.plot(MSE)
plt.show()
plt.figure(2)
x_array,y_array=sess.run([x,y])
plt.plot(x_array,y_array,'o')
yy = []
for xx in range(0,10):
yy.append(sess.run(w)*xx+sess.run(b))
pass
plt.plot(yy)
plt.show()
Python_2019-06-14_机器学习——Tensorflow——回归拟合线实验
最新推荐文章于 2022-08-17 21:29:07 发布