I have a list that contains different sizes of tensors so I used torch.nn.utils.rnn.pad_sequence to the list. For example, the original data is
grp_pt_data_list = [
[[1,2,1],[1,1,5],[2,1,3],[1,2,1],[1,1,1],],
[[1,2,1],[2,3,2],[1,3,2]],
[[1,3,1],[2,4,2],[1,1,2]]]
after padding the tensor is
tensor([[[[1., 1., 2., 1., 1.],
[1., 2., 1., 0., 0.],
[1., 2., 1., 0., 0.]],
[[2., 1., 1., 2., 1.],
[2., 3., 3., 0., 0.],
[3., 4., 1., 0., 0.]],
[[1., 5., 3., 1., 1.],
[1., 2., 2., 0., 0.],
[1., 2., 2., 0., 0.]]]], device=‘cuda:0’)
The length becomes maximum value 5. I wanted to apply a mask to the result of a nn.Conv2d(fea_dim, 4, kernel_size=(1,1)), so that the padded zeros could be reset to 0, and the other values will remain. Expected mask will be tensor([[ True,  True,  True,  True,  True],
[ True,  True,  True, False, False],
[ True,  True,  True, False, False]])
How should I create the mask tensor and apply the mask to conv2d results without a for loop? Or is there any other better way to not calculating the padded values?