Debian Codegraph @

texmf-dist/doc/latex/codebox/hellopy.py

raw
1 import tensorflow as tf
2 import numpy as np
3 import os
4 os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
5
6 # Create 100 phony x, y data points in Numpy, y = x * 0.1 + 0.3
7 x_data = np.random.random(100).astype("float32")
8 y_data = x_data * 0.1 + 0.3
9
10 # Try to find values for W and b that compute y_data = W * x_data + b
11 W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
12 b = tf.Variable(tf.zeros([1]))
13 y = W * x_data + b
14
15 # Minimize the mean squared errors.
16 loss = tf.reduce_mean(tf.square(y -y_data))
17 optimizer = tf.train.GradientDescentOptimizer(0.5)
18 train = optimizer.minimize(loss)
19
20 # Before starting, initialize the variables. We will 'run' this first
21 init = tf.global_variables_initializer()
22
23 # Launch the graph.
24 sess = tf.Session()
25 sess.run(init)
26
27 # Fit the line.
28 for step in range(201):
29 sess.run(train)
30 if step % 20 == 0:
31 print(step, sess.run(W), sess.run(b))
32