MCPcopy Index your code
hub / github.com/lazyprogrammer/machine_learning_examples / train

Function train

ann_class2/pytorch_dropout.py:69–92  ·  view source on GitHub ↗
(model, loss, optimizer, inputs, labels)

Source from the content-addressed store, hash-verified

67# so we encapsulate it in a function
68# Note: inputs and labels are torch tensors
69def 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

Callers 3

theano1.pyFile · 0.70
pytorch_dropout.pyFile · 0.70
mainFunction · 0.70

Calls 3

trainMethod · 0.45
forwardMethod · 0.45
stepMethod · 0.45

Tested by

no test coverage detected