| 387 | |
| 388 | |
| 389 | class MotionLenEstimatorBiGRU(nn.Module): |
| 390 | def __init__(self, word_size, pos_size, hidden_size, output_size): |
| 391 | super(MotionLenEstimatorBiGRU, self).__init__() |
| 392 | |
| 393 | self.pos_emb = nn.Linear(pos_size, word_size) |
| 394 | self.input_emb = nn.Linear(word_size, hidden_size) |
| 395 | self.gru = nn.GRU(hidden_size, hidden_size, batch_first=True, bidirectional=True) |
| 396 | nd = 512 |
| 397 | self.output = nn.Sequential( |
| 398 | nn.Linear(hidden_size*2, nd), |
| 399 | nn.LayerNorm(nd), |
| 400 | nn.LeakyReLU(0.2, inplace=True), |
| 401 | |
| 402 | nn.Linear(nd, nd // 2), |
| 403 | nn.LayerNorm(nd // 2), |
| 404 | nn.LeakyReLU(0.2, inplace=True), |
| 405 | |
| 406 | nn.Linear(nd // 2, nd // 4), |
| 407 | nn.LayerNorm(nd // 4), |
| 408 | nn.LeakyReLU(0.2, inplace=True), |
| 409 | |
| 410 | nn.Linear(nd // 4, output_size) |
| 411 | ) |
| 412 | # self.linear2 = nn.Linear(hidden_size, output_size) |
| 413 | |
| 414 | self.input_emb.apply(init_weight) |
| 415 | self.pos_emb.apply(init_weight) |
| 416 | self.output.apply(init_weight) |
| 417 | # self.linear2.apply(init_weight) |
| 418 | # self.batch_size = batch_size |
| 419 | self.hidden_size = hidden_size |
| 420 | self.hidden = nn.Parameter(torch.randn((2, 1, self.hidden_size), requires_grad=True)) |
| 421 | |
| 422 | # input(batch_size, seq_len, dim) |
| 423 | def forward(self, word_embs, pos_onehot, cap_lens): |
| 424 | num_samples = word_embs.shape[0] |
| 425 | |
| 426 | pos_embs = self.pos_emb(pos_onehot) |
| 427 | inputs = word_embs + pos_embs |
| 428 | input_embs = self.input_emb(inputs) |
| 429 | hidden = self.hidden.repeat(1, num_samples, 1) |
| 430 | |
| 431 | cap_lens = cap_lens.data.tolist() |
| 432 | emb = pack_padded_sequence(input_embs, cap_lens, batch_first=True) |
| 433 | |
| 434 | gru_seq, gru_last = self.gru(emb, hidden) |
| 435 | |
| 436 | gru_last = torch.cat([gru_last[0], gru_last[1]], dim=-1) |
| 437 | |
| 438 | return self.output(gru_last) |
nothing calls this directly
no outgoing calls
no test coverage detected