http://blog.csdn.net/u012151283/article/details/54866329
http://blog.csdn.net/u014281392/article/details/73878271
数据:https://github.com/chiphuyen/stanford-tensorflow-tutorials/tree/master/2017/data fire_theft.xls
这篇文章讲的很详细了
这里补充几点
sess.run()不仅仅是用来run op的
他还可以把变量(各种w_and_b)取出来
然后也就是说tensorflow是把训练出来的变量存在内存中的,那么最好是我们训练时,每100-1000次训练就保存一次数据以免发生意外导致之前都白训练了
而且我对同一个图,我通过run改变的值会留在图里
下一次run的时候是会接着之前的数据接着算
自己试了下
使用退火法(rate = learning_rate)
每次训练时rate = 0.001/(i+1) i为第i 次训练
最后w, b, total_loss = 2.03477, 2.50323, 18582.3533233
不使用退火法
rate = 0.001
最后w, b, total_loss =1.71838, 15.7892, 59351.0519406
虽然loss小了将近4倍但是使用退火的时间比不使用多了将近3倍
因为我每次训练都要根据rate来更新op
其实开始我的rate是0.01结果训练出来的w,b=inf
这个问题说明rate得小心设置
现在放上源码
读入部分参见第一个博客
不使用退火:
from read_data import data import matplotlib.pyplot as plt import tensorflow as tf X = tf.placeholder(tf.float32, name=‘X‘) Y = tf.placeholder(tf.float32, name=‘Y‘) w = tf.Variable(0.0, name=‘weight‘) b = tf.Variable(0.0, name=‘bias‘) Y_predicted = w * X + b loss = tf.square(Y_predicted - Y) op = tf.train.GradientDescentOptimizer(learning_rate = 0.001).minimize(loss) with tf.Session() as sess: #writer = tf.summary.FileWriter(‘./‘,sess.graph) #writer.close() sess.run(tf.global_variables_initializer()) for i in range(100): total_loss = 0 #rate = 0.001/(i+1) #print(rate) #op = tf.train.GradientDescentOptimizer(learning_rate=rate).minimize(loss) for x,y in data: _, l = sess.run([op, loss], feed_dict={X: x, Y: y}) total_loss += l w_value,b_value = sess.run([w,b]) print(str(w_value)+‘ ‘+str(b_value)+‘ ‘+str(total_loss)) X, Y = data.T[0], data.T[1] plt.plot(X, Y, ‘bo‘, label=‘Real data‘) plt.plot(X, X * w_value + b_value, ‘r‘, label=‘Predicted data‘) plt.legend() plt.show()
使用退火:
from read_data import data import matplotlib.pyplot as plt import tensorflow as tf X = tf.placeholder(tf.float32, name=‘X‘) Y = tf.placeholder(tf.float32, name=‘Y‘) w = tf.Variable(0.0, name=‘weight‘) b = tf.Variable(0.0, name=‘bias‘) Y_predicted = w * X + b loss = tf.square(Y_predicted - Y) op = tf.train.GradientDescentOptimizer(learning_rate = 0.001).minimize(loss) with tf.Session() as sess: #writer = tf.summary.FileWriter(‘./‘,sess.graph) #writer.close() sess.run(tf.global_variables_initializer()) for i in range(100): total_loss = 0 rate = 0.001/(i+1) print(rate) op = tf.train.GradientDescentOptimizer(learning_rate=rate).minimize(loss) for x,y in data: _, l = sess.run([op, loss], feed_dict={X: x, Y: y}) total_loss += l w_value,b_value = sess.run([w,b]) print(str(w_value)+‘ ‘+str(b_value)+‘ ‘+str(total_loss)) X, Y = data.T[0], data.T[1] plt.plot(X, Y, ‘bo‘, label=‘Real data‘) plt.plot(X, X * w_value + b_value, ‘r‘, label=‘Predicted data‘) plt.legend() plt.show()