如何覆盖一个方法并选择调用哪个方法

How to override a method and chose which one to call

我正在尝试从头开始实施神经网络。默认情况下,它按我的预期工作,但是,现在我正在尝试将 L2 正则化添加到我的模型中。为此,我需要更改三个方法-

cost() #计算成本, cost_derivative , backward_prop # 向后传播网络

你可以在下面看到,我有 L2_regularization = None 作为 init 函数的输入

def __init__(self,sizes, activations = None , cost_function = 'binary_cross_entropy' ,param_init_type = None, L2_regularization=None,dropout = None):
        self.sizes = sizes
        self.num_layers = len(sizes)
        self.caches = dict() 
        self.cost_function = cost_function
        
        if activations == None:         self.layer_activations = self.default_layer_activations_init(sizes)
        else:                           self.layer_activations = activations
        

        if param_init_type == None:     self.param_init_type = 'default' 
        else:                           self.param_init_type = param_init_type
        self.parameters_initializer()

所以,如果 L2_regularization 是 True,我需要对上述方法稍作改动。

我可以复制所有三个函数并更改它们,在训练时询问:

if self.regularization:   cost =  self.cost_reg(input) # as if i'm overriding the cost function 

以及其他

不过,

这种方式有两个问题

  1. 我不认为这种方式是真正的 pythonic 方式。所以,复制一个方法,稍作改动,给它起另一个名字,就不太好看了。

  2. 我不想在每次迭代中检查是 self.regularization is True 还是 self.regularization is None:。 我认为它可能会减慢模型的速度,并且有更好的方法。如果我错了,请告诉我这个问题。

我想从模型中得到的是事先了解正则化。

比如我有self.regualrization==True

当我在 train 函数中调用 backprop 方法时,它 returns 使用正则表达式进行反向传播

代码。

如果您阅读整个代码并建议我按照我的意愿做事,那就太复杂了。因此,我实际上用相同的场景编写了更简单的代码

class Network():
    def __init__(self,sizes, regularization = None):
        self.sizes = sizes
        self.expression = 5 
        self.regularization = regularization


    def compute_cost(self):
        count = 0 
        for i in self.sizes:
            count+=i
        return count

    def compute_cost_regularized(self):
        count = 0 
        for i in self.sizes:
            count+=i

        #as if self.expression is value of regularization expression 
        count = count + self.expression

        return count
    
    def cost_value(self):
        if self.regularization:
            return self.compute_cost_regularized()
        else:
            return self.compute_cost()



net_default = Network([3,3,4])
net_regularized= Network([3,3,4],regularization=True)

print('This is the answer from net_default ',net_default.cost_value())
print('This is the answer from net_regularized ',net_regularized.cost_value())

输出是: 这是来自 net_default 10 的回答 这是来自 net_regularized 15

的回答

它没有解决我的 none 个问题。

一个方法我写了 2 次,换了 1 行,我在计算时使用了 if 语句。

不这样做我怎么能写呢。我是否需要避免在每次迭代中使用 if 语句

我也试过覆盖方法

class Network():
    def __init__(self,sizes, regularization = None):
        self.sizes = sizes
        self.expression = 5 
        self.regularization = regularization


    def compute_cost(self):
        count = 0 
        for i in self.sizes:
            count+=i
        return count
    
    def cost_value(self):
        if self.regularization:
            return regularized(self).compute_cost()
        else:
            return self.compute_cost()

class regularized(Network):
    def __init__(self, sizes, regularization=None):
        super().__init__(sizes, regularization)
    def compute_cost(self):
        return super().compute_cost() + self.expression


net_default = Network([3,3,4])
net_regularized= Network([3,3,4],regularization=True)

print('This is the answer from net_default ',net_default.cost_value())
print('This is the answer from net_regularized ',net_regularized.cost_value())

但是,我得到一个错误 TypeError : 'Network' object is not iterable

这里是实际的train()和cost()函数,如果你有实现正则化的修改想法


