(self, input, seq_lengths)
| 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, |
nothing calls this directly
no test coverage detected