我有這個代碼:import torchlist_of_tensors = [ torch.randn(3), torch.randn(3), torch.randn(3)]tensor_of_tensors = torch.tensor(list_of_tensors)我收到錯誤消息:ValueError:只有一個元素張量可以轉換為 Python 標量如何將張量列表轉換為 pytorch 中的張量張量?
2 回答

互換的青春
TA貢獻1797條經驗 獲得超6個贊
您還可以將火炬張量類型轉換為 NumPy 數組,然后將它們轉換為張量
list_of_tensors = [torch.randn(3).numpy(),torch.randn(3).numpy(),torch.randn(3).numpy()]
tensor_of_tensors = torch.tensor(list_of_tensors)

慕村225694
TA貢獻1880條經驗 獲得超4個贊
這是一個解決方案:
tensor_of_tensors = torch.stack((list_of_tensors)) print(tensor_of_tensors) #shape (3,3)
添加回答
舉報
0/150
提交
取消