如何查看张量tensor,并将其转换为numpy数据

版权声明:请征得博主同意后,再进行转载 https://blog.csdn.net/qq_34840129/article/details/86419020

在tensorflow 中一般数据都是用tensor来表示,而在python 中一般是用numpy包,然而有时候需要打印变量的数据,可用以下方法来打印:

一、

import tensorflow as tf
a = tf.constant(2.1) #定义tensor常量
with tf.Session() as sess:
    print (sess.run(a))

二、

还可以通过.eval函数可以把tensor转化为numpy类数据,程序如下:

import tensorflow as tf
a = tf.constant(2.1) #定义tensor常量
sess=tf.Session()
b=a.eval(session=sess)
print (b)
print (type(b))

三、

此外,还可以通过tf.convert_to_tensor函数可以把numpy转化为tensor 类数据:

import tensorflow as tf
a = tf.constant(2.1) #定义tensor常量
sess=tf.Session()
b=a.eval(session=sess)
b=tf.convert_to_tensor(a)
print (b)

猜你喜欢

转载自blog.csdn.net/qq_34840129/article/details/86419020