MCPcopy Create free account
hub / github.com/pytorch/examples / train

Function train

siamese_network/main.py:190–208  ·  view source on GitHub ↗
(args, model, device, train_loader, optimizer, epoch)

Source from the content-addressed store, hash-verified

188
189
190def train(args, model, device, train_loader, optimizer, epoch):
191 model.train()
192
193 # we aren't using `TripletLoss` as the MNIST dataset is simple, so `BCELoss` can do the trick.
194 criterion = nn.BCELoss()
195
196 for batch_idx, (images_1, images_2, targets) in enumerate(train_loader):
197 images_1, images_2, targets = images_1.to(device), images_2.to(device), targets.to(device)
198 optimizer.zero_grad()
199 outputs = model(images_1, images_2).squeeze()
200 loss = criterion(outputs, targets)
201 loss.backward()
202 optimizer.step()
203 if batch_idx % args.log_interval == 0:
204 print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
205 epoch, batch_idx * len(images_1), len(train_loader.dataset),
206 100. * batch_idx / len(train_loader), loss.item()))
207 if args.dry_run:
208 break
209
210
211def test(model, device, test_loader):

Callers 1

mainFunction · 0.70

Calls 1

trainMethod · 0.45

Tested by

no test coverage detected