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

Function train

ann_class2/pytorch_batchnorm.py:68–91  ·  view source on GitHub ↗
(model, loss, optimizer, inputs, labels)

Source from the content-addressed store, hash-verified

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

Callers 1

Calls 3

trainMethod · 0.45
forwardMethod · 0.45
stepMethod · 0.45

Tested by

no test coverage detected