liubc
liubc

Reputation: 33

In eager mode, how to convert a tensor to a ndarray

How can I convert a tensor to a numpy array in eager mode? In eager mode, I do not need to create a session, so I cannot use .eval().

And I tried tf.constant(), it gives the following error:

TypeError: Failed to convert object of type <class 'tensorflow.python.ops.variables.Variable'> to Tensor. Contents: <tf.Variable 'filters_C:0' shape=(2, 2) dtype=float32_ref>. Consider casting elements to a supported type.

Here is the supporting code:

filters_C = tf.get_variable('filters_C',
                        shape=[2, 2],
                        initializer=tf.ones_initializer,
                        regularizer=None,
                        trainable=True)
filters_C = tf.constant(filters_C)

Upvotes: 2

Views: 2795

Answers (1)

P-Gn
P-Gn

Reputation: 24651

Simpy call the numpy method:

filters_C.numpy()

It is a property of the EagerTensor class, which is the subclass of Tensor that is used by default in eager execution, which explains why this property pops up then.

Upvotes: 2

Related Questions