如何解决tensorflow-gpu numpy 出现 AttributeError Tensor object has no attribute numpy
在TensorFlow中,Tensor对象没有numpy()方法。如果您想将Tensor对象转换为numpy数组,可以使用以下方法:
1.使用eval()方法
使用Tensor.eval()方法可以将Tensor对象转换为numpy数组。例如:
import tensorflow as tf import numpy as np
a = tf.constant([1, 2, 3]) with tf.Session() as sess: print(a.eval()) # [1 2 3] print(type(a.eval())) # <class 'numpy.ndarray'>
2.使用numpy()方法
在TensorFlow 1.15及更高版本中,您可以使用tf.make_ndarray()方法将Tensor对象转换为numpy数组。例如:
import tensorflow as tf import numpy as np
a = tf.constant([1, 2, 3]) print(tf.make_ndarray(a)) # [1 2 3] print(type(tf.make_ndarray(a))) # <class 'numpy.ndarray'>
注意:在使用numpy()方法时,确保您的Tensor对象已经被计算(即在Session中运行)。否则,您将会得到一个错误。
原文地址: https://www.cveoy.top/t/topic/bJJA 著作权归作者所有。请勿转载和采集!