I was going through this example of a LSTM language model on github (link).
What it does in general is pretty clear to me. But I'm still struggling to understand what calling contiguous() does, which occurs several times in the code.
For example in line 74/75 of the code input and target sequences of the LSTM are created.
Data (stored in ids) is 2-dimensional where first dimension is the batch size.
for i in range(0, ids.size(1) - seq_length, seq_length):
# Get batch inputs and targets
inputs = Variable(ids[:, i:i+seq_length])
targets = Variable(ids[:, (i+1):(i+1)+seq_length].contiguous())
So as a simple example, when using batch size 1 and seq_length 10 inputs and targets looks like this:
inputs Variable containing:
0 1 2 3 4 5 6 7 8 9
[torch.LongTensor of size 1x10]
targets Variable containing:
1 2 3 4 5 6 7 8 9 10
[torch.LongTensor of size 1x10]
So in general my question is, what does contiguous() do and why do I need it?
Further I don't understand why the method is called for the target sequence and but not the input sequence as both variables are comprised of the same data.
How could targets be non-contiguous and inputs still be contiguous?
EDIT:
I tried to leave out calling contiguous(), but this leads to an error message when computing the loss.
RuntimeError: invalid argument 1: input is not contiguous at .../src/torch/lib/TH/generic/THTensor.c:231
So obviously calling contiguous() in this example is necessary.
