Skip to content

Instantly share code, notes, and snippets.

@albertlai431
Created February 7, 2019 22:16
Show Gist options
  • Save albertlai431/e545cc0c886aa3056dcfe9d9bbfdc9b9 to your computer and use it in GitHub Desktop.
Save albertlai431/e545cc0c886aa3056dcfe9d9bbfdc9b9 to your computer and use it in GitHub Desktop.
def init_hidden(self, batch_size):
''' Initializes hidden state '''
# Create two new tensors with sizes n_layers x batch_size x n_hidden,
# initialized to zero, for hidden state and cell state of LSTM
weight = next(self.parameters()).data
if (train_on_gpu):
hidden = (weight.new(self.n_layers, batch_size, self.n_hidden).zero_().cuda(),
weight.new(self.n_layers, batch_size, self.n_hidden).zero_().cuda())
else:
hidden = (weight.new(self.n_layers, batch_size, self.n_hidden).zero_(),
weight.new(self.n_layers, batch_size, self.n_hidden).zero_())
return hidden
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment