In [2]:
import tensorflow as tf
from tensorflow.python.framework import ops 
sess = tf.Session()

In [6]:
a = tf.Variable(tf.constant(4.))
x_val = 5.
x_data = tf.placeholder(dtype=tf.float32)

In [10]:
multiplication = tf.multiply(a, x_data)

In [11]:
loss = tf.square(tf.subtract(multiplication, 50.))

In [13]:
init = tf.global_variables_initializer()
sess.run(init)
my_opt = tf.train.GradientDescentOptimizer(0.01)
train_step = my_opt.minimize(loss)

In [15]:
print('Optimizing a Mutiplicaiton Gate Output to 50.')
for i in range(10):
    sess.run(train_step, feed_dict={x_data: x_val})
    a_val = sess.run(a)
    mult_output = sess.run(multiplication, feed_dict={x_data: x_val})
    print(str(a_val) + ' * ' + str(x_val) + " = " + str(mult_output))


Optimizing a Mutiplicaiton Gate Output to 50.
9.99707 * 5.0 = 49.9854
9.99854 * 5.0 = 49.9927
9.99927 * 5.0 = 49.9963
9.99963 * 5.0 = 49.9982
9.99982 * 5.0 = 49.9991
9.99991 * 5.0 = 49.9995
9.99995 * 5.0 = 49.9998
9.99998 * 5.0 = 49.9999
9.99999 * 5.0 = 49.9999
9.99999 * 5.0 = 50.0

In [16]:
from tensorflow.python.framework import ops
ops.reset_default_graph()
sess = tf.Session()

In [18]:
a = tf.Variable(tf.constant(1.))
b = tf.Variable(tf.constant(1.))
x_val = 5.
x_data = tf.placeholder(dtype=tf.float32)


two_gate = tf.add(tf.multiply(a, x_data), b)

loss = tf.square(tf.subtract(two_gate, 50.))

my_opt = tf.train.GradientDescentOptimizer(0.01)
train_step = my_opt.minimize(loss)

init = tf.global_variables_initializer()
sess.run(init)

In [19]:
print('\n Optimizing Two Gate Output to 50.')
for i in range(10):
    # run the train step
    sess.run(train_step, feed_dict={x_data: x_val})
    # Get the a and b values
    a_val, b_val = (sess.run(a), sess.run(b))
    # run the two_gate graph output
    two_gate_output = sess.run(two_gate, feed_dict={x_data: x_val})
    print(str(a_val) + ' * ' + str(x_val) + ' + ' + str(b_val) + ' = ' + str(two_gate_output))


 Optimizing Two Gate Output to 50.
5.4 * 5.0 + 1.88 = 28.88
7.512 * 5.0 + 2.3024 = 39.8624
8.52576 * 5.0 + 2.50515 = 45.134
9.01236 * 5.0 + 2.60247 = 47.6643
9.24593 * 5.0 + 2.64919 = 48.8789
9.35805 * 5.0 + 2.67161 = 49.4619
9.41186 * 5.0 + 2.68237 = 49.7417
9.43769 * 5.0 + 2.68754 = 49.876
9.45009 * 5.0 + 2.69002 = 49.9405
9.45605 * 5.0 + 2.69121 = 49.9714

In [ ]: