Backgroud: I would like to apply convolutions and average pooling to sequences for sequence classification task.
Problem: without mask for inputs, the calculation of mini-batch and one-by-one will be different. For example:
# Two sequences.
s1 = torch.range(start=1, end=6).view(-1, 1)
s2 = torch.range(start=1, end=3).view(-1, 1)
##########################################
# one-by-one
# Convolutions.
kernels = torch.ones(1, 1, 2)
h1 = F.conv1d(s1.view(1, 1, -1), kernels) #h1=[[[3, 5, 7, 9, 11]]]
h2 = F.conv1d(s2.view(1, 1, -1), kernels) #h2=[[[3, 5]]]
# Average pooling.
h1 = h1.mean(-1) #h1=[[7]]
h2 = h2.mean(-1) #h2=[[4]]
##########################################
# mini-batch
s = torch.nn.utils.rnn.pad_sequence([s1, s2], batch_first=True)
s = s.permute(0, 2, 1)
h = torch.mean(F.conv1d(s, kernels), dim=-1) #h=[[7], [2.2]]
As you can see, the h is different from [h1, h2].
Questions:
- In order to make mini-batch the same as one-by-one, what is the normal way to employ?
- Is there a given pytorch api?
- If not, how to implement that?