将tensor转array

版权声明:本文为博主原创文章,转载请附上博文链接! https://blog.csdn.net/weixin_38314865/article/details/84791234

如果对tensor使用extend方法,如下面这个例子:

import tensorflow as tf

a = tf.convert_to_tensor([1,2,3])
b = []
with tf.Session() as sess:
    b.extend(a)

会报错“Tensor objects are only iterable when eager execution is enabled. To iterate over this tensor use tf.map_fn.”

需要把tensor转换为数组,通过tensor.eval()方法来转换,如下面例子:

a = tf.convert_to_tensor([1,2,3])
b = []
with tf.Session() as sess:
    c = a.eval()
    b.extend(c)
    print(type(c))  # <class 'numpy.ndarray'>

注意:这一句要放到sess里面

c = a.eval()

猜你喜欢

转载自blog.csdn.net/weixin_38314865/article/details/84791234
今日推荐