loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)#选择梯度下降,学习率0.1,最小化误差
important step
sess = tf.Session()
tf.initialize_all_variables() no long valid from
2017-03-02 if using tensorflow >= 0.12
ifint((tf.__version__).split('.')[1])<12andint((tf.__version__).split('.')[0])<1:
init = tf.initialize_all_variables()else:
init = tf.global_variables_initializer()#python3都是使用这种结构了
sess.run(init)#首先运行init这个功能for i inrange(1000):# training
sess.run(train_step, feed_dict={xs: x_data, ys: y_data})if i %50==0:# to see the step improvementprint(sess.run(loss, feed_dict={xs: x_data, ys: y_data}))
莫烦例子全中文向详细注释from __future__ import print_function #使用print就得像python3.X那样加括号使用。python2.X中print不需要括import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt #风格与MATLAB类似,可以方便的绘制图像、展示结...