TensorFlow Cheatsheet

General cheatsheet

Initialize a Variable using constants or random values (zeros, zeros_like, linspace, etc.).

Can save and restore models using tf.train.Saver.

Read on from here for more cool stuff: https://www.tensorflow.org/programmers_guide/threading_and_queues

Graph vs. Session

(following the great explanation by Danijar Hafner)
graph = tf.Graph()
with graph.as_default():
    v = tf.Variable(tf.random_normal(shape=[1]), name='foo')
    print v.shape, v.shape.ndims, v.shape.num_elements()  # (1,) 1 1
    if 1: # Don't do this! tf.global_variables_initializer() defines an op that initializes all variables in the graph (so far); so you should call this AFTER they were all defined, otherwise you'll get something like "FailedPreconditionError: Attempting to use uninitialized value FailedPreconditionError: Attempting to use uninitialized value foo_scalar"
        init_op_notgood = tf.global_variables_initializer()
    v_scalar = tf.Variable(tf.random_normal(shape=[]), name='foo_scalar'))  # shape=[] or shape=() defines a 0-dimensional tensor, i.e. scalar
    print v_scalar.shape, v_scalar.shape.ndims, v_scalar.shape.num_elements()  # () 0 1
    init_op = tf.global_variables_initializer()  # Add an op to initialize all variables in the graph (actually probably best to define this even further down after the entire graph was constructed, but defining it here is already okay for our example)
    assign_v = v.assign([101])
    assign_v_scalar = v_scalar.assign(102)

c = tf.constant(4.0)  # Will be defined as attached to the default graph (!) tf.get_default_graph()

# Sanity check
print c.graph == tf.get_default_graph(), variable.graph == graph, graph == tf.get_default_graph()  # True True False

Then instantiate a Session to run our graph:

with tf.Session(graph=graph) as sess:
    print sess.run(v)  # e.g. [-0.407900009]
    print sess.run(v_scalar)  # e.g. 1.30248
    if 1:  # Don't do this part
        print sess.run(v)  # e.g. [0.33414543], a different value than above
        print sess.run(v_scalar)  # e.g. 1.30248, same as above
    print sess.run([assign_v, assign_v_scalar])  # [array([ 101.], dtype=float32), 102.0] -- return values probably not really interesting here
    print sess.run(v)  # [ 101.]
    print sess.run(v_scalar)  # 102.0

    if 0: # Error, as 'c' is not an element of the current graph...
        print sess.run(c)  # ValueError: Fetch argument <tf.Tensor 'Const_50:0' shape=() dtype=float32> cannot be interpreted as a Tensor. (Tensor Tensor("Const_50:0", shape=(), dtype=float32) is not an element of this graph.)

Leave a Reply

Your email address will not be published. Required fields are marked *