I’ve got a set of sequences that are shaped like this:
# batch_size x features x length
seq = tensor([[[-1.6664, 1.3793, -1.0168, -0.2382, 0.3806, -1.4693, 0.0000,
0.0000],
[ 0.9992, -0.7673, -0.4289, -0.6654, -1.0930, -0.7454, 0.0000,
0.0000],
[ 1.4257, 0.9920, -0.1028, 0.8902, 0.5898, 0.1072, 0.0000,
0.0000],
[ 0.1998, 1.0295, -0.2461, 0.1640, 0.5859, 0.6036, 0.0000,
0.0000]],
[[ 0.7872, 0.3735, -0.5858, -0.6331, -1.7811, -0.2623, 0.8302,
-1.5729],
[-1.7866, -0.9432, -0.0326, 0.5870, 0.9642, 2.0408, -0.8691,
-1.8870],
[-2.1594, -0.4498, 1.0198, 0.7867, 0.9520, -0.8631, -0.5116,
0.2256],
[ 2.0375, 0.5512, -1.4802, -0.5710, 0.8688, 0.8487, 1.3697,
1.0811]]])
As you can see, the last 2 positions of the 0th sequence is just 0.000
s to represent padding.
What would be the recommendation here? Even a layer like this “compatible”:
conv = nn.Conv1D(n_input_features, n_output_features, kernel_size = 3)
o = conv(seq) # this works
For example, if I had a seq
tensor of N x C x L
with a kernel of, say, 3, then I get a new tensor with N x C x L-2
, and this applies even for sequences with 0 padding.
I’ve read here to try and clip the output? Would the idea then be to do something like this?
o = conv(seq)
# o[:, :, :-seq_lengths] something along these lines?
I’m asking as I’m trying to migrate a Keras 1D convolution into PyTorch. The Keras layer is written as a “MaskedConvolution” that involves a Masking
layer from Keras (presumably to handle variable lengths?). From my understanding it’s not a true Masked convolution per se where the kernel has a 0…
In addition to above about handling variable length sequences in nn.Conv1d
, if anyone has tips for Keras’ Masking
equivalent would be for PyTorch, I’d also be keen to hear!