小编wew*_*lie的帖子

Tensorflow:如何将EagerTensor转换为numpy数组?

使用标准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()
Run Code Online (Sandbox Code Playgroud)

输出:

[10 11 12 13 14]
<class 'numpy.ndarray'>
Run Code Online (Sandbox Code Playgroud)

急切执行:

import tensorflow as tf

tf.enable_eager_execution() # requires r1.7

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

print(y)
print(type(y))
Run Code Online (Sandbox Code Playgroud)

输出:

tf.Tensor([10 11 12 13 14], shape=(5,), dtype=int64)
<class 'EagerTensor'>
Run Code Online (Sandbox Code Playgroud)

如果我尝试y.eval(),我会NotImplementedError: eval not supported …

python tensorflow

16
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×1

tensorflow ×1