# Non linear regression methods

**URL:** <https://discuss.pytorch.org/t/non-linear-regression-methods/62060>\
**Category:** Uncategorized\
**Created:** [November 25, 2019, 5:30am UTC](https://discuss.pytorch.org/t/non-linear-regression-methods/62060 "2019-11-25T05:30:16Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![anantguptadbl](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/anantguptadbl/32/17784_2.png) [@anantguptadbl](https://discuss.pytorch.org/u/anantguptadbl)\
**Post date:** [November 25, 2019, 8:59am UTC](https://discuss.pytorch.org/t/non-linear-regression-methods/62060/2 "2019-11-25T08:59:37Z")

</div>

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

```auto

# 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))

```

---

_[View the full topic](https://discuss.pytorch.org/t/non-linear-regression-methods/62060)._
