[英]Convert a list of tensors to tensors of tensors pytorch
我有這個代碼:
import torch
list_of_tensors = [ torch.randn(3), torch.randn(3), torch.randn(3)]
tensor_of_tensors = torch.tensor(list_of_tensors)
我收到錯誤消息:
ValueError: 只有一個元素張量可以轉換為 Python 標量
如何將張量列表轉換為 pytorch 中的張量張量?
這是一個解決方案:
tensor_of_tensors = torch.stack((list_of_tensors))
print(tensor_of_tensors) #shape (3,3)
您還可以將火炬張量類型轉換為 NumPy 數組,然后將它們轉換為張量
list_of_tensors = [torch.randn(3).numpy(),torch.randn(3).numpy(),torch.randn(3).numpy()]
tensor_of_tensors = torch.tensor(list_of_tensors)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.