In [1]:
import tensorflow as tf

In [2]:
sess = tf.Session()

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

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

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

In [6]:
init = tf.global_variables_initializer()
sess.run(init)

In [7]:
my_opt = tf.train.GradientDescentOptimizer(0.01)
train_step = my_opt.minimize(loss)

In [8]:
print('Optimizing a Multiplication Gate Output to 50.')

for i in range(20):
    fd = {
        x_data: x_val,
    }
    sess.run(train_step, feed_dict=fd)
    
    a_val = sess.run(a)
    mult_output = sess.run(multiplication, feed_dict=fd)
    
    print("{} * {} = {}".format(a_val, x_val, mult_output))


Optimizing a Multiplication Gate Output to 50.
7.0 * 5.0 = 35.0
8.5 * 5.0 = 42.5
9.25 * 5.0 = 46.25
9.625 * 5.0 = 48.125
9.8125 * 5.0 = 49.0625
9.90625 * 5.0 = 49.53125
9.953125 * 5.0 = 49.765625
9.9765625 * 5.0 = 49.8828125
9.98828125 * 5.0 = 49.94140625
9.994140625 * 5.0 = 49.970703125
9.9970703125 * 5.0 = 49.9853515625
9.99853515625 * 5.0 = 49.99267578125
9.999267578125 * 5.0 = 49.996337890625
9.9996337890625 * 5.0 = 49.9981689453125
9.99981689453125 * 5.0 = 49.99908447265625
9.999908447265625 * 5.0 = 49.999542236328125
9.999954223632812 * 5.0 = 49.99977111816406
9.999977111816406 * 5.0 = 49.99988555908203
9.999988555908203 * 5.0 = 49.999942779541016
9.999994277954102 * 5.0 = 49.999969482421875

In [9]:
from tensorflow.python.framework import ops

In [10]:
ops.reset_default_graph()

In [11]:
sess = tf.Session()

a = tf.Variable(tf.constant(1.))
b = tf.Variable(tf.constant(1.))
x_val = 5.
x_data = tf.placeholder(dtype=tf.float32)

# f(x) = a*x + b 
two_gate = tf.add(tf.multiply(a, x_data), b)

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

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

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

In [12]:
print('Optimizing Two Gate Output to 50.')

for i in range(20):
    fd = {
        x_data: x_val,
    }
    sess.run(train_step, feed_dict=fd)
    
    a_val, b_val = (sess.run(a), sess.run(b))
    two_gate_output = sess.run(two_gate, feed_dict=fd)
    print('{} * {} + {} = {}'.format(a_val, x_val, b_val, two_gate_output))


Optimizing Two Gate Output to 50.
5.400000095367432 * 5.0 + 1.8799999952316284 = 28.8799991607666
7.51200008392334 * 5.0 + 2.3024001121520996 = 39.86240005493164
8.52575969696045 * 5.0 + 2.5051522254943848 = 45.13395309448242
9.012364387512207 * 5.0 + 2.602473258972168 = 47.6642951965332
9.24593448638916 * 5.0 + 2.6491873264312744 = 48.87886047363281
9.358048439025879 * 5.0 + 2.671610116958618 = 49.46185302734375
9.411863327026367 * 5.0 + 2.682373046875 = 49.74169158935547
9.437694549560547 * 5.0 + 2.6875391006469727 = 49.87601089477539
9.450093269348145 * 5.0 + 2.690018892288208 = 49.94048309326172
9.456045150756836 * 5.0 + 2.691209316253662 = 49.971435546875
9.458901405334473 * 5.0 + 2.6917805671691895 = 49.98628616333008
9.460272789001465 * 5.0 + 2.6920547485351562 = 49.9934196472168
9.460930824279785 * 5.0 + 2.6921863555908203 = 49.99684143066406
9.461246490478516 * 5.0 + 2.6922495365142822 = 49.99848175048828
9.461398124694824 * 5.0 + 2.692279815673828 = 49.999271392822266
9.461470603942871 * 5.0 + 2.6922943592071533 = 49.99964904785156
9.461505889892578 * 5.0 + 2.6923012733459473 = 49.99983215332031
9.461523056030273 * 5.0 + 2.6923046112060547 = 49.99992370605469
9.461530685424805 * 5.0 + 2.6923060417175293 = 49.999961853027344
9.46153450012207 * 5.0 + 2.6923067569732666 = 49.999977111816406

In [ ]: