Manipulating Tensors


In [1]:
import tensorflow as tf

Vector Addition


In [2]:
with tf.Graph().as_default():
    fibonacci = tf.constant([1, 1, 2, 3, 5, 8], dtype=tf.int32)
    ones = tf.ones([6], dtype=tf.int32)
    beyond_fibonacci = tf.add(fibonacci, ones)
    
    with tf.Session() as sess:
        print beyond_fibonacci.eval()


[2 2 3 4 6 9]

Tensor Shapes


In [3]:
with tf.Graph().as_default():
    scalar = tf.zeros([])
    vector = tf.zeros([3])
    matrix = tf.zeros([2,3])
    
    with tf.Session() as sess:
        print 'scalar has shape', scalar.get_shape(), 'and value: \n', scalar.eval()
        print 'vector has shape', vector.get_shape(), 'and value: \n', vector.eval()
        print 'matrix has shape', matrix.get_shape(), 'and value: \n', matrix.eval()


scalar has shape () and value: 
0.0
vector has shape (3,) and value: 
[ 0.  0.  0.]
matrix has shape (2, 3) and value: 
[[ 0.  0.  0.]
 [ 0.  0.  0.]]

Broadcasting


In [4]:
with tf.Graph().as_default():
    fibonacci = tf.constant([1, 1, 2, 3, 5, 8], dtype=tf.int32)
    ones = tf.ones(1, dtype=tf.int32)
    beyond_fibonacci = tf.add(fibonacci, ones)
    
    with tf.Session() as sess:
        print beyond_fibonacci.eval()


[2 2 3 4 6 9]

Matrix Multiplication


In [5]:
with tf.Graph().as_default():
    x = tf.constant([[5, 2, 4, 3], [5, 1, 6, -2], [-1, 3, -1, -2]], dtype=tf.int32)
    y = tf.constant([[2, 2], [3, 5], [4, 5], [1, 6]], dtype=tf.int32)
    xy = tf.matmul(x, y)
    
    with tf.Session() as sess:
        print xy.eval()


[[35 58]
 [35 33]
 [ 1 -4]]

Tensor Reshaping


In [6]:
with tf.Graph().as_default():
    a = tf.constant([5, 2, 4, 3], dtype=tf.int32)
    b = tf.reshape(a, [2, 2])
    c = tf.reshape(b, [4, 1])
    
    with tf.Session() as sess:
        print 'a', a.eval()
        print 'b', b.eval()
        print 'c', c.eval()


a [5 2 4 3]
b [[5 2]
 [4 3]]
c [[5]
 [2]
 [4]
 [3]]

In [7]:
with tf.Graph().as_default():
    a = tf.constant([5, 3, 2, 7, 1, 4])
    b = tf.constant([4, 6, 3])
    
    c = tf.reshape(a, [2, 3])
    d = tf.reshape(b, [3, 1])
    
    cd = tf.matmul(c, d)
    
    with tf.Session() as sess:
        print 'a shape', a.get_shape(), 'value \n', a.eval(), '\n'
        print 'b shape', b.get_shape(), 'value \n', b.eval(), '\n'
        print 'c shape', c.get_shape(), 'value \n', c.eval(), '\n'
        print 'd shape', d.get_shape(), 'value \n', d.eval(), '\n'
        print 'cd shape', cd.get_shape(), 'value \n', cd.eval(), '\n'


a shape (6,) value 
[5 3 2 7 1 4] 

b shape (3,) value 
[4 6 3] 

c shape (2, 3) value 
[[5 3 2]
 [7 1 4]] 

d shape (3, 1) value 
[[4]
 [6]
 [3]] 

cd shape (2, 1) value 
[[44]
 [46]] 

Variables, Initialization and Assignment


In [8]:
g = tf.Graph()

In [9]:
with g.as_default():
    v = tf.Variable([3])
    w = tf.Variable(tf.random_normal([1], mean=1.0, stddev=0.35))

In [10]:
with g.as_default():
    with tf.Session() as sess:
        try:
            v.eval()
        except tf.errors.FailedPreconditionError as e:
            print "Caught expected error", e


Caught expected error Attempting to use uninitialized value Variable
	 [[Node: _send_Variable_0 = _Send[T=DT_INT32, client_terminated=true, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=-7334369538008366203, tensor_name="Variable:0", _device="/job:localhost/replica:0/task:0/cpu:0"](Variable)]]

In [11]:
with g.as_default():
    with tf.Session() as sess:
        initialization = tf.initialize_all_variables()
        sess.run(initialization)
        print v.eval()
        print w.eval()


[3]
[ 1.46645296]

In [12]:
with g.as_default():
    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        print w.eval()
        print w.eval()
        print w.eval()


[ 1.80774593]
[ 1.80774593]
[ 1.80774593]

In [13]:
with g.as_default():
    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        
        print v.eval()
        
        assignment = tf.assign(v,[7])
        print v.eval()
        
        sess.run(assignment)
        print v.eval()


[3]
[3]
[7]

In [ ]: