[model](https://docs.pytorch.org/docs/stable/generated/torch.nn.Sequential.html#torch.nn.Sequential "torch.nn.Sequential"), [opt](https://docs.pytorch.org/docs/stable/generated/torch.optim.SGD.html#torch.optim.SGD "torch.optim.SGD") = get_model() for epoch in range(epochs): [model.train](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module.train "torch.nn.Module.train")() for [xb](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor"), [yb](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor") in train_dl: [pred](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor") = [model](https://docs.pytorch.org/docs/stable/generated/torch.nn.Sequential.html#torch.nn.Sequential "torch.nn.Sequential")([xb](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor")) [loss](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor") = loss_func([pred](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor"), [yb](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor")) [loss.backward](https://docs.pytorch.org/docs/stable/generated/torch.Tensor.backward.html#torch.Tensor.backward "torch.Tensor.backward")() [opt.step](https://docs.pytorch.org/docs/stable/generated/torch.optim.SGD.html#torch.optim.SGD.step "torch.optim.SGD.step")() [opt.zero_grad](https://docs.pytorch.org/docs/stable/generated/torch.optim.SGD.html#torch.optim.SGD.zero_grad "torch.optim.SGD.zero_grad")() [model.eval](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module.eval "torch.nn.Module.eval")() with [torch.no_grad](https://docs.pytorch.org/docs/stable/generated/torch.no_grad.html#torch.no_grad "torch.no_grad")(): [valid_loss](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor") = sum(loss_func([model](https://docs.pytorch.org/docs/stable/generated/torch.nn.Sequential.html#torch.nn.Sequential "torch.nn.Sequential")([xb](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor")), [yb](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor")) for [xb](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor"), [yb](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor") in valid_dl) print(epoch, [valid_loss](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor") / len(valid_dl))