# tensorflow-一元线性模型

数据见deep learning，这是一个一元输入、一元输出的线性模型，下面是具体实现过程：

import tensorflow as tf
import numpy as np
#设置线性函数参数W、b
#W初始化为均匀随机数
W=tf.Variable(tf.random_uniform([1],-1.0,1.0))
#b初始化为0
b=tf.Variable(tf.zeros([1]))
#参数下输出为y_
y_=W*x_data+b
#设置损失函数
loss=tf.reduce_mean(tf.square(y_data-y_))/2
#定义梯度下降优化器 设置学习率
#定义优化方法
train=optimizer.minimize(loss)

#会话执行图
sess=tf.Session()
#初始化变量
init=tf.global_variables_initializer()
sess.run(init)

#执行梯度下降优化
for i in range(1500):
sess.run(train)
if i%100==0:
print sess.run(W),sess.run(b)



[0.38659847] [0.07704204]
[0.13098377] [0.41046652]
[0.09381685] [0.5863949]
[0.07821415] [0.67140335]
[0.07077159] [0.71229064]
[0.06719442] [0.73195165]
[0.06547432] [0.7414059]
[0.06464729] [0.7459518]
[0.06424948] [0.7481382]
[0.06405827] [0.7491891]
[0.06396635] [0.74969447]
[0.06392214] [0.74993736]
[0.06390087] [0.75005424]
[0.06389064] [0.7501105]
[0.0638857] [0.7501377]

[0.48159832] [0.08058839]
[nan] [nan]
[nan] [nan]
[nan] [nan]
[nan] [nan]
[nan] [nan]
[nan] [nan]
[nan] [nan]
[nan] [nan]
[nan] [nan]
[nan] [nan]
[nan] [nan]
[nan] [nan]
[nan] [nan]
[nan] [nan]

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120