tensor 与 array 的相互转化

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_38145317/article/details/88718685
import tensorflow as tf
import numpy as np

a=np.array([[1,2,3],[4,5,6],[7,8,9]])
print (a)
# [[1 2 3]
#  [4 5 6]
#  [7 8 9]]
b=tf.constant(a) #将array a 转换为tensor
tensor_a=tf.convert_to_tensor(a) #将array a 转换为tensor

print(b) #Tensor("Const:0", shape=(3, 3), dtype=int64)
with tf.Session() as sess:
    print(b.eval()) #b.eval()就得到tensor的数组形式
    # [[1 2 3]
    #  [4 5 6]
    #  [7 8 9]]
    tensor_a=tf.convert_to_tensor(a) #将array a 转换为tensor
    print ('现在转换为tensor了...',tensor_a)#现在转换为tensor了... Tensor("Const_1:0", shape=(3, 3), dtype=int64)

猜你喜欢

转载自blog.csdn.net/weixin_38145317/article/details/88718685
今日推荐