MCPcopy Create free account
hub / github.com/pytorch/tutorials / BoWClassifier

Class BoWClassifier

beginner_source/nlp/deep_learning_tutorial.py:259–280  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

257
258
259class BoWClassifier(nn.Module): # inheriting from nn.Module!
260
261 def __init__(self, num_labels, vocab_size):
262 # calls the init function of nn.Module. Dont get confused by syntax,
263 # just always do it in an nn.Module
264 super(BoWClassifier, self).__init__()
265
266 # Define the parameters that you will need. In this case, we need A and b,
267 # the parameters of the affine mapping.
268 # Torch defines nn.Linear(), which provides the affine map.
269 # Make sure you understand why the input dimension is vocab_size
270 # and the output is num_labels!
271 self.linear = nn.Linear(vocab_size, num_labels)
272
273 # NOTE! The non-linearity log softmax does not have parameters! So we don't need
274 # to worry about that here
275
276 def forward(self, bow_vec):
277 # Pass the input through the linear layer,
278 # then pass that through log_softmax.
279 # Many non-linearities and other functions are in torch.nn.functional
280 return F.log_softmax(self.linear(bow_vec), dim=1)
281
282
283def make_bow_vector(sentence, word_to_ix):

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected