1

I try to convert the variable type to Tensor("strided_slice_1:0", shape=(24, 24, 3), dtype=float32) to a NumPy matrix.

image=images_train[1]

with tf.Session() as sess:
    array1 = sess.run(image)

But the program is not executed after this.

1 Answer 1

1

Eager Execution is enabled by default in TF 2.0, so you need to call .numpy() on the Tensor object.

import tensorflow as tf
import numpy as np
print(tf.__version__)

#tf.executing_eagerly()

def tensor_to_array(array_value):
    return array_value.numpy()

a = tf.constant([5.0,6.0,7.0])
b = tf.constant([6.0,8.0,9.0])
c = a * b
print(c)
print(tensor_to_array(c))
print(type(tensor_to_array(c)))

Output:

2.2.0
tf.Tensor([30. 48. 63.], shape=(3,), dtype=float32)
[30. 48. 63.]
<class 'numpy.ndarray'>

If Eager Execution is disabled, you can build a graph and then run it through tf.compat.v1.Session in TF 2.x and tf.Session in TF 1.x

%tensorflow_version 1.x

import tensorflow as tf
print(tf.__version__)

with tf.Session() as sess:
  a = tf.constant([5.0,6.0,7.0])
  b = tf.constant([6.0,8.0,9.0])
  c = a * b
  print(c)
  print(sess.run(c))
  print(type(sess.run(c)))

Output:

1.15.2
Tensor("mul_1:0", shape=(3,), dtype=float32)
[30. 48. 63.]
<class 'numpy.ndarray'>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.