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?
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