def cost(self,X,Y):
        #TODO L2 reg ll change cost function
        """param X : Input that will be given to network , Function itself does forward propagation steps and compute cost
           param Y : Wanted output corresponds to given input data. Cost will be computed by This Y and Y_hat which is output of NN for X input"""
        Y_hat = self.feed_forward(X)
        m = Y.shape[1]
        
        if self.cost_function == 'binary_cross_entropy':
            cost = (-1/m)*np.sum( np.multiply(Y,np.log(Y_hat)) + np.multiply( (1-Y) , np.log(1-Y_hat) )) ; cost = np.squeeze(cost)
            return cost
        elif self.cost_function == 'mse':
            cost = (1/m)*np.sum(np.square(Y-Y_hat)) ; cost = np.squeeze(cost) 
            return cost
        else:
            raise Exception('No such cost function yet')


def train(self,X,Y,lr = 0.0001,epoch=1000 , X_test = None , Y_test = None , regularization  = None , dropout = False):
        assert (X.shape[1] == Y.shape[1]) , "Unmatched In out batch size"
        self.caches['A0'] = X
        for iter in range(epoch):
            A_l = self.feed_forward(X)
            dA_l = self.cost_derivative(A_l,Y)
            for layer_num in reversed(range(1,self.num_layers)):
                grad_w,grad_b,dA_l = self.backward_prop(dA_l,layer_num)
                self.update_param(grad_w,grad_b,layer_num, lr = lr)
            if iter% (epoch/10) ==0:
                print('\n COST:::',self.cost(X,Y),end=' ')    
                self.score(X,Y)
                                                                                                                                               
        if X_test is not None:
            self.score(X_test,Y_test)

        #Saving parameters dictionary to file 
        a_file = open("parameters.pkl", "wb")
        pickle.dump(self.parameters, a_file)
        a_file.close()

如果您有兴趣,我已经在此处上传了完整的代码。

https://github.com/IlkinKarimli0/Neural-Network-from-scratch/blob/main/NeuralNetwork.py

一般

总的来说,您不应该为了覆盖单个方法而在对象内部创建对象,相反您可以这样做

class Network():
    def __init__(self, sizes):
        self.sizes = sizes
        self.expression = 5 

    def compute_cost(self):
        count = 0 
        for i in self.sizes:
            count+=i
        return count
    
    def cost_value(self):
        return self.compute_cost()


class RegularizedNetwork(Network):

    def __init__(self, sizes):
        super().__init__(sizes)

    def compute_cost(self):
        return super().compute_cost() + self.expression


net_default = Network([3,3,4])
net_regularized= RegularizedNetwork([3,3,4])

print('This is the answer from net_default ',net_default.cost_value())
print('This is the answer from net_regularized ',net_regularized.cost_value())

换句话说,您实际上创建了子 class 的实例,它覆盖了特定函数(此处:compute_cost),并继承了所有剩余的函数。现在当调用 cost_value() 时,它会调用相应的 compute_cost。事实上你也不需要compute_cost。

class Network():
    def __init__(self, sizes):
        self.sizes = sizes
        self.expression = 5 

    def cost_value(self):
        count = 0 
        for i in self.sizes:
            count+=i
        return count


class RegularizedNetwork(Network):

    def __init__(self, sizes):
        super().__init__(sizes)

    def cost_value(self):
        return super().cost_value() + self.expression


net_default = Network([3,3,4])
net_regularized= RegularizedNetwork([3,3,4])

print('This is the answer from net_default ',net_default.cost_value())
print('This is the answer from net_regularized ',net_regularized.cost_value())

代码问题

如果出于某种原因您想继续使用自己的代码,问题就在这里

    def cost_value(self):
        if self.regularization:
            return regularized(self).compute_cost()
        else:
            return self.compute_cost()

您正在将对“self”的引用传递给期望 sizesregularized 的构造函数,它应该是

    def cost_value(self):
        if self.regularization:
            return regularized(self.sizes).compute_cost()
        else:
            return self.compute_cost()