<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> from abc import ABCMeta , abstractmethod <EOL> from scipy . optimize import fmin_l_bfgs_b <EOL> import warnings <EOL> from . . base import BaseEstimator , ClassifierMixin , RegressorMixin <EOL> from . _base import logistic , softmax <EOL> from . _base import ACTIVATIONS , DERIVATIVES , LOSS_FUNCTIONS <EOL> from . _stochastic_optimizers import SGDOptimizer , AdamOptimizer <EOL> from . . model_selection import train_test_split <EOL> from . . externals import six <EOL> from . . preprocessing import LabelBinarizer <EOL> from . . utils import gen_batches , check_random_state <EOL> from . . utils import shuffle <EOL> from . . utils import check_array , check_X_y , column_or_1d <EOL> from . . exceptions import ConvergenceWarning <EOL> from . . utils . extmath import safe_sparse_dot <EOL> from . . utils . validation import check_is_fitted <EOL> from . . utils . multiclass import _check_partial_fit_first_call <EOL> _STOCHASTIC_ALGOS = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def _pack ( coefs_ , intercepts_ ) : <EOL> """<STR_LIT>""" <EOL> return np . hstack ( [ l . ravel ( ) for l in coefs_ + intercepts_ ] ) <EOL> class BaseMultilayerPerceptron ( six . with_metaclass ( ABCMeta , BaseEstimator ) ) : <EOL> """<STR_LIT>""" <EOL> @ abstractmethod <EOL> def __init__ ( self , hidden_layer_sizes , activation , algorithm , <EOL> alpha , batch_size , learning_rate , learning_rate_init , power_t , <EOL> max_iter , loss , shuffle , random_state , tol , verbose , <EOL> warm_start , momentum , nesterovs_momentum , early_stopping , <EOL> validation_fraction , beta_1 , beta_2 , epsilon ) : <EOL> self . activation = activation <EOL> self . algorithm = algorithm <EOL> self . alpha = alpha <EOL> self . batch_size = batch_size <EOL> self . learning_rate = learning_rate <EOL> self . learning_rate_init = learning_rate_init <EOL> self . power_t = power_t <EOL> self . max_iter = max_iter <EOL> self . loss = loss <EOL> self . hidden_layer_sizes = hidden_layer_sizes <EOL> self . shuffle = shuffle <EOL> self . random_state = random_state <EOL> self . tol = tol <EOL> self . verbose = verbose <EOL> self . warm_start = warm_start <EOL> self . momentum = momentum <EOL> self . nesterovs_momentum = nesterovs_momentum <EOL> self . early_stopping = early_stopping <EOL> self . validation_fraction = validation_fraction <EOL> self . beta_1 = beta_1 <EOL> self . beta_2 = beta_2 <EOL> self . epsilon = epsilon <EOL> def _unpack ( self , packed_parameters ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( self . n_layers_ - <NUM_LIT:1> ) : <EOL> start , end , shape = self . _coef_indptr [ i ] <EOL> self . coefs_ [ i ] = np . reshape ( packed_parameters [ start : end ] , shape ) <EOL> start , end = self . _intercept_indptr [ i ] <EOL> self . intercepts_ [ i ] = packed_parameters [ start : end ] <EOL> def _forward_pass ( self , activations , with_output_activation = True ) : <EOL> """<STR_LIT>""" <EOL> hidden_activation = ACTIVATIONS [ self . activation ] <EOL> for i in range ( self . n_layers_ - <NUM_LIT:1> ) : <EOL> activations [ i + <NUM_LIT:1> ] = safe_sparse_dot ( activations [ i ] , <EOL> self . coefs_ [ i ] ) <EOL> activations [ i + <NUM_LIT:1> ] += self . intercepts_ [ i ] <EOL> if ( i + <NUM_LIT:1> ) != ( self . n_layers_ - <NUM_LIT:1> ) : <EOL> activations [ i + <NUM_LIT:1> ] = hidden_activation ( activations [ i + <NUM_LIT:1> ] ) <EOL> if with_output_activation : <EOL> output_activation = ACTIVATIONS [ self . out_activation_ ] <EOL> activations [ i + <NUM_LIT:1> ] = output_activation ( activations [ i + <NUM_LIT:1> ] ) <EOL> return activations <EOL> def _compute_loss_grad ( self , layer , n_samples , activations , deltas , <EOL> coef_grads , intercept_grads ) : <EOL> """<STR_LIT>""" <EOL> coef_grads [ layer ] = safe_sparse_dot ( activations [ layer ] . T , <EOL> deltas [ layer ] ) <EOL> coef_grads [ layer ] += ( self . alpha * self . coefs_ [ layer ] ) <EOL> coef_grads [ layer ] /= n_samples <EOL> intercept_grads [ layer ] = np . mean ( deltas [ layer ] , <NUM_LIT:0> ) <EOL> return coef_grads , intercept_grads <EOL> def _loss_grad_lbfgs ( self , packed_coef_inter , X , y , activations , deltas , <EOL> coef_grads , intercept_grads ) : <EOL> """<STR_LIT>""" <EOL> self . _unpack ( packed_coef_inter ) <EOL> loss , coef_grads , intercept_grads = self . _backprop ( <EOL> X , y , activations , deltas , coef_grads , intercept_grads ) <EOL> self . n_iter_ += <NUM_LIT:1> <EOL> grad = _pack ( coef_grads , intercept_grads ) <EOL> return loss , grad <EOL> def _backprop ( self , X , y , activations , deltas , coef_grads , <EOL> intercept_grads ) : <EOL> """<STR_LIT>""" <EOL> n_samples = X . shape [ <NUM_LIT:0> ] <EOL> activations = self . _forward_pass ( activations ) <EOL> loss = LOSS_FUNCTIONS [ self . loss ] ( y , activations [ - <NUM_LIT:1> ] ) <EOL> values = np . sum ( <EOL> np . array ( [ np . dot ( s . ravel ( ) , s . ravel ( ) ) for s in self . coefs_ ] ) ) <EOL> loss += ( <NUM_LIT:0.5> * self . alpha ) * values / n_samples <EOL> last = self . n_layers_ - <NUM_LIT:2> <EOL> diff = y - activations [ - <NUM_LIT:1> ] <EOL> deltas [ last ] = - diff <EOL> coef_grads , intercept_grads = self . _compute_loss_grad ( <EOL> last , n_samples , activations , deltas , coef_grads , intercept_grads ) <EOL> for i in range ( self . n_layers_ - <NUM_LIT:2> , <NUM_LIT:0> , - <NUM_LIT:1> ) : <EOL> deltas [ i - <NUM_LIT:1> ] = safe_sparse_dot ( deltas [ i ] , self . coefs_ [ i ] . T ) <EOL> derivative = DERIVATIVES [ self . activation ] <EOL> deltas [ i - <NUM_LIT:1> ] *= derivative ( activations [ i ] ) <EOL> coef_grads , intercept_grads = self . _compute_loss_grad ( <EOL> i - <NUM_LIT:1> , n_samples , activations , deltas , coef_grads , <EOL> intercept_grads ) <EOL> return loss , coef_grads , intercept_grads <EOL> def _initialize ( self , y , layer_units ) : <EOL> self . n_iter_ = <NUM_LIT:0> <EOL> self . t_ = <NUM_LIT:0> <EOL> self . n_outputs_ = y . shape [ <NUM_LIT:1> ] <EOL> self . n_layers_ = len ( layer_units ) <EOL> if not isinstance ( self , ClassifierMixin ) : <EOL> self . out_activation_ = '<STR_LIT>' <EOL> elif self . label_binarizer_ . y_type_ == '<STR_LIT>' : <EOL> self . out_activation_ = '<STR_LIT>' <EOL> else : <EOL> self . out_activation_ = '<STR_LIT>' <EOL> if self . loss == '<STR_LIT>' : <EOL> self . loss = '<STR_LIT>' <EOL> self . coefs_ = [ ] <EOL> self . intercepts_ = [ ] <EOL> for i in range ( self . n_layers_ - <NUM_LIT:1> ) : <EOL> rng = check_random_state ( self . random_state ) <EOL> coef_init , intercept_init = self . _init_coef ( layer_units [ i ] , <EOL> layer_units [ i + <NUM_LIT:1> ] , <EOL> rng ) <EOL> self . coefs_ . append ( coef_init ) <EOL> self . intercepts_ . append ( intercept_init ) <EOL> if self . algorithm in _STOCHASTIC_ALGOS : <EOL> self . loss_curve_ = [ ] <EOL> self . _no_improvement_count = <NUM_LIT:0> <EOL> if self . early_stopping : <EOL> self . validation_scores_ = [ ] <EOL> self . best_validation_score_ = - np . inf <EOL> else : <EOL> self . best_loss_ = np . inf <EOL> def _init_coef ( self , fan_in , fan_out , rng ) : <EOL> if self . activation == '<STR_LIT>' : <EOL> init_bound = np . sqrt ( <NUM_LIT> / ( fan_in + fan_out ) ) <EOL> elif self . activation == '<STR_LIT>' : <EOL> init_bound = np . sqrt ( <NUM_LIT> / ( fan_in + fan_out ) ) <EOL> elif self . activation == '<STR_LIT:relu>' : <EOL> init_bound = np . sqrt ( <NUM_LIT> / ( fan_in + fan_out ) ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> self . activation ) <EOL> coef_init = rng . uniform ( - init_bound , init_bound , ( fan_in , fan_out ) ) <EOL> intercept_init = rng . uniform ( - init_bound , init_bound , fan_out ) <EOL> return coef_init , intercept_init <EOL> def _fit ( self , X , y , incremental = False ) : <EOL> hidden_layer_sizes = self . hidden_layer_sizes <EOL> if not hasattr ( hidden_layer_sizes , "<STR_LIT>" ) : <EOL> hidden_layer_sizes = [ hidden_layer_sizes ] <EOL> hidden_layer_sizes = list ( hidden_layer_sizes ) <EOL> self . _validate_hyperparameters ( ) <EOL> if np . any ( np . array ( hidden_layer_sizes ) <= <NUM_LIT:0> ) : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> hidden_layer_sizes ) <EOL> X , y = self . _validate_input ( X , y , incremental ) <EOL> n_samples , n_features = X . shape <EOL> if y . ndim == <NUM_LIT:1> : <EOL> y = y . reshape ( ( - <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> self . n_outputs_ = y . shape [ <NUM_LIT:1> ] <EOL> layer_units = ( [ n_features ] + hidden_layer_sizes + <EOL> [ self . n_outputs_ ] ) <EOL> if not hasattr ( self , '<STR_LIT>' ) or ( not self . warm_start and not <EOL> incremental ) : <EOL> self . _initialize ( y , layer_units ) <EOL> if self . algorithm == '<STR_LIT>' : <EOL> batch_size = n_samples <EOL> elif self . batch_size == '<STR_LIT>' : <EOL> batch_size = min ( <NUM_LIT:200> , n_samples ) <EOL> else : <EOL> if self . batch_size < <NUM_LIT:1> or self . batch_size > n_samples : <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> batch_size = np . clip ( self . batch_size , <NUM_LIT:1> , n_samples ) <EOL> activations = [ X ] <EOL> activations . extend ( np . empty ( ( batch_size , n_fan_out ) ) <EOL> for n_fan_out in layer_units [ <NUM_LIT:1> : ] ) <EOL> deltas = [ np . empty_like ( a_layer ) for a_layer in activations ] <EOL> coef_grads = [ np . empty ( ( n_fan_in_ , n_fan_out_ ) ) for n_fan_in_ , <EOL> n_fan_out_ in zip ( layer_units [ : - <NUM_LIT:1> ] , <EOL> layer_units [ <NUM_LIT:1> : ] ) ] <EOL> intercept_grads = [ np . empty ( n_fan_out_ ) for n_fan_out_ in <EOL> layer_units [ <NUM_LIT:1> : ] ] <EOL> if self . algorithm in _STOCHASTIC_ALGOS : <EOL> self . _fit_stochastic ( X , y , activations , deltas , coef_grads , <EOL> intercept_grads , layer_units , incremental ) <EOL> elif self . algorithm == '<STR_LIT>' : <EOL> self . _fit_lbfgs ( X , y , activations , deltas , coef_grads , <EOL> intercept_grads , layer_units ) <EOL> return self <EOL> def _validate_hyperparameters ( self ) : <EOL> if not isinstance ( self . shuffle , bool ) : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> self . shuffle ) <EOL> if self . max_iter <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % self . max_iter ) <EOL> if self . alpha < <NUM_LIT:0.0> : <EOL> raise ValueError ( "<STR_LIT>" % self . alpha ) <EOL> if ( self . learning_rate in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] and <EOL> self . learning_rate_init <= <NUM_LIT:0.0> ) : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> self . learning_rate ) <EOL> if self . momentum > <NUM_LIT:1> or self . momentum < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> self . momentum ) <EOL> if not isinstance ( self . nesterovs_momentum , bool ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % self . nesterovs_momentum ) <EOL> if not isinstance ( self . early_stopping , bool ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % self . early_stopping ) <EOL> if self . validation_fraction < <NUM_LIT:0> or self . validation_fraction >= <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % self . validation_fraction ) <EOL> if self . beta_1 < <NUM_LIT:0> or self . beta_1 >= <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> self . beta_1 ) <EOL> if self . beta_2 < <NUM_LIT:0> or self . beta_2 >= <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> self . beta_2 ) <EOL> if self . epsilon <= <NUM_LIT:0.0> : <EOL> raise ValueError ( "<STR_LIT>" % self . epsilon ) <EOL> supported_activations = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:relu>' ] <EOL> if self . activation not in supported_activations : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( self . activation , <EOL> supported_activations ) ) <EOL> if self . learning_rate not in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> self . learning_rate ) <EOL> if self . algorithm not in _STOCHASTIC_ALGOS + [ "<STR_LIT>" ] : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> self . algorithm ) <EOL> def _fit_lbfgs ( self , X , y , activations , deltas , coef_grads , <EOL> intercept_grads , layer_units ) : <EOL> self . _coef_indptr = [ ] <EOL> self . _intercept_indptr = [ ] <EOL> start = <NUM_LIT:0> <EOL> for i in range ( self . n_layers_ - <NUM_LIT:1> ) : <EOL> n_fan_in , n_fan_out = layer_units [ i ] , layer_units [ i + <NUM_LIT:1> ] <EOL> end = start + ( n_fan_in * n_fan_out ) <EOL> self . _coef_indptr . append ( ( start , end , ( n_fan_in , n_fan_out ) ) ) <EOL> start = end <EOL> for i in range ( self . n_layers_ - <NUM_LIT:1> ) : <EOL> end = start + layer_units [ i + <NUM_LIT:1> ] <EOL> self . _intercept_indptr . append ( ( start , end ) ) <EOL> start = end <EOL> packed_coef_inter = _pack ( self . coefs_ , <EOL> self . intercepts_ ) <EOL> if self . verbose is True or self . verbose >= <NUM_LIT:1> : <EOL> iprint = <NUM_LIT:1> <EOL> else : <EOL> iprint = - <NUM_LIT:1> <EOL> optimal_parameters , self . loss_ , d = fmin_l_bfgs_b ( <EOL> x0 = packed_coef_inter , <EOL> func = self . _loss_grad_lbfgs , <EOL> maxfun = self . max_iter , <EOL> iprint = iprint , <EOL> pgtol = self . tol , <EOL> args = ( X , y , activations , deltas , coef_grads , intercept_grads ) ) <EOL> self . _unpack ( optimal_parameters ) <EOL> def _fit_stochastic ( self , X , y , activations , deltas , coef_grads , <EOL> intercept_grads , layer_units , incremental ) : <EOL> rng = check_random_state ( self . random_state ) <EOL> if not incremental or not hasattr ( self , '<STR_LIT>' ) : <EOL> params = self . coefs_ + self . intercepts_ <EOL> if self . algorithm == '<STR_LIT>' : <EOL> self . _optimizer = SGDOptimizer ( <EOL> params , self . learning_rate_init , self . learning_rate , <EOL> self . momentum , self . nesterovs_momentum , self . power_t ) <EOL> elif self . algorithm == '<STR_LIT>' : <EOL> self . _optimizer = AdamOptimizer ( <EOL> params , self . learning_rate_init , self . beta_1 , self . beta_2 , <EOL> self . epsilon ) <EOL> early_stopping = self . early_stopping and not incremental <EOL> if early_stopping : <EOL> X , X_val , y , y_val = train_test_split ( <EOL> X , y , random_state = self . random_state , <EOL> test_size = self . validation_fraction ) <EOL> if isinstance ( self , ClassifierMixin ) : <EOL> y_val = self . label_binarizer_ . inverse_transform ( y_val ) <EOL> else : <EOL> X_val = None <EOL> y_val = None <EOL> n_samples = X . shape [ <NUM_LIT:0> ] <EOL> if self . batch_size == '<STR_LIT>' : <EOL> batch_size = min ( <NUM_LIT:200> , n_samples ) <EOL> else : <EOL> batch_size = np . clip ( self . batch_size , <NUM_LIT:1> , n_samples ) <EOL> try : <EOL> for it in range ( self . max_iter ) : <EOL> X , y = shuffle ( X , y , random_state = rng ) <EOL> accumulated_loss = <NUM_LIT:0.0> <EOL> for batch_slice in gen_batches ( n_samples , batch_size ) : <EOL> activations [ <NUM_LIT:0> ] = X [ batch_slice ] <EOL> batch_loss , coef_grads , intercept_grads = self . _backprop ( <EOL> X [ batch_slice ] , y [ batch_slice ] , activations , deltas , <EOL> coef_grads , intercept_grads ) <EOL> accumulated_loss += batch_loss * ( batch_slice . stop - <EOL> batch_slice . start ) <EOL> grads = coef_grads + intercept_grads <EOL> self . _optimizer . update_params ( grads ) <EOL> self . n_iter_ += <NUM_LIT:1> <EOL> self . loss_ = accumulated_loss / X . shape [ <NUM_LIT:0> ] <EOL> self . t_ += n_samples <EOL> self . loss_curve_ . append ( self . loss_ ) <EOL> if self . verbose : <EOL> print ( "<STR_LIT>" % ( self . n_iter_ , <EOL> self . loss_ ) ) <EOL> self . _update_no_improvement_count ( early_stopping , X_val , y_val ) <EOL> self . _optimizer . iteration_ends ( self . t_ ) <EOL> if self . _no_improvement_count > <NUM_LIT:2> : <EOL> if early_stopping : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" % self . tol ) <EOL> else : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" % self . tol ) <EOL> is_stopping = self . _optimizer . trigger_stopping ( <EOL> msg , self . verbose ) <EOL> if is_stopping : <EOL> break <EOL> else : <EOL> self . _no_improvement_count = <NUM_LIT:0> <EOL> if incremental : <EOL> break <EOL> if self . n_iter_ == self . max_iter : <EOL> warnings . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( ) , ConvergenceWarning ) <EOL> except KeyboardInterrupt : <EOL> pass <EOL> if early_stopping : <EOL> self . coefs_ = self . _best_coefs <EOL> self . intercepts_ = self . _best_intercepts <EOL> def _update_no_improvement_count ( self , early_stopping , X_val , y_val ) : <EOL> if early_stopping : <EOL> self . validation_scores_ . append ( self . score ( X_val , y_val ) ) <EOL> if self . verbose : <EOL> print ( "<STR_LIT>" % self . validation_scores_ [ - <NUM_LIT:1> ] ) <EOL> last_valid_score = self . validation_scores_ [ - <NUM_LIT:1> ] <EOL> if last_valid_score < ( self . best_validation_score_ + <EOL> self . tol ) : <EOL> self . _no_improvement_count += <NUM_LIT:1> <EOL> else : <EOL> self . _no_improvement_count = <NUM_LIT:0> <EOL> if last_valid_score > self . best_validation_score_ : <EOL> self . best_validation_score_ = last_valid_score <EOL> self . _best_coefs = [ c . copy ( ) for c in self . coefs_ ] <EOL> self . _best_intercepts = [ i . copy ( ) <EOL> for i in self . intercepts_ ] <EOL> else : <EOL> if self . loss_curve_ [ - <NUM_LIT:1> ] > self . best_loss_ - self . tol : <EOL> self . _no_improvement_count += <NUM_LIT:1> <EOL> else : <EOL> self . _no_improvement_count = <NUM_LIT:0> <EOL> if self . loss_curve_ [ - <NUM_LIT:1> ] < self . best_loss_ : <EOL> self . best_loss_ = self . loss_curve_ [ - <NUM_LIT:1> ] <EOL> def fit ( self , X , y ) : <EOL> """<STR_LIT>""" <EOL> return self . _fit ( X , y , incremental = False ) <EOL> @ property <EOL> def partial_fit ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . algorithm not in _STOCHASTIC_ALGOS : <EOL> raise AttributeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self . algorithm ) <EOL> return self . _partial_fit <EOL> def _partial_fit ( self , X , y , classes = None ) : <EOL> return self . _fit ( X , y , incremental = True ) <EOL> def _decision_scores ( self , X ) : <EOL> """<STR_LIT>""" <EOL> X = check_array ( X , accept_sparse = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> hidden_layer_sizes = self . hidden_layer_sizes <EOL> if not hasattr ( hidden_layer_sizes , "<STR_LIT>" ) : <EOL> hidden_layer_sizes = [ hidden_layer_sizes ] <EOL> hidden_layer_sizes = list ( hidden_layer_sizes ) <EOL> layer_units = [ X . shape [ <NUM_LIT:1> ] ] + hidden_layer_sizes + [ self . n_outputs_ ] <EOL> activations = [ X ] <EOL> for i in range ( self . n_layers_ - <NUM_LIT:1> ) : <EOL> activations . append ( np . empty ( ( X . shape [ <NUM_LIT:0> ] , <EOL> layer_units [ i + <NUM_LIT:1> ] ) ) ) <EOL> self . _forward_pass ( activations , with_output_activation = False ) <EOL> y_pred = activations [ - <NUM_LIT:1> ] <EOL> return y_pred <EOL> class MLPClassifier ( BaseMultilayerPerceptron , ClassifierMixin ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , hidden_layer_sizes = ( <NUM_LIT:100> , ) , activation = "<STR_LIT:relu>" , <EOL> algorithm = '<STR_LIT>' , alpha = <NUM_LIT> , <EOL> batch_size = '<STR_LIT>' , learning_rate = "<STR_LIT>" , <EOL> learning_rate_init = <NUM_LIT> , power_t = <NUM_LIT:0.5> , max_iter = <NUM_LIT:200> , <EOL> shuffle = True , random_state = None , tol = <NUM_LIT> , <EOL> verbose = False , warm_start = False , momentum = <NUM_LIT> , <EOL> nesterovs_momentum = True , early_stopping = False , <EOL> validation_fraction = <NUM_LIT:0.1> , beta_1 = <NUM_LIT> , beta_2 = <NUM_LIT> , <EOL> epsilon = <NUM_LIT> ) : <EOL> sup = super ( MLPClassifier , self ) <EOL> sup . __init__ ( hidden_layer_sizes = hidden_layer_sizes , <EOL> activation = activation , algorithm = algorithm , alpha = alpha , <EOL> batch_size = batch_size , learning_rate = learning_rate , <EOL> learning_rate_init = learning_rate_init , power_t = power_t , <EOL> max_iter = max_iter , loss = '<STR_LIT>' , shuffle = shuffle , <EOL> random_state = random_state , tol = tol , verbose = verbose , <EOL> warm_start = warm_start , momentum = momentum , <EOL> nesterovs_momentum = nesterovs_momentum , <EOL> early_stopping = early_stopping , <EOL> validation_fraction = validation_fraction , <EOL> beta_1 = beta_1 , beta_2 = beta_2 , epsilon = epsilon ) <EOL> self . label_binarizer_ = LabelBinarizer ( ) <EOL> def _validate_input ( self , X , y , incremental ) : <EOL> X , y = check_X_y ( X , y , accept_sparse = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> multi_output = True ) <EOL> if y . ndim == <NUM_LIT:2> and y . shape [ <NUM_LIT:1> ] == <NUM_LIT:1> : <EOL> y = column_or_1d ( y , warn = True ) <EOL> self . label_binarizer_ . fit ( y ) <EOL> if not hasattr ( self , '<STR_LIT>' ) or not incremental : <EOL> self . classes_ = self . label_binarizer_ . classes_ <EOL> else : <EOL> classes = self . label_binarizer_ . classes_ <EOL> if not np . all ( np . in1d ( classes , self . classes_ ) ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( self . classes_ , classes ) ) <EOL> y = self . label_binarizer_ . transform ( y ) <EOL> return X , y <EOL> def decision_function ( self , X ) : <EOL> """<STR_LIT>""" <EOL> check_is_fitted ( self , "<STR_LIT>" ) <EOL> y_scores = self . _decision_scores ( X ) <EOL> if self . n_outputs_ == <NUM_LIT:1> : <EOL> return y_scores . ravel ( ) <EOL> else : <EOL> return y_scores <EOL> def predict ( self , X ) : <EOL> """<STR_LIT>""" <EOL> check_is_fitted ( self , "<STR_LIT>" ) <EOL> y_scores = self . decision_function ( X ) <EOL> y_scores = ACTIVATIONS [ self . out_activation_ ] ( y_scores ) <EOL> return self . label_binarizer_ . inverse_transform ( y_scores ) <EOL> @ property <EOL> def partial_fit ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . algorithm not in _STOCHASTIC_ALGOS : <EOL> raise AttributeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % self . algorithm ) <EOL> return self . _partial_fit <EOL> def _partial_fit ( self , X , y , classes = None ) : <EOL> _check_partial_fit_first_call ( self , classes ) <EOL> super ( MLPClassifier , self ) . _partial_fit ( X , y ) <EOL> return self <EOL> def predict_log_proba ( self , X ) : <EOL> """<STR_LIT>""" <EOL> y_prob = self . predict_proba ( X ) <EOL> return np . log ( y_prob , out = y_prob ) <EOL> def predict_proba ( self , X ) : <EOL> """<STR_LIT>""" <EOL> y_scores = self . decision_function ( X ) <EOL> if y_scores . ndim == <NUM_LIT:1> : <EOL> y_scores = logistic ( y_scores ) <EOL> return np . vstack ( [ <NUM_LIT:1> - y_scores , y_scores ] ) . T <EOL> else : <EOL> return softmax ( y_scores ) <EOL> class MLPRegressor ( BaseMultilayerPerceptron , RegressorMixin ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , hidden_layer_sizes = ( <NUM_LIT:100> , ) , activation = "<STR_LIT:relu>" , <EOL> algorithm = '<STR_LIT>' , alpha = <NUM_LIT> , <EOL> batch_size = '<STR_LIT>' , learning_rate = "<STR_LIT>" , <EOL> learning_rate_init = <NUM_LIT> , <EOL> power_t = <NUM_LIT:0.5> , max_iter = <NUM_LIT:200> , shuffle = True , <EOL> random_state = None , tol = <NUM_LIT> , <EOL> verbose = False , warm_start = False , momentum = <NUM_LIT> , <EOL> nesterovs_momentum = True , early_stopping = False , <EOL> validation_fraction = <NUM_LIT:0.1> , beta_1 = <NUM_LIT> , beta_2 = <NUM_LIT> , <EOL> epsilon = <NUM_LIT> ) : <EOL> sup = super ( MLPRegressor , self ) <EOL> sup . __init__ ( hidden_layer_sizes = hidden_layer_sizes , <EOL> activation = activation , algorithm = algorithm , alpha = alpha , <EOL> batch_size = batch_size , learning_rate = learning_rate , <EOL> learning_rate_init = learning_rate_init , power_t = power_t , <EOL> max_iter = max_iter , loss = '<STR_LIT>' , shuffle = shuffle , <EOL> random_state = random_state , tol = tol , verbose = verbose , <EOL> warm_start = warm_start , momentum = momentum , <EOL> nesterovs_momentum = nesterovs_momentum , <EOL> early_stopping = early_stopping , <EOL> validation_fraction = validation_fraction , <EOL> beta_1 = beta_1 , beta_2 = beta_2 , epsilon = epsilon ) <EOL> def predict ( self , X ) : <EOL> """<STR_LIT>""" <EOL> check_is_fitted ( self , "<STR_LIT>" ) <EOL> y_pred = self . _decision_scores ( X ) <EOL> if y_pred . shape [ <NUM_LIT:1> ] == <NUM_LIT:1> : <EOL> return y_pred . ravel ( ) <EOL> return y_pred <EOL> def _validate_input ( self , X , y , incremental ) : <EOL> X , y = check_X_y ( X , y , accept_sparse = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> multi_output = True , y_numeric = True ) <EOL> if y . ndim == <NUM_LIT:2> and y . shape [ <NUM_LIT:1> ] == <NUM_LIT:1> : <EOL> y = column_or_1d ( y , warn = True ) <EOL> return X , y </s>
