TensorFlow Basics

Tensors

Nodes of a Graph


In [ ]:
import tensorflow as tf

def tensors():
    print('Tensor:')
    print(tf.constant(10))
    
    print('\nNamed Tensors:')
    print(tf.constant(10, name='named_tensor'))
    print(tf.constant(10, name='other_tensor'))
    
    print('\nUsing the Same name:')
    print(tf.constant(10, name='named_tensor'))
    
    print('\nFloat Tensor:')
    print(tf.constant(10, dtype=tf.float32))
    
    print('\nN-D Tensor:')
    print(tf.constant([1,2,3,4]))
    print(tf.constant([[1,2],[3,4],[5,6]]))
    
    x = tf.constant(10)
    y = tf.constant(2)
    
    print('\nTensor from multiplication:')
    print(tf.multiply(x, y))
    
tensors()

Graphs

Computational Graph


In [ ]:
def use_new_graph():
    # Create a TensorFlow graph
    graph_g = tf.Graph()
    
    # Using Graph graph_g
    with graph_g.as_default():
        a = tf.constant(10)
        b = tf.constant(2)
        c = tf.multiply(a, b)
        
    # Assert that c got added to graph_g
    assert c.graph == graph_g
    
    # Run session using graph_g
    with tf.Session(graph=graph_g) as sess:
        c_out = sess.run(c)
        print('Output from running tensor "c" in graph "graph_g"')
        print(c_out)

def use_default_graph():
    # Using default graph
    d = tf.constant(10)
    e = tf.constant(5)
    f = tf.multiply(d, e)

    # Assert that f got added to the default graph
    assert f.graph == tf.get_default_graph()

    # Run session using the default graph
    with tf.Session() as sess:
        f_out = sess.run(f)
        print('Output from running tensor "f" in the default graph')
        print(f_out)

use_new_graph()
use_default_graph()

Session

Environment for the Graph


In [ ]:
def session():
    # Run on CPU 0
    with tf.device('/cpu:0'):
        a = tf.constant(10)
        b = tf.constant(5)
        c = tf.multiply(a, b)

    # Run on CPU 0 as well, but this could be any resource including a different computer
    with tf.device('/cpu:0'):
        d = tf.constant(10)
        e = tf.constant(5)
        f = tf.multiply(a, b)

    # Run on default device
    g = tf.add(c, f)

    print('g type: {}'.format(type(g)))

    # Execute graph
    with tf.Session() as sess:
        out = sess.run(g)
        print('out type: {}'.format(type(out)))
        print('out: {}'.format(out))

session()

Math Operations


In [ ]:
def math_operations():
    a = tf.constant(10)
    b = tf.constant(5)

    # The calling tf math operations are only required over *, -, etc. when no inputs are Tensors
    c = tf.multiply(a, b)
    d = a * b
    e = a * 5
    f = 10 * b
    g = tf.multiply(10, 5)
    h = 10 * 5

    print('c type: {}'.format(type(c)))
    print('d type: {}'.format(type(d)))
    print('e type: {}'.format(type(e)))
    print('f type: {}'.format(type(f)))
    print('g type: {}'.format(type(g)))
    print('h type: {}'.format(type(h)))

    with tf.Session() as sess:
        c_out = sess.run(c)
        d_out = sess.run(d)
        e_out = sess.run(e)
        f_out = sess.run(f)
        g_out = sess.run(g)

        # Make sure they are the same results
        assert c_out == d_out and c_out == e_out and c_out == f_out and c_out == g_out
        print('All TensorFlow Results: {}'.format(c_out))

math_operations()

Save Weights

Save Session Variables


In [ ]:
save_path = './1_tensorflow_basics'

def save(save_path):
    v1 = tf.Variable(tf.random_normal((1, 3)), name="v1")
    v2 = tf.Variable(tf.random_normal((1, 3)), name="v2")

    saver = tf.train.Saver()

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        print('v1: {}'.format(sess.run(v1)))
        print('v2: {}'.format(sess.run(v2)))
        save_path = saver.save(sess, save_path)

save(save_path)

Load Weights

Load Session Variables


In [ ]:
def load(save_path):
    tf.reset_default_graph()
    v1 = tf.Variable(tf.random_normal((1, 3)), name="v1")
    v2 = tf.Variable(tf.random_normal((1, 3)), name="v2")

    saver = tf.train.Saver()

    with tf.Session() as sess:
        saver.restore(sess, save_path)
        print('v1: {}'.format(sess.run(v1)))
        print('v2: {}'.format(sess.run(v2)))
        
load(save_path)