tensorflow 入门线性预测

from __future__ import print_function
import tensorflow as tf
import numpy as np

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

Weights = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
biases = tf.Variable(tf.zeros([1]))

y = Weights * x_data + biases

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)

for step in range(201):
    sess.run(train)
    if step % 20 == 0:
        print(step, sess.run(Weights), sess.run(biases))
0 [-0.28746453] [0.6696652]
20 [-0.00834835] [0.35559732]
40 [0.07430416] [0.31318545]
60 [0.09390596] [0.30312708]
80 [0.09855473] [0.30074164]
100 [0.09965724] [0.3001759]
120 [0.0999187] [0.30004174]
140 [0.09998072] [0.3000099]
160 [0.09999542] [0.30000237]
180 [0.09999891] [0.30000058]
200 [0.09999973] [0.30000016]

https://morvanzhou.github.io/tutorials/machine-learning/tensorflow/2-2-example2/

http://www.waitingfy.com/archives/4888

4888

Leave a Reply

Name and Email Address are required fields.
Your email will not be published or shared with third parties.