TypeError: can#39;t convert np.ndarray of type numpy.object_(类型错误:无法转换 numpy.object_ 类型的 np.ndarray)
本文介绍了类型错误:无法转换 numpy.object_ 类型的 np.ndarray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将 numpy ndarry 转换为火炬张量?
How to convert a numpy ndarry to torch tensor?
这是我的数据:
array([array([-0.4287 , -1.193 , -2.156 , -0.2264 , -1.978 , -1.101 , -3.395 , 0.2974 ], dtype=float16),
array([-0.3386 , 1.398 , -1.083 , 0.2961 , -0.7354 , -1.326 , -4.33 , 0.6284 ], dtype=float16)],
dtype=object)
推荐答案
很难正确回答,因为您没有向我们展示您如何尝试去做.从您的错误消息中,我可以看到您尝试将包含对象的 numpy 数组转换为火炬张量.这不起作用,您将需要数字数据类型:
It is difficult to answer properly since you do not show us how you try to do it. From your error message I can see that you try to convert a numpy array containing objects to a torch tensor. This does not work, you will need a numeric data type:
import torch
import numpy as np
# Your test array without 'dtype=object'
a = np.array([
np.array([-0.4287 , -1.193 , -2.156 , -0.2264 , -1.978 , -1.101 , -3.395 , 0.2974 ], dtype=np.float16),
np.array([-0.3386 , 1.398 , -1.083 , 0.2961 , -0.7354 , -1.326 , -4.33 , 0.6284 ], dtype=np.float16)
])
print(a.dtype) # This should not be 'object'
b = torch.from_numpy(a)
print(b)
输出
float16
tensor([[-0.4287, -1.1934, -2.1562, -0.2264, -1.9775, -1.1006, -3.3945, 0.2974],
[-0.3386, 1.3984, -1.0830, 0.2961, -0.7354, -1.3262, -4.3281, 0.6284]],
dtype=torch.float16)
这篇关于类型错误:无法转换 numpy.object_ 类型的 np.ndarray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:类型错误:无法转换 numpy.object_ 类型的 np.ndarray


基础教程推荐
猜你喜欢
- 筛选NumPy数组 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01