Method 'detach' already has a docstring

Angry_potato: <p>Hi<br>
When i try to save my model using torch.save i get this error.</p>

ptrblck: <p>Could you post a minimal code snippet to reproduce this issue, please?</p>

Angry_potato: <p>Sorry<br>
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</p>
<pre><code class="lang-python">#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)
</code></pre>

ptrblck: <p>Thanks for the code.<br>
I’ve formatted it for easier debugging. You can add code snippets by wrapping them in three backticks ``` <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>
<p>Could you try to save the <code>state_dict</code> instead of the complete model via: <code>torch.save(net.state_dict(), path)</code>?<br>
Also, note that you are currently not training the model, since you are zeroing out the gradients after they were calculated.<br>
Move <code>net.zero_grad()</code> further up or down in the code. It should not be called between <code>loss.backward()</code> and <code>optimizer.step()</code>.</p>

Angry_potato: <p>Thank you very much.I will try this and inform you the results.</p>

Angry_potato: <p>I tried that, still got the error</p>

ptrblck: <p>The code works for me:</p>
<pre><code class="lang-python">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')
</code></pre>
<p>I can’t even find a call to <code>detach()</code>, so I’m not sure what’s causing this error.<br>
Could you post the stack trace?</p>

Angry_potato: <p>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</p>

ptrblck: <p>Could you post the complete error message, please?</p>

IvanKush: <p>here <a class="mention" href="/u/ptrblck">@ptrblck</a></p>
<blockquote>
<p>File “”, line 1, in <br>
runfile(‘/home/trek/projects/CNN/gans.py’, wdir=‘/home/trek/projects/CNN’)</p>
<p>File “/usr/lib/python3/dist-packages/spyder_kernels/customize/spydercustomize.py”, line 678, in runfile<br>
execfile(filename, namespace)</p>
<p>File “/usr/lib/python3/dist-packages/spyder_kernels/customize/spydercustomize.py”, line 106, in execfile<br>
exec(compile(f.read(), filename, ‘exec’), namespace)</p>
<p>File “/home/trek/projects/CNN/gans.py”, line 8, in <br>
from torchvision.utils import save_image</p>
<p>File “/home/trek/.local/lib/python3.7/site-packages/torchvision/<strong>init</strong>.py”, line 3, in <br>
from torchvision import models</p>
<p>File “/home/trek/.local/lib/python3.7/site-packages/torchvision/models/<strong>init</strong>.py”, line 1, in <br>
from .alexnet import *</p>
<p>File “/home/trek/.local/lib/python3.7/site-packages/torchvision/models/alexnet.py”, line 1, in <br>
import torch</p>
<p>File “/home/trek/.local/lib/python3.7/site-packages/torch/<strong>init</strong>.py”, line 239, in <br>
from .tensor import Tensor</p>
<p>File “/home/trek/.local/lib/python3.7/site-packages/torch/tensor.py”, line 41, in <br>
class Tensor(torch._C._TensorBase):</p>
<p>File “/home/trek/.local/lib/python3.7/site-packages/torch/tensor.py”, line 285, in Tensor<br>
“”")</p>
<p>RuntimeError: method ‘detach’ already has a docstring</p>
</blockquote>

ptrblck: <p>Sorry, I’ve never seen this error before.<br>
Is this error only raised in Spyder?<br>
If so, could you update it to the latest version?</p>

Angry_potato: <p>Yes,this occurs only in spyder. I ran the same code in jupyter and it was perfectly fine</p>

ptrblck: <p>Thanks for the information.<br>
Which Spyder version are you using? If the latest (&gt;= 4), could you create an issue on the Spyder GitHub and link it here, please?</p>

alidiak: <p>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: <a href="https://github.com/spyder-ide/spyder/issues/12701" rel="nofollow noopener">https://github.com/spyder-ide/spyder/issues/12701</a> . Thanks and best.</p>