I am new to machine learning and recently I joined a course where I was given a logistic regression assignment in which I had to split 20% of the training dataset for the validation dataset and then use the validation dataset to capture the minimum possible loss and then use the test dataset to find the accuracy of the model. Below is my code for implementing logistic regression
class LogReg(LinReg):
def __init__(self, n_dim, bias=True):
if bias:
n_dim = n_dim + 1
super(LogReg, self).__init__(n_dim)
self.bias = bias
def __call__(self, x):
return x.mm(self.theta).sigmoid()
def compute_loss(self, x, y, lambda_reg):
# The function has a generic implementation, and can also work for the neural nets!
predictions = self(x)
loss = -(y * torch.log(predictions) + (1-y) * torch.log(1 - predictions)).mean()
regularizer = self.theta.transpose(0, 1).mm(self.theta)
return loss + regularizer.mul(lambda_reg)
@staticmethod
def add_bias(x):
ones = torch.ones((x.size(0), 1), dtype=torch.float32)
x_hat = torch.cat((ones, x), dim=-1)
return x_hat
def fit(self, x, y, num_iter=10, mb_size=32, lr=1e-1, lambda_reg=1e-2, reset=True):
N = x.size(0)
losses = []
x_hat = x
# Adding a bias term if needed
if self.bias:
x_hat = self.add_bias(x)
if reset:
self.reset() # Very important if you want to call fit multiple times
num_batches = x.size(0) // mb_size
# The outer loop goes over `epochs`
# The inner loop goes over the whole training data
for it in range(num_iter):
loss_per_epoch = 0
for batch_it in range(num_batches):
# has been implemented for the linear model
self.zero_grad()
ind = torch.randint(0, N, (mb_size, 1)).squeeze()
x_mb, y_mb = x_hat[ind, :], y[ind, :]
loss = self.compute_loss(x_mb, y_mb, lambda_reg)
loss.backward()
self.theta.data = self.theta.data - lr*self.grad().data
loss_per_epoch += loss.item()
loss_per_epoch /= num_batches
losses.append(loss_per_epoch)
return losses
How should I use the validation set at the level of epoch to find the best loss?