我想在sklearn的MLP分类器中实现L1正则化。这是我的代码,其中alpha=0.0001
是L2正则化的默认值。我想使用L1正则化而不是L2。
# evaluate a Neural Networks with ReLU and L1 norm regularization
from numpy import mean
from numpy import std
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.neural_network import MLPClassifier
# prepare the cross-validation procedure (10X10)
cv = KFold(n_splits=10, random_state=1, shuffle=True)
# create model L2 Regularization ["alpha" here is used as a hyperparamter for L2
regularization]
model = MLPClassifier(alpha=0.0001, hidden_layer_sizes=(100,), activation='relu',
solver='adam')
# evaluate model
scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
# report performance
print('Accuracy: %.3f (%.3f)' % (mean(scores), std(scores)))
这是不可能的。Scikit-learn有很多关于支持神经网络的长期讨论,并决定不支持它。它们提供了非常基本/严格的实现,仅此而已。对于定制,您需要查看keras、tf、torch、jax等。
甚至scikit learn自己也为此推荐了其他图书馆https://scikit-learn.org/stable/related_projects.html#related-项目
Deep neural networks etc.
nolearn A number of wrappers and abstractions around existing neural network libraries
Keras High-level API for TensorFlow with a scikit-learn inspired API.
lasagne A lightweight library to build and train neural networks in Theano.
skorch A scikit-learn compatible neural network library that wraps PyTorch.
scikeras provides a wrapper around Keras to interface it with scikit-learn. SciKeras is the successor of tf.keras.wrappers.scikit_learn.