【Tensorflow】将Eager Tensors转化为numpy数组

将Eager Tensors转化为numy数组

某些时候需要将numpy数组转化为tensor类型进行操作,这时候会使用 tf.convert_to_tensor()进行转化:

import tensorflow as tf
import numpy as np

numpyData = np.random.random(5)
tensorData = tf.convert_to_tensor(numpyData, dtype=tf.float32)

此时tensorData变为了Eager Tensors类型,但是当我们想要通过经常使用的tensorData.eval()操作将其转化为numpy数组时,这是会报错,对某些需要来说这就会很尴尬。

解决方法:
使用.numpy()函数,或者也可以执行numpy.array(tensorData )

numpyOut = numpyData .numpy()
或:
numpyOut = numpy.array(tensorData )

猜你喜欢

转载自blog.csdn.net/phdongou/article/details/113816571