nj2237
nj2237

Reputation: 1278

How to use num_elements from TensorFlow?

I just want the number of elements in my tensor, regardless of shape.

I see in the documentation num_elements serves my purpose. However, if I try to use it as myTensor.num_elements(), I get this error:

AttributeError: 'Tensor' object has no attribute 'num_elements'

How do I do this?

Upvotes: 0

Views: 672

Answers (1)

zihaozhihao
zihaozhihao

Reputation: 4475

This method belongs to TensorShape instead of Tensor. You can get the number of elements like this

import tensorflow as tf
a = tf.random.uniform((1,16,23))
a.shape.num_elements()
# 368

Upvotes: 1

Related Questions