def get_model(): [model](https://docs.pytorch.org/docs/stable/generated/torch.nn.Sequential.html#torch.nn.Sequential "torch.nn.Sequential") = [Mnist_Logistic](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module "torch.nn.Module")() return [model](https://docs.pytorch.org/docs/stable/generated/torch.nn.Sequential.html#torch.nn.Sequential "torch.nn.Sequential"), [optim.SGD](https://docs.pytorch.org/docs/stable/generated/torch.optim.SGD.html#torch.optim.SGD "torch.optim.SGD")([model.parameters](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module.parameters "torch.nn.Module.parameters")(), lr=lr) [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() print(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 epoch in range(epochs): for i in range((n - 1) // bs + 1): start_i = i * bs end_i = start_i + bs [xb](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor") = [x_train](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor")[start_i:end_i] [yb](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor") = [y_train](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "torch.Tensor")[start_i:end_i] [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")() print(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")))