Method 'detach' already has a docstring

Hi
When i try to save my model using torch.save i get this error.

1 Like

Could you post a minimal code snippet to reproduce this issue, please?

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)

Thanks for the code.
I’ve formatted it for easier debugging. You can add code snippets by wrapping them in three backticks ``` :wink:

Could you try to save the state_dict instead of the complete model via: torch.save(net.state_dict(), path)?
Also, note that you are currently not training the model, since you are zeroing out the gradients after they were calculated.
Move net.zero_grad() further up or down in the code. It should not be called between loss.backward() and optimizer.step().

1 Like

Thank you very much.I will try this and inform you the results.

I tried that, still got the error

The code works for me:

import matplotlib.pyplot as plt
import numpy as np 

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,1)
        self.d2=down(1,1)
        self.d3=down(1,1)
        self.d4=down(1,1)
        self.u11=up(1,1)
        self.u1=up(1,1)
        self.u2=up(1,1)
        self.u3=up(1,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 = torch.randn(1, 3, 128, 128)
print(len(X))
y = torch.randn(1, 1, 120, 120)

loss_function=torch.nn.BCELoss()
optimizer=torch.optim.Adam(net.parameters())
for i in range(1):
    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()

torch.save(net.state_dict(),'tmp.pt')

I can’t even find a call to detach(), so I’m not sure what’s causing this error.
Could you post the stack trace?

This one works for me too. But when i run it again in Spyder i get the error,even when i change the file name. And btw what is a stack trace

1 Like

Could you post the complete error message, please?

here @ptrblck

File “”, line 1, in
runfile(‘/home/trek/projects/CNN/gans.py’, wdir=‘/home/trek/projects/CNN’)

File “/usr/lib/python3/dist-packages/spyder_kernels/customize/spydercustomize.py”, line 678, in runfile
execfile(filename, namespace)

File “/usr/lib/python3/dist-packages/spyder_kernels/customize/spydercustomize.py”, line 106, in execfile
exec(compile(f.read(), filename, ‘exec’), namespace)

File “/home/trek/projects/CNN/gans.py”, line 8, in
from torchvision.utils import save_image

File “/home/trek/.local/lib/python3.7/site-packages/torchvision/init.py”, line 3, in
from torchvision import models

File “/home/trek/.local/lib/python3.7/site-packages/torchvision/models/init.py”, line 1, in
from .alexnet import *

File “/home/trek/.local/lib/python3.7/site-packages/torchvision/models/alexnet.py”, line 1, in
import torch

File “/home/trek/.local/lib/python3.7/site-packages/torch/init.py”, line 239, in
from .tensor import Tensor

File “/home/trek/.local/lib/python3.7/site-packages/torch/tensor.py”, line 41, in
class Tensor(torch._C._TensorBase):

File “/home/trek/.local/lib/python3.7/site-packages/torch/tensor.py”, line 285, in Tensor
“”")

RuntimeError: method ‘detach’ already has a docstring

1 Like

Sorry, I’ve never seen this error before.
Is this error only raised in Spyder?
If so, could you update it to the latest version?

1 Like

Yes,this occurs only in spyder. I ran the same code in jupyter and it was perfectly fine

Thanks for the information.
Which Spyder version are you using? If the latest (>= 4), could you create an issue on the Spyder GitHub and link it here, please?

Hey, I was experiencing a similar issue (it seems to occur when re-importing torch by say running a script twice). I made sure my Spyder was up to date and submitted an issue here: https://github.com/spyder-ide/spyder/issues/12701 . Thanks and best.

1 Like