What is the tensor?

When you do programming of tensorflow, Tensor is everything. I mean the programming of Tensorflow is building a graph that freely transport tensors. Tensor in Tensorflow is a generalization of vectos and matrices. it potentially contains higher dimensions. Internally, Tensorflow representas tensors as n-dimension arrays of base datatype(string, int32, float32 and so on).

A tf.Tensor has two properties :

  • a data type(float32, int32 or string and so on): each element in the Tensor has the same datatype.

  • a shape: The number of dimension tensor has and the size of each dimension

    • shape can be partially or fully known.

There are some types of tensor :

  • tf.Variable, tf.Constant, tf.Placeholder, tf.SparseTensor.

In the key point, with exceptoin of tf.Variable, The value of a tensor is immutable, which means that in the context of a single execution tensors only have a single value.

Rank vs Shape

  • rank: tensor object’s number of dimensions.
# Rank 0 (scalar)
animal = tf.Variable("Elephant", tf.string)
integer = tf.Variable(451, tf.int32)
# Rank 1 (1-dimension, vector)
floating_array  = tf.Variable([3.14159, 2.71828], tf.float32)
# Rank 2 (2-dimension, matrix)
# Normally, A rank 2 tensor object consists of as least one row and at least one column.
matrix = tf.Variable([[7],[11]], tf.int32)
# To check what version of rank each variables is?
rank0 = tf.rank(animal)
rank1 = tf.rank(floating_array)
rank2 = tf.rank(matrix)
  • shape: tensor object’s the number of elements in each dimension.
# Every element in a tensor is one 
rank_three_tensor = tf.ones([3, 4, 5])
# To check the shape of a tensor
tf.shape(rank_three_tensor)
# To reshape of a tensor(rank 2)
matrix = tf.reshape(rank_three_tensor, [6, 10])
# To Check what kind of shape matrix has
tf.shape(matrix)
tf.shape(matrix)[0]
tf.shape(matrix)[1]

There are three notation used in Tensorflow:

  • rank, shape, dimencsion number
Rank Shape dimension number Example
0 [] 0-D A 0-D tensor, A scalar.
1 [D0] 1-D A 1-D tensor with shape[3].
2 [D0, D1] 2-D A 2-D tensor with shape[3,2]
n [D0, D0, … , Dn-1] n-D A n-D tensor with shape[D0, D1,…, Dn-1]

If you don’t specify datatye of python object, when you create a tf.tensor, Tensorflow automatically choose datatype that can represent your data.

  • Python’s Integers –> tf.int32

  • Python’s Floating point numbers —> tf.float32

If you want to check an executed example code above, visit 01.tensor of hyunyoung2 git repository

Reference