首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何覆盖方法并选择要调用的方法

如何覆盖方法并选择要调用的方法
EN

Stack Overflow用户
提问于 2022-03-22 17:21:00
回答 1查看 44关注 0票数 1

我正试图从零开始实施神经网络。默认情况下,它可以像我预期的那样工作,但是,现在我正在尝试将L2正则化添加到我的模型中。为此,我需要改变三种方法-

计算成本的成本()#,cost_derivative,backward_prop #向后繁殖

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

代码语言:javascript
复制
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,则需要对上述方法进行轻微更改。

我可以复制这三种功能并对它们进行更改,在培训中询问:

代码语言:javascript
复制
if self.regularization:   cost =  self.cost_reg(input) # as if i'm overriding the cost function 

对其他人也一样

然而

这种方法有两个问题

  1. ,我不认为这是一种真正的仿生方式。因此,当复制一个方法并给它取另一个名称时,稍微做一些修改,看起来就不太好。

  1. ,我不想在每次迭代中检查self.regularization is True还是self.regularization is None。我认为这样可以减缓模型的速度,而且还有更好的方法。如果我错了,请告诉我这个问题。--

我从模型中想要的是事先意识到正规化。

例如,我有self.regualrization==True

当我在火车函数中调用逆逆方法时,它用正则化表达式返回反向传播。

代码。

这对您来说太复杂了,无法阅读整个代码,并建议我一种我想做的事情的方法。因此,实际上我用相同的场景编写了更简单的代码。

代码语言:javascript
复制
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的答案

这解决不了我的任何问题。

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

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

我还试图重写该方法。

代码语言:javascript
复制
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:“网络”对象不可迭代

如果您有实现正则化的修改想法,那么下面是实际的train()和cost()函数

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-22 20:22:39

一般信息

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

代码语言:javascript
复制
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())

换句话说,您实际上创建了一个子类的实例,它覆盖了一个特定的函数(这里是: compute_cost),并继承了所有剩余的函数。现在,当调用cost_value()时,它将调用相应的compute_cost。实际上,您也不需要compute_cost。

代码语言:javascript
复制
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())

代码问题

如果出于某种原因,您想继续使用您自己的代码,那么问题是

代码语言:javascript
复制
    def cost_value(self):
        if self.regularization:
            return regularized(self).compute_cost()
        else:
            return self.compute_cost()

您正在将对"self“的引用传递给regularized的构造函数,该构造函数期待sizes,它应该是

代码语言:javascript
复制
    def cost_value(self):
        if self.regularization:
            return regularized(self.sizes).compute_cost()
        else:
            return self.compute_cost()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71576367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档