I was looking at some code for logistic regression and I came across the following:
    for i in range(100):
        cost = 0.
        num_batches = n_examples // batch_size
        for k in range(num_batches):
            start, end = k * batch_size, (k + 1) * batch_size
            cost += train(model, loss, optimizer,
                          trX[start:end], trY[start:end])
        predY = predict(model, teX)
        print("Epoch %d, cost = %f, acc = %.2f%%"
              % (i + 1, cost / num_batches, 100. * np.mean(predY == teY)))

is this the standard way to process 1 epoch? It seemed odd to me cuz it extracted contigent chunks of data rather than random…


  
      github.com
  
  
    vinhkhuc/PyTorch-Mini-Tutorials/blob/master/2_logistic_regression.py#L20 6

def build_model(input_dim, output_dim):
# We don't need the softmax layer here since CrossEntropyLoss already
# uses it internally.
model = torch.nn.Sequential()
model.add_module("linear",
                 torch.nn.Linear(input_dim, output_dim, bias=False))
return model




def train(model, loss, optimizer, x_val, y_val):
x = Variable(x_val, requires_grad=False)
y = Variable(y_val, requires_grad=False)


# Reset gradient
optimizer.zero_grad()


# Forward
fx = model.forward(x)
output = loss.forward(fx, y)


# Backward