First TensorFlow Graphs

In this notebook, we execute elementary TensorFlow computational graphs.

Load dependencies


In [1]:
import numpy as np
import tensorflow as tf

Simple arithmetic


In [2]:
x1 = tf.placeholder(tf.float32)
x2 = tf.placeholder(tf.float32)

In [3]:
sum_op = tf.add(x1, x2)
product_op = tf.multiply(x1, x2)

In [4]:
with tf.Session() as session:
    sum_result = session.run(sum_op, feed_dict={x1: 2.0, x2: 0.5}) # run again with {x1: [2.0, 2.0, 2.0], x2: [0.5, 1.0, 2.0]}
    product_result = session.run(product_op, feed_dict={x1: 2.0, x2: 0.5}) # ...and with {x1: [2.0, 4.0], x2: 0.5}

In [5]:
sum_result


Out[5]:
2.5

In [6]:
product_result


Out[6]:
1.0

Simple array arithmetic


In [7]:
with tf.Session() as session:
    sum_result = session.run(sum_op, feed_dict={x1: [2.0, 2.0, 2.0], x2: [0.5, 1.0, 2.0]})
    product_result = session.run(product_op, feed_dict={x1: [2.0, 4.0], x2: 0.5})

In [8]:
sum_result


Out[8]:
array([ 2.5,  3. ,  4. ], dtype=float32)

In [9]:
product_result


Out[9]:
array([ 1.,  2.], dtype=float32)

In [ ]: