google Tensorflow is an open source libary of machine leanring and has been widely used inside Google, since it was published open source , it atracted a lot of attentions , for the first time I came cross this when I learned the RNN  and CNN , the first code of Tensor flow has been rewriten by the fellowing .

Here we try to fit a line with linear regression. I will make the code in Chinese, Hoping this will help some newbies to learn fastly.

import tensorflow as tf
import numpy as np

# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3  这里创造一个数据X随机取值100个，
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.1 + 0.3   根据X数据构造y数据

# Try to find values for W and b that compute y_data = W * x_data + b
# (We know that W should be 0.1 and b 0.3, but TensorFlow will
# figure that out for us.)
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))   tf.变量表示方法，后面tf.random_uniform()这个 X的范围被限制在-1到1之间  并且只取一个值
b = tf.Variable(tf.zeros([1]))   构造一个常量b,这里并不知道 所以用变量
y = W * x_data + b                   线性回归

# Minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y – y_data))  最小二乘法的损失函数
train = optimizer.minimize(loss)          用数据最小化loss

# Before starting, initialize the variables. We will ‘run’ this first.
init = tf.initialize_all_variables()  记得要初始化所有  有的 VAriable()的

# Launch the graph.
sess = tf.Session()  Session开始
sess.run(init)

# Fit the line.
for step in range(201):
sess.run(train)  训练步长
if step % 20 == 0:
print(step, sess.run(W), sess.run(b)) 打印值

# Learns best fit is W: [0.1], b: [0.3]

(175, array([ 0.10000011], dtype=float32), array([ 0.29999995], dtype=float32))
(176, array([ 0.10000011], dtype=float32), array([ 0.29999995], dtype=float32))
(177, array([ 0.10000011], dtype=float32), array([ 0.29999995], dtype=float32))
(178, array([ 0.10000011], dtype=float32), array([ 0.29999995], dtype=float32))