签名与类中的基方法不匹配

2024-10-01 07:45:18 发布

您现在位置:Python中文网/ 问答频道 /正文

请帮助理解如何指定基类方法签名,该签名至少可以包含一个可能的附加参数

我需要有一个forward方法,该方法至少接受X,以及根据子类可能不需要的附加参数

class Layer:
    def forward(self, X, *args) -> float:

子类SoftmaxWithLogLossforwardX和一个附加参数T

class SoftmaxWithLogLoss(Layer):
    def forward(self, X: np.ndarray, T: np.ndarray) -> float:  <--- Signature does not match 

它会导致警告Signature of method 'SoftmaxWithLogLoss.forward()' does not match signature of base method in class 'Layer'

请解释原因以及如何修复


Tags: 方法selflayer参数defnpnotfloat