[英]Reshape rows into groups of columns
我有許多行向量,我想將它們批處理為列向量並用作Conv1d
的輸入。 作為一個例子,我想將張量x
重塑為y
,即制作兩組兩個列向量。
# size = [4, 3]
x = torch.tensor([
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[9, 10, 11]
])
# size = [2, 3, 2]
y = torch.tensor([
[[0, 3],
[1, 4],
[2, 5]],
[[6, 9],
[7, 10],
[8, 11]]
])
有沒有辦法只用reshape
和類似的功能來做到這一點? 我能想到的唯一方法是使用循環並復制到一個新的張量中。
您也可以使用torch.split
和torch.stack
類的
torch.stack(x.split(2), dim=2) # or torch.stack(x.T.split(2, dim=1))
tensor([[[ 0, 3],
[ 1, 4],
[ 2, 5]],
[[ 6, 9],
[ 7, 10],
[ 8, 11]]])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.