tensor

How can I chunk a PyTorch tensor into a specified bucket size with overlap?

点点圈 提交于 2020-12-26 11:07:43
问题 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.],

How can I chunk a PyTorch tensor into a specified bucket size with overlap?

扶醉桌前 提交于 2020-12-26 11:06:04
问题 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.],