# Declaring tensors in TensorFlow

[Requirement: Tensorflow and NumPy installed on Python +3.5]

[Requirement: import tensorflow as tf]
[Requirement: import numpy as np]

Tensors are the primary data structure we use in TensorFlow, and, as Wikipedia describes them, “tensors are geometric objects that describe linear relations between geometric vectors, scalars and other tensors”. Tensors can be described as multidimensional arrays, embracing the concepts of scalar, vector and matrix, without taking in consideration the coordinate system

The tensor order is the number of indexes we need to specify one element; so, an scalar will be an order 0 tensor, a vector will be an order 1 tensor, a matriz an order 2 tensor, and so on.

Now we know what a tensor is, I’m going to show you how we can declare tensors in TensorFlow.

### 1. Fixed tensors

>>> zeros_tsr = tf.zeros([5, 5], dtype=tf.int32, name='zeros5x5')
>>> print(zeros_tsr)
Tensor("zeros5x5:0", shape=(5, 5), dtype=int32)
>>> tf.InteractiveSession().run(zeros_tsr)
array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
>>> ones_tsr = tf.ones([5, 5], dtype=tf.float32, name='ones5x5')
>>> print(ones_tsr)
Tensor("ones5x5:0", shape=(5, 5), dtype=float32)
>>> tf.InteractiveSession().run(ones_tsr)
array([[ 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1.]], dtype=float32)
>>> filled_tsr = tf.fill([5, 5], 123, name='filled123')
>>> print(filled_tsr)
Tensor("filled123:0", shape=(5, 5), dtype=int32)
>>> tf.InteractiveSession().run(filled_tsr)
array([[123, 123, 123, 123, 123],
[123, 123, 123, 123, 123],
[123, 123, 123, 123, 123],
[123, 123, 123, 123, 123],
[123, 123, 123, 123, 123]])
>>> filled2_tsr = tf.constant(123, shape=[5, 5], name='filled123_2', dtype=tf.int16)
>>> print(filled2_tsr)
Tensor("filled123_2:0", shape=(5, 5), dtype=int16)
>>> tf.InteractiveSession().run(filled2_tsr)
array([[123, 123, 123, 123, 123],
[123, 123, 123, 123, 123],
[123, 123, 123, 123, 123],
[123, 123, 123, 123, 123],
[123, 123, 123, 123, 123]], dtype=int16)
>>> constant_tsr = tf.constant([1, 2, 3], name='vector')
>>> print(constant_tsr)
Tensor("vector:0", shape=(3,), dtype=int32)
>>> tf.InteractiveSession().run(constant_tsr)
array([1, 2, 3])

### 2. Copying dimensions

It is necessary to previously define tensors from which we are going to copy dimensions.

>>> zeros_similar = tf.zeros_like(constant_tsr)
>>> print(zeros_similar)
Tensor("zeros_like:0", shape=(3,), dtype=int32)
>>> tf.InteractiveSession().run(zeros_similar)
array([0, 0, 0])
>>> ones_similar = tf.ones_like(constant_tsr)
>>> print(ones_similar)
Tensor("ones_like:0", shape=(3,), dtype=int32)
>>> tf.InteractiveSession().run(ones_similar)
array([1, 1, 1])

### 3. Sequence tensors

# This tensor defines 7 regular intervals between 0 and 2, 1st param should be float32/64
>>> linear_tsr = tf.linspace(0., 2, 7)
>>> print(linear_tsr)
Tensor("LinSpace_5:0", shape=(7,), dtype=float32)
>>> tf.InteractiveSession().run(linear_tsr)
array([ 0. , 0.33333334, 0.66666669, 1. , 1.33333337,
1.66666675, 2. ], dtype=float32)
# This tensor defines 4 elements between 6 and 17, with a delta of 3
>>> int_seq_tsr = tf.range(start=6, limit=17, delta=3)
>>> print(int_seq_tsr)
Tensor("range_1:0", shape=(4,), dtype=int32)
>>> tf.InteractiveSession().run(int_seq_tsr)
array([ 6, 9, 12, 15])

