Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable input and target sizes #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions code/lstm_encoder_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def forward(self, x_input, encoder_hidden_states):
class lstm_seq2seq(nn.Module):
''' train LSTM encoder-decoder and make predictions '''

def __init__(self, input_size, hidden_size):
def __init__(self, input_size,target_size,hidden_size):

'''
: param input_size: the number of expected features in the input X
Expand All @@ -108,10 +108,11 @@ def __init__(self, input_size, hidden_size):
super(lstm_seq2seq, self).__init__()

self.input_size = input_size
self.target_size= target_size
self.hidden_size = hidden_size

self.encoder = lstm_encoder(input_size = input_size, hidden_size = hidden_size)
self.decoder = lstm_decoder(input_size = input_size, hidden_size = hidden_size)
self.decoder = lstm_decoder(input_size = target_size, hidden_size = hidden_size)


def train_model(self, input_tensor, target_tensor, n_epochs, target_len, batch_size, training_prediction = 'recursive', teacher_forcing_ratio = 0.5, learning_rate = 0.01, dynamic_tf = False):
Expand Down Expand Up @@ -161,7 +162,7 @@ def train_model(self, input_tensor, target_tensor, n_epochs, target_len, batch_s
target_batch = target_tensor[:, b: b + batch_size, :]

# outputs tensor
outputs = torch.zeros(target_len, batch_size, input_batch.shape[2])
outputs = torch.zeros(target_len, batch_size, target_batch.shape[2])

# initialize hidden state
encoder_hidden = self.encoder.init_hidden(batch_size)
Expand All @@ -173,7 +174,7 @@ def train_model(self, input_tensor, target_tensor, n_epochs, target_len, batch_s
encoder_output, encoder_hidden = self.encoder(input_batch)

# decoder with teacher forcing
decoder_input = input_batch[-1, :, :] # shape: (batch_size, input_size)
decoder_input = target_batch[-1, :, :] # shape: (batch_size, input_size)
decoder_hidden = encoder_hidden

if training_prediction == 'recursive':
Expand Down