首页 > 解决方案 > 如何将张量转换为 numpy 数组?

问题描述

我尝试使用 conver_to_tensor 函数

k = np.array([1,5,6,9,])
print(list(k))
k = list(k)
k = tf.convert_to_tensor(k)
k

输出:

[1, 5, 6, 9]
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([1, 5, 6, 9], dtype=int32)>

在这里,我将它转换为张量,但在里面它仍然只包含在 numpy 数组中。有没有办法完全转换为张量?基本上我想要一个张量包含一个数字列表/数组。

标签: pythonnumpytensorflow

解决方案


我认为这是您期望得到的:

k = np.array([1,5,6,9,])
print(tf.convert_to_tensor(k))  #(remove `list()` while converting to tensor)

输出:

tf.Tensor([1 5 6 9], shape=(4,), dtype=int64)

推荐阅读