B站:
http://www.bilibili.com/video/av20542427?share_medium=android&share_source=copy_link&bbid=8099B994-2CEF-4DB4-A914-9DC322B1E3A531040infoc&ts=1532410379035
强烈推荐小哥哥的课,又有耐心,声音又好听
关于 学习率:https://baijiahao.baidu.com/s?id=1591531217345055627&wfr=spider&for=pc
import tensorflow as tf
import numpy as np
#使用numpy生成100个随机点
x_date = np.random.rand(100)
y_date = x_date*0.1 + 0.2
#构造一个线性模型
b = tf.Variable(0.)
k = tf.Variable(0.)
y = k*x_date+b
#二次代价函数,reduce_mean求平均值
loss = tf.reduce_mean(tf.square(y_date-y))
#定义一个梯度下降法来进行训练的优化器,学习率0.2
optimizer = tf.train.GradientDescentOptimizer(0.2)
#最小化代价函数
train = optimizer.minimize(loss)
#初始化变量
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for step in range(201):
sess.run(train)
if step%20==0:
print(step,sess.run([k,b]))
"""
result:
0 [0.053516865, 0.100107215]
20 [0.103312664, 0.1982229]
40 [0.10198982, 0.19893262]
60 [0.10119521, 0.19935887]
80 [0.100717925, 0.19961488]
100 [0.10043123, 0.19976868]
120 [0.10025902, 0.19986106]
140 [0.100155585, 0.19991654]
160 [0.10009344, 0.19994988]
180 [0.10005612, 0.19996989]
200 [0.1000337, 0.19998193]
"""