Learn Tensorflow by Examples Series - Part 3

Tensorflow Graphs


A graph defines what functions or results to compute. It doesn't actually compute anything, or hold any values, just defines what to compute.
Example of a graph:

Tensorflow code example:
import tensorflow as tf

# Declare variables (with initial values)
x = tf.Variable(3, name='x')
y = tf.Variable(4, name='y')

# Graph node to compute
f = (x * x * y) + y + 2
Full code
x, y, f in above example are graph nodes.

Variable initialization

1. One by one

import tensorflow as tf

# Declare variables (with initial values)
x = tf.Variable(3, name='x')
y = tf.Variable(4, name='y')

# Instantiate x and y
with tf.Session() as sess:
    x.initializer.run()
    y.initializer.run()

2. Using global initializer

import tensorflow as tf

# Declare variables (with initial values)
x = tf.Variable(3, name='x')
y = tf.Variable(4, name='y')

init = tf.global_variables_initializer()

# Instantiate x and y
with tf.Session() as sess:
    init.run()