如何在 MLPClassifier 中设置初始权重? [英] How to set initial weights in MLPClassifier?

查看:69
本文介绍了如何在 MLPClassifier 中设置初始权重?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到设置神经网络初始权重的方法,有人能告诉我怎么做吗?我正在使用 python 包 sklearn.neural_network.MLPClassifier.

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.

代码如下:

from sklearn.neural_network import MLPClassifier
classifier = MLPClassifier(solver="sgd")
classifier.fit(X_train, y_train)

推荐答案

解决方案:一个可行的解决方案是从 MLPClassifier 继承并覆盖 _init_coef 方法.在 _init_coef 中编写代码来设置初始权重.然后使用新类 "MLPClassifierOverride" 如下例所示,而不是 "MLPClassifier"

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

这篇关于如何在 MLPClassifier 中设置初始权重?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