(self, word_embs, pos_onehot, cap_lens)
| 283 | |
| 284 | # input(batch_size, seq_len, dim) |
| 285 | def forward(self, word_embs, pos_onehot, cap_lens): |
| 286 | num_samples = word_embs.shape[0] |
| 287 | |
| 288 | pos_embs = self.pos_emb(pos_onehot) |
| 289 | inputs = word_embs + pos_embs |
| 290 | input_embs = self.input_emb(inputs) |
| 291 | hidden = self.hidden.repeat(1, num_samples, 1) |
| 292 | |
| 293 | cap_lens = cap_lens.data.tolist() |
| 294 | emb = pack_padded_sequence(input_embs, cap_lens, batch_first=True) |
| 295 | |
| 296 | gru_seq, gru_last = self.gru(emb, hidden) |
| 297 | |
| 298 | gru_last = torch.cat([gru_last[0], gru_last[1]], dim=-1) |
| 299 | gru_seq = pad_packed_sequence(gru_seq, batch_first=True)[0] |
| 300 | forward_seq = gru_seq[..., :self.hidden_size] |
| 301 | backward_seq = gru_seq[..., self.hidden_size:].clone() |
| 302 | |
| 303 | # Concate the forward and backward word embeddings |
| 304 | for i, length in enumerate(cap_lens): |
| 305 | backward_seq[i:i+1, :length] = torch.flip(backward_seq[i:i+1, :length].clone(), dims=[1]) |
| 306 | gru_seq = torch.cat([forward_seq, backward_seq], dim=-1) |
| 307 | |
| 308 | return gru_seq, gru_last |
| 309 | |
| 310 | |
| 311 | class TextEncoderBiGRUCo(nn.Module): |
nothing calls this directly
no outgoing calls
no test coverage detected