cannot assign module before Module.__init__() call

 

 

错误翻译成中文是:属性错误,模块不能在初始化之前赋值。错误原因有可能是:

在类的初始化里面没有加上父类的初始化,比如:

class Conv2dBatchLeaky(nn.Module):
    def __init__(self, in_channels, out_channels, kernel_size, stride, leaky_slope=0.1):

        self.in_channels = in_channels
        self.out_channels = out_channels
        self.kernel_size = kernel_size

 

解决方法:

加上:

super(Conv2dBatchLeaky, self).__init__()

代码如下即可:

class Conv2dBatchLeaky(nn.Module):
    def __init__(self, in_channels, out_channels, kernel_size, stride, leaky_slope=0.1):
        super(Conv2dBatchLeaky, self).__init__()

        self.in_channels = in_channels
        self.out_channels = out_channels
        self.kernel_size = kernel_size

 

你可能感兴趣的:(pytorch知识宝典)