How to set initial weights in MLPClassifier?

后端 未结 3 1988
無奈伤痛
無奈伤痛 2021-01-02 08:07

I cannot find a way to set the initial weights of the neural network, could someone tell me how please? I am using python package sklearn.neural_network.MLPClassifier.

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-02 08:26

    Solution: A working solution is to inherit from MLPClassifier and override the _init_coef method. In the _init_coef write the code to set the initial weights. Then use the new class "MLPClassifierOverride" as in the example below instead of "MLPClassifier"

    # new class
    class MLPClassifierOverride(MLPClassifier):
    # Overriding _init_coef method
    def _init_coef(self, fan_in, fan_out):
        if self.activation == 'logistic':
            init_bound = np.sqrt(2. / (fan_in + fan_out))
        elif self.activation in ('identity', 'tanh', 'relu'):
            init_bound = np.sqrt(6. / (fan_in + fan_out))
        else:
            raise ValueError("Unknown activation function %s" %
                             self.activation)
        coef_init = ### place your initial values for coef_init here
    
        intercept_init = ### place your initial values for intercept_init here
    
        return coef_init, intercept_init
    

提交回复
热议问题