Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow: How to convert scalar tensor to scalar variable in python?

Tags:

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]) 
like image 291
Fei Avatar asked May 05 '16 11:05

Fei


People also ask

Can you convert a tensor to Numpy array?

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.

What is scalar in TensorFlow?

A scalar contains a single value, and no "axes". # This will be an int32 tensor by default; see "dtypes" below.


1 Answers

In Tensorflow 2.0+, it's as simple as:

my_tensor.numpy() 
like image 151
Eric McLachlan Avatar answered Sep 18 '22 21:09

Eric McLachlan