# -*- coding: utf-8 -*-
"""
Created on Wed Feb 28 15:13:05 2018
@author: 李慧泽
"""
#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)#在-3与3之间取得100个数。
ys=np.sin(xs)+np.random.uniform(-0.5,0.5, n_observations)#sin函数并加一些噪声。
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_dict把数据装进去, optimizer和loss为两个节点,但是我只要loss的输出结果.
o,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.close()
#取出w和b的值
W,b=sess.run([W,b])
print("w:"+str(W[0]))
print("b:"+str(b[0]))
plt.plot(xs,ys,label='Real data')
plt.plot(xs,xs*W+b,'r',label='Predicted data')
plt.legend()
plt.show()