Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def train_batch_ch12(net, features, labels, loss, trainer, ctx_list):
Xs, ys = d2l.split_batch(features, labels, ctx_list)
with autograd.record():
pys = [net(X) for X in Xs]
ls = [loss(py, y) for py, y in zip(pys, ys)]
for l in ls:
l.backward()
trainer.step(features.shape[0])
train_loss_sum = sum([l.sum().asscalar() for l in ls])
train_acc_sum = sum(d2l.accuracy(py, y) for py, y in zip(pys, ys))
return train_loss_sum, train_acc_sum