I am trying to build a simple resnet model but getting an error "conv2d() received an invalid combination of arguments"

here is my code and error
Residual block
def Residual_block(in_channel,out_channel):

res_layer = [nn.Conv2d(in_channel, out_channel, kernel_size=3, padding=1, stride=1 ),

          nn.BatchNorm2d(out_channel),

          nn.ReLU(inplace= True),

          

          nn.Conv2d(out_channel, out_channel, kernel_size=3, padding=1, stride=1),

          nn.BatchNorm2d(out_channel)]

return nn.Sequential(*res_layer)
convolutional
def convo_block(in_channel, out_channel):

convo_layer = [nn.Conv2d(in_channel, out_channel, kernel_size=3, padding=1,stride=1),

             nn.BatchNorm2d(out_channel),

             nn.ReLU() ]

return nn.Sequential(*convo_layer)

Model
class Resnet_9(nn.Module):

def init(self,in_channel,out_classes):

super().__init__()

self.conv_1 = convo_block(in_channel, 64)

                                      

self.conv_2 = convo_block(64, 128)

self.pool_1= nn.MaxPool2d(2,2)        #16*16*128

                                    

self.residual_block1 = Residual_block(128,128)

self.conv_3 = convo_block(128, 256)

self.pool_2= nn.MaxPool2d(2,2)        #8*8*128

                       

self.conv_4 = convo_block(256, 512)

self.pool_3= nn.MaxPool2d(2,2)        #4*4*128

                                    

self.residual_block2 = Residual_block(512,512) 

self.classfier = nn.Sequential(nn.MaxPool2d(2,2),        #2*2*512

                                nn.Flatten(),

                                nn.Dropout(0.2),

                                nn.Linear(512,out_classes)

                                )

def forward(self,x):

out = self.conv_1(x)

out = self.conv_2(out)

out = self.pool_1(out)

out = self.residual_block1(out) + out

out = nn.ReLU(out)

out = self.conv_3(out)

out = self.pool_2(out)

out = self.conv_4(out)

out = self.pool_3(out)

out = self.residual_block2(out) + out

out = nn.ReLU(out)

out = self.classfier(out)

return out

Error
TypeError: conv2d() received an invalid combination of arguments - got (ReLU, Parameter, Parameter, tuple, tuple, tuple, int), but expected one of:

  • (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, tuple of ints padding, tuple of ints dilation, int groups)
    didn’t match because some of the arguments have invalid types: (!ReLU!, !Parameter!, !Parameter!, !tuple!, !tuple!, !tuple!, int)
  • (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, str padding, tuple of ints dilation, int groups)
    didn’t match because some of the arguments have invalid types: (!ReLU!, !Parameter!, !Parameter!, !tuple!, !tuple!, !tuple!, int)

Hi Umar!

The problem is that nn.ReLU(out) constructs a ReLU object so
that you then pass that ReLU object to conv_3.

You want either:

out = nn.ReLU() (out)
# or
out = nn.functional.relu (out)

(I’m a little surprised that nn.ReLU(out) itself doesn’t throw an
error because in most cases your out won’t be able to be cast
to a bool.)

Best.

K. Frank

@KFrank
it worked .
Thankyou so much.