sklearn.neural network.MLPClassifier

From GM-RKB
Jump to navigation Jump to search

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:

{| class="wikitable" style="margin-left: 50px;border:1px;background:#f2f2f2"

|style="font-family:monospace; font-size:10pt;font-weight=bold;text-align:left;width:700px;"|>>> 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.] ])