Sorry
Here is my code. As far as i know this error also occurs in spyder when you run the code twice in ide like spyder without commenting the import torch line on the second run
#import torch
import matplotlib.pyplot as plt
import numpy as np 
import cv2
class down(torch.nn.Module):
    def __init__(self,channel_in,channel_out,ksize=1):
        super(down, self).__init__()
        self.conv1=torch.nn.Conv2d(channel_in,channel_out,ksize)
        self.conv2=torch.nn.Conv2d(channel_out,channel_out,ksize)
    def forward(self,x):
        relu=self.conv1(x).clamp(min=0)
        #relu=self.conv2(relu).clamp(min=0)
        return relu
class up(torch.nn.Module):
    def __init__(self,channel_in,channel_out,ksize=3,space_dropout=False):
        super(up, self).__init__()
        self.up1=torch.nn.ConvTranspose2d(channel_in,channel_out,ksize)
        self.conv3=torch.nn.Conv2d(channel_out,channel_out,ksize)
        self.conv4=torch.nn.Conv2d(channel_out,channel_out,ksize)    
    def forward(self,x):
        relu=self.up1(x)
        relu=self.conv3(relu).clamp(min=0)
        relu=self.conv4(relu).clamp(min=0)
        return relu

class Net(torch.nn.Module):
    def __init__(self):
        super(Net, self).__init__()  
        self.d1=down(3,64)
        self.d2=down(64,128)
        self.d3=down(128,256)
        self.d4=down(256,512)
        self.u11=up(512,256)
        self.u1=up(256,128)
        self.u2=up(128,64)
        self.u3=up(64,1)
    def forward(self,x):
        b=self.d1(x)
        b=self.d2(b)
        b=self.d3(b)
        b=self.d4(b)
        b=self.u11(b)
        b=self.u1(b)
        b=self.u2(b)
        b=self.u3(b)
        return b
net=Net()
net=net.float()
lr=1e-05


X=np.load('/media/jatin/OS/Users/Jatin/Desktop/STUFF/CNN/Semantic segmentation/input_128.npy')
print(len(X))
Y=np.load('/media/jatin/OS/Users/Jatin/Desktop/STUFF/CNN/Semantic segmentation/output_128.npy')
loss_function=torch.nn.BCELoss()
optimizer=torch.optim.Adam(net.parameters())
for i in range(1):
    x=X[i]
    y=Y[i]
    y=cv2.resize(y,(120,120))
    x=np.reshape(x,(-1,3,128,128))
    x=torch.from_numpy(x)
    y=np.reshape(y,(-1,1,120,120))    
    y=torch.from_numpy(y)
    y_pred=net(x.float())
    loss=loss_function(y_pred.float(),y.float())
    print('loss '+str(i)+'th iter=',loss)
    loss.backward()
    net.zero_grad()
    optimizer.step()
path='/home/jatin/CNN/sem_seg_12.pt'
torch.save(net,path)