8000 Fix MLP batch_size test warning for default value · scikit-learn/scikit-learn@5ff3d02 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5ff3d02

Browse files
committed
Fix MLP batch_size test warning for default value
Initializing batch_size to 'auto' and setting it to n_samples value for the default case.
1 parent 13cc60a commit 5ff3d02

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

doc/modules/neural_networks_supervised.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ training samples::
8282
>>> clf = MLPClassifier(algorithm='l-bfgs', alpha=1e-5, hidden_layer_sizes=(5, 2), random_state=1)
8383
>>> clf.fit(X, y) # doctest: +NORMALIZE_WHITESPACE
8484
MLPClassifier(activation='relu', algorithm='l-bfgs', alpha=1e-05,
85-
batch_size=200, beta_1=0.9, beta_2=0.999, early_stopping=False,
85+
batch_size='auto', beta_1=0.9, beta_2=0.999, early_stopping=False,
8686
epsilon=1e-08, hidden_layer_sizes=(5, 2), learning_rate='constant',
8787
learning_rate_init=0.001, max_iter=200, momentum=0.9,
8888
nesterovs_momentum=True, power_t=0.5, random_state=1, shuffle=True,
@@ -137,7 +137,7 @@ value is `1` represents the assigned classes of that sample::
137137
>>> clf = MLPClassifier(algorithm='l-bfgs', alpha=1e-5, hidden_layer_sizes=(15,), random_state=1)
138138
>>> clf.fit(X, y)
139139
MLPClassifier(activation='relu', algorithm='l-bfgs', alpha=1e-05,
140-
batch_size=200, beta_1=0.9, beta_2=0.999, early_stopping=False,
140+
batch_size='auto', beta_1=0.9, beta_2=0.999, early_stopping=False,
141141
epsilon=1e-08, hidden_layer_sizes=(15,), learning_rate='constant',
142142
learning_rate_init=0.001, max_iter=200, momentum=0.9,
143143
nesterovs_momentum=True, power_t=0.5, random_state=1, shuffle=True,

sklearn/neural_network/multilayer_perceptron.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def _fit(self, X, y, incremental=False):
346346
self._initialize(y, layer_units)
347347

348348
# l-bfgs does not support mini-batches
349-
if self.algorithm == 'l-bfgs':
349+
if self.batch_size == 'auto' or self.algorithm == 'l-bfgs':
350350
batch_size = n_samples
351351
else:
352352
if self.batch_size < 1 or self.batch_size > n_samples:
@@ -493,7 +493,11 @@ def _fit_stochastic(self, X, y, activations, deltas, coef_grads,
493493
y_val = None
494494

495495
n_samples = X.shape[0]
496-
batch_size = np.clip(self.batch_size, 1, n_samples)
496+
497+
if self.batch_size == 'auto':
498+
batch_size = n_samples
499+
else:
500+
batch_size = np.clip(self.batch_size, 1, n_samples)
497501

498502
try:
499503
for it in range(self.max_iter):
@@ -714,7 +718,7 @@ class MLPClassifier(BaseMultilayerPerceptron, ClassifierMixin):
714718
alpha : float, optional, default 0.0001
715719
L2 penalty (regularization term) parameter.
716720
717-
batch_size : int, optional, default 200
721+
batch_size : int, optional, default 'auto'
718722
Size of minibatches for stochastic optimizers.
719723
If the algorithm is 'l-bfgs', the classifier will not use minibatch.
720724
@@ -864,7 +868,7 @@ class MLPClassifier(BaseMultilayerPerceptron, ClassifierMixin):
864868
"""
865869
def __init__(self, hidden_layer_sizes=(100,), activation="relu",
866870
algorithm='adam', alpha=0.0001,
867-
batch_size=200, learning_rate="constant",
871+
batch_size='auto', learning_rate="constant",
868872
learning_rate_init=0.001, power_t=0.5, max_iter=200,
869873
shuffle=True, random_state=None, tol=1e-4,
870874
verbose=False, warm_start=False, momentum=0.9,
@@ -1067,7 +1071,7 @@ class MLPRegressor(BaseMultilayerPerceptron, RegressorMixin):
10671071
alpha : float, optional, default 0.0001
10681072
L2 penalty (regularization term) parameter.
10691073
1070-
batch_size : int, optional, default 200
1074+
batch_size : int, optional, default 'auto'
10711075
Size of minibatches for stochastic optimizers.
10721076
If the algorithm is 'l-bfgs', the classifier will not use minibatch.
10731077
@@ -1211,7 +1215,7 @@ class MLPRegressor(BaseMultilayerPerceptron, RegressorMixin):
12111215
"""
12121216
def __init__(self, hidden_layer_sizes=(100,), activation="relu",
12131217
algorithm='adam', alpha=0.0001,
1214-
batch_size=200, learning_rate="constant",
1218+
batch_size='auto', learning_rate="constant",
12151219
learning_rate_init=0.001,
12161220
power_t=0.5, max_iter=200, shuffle=True,
12171221
random_state=None, tol=1e-4,

0 commit comments

Comments
 (0)
0