Folks , I am relatively new to PyTorch and request your help as loss with SGD is not coverging. If anyone can take a look and advise I would really appreciate. Thanks in advance. Please excuse any formatting errors.
**# Custom Loss Function for Regression "Need to check if the formula is correct"**
**# Loss(h(xn),yn)=Log(1+exp(-yn wt xn)**
class Regress_Loss(nn.modules.Module):
def __init__(self):
super(Regress_Loss,self).__init__()
def forward(self, outputs, labels):
batch_size = outputs.size()[0]
mult = Variable((outputs*labels), requires_grad=True)
loss = torch.sum(torch.log(1 + torch.exp(-mult)))
return loss/batch_size
**# SGD**
loss_criteria = Regress_Loss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
#Logistic regression model and Loss
model = nn.Linear(input_size,num_classes)
total_step = len(train_loader)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
# Reshape images to (batch_size, input_size)
images = images.reshape(-1, 28*28)
#Convert labels from 0,1 to -1,1
labels = Variable(2*(labels.float()-0.5))
# Forward pass
outputs = model(images)
# Getting the max
oneout = torch.max(outputs.data, 1)[0]
## Converting output to 1 and -1 " Not sure if the below statements are correct...
oneout[oneout < 0] = -1
oneout[oneout > 0] = 1
loss = loss_criteria(oneout, labels)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch+1, num_epochs, i+1, total_step, loss.item()))
Loss output: 0.8445,0.6883,0.7976,0.8133,0.8289,0.7195 and so forth…