Python Tensorflow:如何将张量转换为numpy数组?

Python Tensorflow:如何将张量转换为numpy数组?,python,tensorflow,Python,Tensorflow,使用标准Tensorflow: import tensorflow as tf x = tf.convert_to_tensor([0,1,2,3,4], dtype=tf.int64) y = x + 10 sess = tf.InteractiveSession() sess.run([ tf.local_variables_initializer(), tf.global_variables_initializer(), ]) coord = tf.train.Coor

使用标准Tensorflow:

import tensorflow as tf

x = tf.convert_to_tensor([0,1,2,3,4], dtype=tf.int64)
y = x + 10

sess = tf.InteractiveSession()
sess.run([
    tf.local_variables_initializer(),
    tf.global_variables_initializer(),
])
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)

z = y.eval(feed_dict={x:[0,1,2,3,4]})
print(z)
print(type(z))

coord.request_stop()
coord.join(threads)
sess.close()
输出:

[10 11 12 13 14]
<class 'numpy.ndarray'>
tf.Tensor([10 11 12 13 14], shape=(5,), dtype=int64)
<class 'EagerTensor'>
输出:

[10 11 12 13 14]
<class 'numpy.ndarray'>
tf.Tensor([10 11 12 13 14], shape=(5,), dtype=int64)
<class 'EagerTensor'>
tf.Tensor([10 11 12 13 14],shape=(5,),dtype=int64)
如果我尝试
y.eval()
,我会得到
NotImplementedError:eval不受急切张量的支持。没有办法转换这个吗?这使得急切的Tensorflow毫无价值

编辑:

有一个函数
tf.make\ndarray
应该将张量转换为numpy数组,但它会导致
AttributeError:“热切张量”对象没有属性“tensor\u shape”
有一个
.numpy()
函数,您可以使用,或者您也可以执行
numpy.array(y)
。例如:

import tensorflow as tf
import numpy as np

tf.enable_eager_execution()

x = tf.constant([1., 2.])
print(type(x))            # <type 'EagerTensor'>
print(type(x.numpy()))    # <type 'numpy.ndarray'>
print(type(np.array(x)))  # <type 'numpy.ndarray'>
将tensorflow导入为tf
将numpy作为np导入
tf.enable_eager_execution()
x=tf.常数([1,2.])
打印(x型)
打印(键入(x.numpy())#
打印(类型(np.数组(x))#


希望能有所帮助。

在不启用“急切执行”的情况下,是否可以执行此操作?