(model, loss, optimizer, inputs, labels)
| 67 | # so we encapsulate it in a function |
| 68 | # Note: inputs and labels are torch tensors |
| 69 | def train(model, loss, optimizer, inputs, labels): |
| 70 | # set the model to training mode |
| 71 | # because dropout has 2 different modes! |
| 72 | model.train() |
| 73 | |
| 74 | inputs = Variable(inputs, requires_grad=False) |
| 75 | labels = Variable(labels, requires_grad=False) |
| 76 | |
| 77 | # Reset gradient |
| 78 | optimizer.zero_grad() |
| 79 | |
| 80 | # Forward |
| 81 | logits = model.forward(inputs) |
| 82 | output = loss.forward(logits, labels) |
| 83 | |
| 84 | # Backward |
| 85 | output.backward() |
| 86 | |
| 87 | # Update parameters |
| 88 | optimizer.step() |
| 89 | |
| 90 | # what's the difference between backward() and step()? |
| 91 | |
| 92 | return output.item() |
| 93 | |
| 94 | |
| 95 | # similar to train() but not doing the backprop step |
no test coverage detected