| 37 | return output |
| 38 | |
| 39 | class BidirectionalLSTM(nn.Module): |
| 40 | |
| 41 | def __init__(self, input_size, hidden_size, output_size): |
| 42 | super(BidirectionalLSTM, self).__init__() |
| 43 | self.rnn = nn.LSTM(input_size, hidden_size, bidirectional=True, batch_first=True) |
| 44 | self.linear = nn.Linear(hidden_size * 2, output_size) |
| 45 | # self.h0 = torch.randn(2, 1, hidden_size).cuda() |
| 46 | # self.c0 = torch.randn(2, 1, hidden_size).cuda() |
| 47 | |
| 48 | def forward(self, input): |
| 49 | """ |
| 50 | input : visual feature [batch_size x T x input_size] |
| 51 | output : contextual feature [batch_size x T x output_size] |
| 52 | """ |
| 53 | self.rnn.flatten_parameters() |
| 54 | recurrent, _ = self.rnn(input) # batch_size x T x input_size -> batch_size x T x (2*hidden_size) |
| 55 | # T, b, h = recurrent.size() |
| 56 | # print("recurrent.size: ", recurrent.size()) |
| 57 | # t_rec = recurrent.contiguous().view(T * b, h) |
| 58 | |
| 59 | output = self.linear(recurrent) # batch_size x T x output_size |
| 60 | # output = output.view(T, b, -1) |
| 61 | # print("output.size: ", output.size()) |
| 62 | return output |
| 63 | |
| 64 | class BidirectionalGRU(nn.Module): |
| 65 | |