Tanh inplace error

x = self.tanh(x) made this RuntimeError. But if this code line is changed with “x += bias”, no error exists.
Can anybody help me with error reasion?

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation

def conv(in_channels, out_channels, kernel_size,                                   
           stride=1, groups=1, bias=True):                                           
     return nn.Conv2d(in_channels, out_channels, kernel_size,                                            
                       stride=stride, padding=(kernel_size//2),                      
                       groups=groups, bias=bias)                                     
 class ESPCN(nn.Module):                                                            
      def __init__(self, colors=3, channels=24, scale_factor=2):                      
         super(ESPCN, sefl).__init__()                                              
         self.bias = Interpolation(scale_factor)                                    
         self.conv1 = conv(colors, channels, 5, 1)                                  
         self.conv2 = conv(channels, channels, 3, 1)                                
         self.conv3 = conv(channels, (scale_factor**2)*colors, 3, 1)                
         self.PS    = nn.PixelShuffle(scale_factor)                                 
         self.tanh  = nn.Tanh()                                                     
      def forward(self, x):                                                          
         bias = self.bias(x)                                                        
         x = F.relu(self.conv1(x), inplace=True)                                    
         x = F.relu(self.conv2(x), inplace=True)                                    
         x = F.relu(self.conv3(x), inplace=True)                                    
         x = self.PS(x)                                                             
         x = self.tanh(x)    #<--------                                                       
         x += bias                                                                  
         return x   

I’m not sure, but maybe this answer is related to this question.

1 Like