Learning Machines

Taught by Patrick Hebron at NYU/ITP, Fall 2017

TensorFlow Basics: "Basic Operations"



In [1]:
# Import TensorFlow library:
import tensorflow as tf

In [2]:
# Create constants:
X = tf.constant( 2.0 )
Y = tf.constant( 3.0 )

# Create addition operation:
Z = tf.add( X, Y )

# Create session:
with tf.Session() as sess:
    # Run session:
    output = sess.run( Z )
    # Print output:
    print output


5.0

In [3]:
# Create placeholders:
X = tf.placeholder( tf.float32 )
Y = tf.placeholder( tf.float32 )

# Create addition operation:
Z = tf.add( X, Y )

# Create session:
with tf.Session() as sess:
    # Run session and print output:
    print sess.run( Z, feed_dict={ X: 2.0, Y: 3.0 } )
    # Run session and print output (with different input values):
    print sess.run( Z, feed_dict={ X: 4.0, Y: 5.0 } )


5.0
9.0

In [4]:
# Create matrix constants:
X = tf.constant( [ [ 2.0, -4.0, 6.0 ], [ 5.0, 7.0, -3.0 ] ] )
Y = tf.constant( [ [ 8.0, -5.0 ], [ 9.0, 3.0 ], [ -1.0, 4.0 ] ] )

# Create matrix multiplication operation:
Z = tf.matmul( X, Y )

# Create session:
with tf.Session() as sess:
    # Run session:
    output = sess.run( Z )
    # Print output:
    print output


[[ -26.    2.]
 [ 106.  -16.]]

In [ ]: