(self, vocab_size, tag_to_ix, embedding_dim, hidden_dim)
| 152 | class BiLSTM_CRF(nn.Module): |
| 153 | |
| 154 | def __init__(self, vocab_size, tag_to_ix, embedding_dim, hidden_dim): |
| 155 | super(BiLSTM_CRF, self).__init__() |
| 156 | self.embedding_dim = embedding_dim |
| 157 | self.hidden_dim = hidden_dim |
| 158 | self.vocab_size = vocab_size |
| 159 | self.tag_to_ix = tag_to_ix |
| 160 | self.tagset_size = len(tag_to_ix) |
| 161 | |
| 162 | self.word_embeds = nn.Embedding(vocab_size, embedding_dim) |
| 163 | self.lstm = nn.LSTM(embedding_dim, hidden_dim // 2, |
| 164 | num_layers=1, bidirectional=True) |
| 165 | |
| 166 | # Maps the output of the LSTM into tag space. |
| 167 | self.hidden2tag = nn.Linear(hidden_dim, self.tagset_size) |
| 168 | |
| 169 | # Matrix of transition parameters. Entry i,j is the score of |
| 170 | # transitioning *to* i *from* j. |
| 171 | self.transitions = nn.Parameter( |
| 172 | torch.randn(self.tagset_size, self.tagset_size)) |
| 173 | |
| 174 | # These two statements enforce the constraint that we never transfer |
| 175 | # to the start tag and we never transfer from the stop tag |
| 176 | self.transitions.data[tag_to_ix[START_TAG], :] = -10000 |
| 177 | self.transitions.data[:, tag_to_ix[STOP_TAG]] = -10000 |
| 178 | |
| 179 | self.hidden = self.init_hidden() |
| 180 | |
| 181 | def init_hidden(self): |
| 182 | return (torch.randn(2, 1, self.hidden_dim // 2), |
nothing calls this directly
no test coverage detected