model = [LSTMTagger](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module "torch.nn.Module")(EMBEDDING_DIM, HIDDEN_DIM, len(word_to_ix), len(tag_to_ix)) [loss_function](https://docs.pytorch.org/docs/stable/generated/torch.nn.NLLLoss.html#torch.nn.NLLLoss "torch.nn.NLLLoss") = [nn.NLLLoss](https://docs.pytorch.org/docs/stable/generated/torch.nn.NLLLoss.html#torch.nn.NLLLoss "torch.nn.NLLLoss")() [optimizer](https://docs.pytorch.org/docs/stable/generated/torch.optim.SGD.html#torch.optim.SGD "torch.optim.SGD") = [optim.SGD](https://docs.pytorch.org/docs/stable/generated/torch.optim.SGD.html#torch.optim.SGD "torch.optim.SGD")([model.parameters](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module.parameters "torch.nn.Module.parameters")(), lr=0.1) # See what the scores are before training # Note that element i,j of the output is the score for tag j for word i. # Here we don't need to train, so the code is wrapped in torch.no_grad() with [torch.no_grad](https://docs.pytorch.org/docs/stable/generated/torch.no_grad.html#torch.no_grad "torch.no_grad")(): [inputs](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor") = prepare_sequence(training_data[0][0], word_to_ix) [tag_scores](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor") = model([inputs](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor")) print([tag_scores](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor")) for epoch in range(300): # again, normally you would NOT do 300 epochs, it is toy data for sentence, tags in training_data: # Step 1. Remember that Pytorch accumulates gradients. # We need to clear them out before each instance [model.zero_grad](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module.zero_grad "torch.nn.Module.zero_grad")() # Step 2. Get our inputs ready for the network, that is, turn them into # Tensors of word indices. [sentence_in](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor") = prepare_sequence(sentence, word_to_ix) [targets](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor") = prepare_sequence(tags, tag_to_ix) # Step 3. Run our forward pass. [tag_scores](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor") = model([sentence_in](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor")) # Step 4. Compute the loss, gradients, and update the parameters by # calling optimizer.step() [loss](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor") = [loss_function](https://docs.pytorch.org/docs/stable/generated/torch.nn.NLLLoss.html#torch.nn.NLLLoss "torch.nn.NLLLoss")([tag_scores](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor"), [targets](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor")) [loss.backward](https://docs.pytorch.org/docs/stable/generated/torch.Tensor.backward.html#torch.Tensor.backward "torch.Tensor.backward")() [optimizer.step](https://docs.pytorch.org/docs/stable/generated/torch.optim.SGD.html#torch.optim.SGD.step "torch.optim.SGD.step")() # See what the scores are after training with [torch.no_grad](https://docs.pytorch.org/docs/stable/generated/torch.no_grad.html#torch.no_grad "torch.no_grad")(): [inputs](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor") = prepare_sequence(training_data[0][0], word_to_ix) [tag_scores](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor") = model([inputs](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor")) # The sentence is "the dog ate the apple". i,j corresponds to score for tag j # for word i. The predicted tag is the maximum scoring tag. # Here, we can see the predicted sequence below is 0 1 2 0 1 # since 0 is index of the maximum value of row 1, # 1 is the index of maximum value of row 2, etc. # Which is DET NOUN VERB DET NOUN, the correct sequence! print([tag_scores](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor"))