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?


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

  • $\begingroup$ 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... $\endgroup$ – neel g Apr 25 '20 at 16:42
  • $\begingroup$ Which command you are using to get the dtype. stackoverflow.com/questions/43978179/… $\endgroup$ – 10xAI Apr 25 '20 at 18:30
  • $\begingroup$ thanx, I got it. Mine is float32 so it should have half the no. of bytes? $\endgroup$ – neel g Apr 26 '20 at 11:04
  • $\begingroup$ 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. $\endgroup$ – 10xAI Apr 26 '20 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.