Pytorch TypeError: can't convert np.ndarray of type numpy.object_.The only supported types are:

报错:TypeError: can’t convert np.ndarray of type numpy.object_. The only supported types are: float64, float32, float16, int64, int32, int16, int8, uint8, and bool.

问题描述
当把np转换成torch tensor时,

trainx = torch.from_numpy(np.reshape(train_x, newshape=(-1,25)))

问题解决
由于读入的numpy数组里的元素是object类型,无法将这种类型转换成tensor。
所以,将numpy数组进行强制类型转换成float类型(或者任何pytorch支持的类型:float64, float32, float16, int64, int32, int16, int8, uint8, and bool)即可。

trainx = trainx.astype(float)  # numpy强制类型转换

你可能感兴趣的:(Pytorch神经网络)