Python 如何解决;“未实施错误”;

Python 如何解决;“未实施错误”;,python,pytorch,convolution,Python,Pytorch,Convolution,我定义了一个三层卷积层(self.convs),输入张量的形状为([100,10,24]) 当我执行x\u convs=self.convs(变量(torch.from\u numpy(x).type(torch.FloatTensor))时,它给出了错误 `94 registered hooks while the latter silently ignores them. 95 """ --->

我定义了一个三层卷积层(self.convs),输入张量的形状为([100,10,24])

当我执行
x\u convs=self.convs(变量(torch.from\u numpy(x).type(torch.FloatTensor))
时,它给出了错误

    `94             registered hooks while the latter silently ignores them.

     95         """

---> 96         raise NotImplementedError`
ConvBlock的定义如下所示

class ConvBlock(nn.Module):
    def __init__(self, T, in_channels, out_channels, filter_size):
        super(ConvBlock, self).__init__()
        padding = self._calc_padding(T, filter_size)
        self.conv=nn.Conv1d(in_channels, out_channels, filter_size, padding=padding)
        self.relu=nn.ReLU()
        self.maxpool=nn.AdaptiveMaxPool1d(T)       
        self.zp=nn.ConstantPad1d((1, 0), 0)        
        
    def _calc_padding(self, Lin, kernel_size, stride=1, dilation=1):
        p = int(((Lin-1)*stride + 1 + dilation*(kernel_size - 1) - Lin)/2)    
        return p                                                              
    
    def forward(self, x):
        x = x.permute(0,2,1)    
        x = self.conv(x)        
        x = self.relu(x)
        x = self.maxpool(x)     
        x = x.permute(0,2,1)    
        return x

“forward”函数有正确的缩进,因此我无法确定它是怎么回事。

如果您希望这三个层按顺序执行,您应该使用
nn.Sequential
而不是
nn.ModuleList
nn.ModuleList
不实现
forward()
方法,但
nn.Sequential
实现


如果希望在
forward()
方法中有一些特殊行为,可以将
nn.ModuleList
子类化,并覆盖它的
forward()
您试图调用的
ModuleList
,它是一个
列表(即Python中的列表对象),稍作修改以便与Pytork一起使用

快速修复方法是将
self.convs
调用为:

x_convs=self.convs[0](变量(torch.from_numpy(x.type)(torch.FloatTensor)))
如果len(self.convs)>1:
对于self中的conv.convs[1:]:
x_convs=conv(x_convs)
也就是说,尽管
self.convs
是一个
列表
,但它的每个成员都是一个
模块
。您可以直接调用self.convs的每个成员,使用其索引,例如“self.convsan_index”

或者,您可以借助
functools
模块:

从functools导入reduce
def应用层(层输入,层):
返回层(层\输入)
_self_convs的输出_=reduce(应用_层,self.convs,变量(torch.from_numpy(X).type(torch.FloatTensor)))
注意,
变量
关键字不再使用。

请提取并提供一个,以及完整的错误消息。作为一个新用户,也需要阅读。
class ConvBlock(nn.Module):
    def __init__(self, T, in_channels, out_channels, filter_size):
        super(ConvBlock, self).__init__()
        padding = self._calc_padding(T, filter_size)
        self.conv=nn.Conv1d(in_channels, out_channels, filter_size, padding=padding)
        self.relu=nn.ReLU()
        self.maxpool=nn.AdaptiveMaxPool1d(T)       
        self.zp=nn.ConstantPad1d((1, 0), 0)        
        
    def _calc_padding(self, Lin, kernel_size, stride=1, dilation=1):
        p = int(((Lin-1)*stride + 1 + dilation*(kernel_size - 1) - Lin)/2)    
        return p                                                              
    
    def forward(self, x):
        x = x.permute(0,2,1)    
        x = self.conv(x)        
        x = self.relu(x)
        x = self.maxpool(x)     
        x = x.permute(0,2,1)    
        return x