当前位置: 首页>>技术问答>>正文


在TensorFlow中如何将Tensor张量转换为numpy数组?

在Tensorflow中,使用Python,如何将张量(Tensor)转换为numpy数组呢?

tensorflow tensor

最佳解决办法

Session.runeval返回的任何张量都是NumPy数组。

>>> print(type(tf.Session().run(tf.constant([1,2,3]))))
<class 'numpy.ndarray'>

要么:

>>> sess = tf.InteractiveSession()
>>> print(type(tf.constant([1,2,3]).eval()))
<class 'numpy.ndarray'>

或者等同地:

>>> sess = tf.Session()
>>> with sess.as_default():
>>>    print(type(tf.constant([1,2,3]).eval()))
<class 'numpy.ndarray'>

numpy array数组

参考资料

本文由《纯净天空》出品。文章地址: https://vimsky.com/article/3725.html,未经允许,请勿转载。