# How to find the size of a tensor in bytes?

Supposing I have a tensor named encoding which is of the shape (1,32) Now I would like to find out the size of the tensor in bytes not the shape of it. So basically I want to know how much memory it takes.

I found this StackOverflow question which does this exact thing but in TensorFlow 1. So how can I accomplish the same?? Here is the link ===> https://stackoverflow.com/questions/36966316/how-to-get-the-dimensions-of-a-tensor-in-tensorflow-at-graph-construction-time

The SO question uses the Tensorflow.contrib module which (sadly) has been removed. So anyone know a way?

## 1 Answer

Step 1 :
Get the dtype of the tensor.
This will tell you about the number of bytes e.g.float64 is 64 bits = 8 Bytes.

Step 2
Get the shape of the Tensor.
This will give you the number of place-holders of the dtype.
lets's assume shape = m x n x p
Count of the placeholders is C = m * n * p
Memory = 8 * C => Memory = 8 *m * n * p Bytes.

See this excerpt from Deep Learning with Python, by Francois Chollet

A 60-second, 144 × 256 YouTube video clip sampled at 4 frames per second would have 240 frames. A batch of four such video clips would be stored in a tensor of shape (4, 240, 144, 256, 3). That’s a total of 106,168,320 values! If the dtype of the tensor was float32, then each value would be stored in 32 bits, so the tensor would represent 405 MB

• dtype of tensor is tensorflow.python.framework.ops.Tensor and the shape of the tensor is TensorShape([None, 150, 100, 144]) But nowhere it mentions that it is float64 or not... – neel g Apr 25 at 16:42
• Which command you are using to get the dtype. stackoverflow.com/questions/43978179/… – 10xAI Apr 25 at 18:30
• thanx, I got it. Mine is float32 so it should have half the no. of bytes? – neel g Apr 26 at 11:04
• 32 bits = 4 Bytes.....Your total memory = ~150*100*144*32 Bits......Please accept/+1 the answer if it helped. It helps the community to grow. – 10xAI Apr 26 at 11:43