def loss_batch([model](https://docs.pytorch.org/docs/stable/generated/torch.nn.Sequential.html#torch.nn.Sequential "torch.nn.Sequential"), loss_func, [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"), [opt](https://docs.pytorch.org/docs/stable/generated/torch.optim.SGD.html#torch.optim.SGD "torch.optim.SGD")=None): [loss](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor") = 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")) if [opt](https://docs.pytorch.org/docs/stable/generated/torch.optim.SGD.html#torch.optim.SGD "torch.optim.SGD") is not None: [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")() return [loss](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor").item(), len([xb](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor"))