tensorflow2

# step1 加载包
import tensorflow
as tf import numpy as np
# step2 输入:随机产生数据 # Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3 x_data = np.random.rand(100).astype(np.float32) y_data = x_data * 0.1 + 0.3
#step 3: 参数:定义参数并初始化 # 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)) b = tf.Variable(tf.zeros([1])) y = W * x_data + b
#steo 4:预测的值y,损失函数,求解器
# Minimize the mean squared errors. loss = tf.reduce_mean(tf.square(y - y_data)) optimizer = tf.train.GradientDescentOptimizer(0.5) train = optimizer.minimize(loss)
# step 5:初始化 # Before starting, initialize the variables. We will 'run' this first. init = tf.initialize_all_variables()
# step 6: 创建会话并运行初始化 # Launch the graph. sess = tf.Session() sess.run(init)

# step 7: 迭代求解
# 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]

posted on 2016-09-06 22:53  一动不动的葱头  阅读(182)  评论(0编辑  收藏  举报

导航