can't convert a given np.ndarray to a tensor


source_label_ce = Variable(torch.from_numpy(ids_cvt[source_label.argmax(-1)]).long(), requires_grad=False).cuda()
CE_loss = CE(source_seen_output * self.cross_entropy_lambda, source_label_ce)

RuntimeError: can't convert a given np.ndarray to a tensor - it has an invalid type. The only supported types are: double, float, int64, int32, and uint8. pytorch报错怎么改?

由于读入的numpy数组里的元素是object类型,无法将这种类型转换成tensor。

所以,将numpy数组进行强制类型转换成float类型(或者任何pytorch支持的类型:float64, float32, float16, int64, int32, int16, int8, uint8, and bool)即可。

trainx = trainx.astype(float)

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632