| 17 | |
| 18 | |
| 19 | class Encoder(nn.Module): |
| 20 | |
| 21 | def __init__(self, config): |
| 22 | super(Encoder, self).__init__() |
| 23 | self.config = config |
| 24 | input_size = config.d_proj if config.projection else config.d_embed |
| 25 | dropout = 0 if config.n_layers == 1 else config.dp_ratio |
| 26 | self.rnn = nn.LSTM(input_size=input_size, hidden_size=config.d_hidden, |
| 27 | num_layers=config.n_layers, dropout=dropout, |
| 28 | bidirectional=config.birnn) |
| 29 | |
| 30 | def forward(self, inputs): |
| 31 | batch_size = inputs.size()[1] |
| 32 | state_shape = self.config.n_cells, batch_size, self.config.d_hidden |
| 33 | h0 = c0 = inputs.new_zeros(state_shape) |
| 34 | outputs, (ht, ct) = self.rnn(inputs, (h0, c0)) |
| 35 | return ht[-1] if not self.config.birnn else ht[-2:].transpose(0, 1).contiguous().view(batch_size, -1) |
| 36 | |
| 37 | |
| 38 | class SNLIClassifier(nn.Module): |