莫凡Tensorflow视频学习001 一元线性函数y = w *x + b

import tensorflow as tf
import numpy as np

#create data
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.1 + 0.3

###create tensorflow structure start###
weight = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
bias = tf.Variable(tf.zeros([1]))

y = x_data * weight + bias

loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

init = tf.initialize_all_variables()
###create tensorflow structure end###

sess = tf.Session()
sess.run(init)  #Donnot forget

for step in range(201):
    sess.run(train)
    if step % 20 == 0:
        print(step, sess.run(weight), sess.run(bias))

输出:

莫凡Tensorflow视频学习001 一元线性函数y = w *x + b_第1张图片

你可能感兴趣的:(tensorflow学习)