Skip to content

Instantly share code, notes, and snippets.

@njellinas
Last active November 20, 2018 16:08
Show Gist options
  • Save njellinas/f32098e11234f475332a73551535259b to your computer and use it in GitHub Desktop.
Save njellinas/f32098e11234f475332a73551535259b to your computer and use it in GitHub Desktop.
# Help for Step 8
# 1) Use MSE loss function
# 2) Inside the train loop, if you shaped your input features into a 2D array,
# augment their dimensionality by 1 before feeding a batch of them in the LSTM as the batch must be a 3D array, not 2D.
# The command for doing this is: your_batch.unsqueeze_(-1)
# and it is an inplace operation, you don't have to assign it to a new variable
# In the same way, you must .squeeze_() the outputs of the LSTM to reshape them into a 2D array.
# 3) In order to apply a neural network layer to a sequence you must use the given function: apply_layer_to_timesteps
# 4) The input sequences in the main part of the exercise will not be of the same length. For this reason, we use
# the PyTorch helper functions: pack_padded_sequence and pad_packed sequence. Note that these functions do not pad
# the sequences, we still have to do this operation manually before feeding the data into the LSTM.
def apply_layer_to_timesteps(x, layer, batch_first=True):
# Reshape x into a flat array
x_reshape = x.contiguous().view(-1, x.size(-1))
# Apply the layer
y = layer(x_reshape)
# Reshape y to previous form
if batch_first:
y = y.contiguous().view(x.size(0), -1, y.size(-1)) # (samples, timesteps, output_size)
else:
y = y.view(-1, x.size(1), y.size(-1)) # (timesteps, samples, output_size)
return y
class BasicLSTM(nn.Module):
def __init__(self, my_parameters):
super(BasicLSTM, self).__init__()
# Implement the init function
def forward(self, features, lengths):
""" Features: N x L x D
N: batch index
L: sequence index
D: feature index
lengths: N x 1 -> lengths of sequences (needed for pack_padded_sequence nad pad_packed_sequence)
"""
# Use pack_padded_sequence to create a PackedSequence for inputs of different length
packed = pack_padded_sequence(features, lengths, batch_first=True)
# Pass the whole packed sequence from the LSTM
# The LSTM also returns the hidden state which we do not need here
output, _ = self.lstm(packed)
# Return output in the same form as the input we gave in the pack_padded_sequence function
output, _ = pad_packed_sequence(output, batch_first=True)
# Continue the implementation of forward function here
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment