TextRNN + TextCNN
| 21 | |
| 22 | |
| 23 | class TextRCNN(Classifier): |
| 24 | """TextRNN + TextCNN |
| 25 | """ |
| 26 | def __init__(self, dataset, config): |
| 27 | super(TextRCNN, self).__init__(dataset, config) |
| 28 | self.rnn = RNN( |
| 29 | config.embedding.dimension, config.TextRCNN.hidden_dimension, |
| 30 | num_layers=config.TextRCNN.num_layers, |
| 31 | batch_first=True, bidirectional=config.TextRCNN.bidirectional, |
| 32 | rnn_type=config.TextRCNN.rnn_type) |
| 33 | |
| 34 | hidden_dimension = config.TextRCNN.hidden_dimension |
| 35 | if config.TextRCNN.bidirectional: |
| 36 | hidden_dimension *= 2 |
| 37 | self.kernel_sizes = config.TextRCNN.kernel_sizes |
| 38 | self.convs = torch.nn.ModuleList() |
| 39 | for kernel_size in self.kernel_sizes: |
| 40 | self.convs.append(torch.nn.Conv1d( |
| 41 | hidden_dimension, config.TextRCNN.num_kernels, |
| 42 | kernel_size, padding=kernel_size - 1)) |
| 43 | |
| 44 | self.top_k = self.config.TextRCNN.top_k_max_pooling |
| 45 | hidden_size = len(config.TextRCNN.kernel_sizes) * \ |
| 46 | config.TextRCNN.num_kernels * self.top_k |
| 47 | |
| 48 | self.linear = torch.nn.Linear(hidden_size, len(dataset.label_map)) |
| 49 | self.dropout = torch.nn.Dropout(p=config.train.hidden_layer_dropout) |
| 50 | |
| 51 | def get_parameter_optimizer_dict(self): |
| 52 | params = list() |
| 53 | params.append({'params': self.token_embedding.parameters()}) |
| 54 | params.append({'params': self.char_embedding.parameters()}) |
| 55 | params.append({'params': self.rnn.parameters()}) |
| 56 | params.append({'params': self.convs.parameters()}) |
| 57 | params.append({'params': self.linear.parameters()}) |
| 58 | return params |
| 59 | |
| 60 | def update_lr(self, optimizer, epoch): |
| 61 | """ |
| 62 | """ |
| 63 | if epoch > self.config.train.num_epochs_static_embedding: |
| 64 | for param_group in optimizer.param_groups[:2]: |
| 65 | param_group["lr"] = self.config.optimizer.learning_rate |
| 66 | else: |
| 67 | for param_group in optimizer.param_groups[:2]: |
| 68 | param_group["lr"] = 0 |
| 69 | |
| 70 | def forward(self, batch): |
| 71 | if self.config.feature.feature_names[0] == "token": |
| 72 | embedding = self.token_embedding( |
| 73 | batch[cDataset.DOC_TOKEN].to(self.config.device)) |
| 74 | seq_length = batch[cDataset.DOC_TOKEN_LEN].to(self.config.device) |
| 75 | else: |
| 76 | embedding = self.char_embedding( |
| 77 | batch[cDataset.DOC_CHAR].to(self.config.device)) |
| 78 | seq_length = batch[cDataset.DOC_CHAR_LEN].to(self.config.device) |
| 79 | output, _ = self.rnn(embedding, seq_length) |
| 80 |
nothing calls this directly
no outgoing calls
no test coverage detected