In [2]:
import tensorflow as tf

playing with constants and variables


In [31]:
x=tf.constant(5)
y=tf.constant(8)
z=tf.Variable(2)
c=tf.mul(x,z)
model = tf.initialize_all_variables()

In [32]:
sess=tf.Session() 
sess.run(model)
print(sess.run(x*y))
print(sess.run(c))


40
10

Intermediate and Final


In [37]:
intermed=x+y
final=tf.mul(intermed,c)

In [38]:
sess.run([intermed,final])


Out[38]:
[13, 130]

Placeholder and Feed Dictionary


In [40]:
input1=tf.placeholder(tf.float16)
input2=tf.placeholder(tf.float16)
output=tf.add(input1,input2)

In [44]:
# always specify which input you are feeding
sess.run(output,feed_dict={input1:788,input2:677})


Out[44]:
1465.0

In [49]:
sess.close()

Interactive session


In [55]:
sess=tf.InteractiveSession()
x = tf.Variable([1.0, 2.0])
a = tf.constant([3.0, 3.0])

sub = tf.sub(x, a)

x.initializer.run()

print(sess.run(sub))
sess.close()


[-2. -1.]

More Variables


In [63]:
# Create a Variable, that will be initialized to the scalar value 0.
state = tf.Variable(10, name="counter")

In [64]:
# Create an Op to add one to `state`.
one=tf.constant(1)
new_value=tf.add(state,one)
update=tf.assign(state,new_value)

In [65]:
# Variables must be initialized by running an `init` Op after having
# launched the graph.  We first have to add the `init` Op to the graph.

In [66]:
init_op = tf.initialize_all_variables()

In [67]:
with tf.Session() as sess:
    sess.run(init_op)
    print(sess.run(state))
    print(sess.run(new_value))
    print(sess.run(update))


10
11
11

In [69]:
### Run a for loop

In [70]:
with tf.Session() as sess:
  # Run the 'init' op
  sess.run(init_op)
  # Print the initial value of 'state'
  print(sess.run(state))
  # Run the op that updates 'state' and print 'state'.
  for _ in range(3):
    sess.run(update)
    print(sess.run(state))


10
11
12
13

Casting


In [76]:
#tensor `a` is [1.8, 2.2], dtype=tf.float
a= [1.8, 2.2]
n=tf.cast(a, tf.int32)
#==> [1, 2]  # dtype=tf.int32

In [77]:
model=tf.initialize_all_variables()
sess=tf.Session()
sess.run(model)
sess.run(n)


Out[77]:
array([1, 2], dtype=int32)

Size and Shape


In [86]:
t=[[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]]
print(sess.run(tf.shape(t)))# ==> [2, 2, 3]
print(sess.run(tf.size(t)))
sess.run(tf.rank(t))


[2 2 3]
12
Out[86]:
3

Reshape


In [ ]:
# tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]
# tensor 't' has shape [9]
reshape(t, [3, 3]) ==> [[1, 2, 3],
                        [4, 5, 6],
                        [7, 8, 9]]

In [88]:
t= [1, 2, 3, 4, 5, 6, 7, 8, 9]
# tensor 't' has shape [9]
sess.run(tf.reshape(t, [3, 3]))


Out[88]:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]], dtype=int32)

In [91]:
# tensor 't' has shape [2, 2, 2]

t= [[[1, 1], [2, 2]],
    [[3, 3], [4, 4]]]

sess.run(tf.reshape(t, [2, 4]))


Out[91]:
array([[1, 1, 2, 2],
       [3, 3, 4, 4]], dtype=int32)

MeshGrid


In [96]:
### tf.meshgrid(*args, **kwargs)

In [97]:
x = [1, 2, 3]
y = [4, 5, 6]
sess.run(tf.meshgrid(x,y))


Out[97]:
[array([[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]], dtype=int32), array([[4, 4, 4],
        [5, 5, 5],
        [6, 6, 6]], dtype=int32)]

Slicing and Joining


In [98]:
## tf.slice(input_, begin, size, name=None)                Extracts a slice from a tensor.

