问题
Specifically, I have a tensor of shape: torch.Size([1, 16])
I want to bucket this into 7 buckets (of 4 each). Example:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
should become:
[[1, 2, 3, 4],
[3, 4, 5, 6],
[5, 6, 7, 8],
[7, 8, 9, 10],
[9, 10, 11, 12],
[11, 12, 13, 14],
[13, 14, 15, 16],
]
How can I achieve this with PyTorch?
回答1:
Looks like an unfold:
t.unfold(0,4,2)
Output:
tensor([[ 1., 2., 3., 4.],
[ 3., 4., 5., 6.],
[ 5., 6., 7., 8.],
[ 7., 8., 9., 10.],
[ 9., 10., 11., 12.],
[11., 12., 13., 14.],
[13., 14., 15., 16.]])
来源:https://stackoverflow.com/questions/64355180/how-can-i-chunk-a-pytorch-tensor-into-a-specified-bucket-size-with-overlap