tensorflow
1.一切都要tf.
2.只有sess.run才能生效
import tensorflow as tf import numpy as np import matplotlib.pyplot as plt ###################################################### #data nump_points=1000 vector_set=[] for i in range(nump_points): x1=np.random.normal(0.0,0.55) y1=x1*0.1+0.3+np.random.normal(0.0,0.03) vector_set.append([x1,y1]) x_data=[v[0] for v in vector_set] y_data=[v[1] for v in vector_set] plt.scatter(x_data,y_data,c='r') plt.show() ######################################################## #linear regression #y=wx+b,initial value w=tf.Variable(tf.random_uniform([1],-1.0,1.0),name='w') b=tf.Variable(tf.zeros([1])) #model y=w*x_data+b #trainer set up loss=tf.reduce_mean(tf.square(y-y_data)) optimizer=tf.train.GradientDescentOptimizer(0.5) train=optimizer.minimize(loss) sess=tf.Session() init=tf.global_variables_initializer() sess.run(init) #first step print('w=',sess.run(w),'b=',"loss:",sess.run(loss)) for step in range(20): sess.run(train) print('w=', sess.run(w), 'b=', "loss:", sess.run(loss))