In [99]:
input     = [[[1, 1, 1],[2, 2, 2]],
            [[3, 3, 3], [4, 4, 4]],
            [[5, 5, 5], [6, 6, 6]]]

In [104]:
print(sess.run(tf.slice(input, [1, 0, 0], [1, 1, 3])))


[[[3 3 3]]]

In [103]:
sess.run(tf.slice(input, [1, 0, 0], [1, 2, 3]))


Out[103]:
array([[[3, 3, 3],
        [4, 4, 4]]], dtype=int32)

SPLIT


In [116]:
### Splits a tensor into num_split tensors along one dimension.

In [117]:
import numpy as np
values=np.random.rand(5,30)
split0, split1, split2 = tf.split(1, 3, values)

In [118]:
sess.run(split0)


Out[118]:
array([[ 0.44924159,  0.37360267,  0.47832511,  0.7143224 ,  0.98518777,
         0.20598492,  0.93705385,  0.82026207,  0.20451826,  0.88182826],
       [ 0.71443003,  0.27634112,  0.51171081,  0.92870473,  0.32600575,
         0.80883048,  0.67374   ,  0.27520197,  0.68602859,  0.52805298],
       [ 0.25569944,  0.70710029,  0.15859885,  0.54075931,  0.56502724,
         0.12297512,  0.30518993,  0.65242676,  0.39516749,  0.96425345],
       [ 0.73673174,  0.29317802,  0.56603424,  0.87338748,  0.33677064,
         0.54074344,  0.37880523,  0.30712992,  0.18786031,  0.57749722],
       [ 0.41075745,  0.93489652,  0.57254638,  0.50842384,  0.894683  ,
         0.42459464,  0.43396195,  0.70989516,  0.04443195,  0.92143278]])

In [119]:
print(sess.run(tf.size(split0)))
sess.run(tf.shape(split0))


50
Out[119]:
array([ 5, 10], dtype=int32)

Tiling

Splits a tensor into num_split tensors along one dimension.


In [ ]:
## tf.tile(input, multiples, name=None)

In [124]:
x=['a', 'b', 'c', 'd']
sess.run(tf.tile(x,[2]))


Out[124]:
array([b'a', b'b', b'c', b'd', b'a', b'b', b'c', b'd'], dtype=object)

PAD

Pads a tensor.


In [125]:
## tf.pad(tensor, paddings, mode='CONSTANT', name=None)

t= [[1, 2, 3], [4, 5, 6]]

In [129]:
paddings=[[1, 1,], [2, 2]]
sess.run(tf.pad(t,paddings))


Out[129]:
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 2, 3, 0, 0],
       [0, 0, 4, 5, 6, 0, 0],
       [0, 0, 0, 0, 0, 0, 0]], dtype=int32)

Concat

Concatenates tensors along one dimension.


In [130]:
t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]

In [131]:
sess.run(tf.concat(0, [t1, t2]))


Out[131]:
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12]], dtype=int32)

In [132]:
sess.run(tf.concat(1, [t1, t2]))


Out[132]:
array([[ 1,  2,  3,  7,  8,  9],
       [ 4,  5,  6, 10, 11, 12]], dtype=int32)

In [ ]:
# tensor t3 with shape [2, 3]
# tensor t4 with shape [2, 3]
tf.shape(tf.concat(0, [t3, t4])) ==> [4, 3]
tf.shape(tf.concat(1, [t3, t4])) ==> [2, 6]

Pack

tf.pack(values, axis=0, name='pack')

Packs a list of rank-R tensors into one rank-(R+1) tensor.


In [137]:
x= [1, 4]
y= [2, 5]
z= [3, 6]
sess.run(tf.pack([x, y, z]))


Out[137]:
array([[1, 4],
       [2, 5],
       [3, 6]], dtype=int32)

In [139]:
sess.run(tf.pack([x, y, z], axis=1))


Out[139]:
array([[1, 2, 3],
       [4, 5, 6]], dtype=int32)

In [ ]: