Kristofer
Kristofer

Reputation: 1487

How to set up the number of inputs neurons in sklearn MLPClassifier?

Given a dataset of n samples, m features, and using [sklearn.neural_network.MLPClassifier][1], how can I set hidden_layer_sizes to start with m inputs? For instance, I understand that if hidden_layer_sizes= (10,10) it means there are 2 hidden layers each of 10 neurons (i.e., units) but I don't know if this also implies 10 inputs as well.

Thank you

Upvotes: 2

Views: 3433

Answers (1)

sascha
sascha

Reputation: 33522

This classifier/regressor, as implemented, is doing this automatically when calling fit.

This can be seen in it's code here.

Excerpt:

n_samples, n_features = X.shape

# Ensure y is 2D
if y.ndim == 1:
    y = y.reshape((-1, 1))

self.n_outputs_ = y.shape[1]

layer_units = ([n_features] + hidden_layer_sizes +
               [self.n_outputs_])

You see, that your potentially given hidden_layer_sizes is surrounded by layer-dimensions defined by your data within .fit(). This is the reason, the signature reads like this with a subtraction of 2!:

Parameters

hidden_layer_sizes : tuple, length = n_layers - 2, default (100,)

The ith element represents the number of neurons in the ith hidden layer.

Upvotes: 3

Related Questions