MCPcopy
hub / github.com/hunkim/PyTorchZeroToAll / forward

Method forward

13_2_rnn_classification.py:103–127  ·  view source on GitHub ↗
(self, input, seq_lengths)

Source from the content-addressed store, hash-verified

101 self.fc = nn.Linear(hidden_size, output_size)
102
103 def forward(self, input, seq_lengths):
104 # Note: we run this all at once (over the whole input sequence)
105 # input shape: B x S (input size)
106 # transpose to make S(sequence) x B (batch)
107 input = input.t()
108 batch_size = input.size(1)
109
110 # Make a hidden
111 hidden = self._init_hidden(batch_size)
112
113 # Embedding S x B -> S x B x I (embedding size)
114 embedded = self.embedding(input)
115
116 # Pack them up nicely
117 gru_input = pack_padded_sequence(
118 embedded, seq_lengths.data.cpu().numpy())
119
120 # To compact weights again call flatten_parameters().
121 self.gru.flatten_parameters()
122 output, hidden = self.gru(gru_input, hidden)
123
124 # Use the last layer output as FC's input
125 # No need to unpack, since we are going to use hidden
126 fc_output = self.fc(hidden[-1])
127 return fc_output
128
129 def _init_hidden(self, batch_size):
130 hidden = torch.zeros(self.n_layers * self.n_directions,

Callers

nothing calls this directly

Calls 1

_init_hiddenMethod · 0.95

Tested by

no test coverage detected