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

众所周知,在torch中,与numpy数组互相转换的API是torch.from_numpy() 或者.numpy()

但有时候,二者直接转换会遇到报错(下图第87行)

 

解决方法很简单,想到很难。 既先转为list,然后在转tensor:


labels = list(labels)

myLabel = torch.LongTensor(labels)

myLabel

Out[98]: tensor([0, 0, 0,  ..., 7, 7, 7])

 

 

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