1
votes

What is the shape of a tensor in tensorflow? What does it represent?

I've read this and what I understood is that the shape of a tensor is the number of elements for every dimension of a tensor, but in the first code snippet:

[[1., 2., 3.], [4., 5., 6.]] # a rank 2 tensor; a matrix with shape [2, 3] 

It says that the shape is 2 and 3 but in the first dimension of the tensor there are three elements and not 2, why?

1

1 Answers

4
votes

The shape is the size of the dimensions.

[[1., 2., 3.], [4., 5., 6.]] # a rank 2 tensor; a matrix with shape [2, 3] 

Because it has 2 rows and 3 columns. So it is not about the elements but the size of each dimension. So rank is the number of dimensions, and shape is size of the dimension.

If you look at the next example

[[[1., 2., 3.]], [[7., 8., 9.]]] # a rank 3 tensor with shape [2, 1, 3]

It has 2 rows, 1 set of elements in this dimension, and 3 columns.

This resource might help https://www.tensorflow.org/versions/r1.1/programmers_guide/dims_types https://www.gsrikar.com/2017/06/what-is-tensor-rank-and-tensor-shape.html