sklearn.neural network.MLPClassifier: Difference between revisions

From GM-RKB
Jump to navigation Jump to search
(Created page with "A sklearn.neural_network.MLPClassifier is a Multi-layer Perceptron Classification System within <code>sklearn.neural_network</code>. * <B>Context</B> ** Usage: :...")
 
No edit summary
Line 16: Line 16:
** [http://scikit-learn.org/stable/auto_examples/neural_networks/plot_mlp_alpha.html Varying regularization in Multi-layer Perceptron].
** [http://scikit-learn.org/stable/auto_examples/neural_networks/plot_mlp_alpha.html Varying regularization in Multi-layer Perceptron].
** [http://scikit-learn.org/stable/auto_examples/neural_networks/plot_mlp_training_curves.html Compare Stochastic learning strategies for MLPClassifier]
** [http://scikit-learn.org/stable/auto_examples/neural_networks/plot_mlp_training_curves.html Compare Stochastic learning strategies for MLPClassifier]
** [http://scikit-learn.org/stable/auto_examples/neural_networks/plot_mnist_filters.html Visualization of MLP weights on MNIST]
* <B>Counter-Example(s):</B>
* <B>Counter-Example(s):</B>
** <code>[[sklearn.neural network.MLPRegressor]]</code>
** <code>[[sklearn.neural network.MLPRegressor]]</code>

Revision as of 22:54, 17 December 2017

A sklearn.neural_network.MLPClassifier is a Multi-layer Perceptron Classification System within sklearn.neural_network.

  • Context
    • Usage:
1) Import MLP Classification System from scikit-learn : from sklearn.neural_network import MLPClassifier
2) Create design matrix X and response vector Y
3) Create Classifier object: clf=MLPClassifier([hidden_layer_sizes=(100, ), activation=’relu’, solver=’adam’, alpha=0.0001, batch_size=’auto’, learning_rate=’constant’, learning_rate_init=0.001,...])
4) Choose method(s):


References

2017a

2017b

>>> from sklearn.neural_network import MLPClassifier

X = [ [0., 0.], [1., 1.] ]

y = [0, 1]

clf = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1)

clf.fit(X, y)

After fitting (training), the model can predict labels for new samples:

>>> clf.predict([ [2., 2.], [-1., -2.] ])
MLP can fit a non-linear model to the training data. clf.coefs_ contains the weight matrices that constitute the model parameters:
>>> [coef.shape for coef in clf.coefs_]
Currently, MLPClassifier supports only the Cross-Entropy loss function, which allows probability estimates by running the predict_proba method. MLP trains using Backpropagation. More precisely, it trains using some form of gradient descent and the gradients are calculated using Backpropagation. For classification, it minimizes the Cross-Entropy loss function, giving a vector of probability estimates P(y|x) per sample x:
>>> clf.predict_proba([ [2., 2.], [1., 2.] ])
MLPClassifier supports multi-class classification by applying Softmax as the output function. Further, the model supports multi-label classification in which a sample can belong to more than one class. For each class, the raw output passes through the logistic function. Values larger or equal to 0.5 are rounded to 1, otherwise to 0. For a predicted output of a sample, the indices where the value is 1 represents the assigned classes of that sample:
>>> X = [ [0., 0.], [1., 1.] ] y = [ [0, 1], [1, 1] ]

clf = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(15,), random_state=1)

clf.fit(X, y)

clf.predict([ [1., 2.] ])

clf.predict([ [0., 0.] ])