In Tensorflow, I'd like to convert a scalar tensor to an integer. Is it possible to do?
I need to create a loop and the index of the loop is a scalar tensor, and inside the loop body, I want to use the index to access an entry in a tensor array.
For example:
idx = tf.constant(0) c = lambda i : tf.less(i, 10) def body(idx) : i = # convert idx to int b = weights[i] # access an entry in a tensor array, tensor cannot be used directly .... return idx+1 tf.while_loop(c, body, [idx])
To convert the tensor into a numpy array first we will import the eager_execution function along with the TensorFlow library. Next, we will create the constant values by using the tf. constant() function and, then we are going to run the session by using the syntax session=tf. compat.
A scalar contains a single value, and no "axes". # This will be an int32 tensor by default; see "dtypes" below.
In Tensorflow 2.0+, it's as simple as:
my_tensor.numpy()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With