Hi every one,
I want to predict continuous values from a CNN model as output by giving an image as input:
below is the code
class MultiLabelNN(nn.Module):
def init(self):
super(MultiLabelNN, self).init()
self.conv1 = nn.Conv2d(3,64, 5)
self.pool = nn.MaxPool2d(2,2)
self.conv2 = nn.Conv2d(64, 128, 5)
self.conv3 = nn.Conv2d(128, 256, 5)
self.conv4 = nn.Conv2d(256,320,5)
self.fc1 = nn.Linear(250880,2048)
self.fc2 = nn.Linear(2048, 1024)
self.fc3 = nn.Linear(1024, 512)
self.fc4 = nn.Linear(512, 6)
def forward(self, x):
x = self.conv1(x)
x = nn.ReLU(x)
x = self.pool(x)
x = self.conv2(x)
x = nn.ReLU(x)
x = self.pool(x)
x = self.conv3(x)
x = nn.ReLU(x)
x = self.pool(x)
x = self.conv4(x)
x = nn.ReLU(x)
x = self.pool(x)
x = x.view(-1, 250880)
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
x = self.fc4(x)
return x
I want to do support vector regression instead of linear regression in my last fully connected layer self.fc4 = nn.Linear(512, 6)…
my question is that is there any option to do this ??
Hello @Aditya_Raj_P15VSS201
In order to perform Support Vector Regression, you will have to add a HingeLoss element to your code
A sample script would be the following
# HINGE LOSS
import os
import numpy as np
import torch
from torch import nn
from torch.autograd import Variable
torch.manual_seed(42)
class SVR(nn.Module):
def __init__(self):
super(SVR,self).__init__()
self.linearModel=nn.Linear(100,1)
def forward(self,x):
x = self.linearModel(x)
return x
model=SVR()
def hingeLoss(outputVal,dataOutput,model):
loss1=torch.sum(torch.clamp(1 - torch.matmul(outputVal.t(),dataOutput),min=0))
loss2=torch.sum(model.linearModel.weight ** 2) # l2 penalty
totalLoss=loss1+loss2
return(totalLoss)
optimizer=torch.optim.SGD(model.parameters(),lr=0.001)
X=np.random.rand(1000,100).astype(np.float32)
Y=np.random.randint(2,size=(1000)).reshape(1000,1).astype(np.float32)
for epoch in range(10000):
inputVal=Variable(torch.from_numpy(X))
outputVal=Variable(torch.from_numpy(Y))
optimizer.zero_grad()
modelOutput = model(inputVal)
totalLoss=hingeLoss(outputVal,modelOutput,model)
totalLoss.backward()
optimizer.step()
if epoch % 100 == 0:
print('epoch [{}/{}], loss:{:.4f}'.format(epoch + 1, 100, totalLoss))
Please accept my sincere thanks for your valuable suggestion and code
1 Like