多GPU运行PyTorch报错dimension specified as 0 but tensor has no dimensions

错误信息

dimension specified as 0 but tensor has no dimensions

问题原因

CrossEntropyLoss的输入必须为tensor,不能为scalar(标量),即输入的数据维度不能为None.

解决办法

使用view方法改变维度为1,如target = target.view(-1),这样假如原来target是1,使用后变为[1],维度从None变为1.

Problem Sovled.

参考