In [1]:
import tensorflow as tf

In [2]:
vec = tf.constant([7,7], name='vector')

In [3]:
mat = tf.constant([[7,7],[9,9]], name='matrix')

create tensors whose elements are of a specific value


In [4]:
shape_tensor = tf.zeros([2,3],tf.int32)

In [5]:
with tf.Session() as ses:
    print ses.run(shape_tensor)


[[0 0 0]
 [0 0 0]]

tensor of shape and type (unless type is specified) as the input_tensor but all elements are zeros


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

In [7]:
zeroslike_tensor = tf.zeros_like(input_tensor_model)

In [8]:
with tf.Session() as ses:
    print ses.run(zeroslike_tensor)


[[0 0]
 [0 0]
 [0 0]]

tensor of shape and all elements are ones


In [9]:
shape_one_tensor = tf.ones([3,3],tf.int32)

In [10]:
with tf.Session() as ses:
    print ses.run(shape_one_tensor)


[[1 1 1]
 [1 1 1]
 [1 1 1]]

tensor of shape and type (unless type is specified) as the input_tensor but all elements are ones.


In [12]:
input_tensor_model_ones = [[1,2],[3,4],[5,6]]

In [13]:
onelikes = tf.ones_like(input_tensor_model_ones)

In [14]:
with tf.Session() as ses:
    print ses.run(onelikes)


[[1 1]
 [1 1]
 [1 1]]

create a tensor filled with a scalar value


In [15]:
tensor_scalar = tf.fill([3, 3], 8)

In [16]:
with tf.Session() as ses:
    print ses.run(tensor_scalar)


[[8 8 8]
 [8 8 8]
 [8 8 8]]

creating constants that are sequences


In [19]:
tensor_lin = tf.linspace(50.0 ,55.0, 5 , name="linspace")

In [20]:
with tf.Session() as ses:
    print ses.run(tensor_lin)


[ 50.    51.25  52.5   53.75  55.  ]

create a sequence of numbers that begins at start and extends by increments of delta up to but not including limit


In [23]:
tensor_range = tf.range(3 ,15 , 3)

In [24]:
with tf.Session() as ses:
    print ses.run(tensor_range)


[ 3  6  9 12]

TensorFlow sequences are not iterable


In [26]:
for a in range(4):
    print a


0
1
2
3

In [27]:
for b in tf.range(4):
    print b


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-5babc1419aff> in <module>()
----> 1 for b in tf.range(4):
      2     print b

/Users/nickbortolotti/tensordev/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in __iter__(self)
    474       TypeError: when invoked.
    475     """
--> 476     raise TypeError("'Tensor' object is not iterable.")
    477 
    478   def __bool__(self):

TypeError: 'Tensor' object is not iterable.

In [ ]: