python - 如何将张量转换为 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 数组中。有没有办法完全转换为张量?基本上我想要一个张量包含一个数字列表/数组。
解决方案
我认为这是您期望得到的:
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)
推荐阅读
- c++ - 在 C++ 中如何只打印二维向量的行?
- spring-boot - JackRabbit Oak:我的应用程序需要很长时间才能启动/重新启动
- c# - MassTransit - AWS SQS/SNS - 向 SNS 添加标签
- ios - 应用内自动续订订阅的服务器到服务器通知是否会在订阅自动续订时通知?(不在现场沙箱中)
- vue.js - 如何从重定向
- 使用 Vue js 将搜索组件结果到另一个页面的项目?
- angular - 在移动设备上从上到下滑动不起作用(锤子 js)
- reactjs - 从@fluentui/react-northstar@0.48.0 读取表单组件
- python - 为我的情节中的每条曲线提供独特的颜色
- git - 警告:在包文件中找不到符合条件的大 blob - 是否需要打包 repo?
- node.js - 执行 npm install 导致 fsevents 错误