Variables and Placeholders


In [1]:
import tensorflow as tf

In [2]:
sess = tf.InteractiveSession()

Variables


In [3]:
# Create a tensor of shape 4 by 4 with uniformly distributed random values
my_tensor = tf.random_uniform(shape = (4,4), 
                              minval = 0,
                              maxval = 1)

In [4]:
my_tensor


Out[4]:
<tf.Tensor 'random_uniform:0' shape=(4, 4) dtype=float32>

In [5]:
# Initialize the tensor as a Variable
my_var = tf.Variable(initial_value = my_tensor)

In [6]:
my_var


Out[6]:
<tf.Variable 'Variable:0' shape=(4, 4) dtype=float32_ref>

Note! You must initialize all global variables!


In [7]:
# OBLIGATORY!
# Initializing all variables
init = tf.global_variables_initializer()

In [8]:
init.run()

In [9]:
my_var.eval()


Out[9]:
array([[0.10739136, 0.8106226 , 0.00213122, 0.88670003],
       [0.3259567 , 0.7325934 , 0.97831714, 0.0457505 ],
       [0.71009386, 0.12011993, 0.11913311, 0.2890252 ],
       [0.9749534 , 0.15022421, 0.09399927, 0.5066788 ]], dtype=float32)

In [10]:
sess.run(my_var)


Out[10]:
array([[0.10739136, 0.8106226 , 0.00213122, 0.88670003],
       [0.3259567 , 0.7325934 , 0.97831714, 0.0457505 ],
       [0.71009386, 0.12011993, 0.11913311, 0.2890252 ],
       [0.9749534 , 0.15022421, 0.09399927, 0.5066788 ]], dtype=float32)

Placeholders


In [11]:
# Defining a Plaholder of a specific type
ph = tf.placeholder(tf.float64)

In [12]:
ph = tf.placeholder(tf.int32)

In [13]:
# For shape its common to use (None, Numbers of Features) 
# because None can be filled by number of samples in data
ph = tf.placeholder(shape = (None, 5),
                    dtype = tf.float32 )

Coming up next is where we'll put this all together!