### 4. Random tensors

# Random numbers from uniform distribution
>>> rand_unif_tsr = tf.random_uniform([5, 5], minval=0, maxval=1)
>>> print(rand_unif_tsr)
Tensor("random_uniform:0", shape=(5, 5), dtype=float32)
>>> tf.InteractiveSession().run(rand_unif_tsr)
array([[ 0.81911492, 0.01300693, 0.47359812, 0.50176537, 0.27962267],
[ 0.47069478, 0.7151444 , 0.56615186, 0.5431906 , 0.45684898],
[ 0.00939894, 0.19539773, 0.37774849, 0.08342052, 0.87758613],
[ 0.46707201, 0.32422674, 0.90311491, 0.42251813, 0.3496896 ],
[ 0.75080729, 0.48055971, 0.49421525, 0.77542639, 0.99400854]], dtype=float32)
# Random numbers from normal distribution
>>> rand_normal_tsr = tf.random_normal([5, 5], mean=0.0, stddev=1.0)
>>> print(rand_normal_tsr)
Tensor("random_normal:0", shape=(5, 5), dtype=float32)
>>> tf.InteractiveSession().run(rand_normal_tsr)
array([[ 2.13312769, 2.46189046, -0.34942248, -0.39776739, 1.79048693],
[ 0.22045165, 0.05164593, -1.05943978, -0.32593197, -1.66411078],
[-0.94263768, 1.77081263, -0.22290479, -0.24516548, 1.26560402],
[-1.14855564, -0.89211422, 1.10751343, -2.17768288, -1.07004178],
[ 0.635813 , 0.24745767, 0.80117846, -0.25315794, -1.88987064]], dtype=float32)
# Random numbers from normal distribution, limitating values within 2 SD from mean
>>> trunc_norm_tsr = tf.truncated_normal([5, 5], mean=0.0, stddev=1.0)
>>> print(trunc_norm_tsr)
Tensor("truncated_normal:0", shape=(5, 5), dtype=float32)
>>> tf.InteractiveSession().run(trunc_norm_tsr)
array([[-1.07923198, 0.66122717, -0.98569149, -0.11161296, -1.39560068],
[-1.04248953, 1.83589756, 0.00709002, -0.70119679, 0.81637812],
[-1.14046562, 0.65371871, 0.25081205, 1.59802651, -0.17030434],
[ 0.61106592, -0.39884251, -0.02136615, 0.36585283, -1.45338166],
[-0.64861351, 0.930076 , -0.1549242 , 1.45601475, 0.56357914]], dtype=float32)
# Shuffles existing tensor
>>> seq = tf.linspace(0., 7, 8)
>>> tf.InteractiveSession().run(seq)
array([ 0., 1., 2., 3., 4., 5., 6., 7.], dtype=float32)
>>> tf.InteractiveSession().run(tf.random_shuffle(seq))
array([ 5., 0., 4., 1., 6., 7., 2., 3.], dtype=float32)
# Randomicaly crops existing tensor to specified dimension
>>> tf.InteractiveSession().run(tf.random_crop(seq, [3,]))
array([ 2., 3., 4.], dtype=float32)

### 5. Converted from NumPy

>>> np_array = np.array([[1, 2], [3, 4]])
>>> np_tsr = tf.convert_to_tensor(np_array, dtype=tf.int32)
>>> print(np_tsr)
Tensor("Const:0", shape=(2, 2), dtype=int32)
>>> tf.InteractiveSession().run(np_tsr)
array([[1, 2],
[3, 4]])

Once you have created your desired tensor, you should wrap it as a TensorFlow Variable, like this:

seq_var = tf.Variable(seq)

That’s all for today. See you next time with more TensorFlow!

Share