Stoner
Stoner

Reputation: 906

How many nodes in input and output layers of sklearn's MLPClassifier for MNIST digits classification task

I am following the example on https://scikit-learn.org/stable/auto_examples/neural_networks/plot_mnist_filters.html#sphx-glr-auto-examples-neural-networks-plot-mnist-filters-py and I'm trying to figure out if my understanding is correct on the number of nodes in the input and output layers in the example. The code required is as follows:

import matplotlib.pyplot as plt
from sklearn.datasets import fetch_openml
from sklearn.neural_network import MLPClassifier

print(__doc__)

# Load data from https://www.openml.org/d/554
X, y = fetch_openml('mnist_784', version=1, return_X_y=True)
X = X / 255.

# rescale the data, use the traditional train/test split
X_train, X_test = X[:60000], X[60000:]
y_train, y_test = y[:60000], y[60000:]

mlp = MLPClassifier(hidden_layer_sizes=(50,), max_iter=10, alpha=1e-4,
                    solver='sgd', verbose=10, random_state=1,
                    learning_rate_init=.1)

mlp.fit(X_train, y_train)
score = mlp.score(X_test, y_test)

According to https://dudeperf3ct.github.io/mlp/mnist/2018/10/08/Force-of-Multi-Layer-Perceptron/, the example states 784 nodes in the input layer (which I presume is from the shape of the data) and 10 nodes for the output layer, 1 for each digit.

Is that the case for the MLPClassifier in the code above?

Thank you and some clarification will be great!

Upvotes: 1

Views: 2227

Answers (1)

Vinu
Vinu

Reputation: 116

Your understanding is correct. The image size of MNIST digits data is 28x28 which is flatten to 784 and output size is 10 (one for each number from 0 to 9). MLPClassifier implicitly designs the input and output layer based on the provided data in Fit method.

Your NN configuration will look like: Input: 200 x 784 Hidden layer: 784 x 50 (feature size: 200 x 50) Output layer: 50 x 10 (feature size: 200 x 10)

Batch size is 200 by default in MLPClassifier as the training data size is 60000

Upvotes: 1

Related Questions