### Setup development environment Source: https://github.com/adriangb/scikeras/blob/master/docs/source/install.rst Commands to clone the repository, install dependencies via Poetry, and run the test suite. ```bash git clone https://github.com/adriangb/scikeras.git cd scikeras poetry install poetry shell pytest -v ``` -------------------------------- ### Install SciKeras with TensorFlow Backend Source: https://github.com/adriangb/scikeras/blob/master/README.md Use this command to install SciKeras along with the TensorFlow backend. This is equivalent to installing scikeras and tensorflow separately. ```bash pip install scikeras[tensorflow] ``` -------------------------------- ### Install memory_profiler Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/sparse.md Installs the memory_profiler package and loads the extension for memory usage analysis. ```python !pip install memory_profiler %load_ext memory_profiler ``` -------------------------------- ### Install dependencies and run tests Source: https://github.com/adriangb/scikeras/blob/master/CONTRIBUTING.md Use Poetry to install project dependencies and verify the environment by running the test suite. ```bash poetry install poetry run pytest tests/ ``` -------------------------------- ### Install TensorFlow backend Source: https://github.com/adriangb/scikeras/blob/master/docs/source/install.rst Command to install the TensorFlow backend required for Keras. ```bash pip install tensorflow ``` -------------------------------- ### Launch Jupyter Notebook Source: https://github.com/adriangb/scikeras/blob/master/docs/README.md Starts the Jupyter notebook server to edit markdown notebooks. ```bash poetry run jupyter notebook ``` -------------------------------- ### Install SciKeras Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/Benchmarks.md Installs scikeras with TensorFlow support if not already present. ```python try: import scikeras except ImportError: !python -m pip install scikeras[tensorflow] ``` -------------------------------- ### Install SciKeras via pip Source: https://github.com/adriangb/scikeras/blob/master/docs/source/install.rst Standard installation command for SciKeras and its Keras dependency. ```bash pip install scikeras ``` -------------------------------- ### Install SciKeras if needed Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/sparse.md Checks if SciKeras is installed and installs it using pip if it's not found. ```python try: import scikeras except ImportError: !python -m pip install scikeras ``` -------------------------------- ### Install SciKeras without dependencies Source: https://github.com/adriangb/scikeras/blob/master/docs/source/install.rst Installation command that skips automatic dependency resolution, useful for custom environments. ```bash pip install --no-deps scikeras ``` -------------------------------- ### Example of Parameter Routing for fit and predict Source: https://github.com/adriangb/scikeras/blob/master/docs/source/advanced.rst Demonstrates how to route parameters to the `fit` and `predict` methods of a Keras model using SciKeras. This allows for different configurations during training and prediction. ```python clf = KerasClassifier(..., fit__batch_size=32, predict__batch_size=10000) ``` -------------------------------- ### Manage pre-commit hooks Source: https://github.com/adriangb/scikeras/blob/master/CONTRIBUTING.md Commands to install or manually trigger pre-commit hooks for automated code style checking. ```bash poetry run pre-commit install ``` ```bash poetry run pre-commit run --all-files ``` -------------------------------- ### Dynamic Model Building with Meta Parameter Source: https://context7.com/adriangb/scikeras/llms.txt The model building function can accept a `meta` dictionary containing input data characteristics, enabling dynamic model construction. This example shows how to configure the output layer based on task type (binary/multiclass). Requires Keras and scikeras.wrappers. ```python import keras from scikeras.wrappers import KerasClassifier def get_model(meta, compile_kwargs): """Build model dynamically based on input metadata.""" n_features_in_ = meta["n_features_in_"] n_classes_ = meta["n_classes_"] target_type_ = meta["target_type_"] model = keras.Sequential() model.add(keras.layers.Input(shape=(n_features_in_,))) model.add(keras.layers.Dense(100, activation="relu")) # Configure output layer based on task type if target_type_ == "binary": model.add(keras.layers.Dense(1, activation="sigmoid")) loss = "binary_crossentropy" elif target_type_ == "multiclass": model.add(keras.layers.Dense(n_classes_, activation="softmax")) loss = "sparse_categorical_crossentropy" else: raise NotImplementedError(f"Unsupported task: {target_type_}") model.compile(loss=loss, optimizer=compile_kwargs["optimizer"]) return model clf = KerasClassifier( model=get_model, optimizer="adam", optimizer__learning_rate=0.001, verbose=0, ) ``` -------------------------------- ### Return Uncompiled Model Source: https://github.com/adriangb/scikeras/blob/master/docs/source/advanced.rst Example of returning a model from a build function, allowing SciKeras to handle compilation. ```python return model # That's it, SciKeras will compile your model for you ``` -------------------------------- ### Subclass Wrappers for Custom Transformation Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/DataTransformers.md Example of overriding encoder functions to implement custom transformation routines. ```python if False: # avoid executing pseudocode class MultiOutputTransformer(BaseEstimator, TransformerMixin): ... class MultiOutputClassifier(KerasClassifier): @property def target_encoder(self): return MultiOutputTransformer(...) ``` -------------------------------- ### Build Documentation Locally Source: https://github.com/adriangb/scikeras/blob/master/docs/README.md Commands to execute notebooks and build the HTML documentation. Adjust the parallel processing flags based on available CPU cores. ```bash export TF_CPP_MIN_LOG_LEVEL=3 find docs/source/notebooks -maxdepth 1 -type f -name '*.md' -print0 | xargs -0 -n 1 -P 8 poetry run jupytext --set-formats ipynb,md --execute poetry run sphinx-build -b html -j 8 docs/source docs/_build ``` ```bash export TF_CPP_MIN_LOG_LEVEL=3 poetry run jupytext --set-formats ipynb,md --execute docs/source/notebooks/*.md poetry run sphinx-build -b html docs/source docs/_build ``` -------------------------------- ### Clone the repository Source: https://github.com/adriangb/scikeras/blob/master/CONTRIBUTING.md Initial steps to download the source code and navigate to the project directory. ```bash git clone git@github.com:adriangb/scikeras.git cd scikeras ``` -------------------------------- ### Get Prediction Probabilities with KerasClassifier Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/Basic_Usage.md Use the predict_proba method to get class probabilities from a trained KerasClassifier. ```python y_proba = clf.predict_proba(X[:5]) y_proba ``` -------------------------------- ### Test Multi-Output Model with Mock Data Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/DataTransformers.md Demonstrates how to prepare mock input data and metadata to fit and predict using the multi-output model. ```python X = np.random.random(size=(100, 10)) y_bin = np.random.randint(0, 2, size=(100,)) y_cat = np.random.randint(0, 5, size=(100, )) y = [y_bin, y_cat] # build mock meta meta = { "n_features_in_": 10, "n_classes_": [2, 5] # note that we made this a list, one for each output } model = get_clf_model(meta=meta) model.fit(X, y, verbose=0) y_pred = model.predict(X) ``` -------------------------------- ### Prepare Test Data for Callback Demonstration Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/Basic_Usage.md Generates a synthetic dataset using make_moons for testing model training behavior. ```python from sklearn.datasets import make_moons X, y = make_moons(n_samples=100, noise=0.2, random_state=0) ``` -------------------------------- ### Configure Metrics Source: https://github.com/adriangb/scikeras/blob/master/docs/source/advanced.rst Apply metrics to model outputs using various mapping strategies. ```python from keras.metrics import BinaryAccuracy, AUC clf = KerasClassifier( ..., metrics=BinaryAccuracy, metrics__threshold=0.65, ) # or to apply multiple metrics to all outputs clf = KerasClassifier( ..., metrics=[BinaryAccuracy, AUC], # both applied to all outputs metrics__0__threshold=0.65, ) # or for different metrics to each output clf = KerasClassifier( ..., metrics={"out1": BinaryAccuracy, "out2": [BinaryAccuracy, AUC]}, metrics__out1__threshold=0.65, metrics__out2__0__threshold=0.65, ) # assuming you ordered your outputs like [out1, out2], this is equivalent to clf = KerasClassifier( ..., metrics=[BinaryAccuracy, [AUC, BinaryAccuracy]], metrics__0__threshold=0.65, metrics__1__0__threshold=0.65, ) ``` -------------------------------- ### Make Predictions with KerasClassifier Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/Basic_Usage.md Use the predict method to get class predictions from a trained KerasClassifier. ```python y_pred = clf.predict(X[:5]) y_pred ``` -------------------------------- ### Continue Training with warm_start Source: https://context7.com/adriangb/scikeras/llms.txt Enable warm_start=True to allow subsequent calls to fit to resume training from the previous model state. ```python import numpy as np import keras from scikeras.wrappers import KerasClassifier def get_model(meta): model = keras.models.Sequential([ keras.layers.Input(shape=(meta["n_features_in_"],)), keras.layers.Dense(50, activation="relu"), keras.layers.Dense(1, activation="sigmoid") ]) return model clf = KerasClassifier( model=get_model, loss="binary_crossentropy", warm_start=True, epochs=5, verbose=0, ) X = np.random.random((100, 10)) y = np.random.randint(0, 2, 100) # First fit: trains for 5 epochs clf.fit(X, y) print(f"After first fit: {len(clf.history_['loss'])} epochs") # Second fit with warm_start: continues training for 5 more epochs clf.fit(X, y) print(f"After second fit: {len(clf.history_['loss'])} epochs") # Access training history print(f"Loss history: {clf.history_['loss'][-3:]}") ``` -------------------------------- ### Make Predictions with KerasRegressor Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/Basic_Usage.md Use the predict method to get regression predictions from a trained KerasRegressor. predict_proba returns the same values for regression. ```python y_pred = reg.predict(X_regr[:5]) y_pred ``` -------------------------------- ### Perform Grid Search with KerasClassifier Source: https://github.com/adriangb/scikeras/blob/master/docs/source/quickstart.rst Tune hyperparameters of a KerasClassifier using scikit-learn's GridSearchCV. This example shows how to specify parameters for both the model and its optimizer. ```python from sklearn.model_selection import GridSearchCV params = { "hidden_layer_dim": [50, 100, 200], "loss": ["sparse_categorical_crossentropy"], "optimizer": ["adam", "sgd"], "optimizer__learning_rate": [0.0001, 0.001, 0.1], } gs = GridSearchCV(clf, params, refit=False, cv=3, scoring='accuracy') gs.fit(X, y) print(gs.best_score_, gs.best_params_) ``` -------------------------------- ### Configure fit and predict parameters Source: https://github.com/adriangb/scikeras/blob/master/docs/source/migration.rst Use constructor arguments or set_params to define fit and predict parameters instead of passing them directly to fit(). ```diff - clf = KerasClassifier(...) - clf.fit(..., batch_size=32) + clf = KerasClassifier(..., batch_size=32) + clf.fit(...) ``` -------------------------------- ### Set parameters via constructor or set_params Source: https://github.com/adriangb/scikeras/blob/master/docs/source/migration.rst Use double underscore prefixes to specify parameters for fit or predict methods. ```python clf = KerasClassifier(..., fit__batch_size=32, predict__batch_size=10000) ``` ```python clf = KerasClassifier(...) clf.set_params(fit__batch_size=32, predict__batch_size=10000) clf.fit(...) ``` -------------------------------- ### Instantiate and Use MLPRegressor Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/MLPClassifier_MLPRegressor.md Demonstrates how to instantiate MLPRegressor with custom epochs and use it for a regression task. It includes generating sample regression data, fitting the model, and printing the R-squared score. ```python from sklearn.datasets import make_regression reg = MLPRegressor(epochs=20) # for notebook execution time # Define a simple linear relationship y = np.arange(100) X = (y/2).reshape(-1, 1) # check score reg.fit(X, y) print(reg.score(X, y)) ``` -------------------------------- ### Routing Parameters to Multiple Losses Source: https://github.com/adriangb/scikeras/blob/master/docs/source/advanced.rst Demonstrates how SciKeras supports routing parameters to individual losses when a list or dictionary of losses is provided. This allows fine-grained control and tuning for each output's loss. ```python from keras.losses import BinaryCrossentropy, CategoricalCrossentropy clf = KerasClassifier( ..., ``` -------------------------------- ### Load Keras Model from Disk and Re-instantiate Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/Basic_Usage.md Load a Keras model from disk using keras.saving.load_model and then instantiate a new SciKeras wrapper around it. Use initialize to set up the SciKeras object without re-fitting. ```python # Load the model back into memory new_reg_model = keras.saving.load_model("/tmp/my_model.keras") # Now we need to instantiate a new SciKeras object # since we only saved the Keras model reg_new = KerasRegressor(new_reg_model) # use initialize to avoid re-fitting reg_new.initialize(X_regr, y_regr) pred_new = reg_new.predict(X_regr) np.testing.assert_allclose(pred_old, pred_new) ``` -------------------------------- ### Configure Output Layers Dynamically Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/MLPClassifier_MLPRegressor.md Uses meta information to determine the number of output units and activation function for binary or multiclass classification. ```python def get_clf_model(hidden_layer_sizes: Iterable[int], meta: Dict[str, Any]): model = keras.Sequential() inp = keras.layers.Input(shape=(meta["n_features_in_"],)) model.add(inp) for hidden_layer_size in hidden_layer_sizes: layer = keras.layers.Dense(hidden_layer_size, activation="relu") model.add(layer) if meta["target_type_"] == "binary": n_output_units = 1 output_activation = "sigmoid" elif meta["target_type_"] == "multiclass": n_output_units = meta["n_classes_"] output_activation = "softmax" else: raise NotImplementedError(f"Unsupported task type: {meta['target_type_']}") out = keras.layers.Dense(n_output_units, activation=output_activation) model.add(out) return model ``` -------------------------------- ### Configure Loss Parameters Source: https://github.com/adriangb/scikeras/blob/master/docs/source/advanced.rst Use parameter routing to tune loss function parameters for single or multiple outputs. ```python loss=[BinaryCrossentropy, CategoricalCrossentropy], loss__from_logits=True, # BinaryCrossentropy(from_logits=True) & CategoricalCrossentropy(from_logits=True) loss__1__label_smoothing=0.5, # overrides the above, results in CategoricalCrossentropy(label_smoothing=0.5) ) # or clf = KerasClassifier( ..., loss={"out1": BinaryCrossentropy, "out2": CategoricalCrossentropy}, loss__from_logits=True, # BinaryCrossentropy(from_logits=True) & CategoricalCrossentropy(from_logits=True) loss__out2__label_smoothing=0.5, # overrides the above, results in CategoricalCrossentropy(label_smoothing=0.5) ) ``` -------------------------------- ### Route Callbacks by Method Source: https://github.com/adriangb/scikeras/blob/master/docs/source/advanced.rst Use fit__ or predict__ prefixes to route callbacks to specific methods. ```python clf = KerasClassifier( ..., callbacks=keras.callbacks.Callback, # called from both fit and predict fit__callbacks=keras.callbacks.Callback, # called only from fit predict__callbacks=keras.callbacks.Callback, # called only from predict ) ``` -------------------------------- ### Configure Keras Callbacks with Routed Parameters Source: https://context7.com/adriangb/scikeras/llms.txt Integrate and configure Keras callbacks, such as EarlyStopping, with routed parameters for fine-tuning training behavior. Specify validation split and epochs for callback effectiveness. ```python import keras from scikeras.wrappers import KerasClassifier def get_model(meta): model = keras.models.Sequential([ keras.layers.Input(shape=(meta["n_features_in_"],)), keras.layers.Dense(100, activation="relu"), keras.layers.Dense(1, activation="sigmoid") ]) return model # Single callback with routed parameters clf = KerasClassifier( model=get_model, loss="binary_crossentropy", callbacks=keras.callbacks.EarlyStopping, callbacks__monitor="val_loss", callbacks__patience=3, callbacks__restore_best_weights=True, validation_split=0.2, epochs=100, verbose=0, ) ``` -------------------------------- ### Configure Callbacks Source: https://github.com/adriangb/scikeras/blob/master/docs/source/advanced.rst Route parameters to Keras callbacks using different syntax options. ```python clf = KerasClassifier( ..., callbacks=keras.callbacks.EarlyStopping callbacks__monitor="loss", ) ``` ```python # for multiple callbacks using dict syntax clf = KerasClassifier( ..., callbacks={"bl": keras.callbacks.BaseLogger, "es": keras.callbacks.EarlyStopping} callbacks__es__monitor="loss", ) # or using list sytnax clf = KerasClassifier( ..., callbacks=[keras.callbacks.BaseLogger, keras.callbacks.EarlyStopping] callbacks__1__monitor="loss", # EarlyStopping(monitor="loss") ) ``` -------------------------------- ### Optimizer Routing with Learning Rate Source: https://github.com/adriangb/scikeras/blob/master/docs/source/advanced.rst Shows how to route parameters to an optimizer, specifically setting the learning rate for SGD. This enables hyperparameter tuning of the learning rate via Scikit-Learn's search utilities. ```python from scikeras.wrappers import KerasClassifier import keras clf = KerasClassifier( model=model_build_fn, optimizer=keras.optimizers.SGD, optimizer__learning_rate=0.05 ) ``` ```python clf = KerasClassifier( # equivalent model model=model_build_fn, optimizer=keras.optimizers.SGD(learning_rate=0.5) ) ``` -------------------------------- ### Import Benchmark Utilities Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/Benchmarks.md Imports accuracy_score from scikit-learn and tensorflow_random_state for reproducible results. ```python from sklearn.metrics import accuracy_score from scikeras.utils.random_state import tensorflow_random_state ``` -------------------------------- ### Loss Routing with Label Smoothing Source: https://github.com/adriangb/scikeras/blob/master/docs/source/advanced.rst Illustrates routing parameters to a loss function, such as setting `label_smoothing` for `BinaryCrossentropy`. Only routed parameters are tunable; direct object instantiation does not support tuning. ```python from keras.losses import BinaryCrossentropy, CategoricalCrossentropy clf = KerasClassifier( ..., loss=BinaryCrossentropy, loss__label_smoothing=0.1, # results in BinaryCrossentropy(label_smoothing=0.1) ) ``` ```python # pure Keras object only syntax is still supported, but parameters can't be tuned clf = KerasClassifier(..., loss=BinaryCrossentropy(label_smoothing=0.1)) ``` -------------------------------- ### Import necessary libraries Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/sparse.md Imports core libraries for data manipulation, preprocessing, pipeline creation, and deep learning with Keras. ```python import scipy import numpy as np from scikeras.wrappers import KerasRegressor from sklearn.preprocessing import OneHotEncoder from sklearn.pipeline import Pipeline import keras ``` -------------------------------- ### Instantiate and Train Deep AutoEncoder Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/AutoEncoders.md Initializes a `DeepAutoEncoder` instance with specified parameters and trains it on the provided training data `X`. Ensure `x_train` is defined. ```python deep = DeepAutoEncoder( loss="binary_crossentropy", encoding_dim=32, hidden_layer_sizes=[128], random_state=0, epochs=5, verbose=False, optimizer="adam", ) _ = deep.fit(X=x_train) ``` -------------------------------- ### Import SciKeras and Keras Utilities Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/Benchmarks.md Imports necessary libraries including KerasClassifier, KerasRegressor, and Keras. ```python import numpy as np from scikeras.wrappers import KerasClassifier, KerasRegressor import keras ``` -------------------------------- ### Configure Fit and Predict Batch Sizes Source: https://context7.com/adriangb/scikeras/llms.txt Set distinct batch sizes for the `fit` and `predict` methods using SciKeras's parameter routing. This can be useful for memory management or performance tuning. ```python import keras from scikeras.wrappers import KerasClassifier def get_model(meta): model = keras.models.Sequential([ keras.layers.Input(shape=(meta["n_features_in_"],)), keras.layers.Dense(100, activation="relu"), keras.layers.Dense(1, activation="sigmoid") ]) return model # Separate batch sizes for fit and predict clf = KerasClassifier( model=get_model, loss="binary_crossentropy", fit__batch_size=32, predict__batch_size=1000, verbose=0, ) ``` -------------------------------- ### Load and Preprocess MNIST Dataset Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/Benchmarks.md Loads the MNIST dataset, scales pixel values to [0, 1], and reshapes images. Reduces dataset size for faster benchmarks. ```python (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() # Scale images to the [0, 1] range x_train = x_train.astype("float32") / 255 x_test = x_test.astype("float32") / 255 # Make sure images have shape (28, 28, 1) x_train = np.expand_dims(x_train, -1) x_test = np.expand_dims(x_test, -1) # Reduce dataset size for faster benchmarks x_train, y_train = x_train[:2000], y_train[:2000] x_test, y_test = x_test[:500], y_test[:500] ``` -------------------------------- ### Execute Training with and without Callbacks Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/Basic_Usage.md Demonstrates fitting a KerasClassifier with and without the custom MaxValLoss callback. ```python kwargs = dict( model=get_clf, loss="binary_crossentropy", dropout=0.5, hidden_layer_sizes=(100,), metrics=["binary_accuracy"], fit__validation_split=0.2, epochs=20, verbose=False, random_state=0 ) # First test without the callback clf = KerasClassifier(**kwargs) clf.fit(X, y) print(f"Trained {len(clf.history_['loss'])} epochs") print(f"Final accuracy: {clf.history_['val_binary_accuracy'][-1]}") # get last value of last fit/partial_fit call ``` ```python # Test with the callback cb = MaxValLoss(monitor="val_binary_accuracy", threashold=0.75) clf = KerasClassifier( **kwargs, callbacks=[cb] ) clf.fit(X, y) print(f"Trained {len(clf.history_['loss'])} epochs") print(f"Final accuracy: {clf.history_['val_binary_accuracy'][-1]}") # get last value of last fit/partial_fit call ``` -------------------------------- ### Route Parameters to Optimizer Source: https://context7.com/adriangb/scikeras/llms.txt Configure Keras optimizer parameters directly through SciKeras wrapper. Ensure the optimizer is imported or specified as a string. ```python import keras from scikeras.wrappers import KerasClassifier def get_model(meta): model = keras.models.Sequential([ keras.layers.Input(shape=(meta["n_features_in_"],)), keras.layers.Dense(100, activation="relu"), keras.layers.Dense(1, activation="sigmoid") ]) return model # Route parameters to optimizer clf = KerasClassifier( model=get_model, loss="binary_crossentropy", optimizer=keras.optimizers.Adam, optimizer__learning_rate=0.001, optimizer__beta_1=0.9, verbose=0, ) ``` -------------------------------- ### SciKeras Utilities API Source: https://github.com/adriangb/scikeras/blob/master/docs/source/api.rst Documentation for utility modules within SciKeras, including transformers. ```APIDOC ## SciKeras Utilities API ### Description This section covers the utility modules provided by SciKeras, which offer helpful functions and tools for working with Keras models. ### Modules #### scikeras.utils **Description:** General utility functions. #### scikeras.utils.transformers **Description:** Utility functions related to data transformers. ``` -------------------------------- ### Use get_metadata for Model Parameters Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/DataTransformers.md Demonstrates passing metadata from a transformer to the model building function via the meta keyword argument. ```python if False: # avoid executing pseudocode class MultiOutputTransformer(BaseEstimator, TransformerMixin): def get_metadata(self): return {"my_param_": "foobarbaz"} class MultiOutputClassifier(KerasClassifier): @property def target_encoder(self): return MultiOutputTransformer(...) def get_model(meta): print(f"Got: {meta['my_param_']}") clf = MultiOutputClassifier(model=get_model) clf.fit(X, y) # Got: foobarbaz print(clf.my_param_) # foobarbaz ``` -------------------------------- ### Configure KerasClassifier for Grid Search Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/Basic_Usage.md Initializes a KerasClassifier with routed parameters for hyperparameter tuning. ```python from sklearn.model_selection import GridSearchCV clf = KerasClassifier( model=get_clf, loss="binary_crossentropy", optimizer="adam", optimizer__learning_rate=0.1, model__hidden_layer_sizes=(100,), model__dropout=0.5, verbose=False, ) ``` -------------------------------- ### Instantiate AutoEncoder Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/AutoEncoders.md Initializes the AutoEncoder estimator with specific hyperparameters for training. ```python autoencoder = AutoEncoder( loss="binary_crossentropy", encoding_dim=32, random_state=0, epochs=5, verbose=False, optimizer="adam", ) ``` -------------------------------- ### Keras Benchmark Training Arguments Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/Benchmarks.md Sets keyword arguments for model training, including batch size, validation split, verbosity, and number of epochs. ```python fit_kwargs = {"batch_size": 128, "validation_split": 0.1, "verbose": 0, "epochs": 5} ``` -------------------------------- ### Initialize and Fit KerasClassifier Source: https://github.com/adriangb/scikeras/blob/master/docs/source/advanced.rst Basic usage of KerasClassifier to compile and train a model. ```python clf = KerasClassifier(model=model_build_fn, optimizer=Adam) clf.fit(X, y) y_pred = clf.predict(X_valid) ``` -------------------------------- ### Implement Data Transformer Interface Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/DataTransformers.md Pseudocode demonstrating the internal structure of BaseWrapper, KerasClassifier, and KerasRegressor for data transformation. ```python if False: # avoid executing pseudocode from scikeras.utils.transformers import ( ClassifierLabelEncoder, RegressorTargetEncoder, ) class BaseWrapper: def fit(self, X, y): self.target_encoder_ = self.target_encoder self.feature_encoder_ = self.feature_encoder y = self.target_encoder_.fit_transform(y) X = self.feature_encoder_.fit_transform(X) self.model_.fit(X, y) return self def predict(self, X): X = self.feature_encoder_.transform(X) y_pred = self.model_.predict(X) return self.target_encoder_.inverse_transform(y_pred) class KerasClassifier(BaseWrapper): @property def target_encoder(self): return ClassifierLabelEncoder(loss=self.loss) def predict_proba(self, X): X = self.feature_encoder_.transform(X) y_pred = self.model_.predict(X) return self.target_encoder_.inverse_transform(y_pred, return_proba=True) class KerasRegressor(BaseWrapper): @property def target_encoder(self): return RegressorTargetEncoder() ``` -------------------------------- ### Initialize KerasClassifier Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/Meta_Estimators.md Wraps the Keras model factory into a scikit-learn compatible classifier. ```python clf = KerasClassifier( model=get_clf_model, hidden_layer_sizes=(100, ), optimizer="adam", optimizer__learning_rate=0.001, verbose=0, random_state=0, ) ``` -------------------------------- ### Load and Preprocess MNIST Dataset Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/DataTransformers.md Loads the MNIST dataset and flattens/scales images for use with sklearn utilities. ```python (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() x_train.shape ``` ```python print(y_train.shape) print(np.unique(y_train)) ``` ```python from sklearn.preprocessing import MinMaxScaler n_samples_train = x_train.shape[0] n_samples_test = x_test.shape[0] x_train = x_train.reshape((n_samples_train, -1)) x_test = x_test.reshape((n_samples_test, -1)) x_train = MinMaxScaler().fit_transform(x_train) x_test = MinMaxScaler().fit_transform(x_test) ``` ```python print(x_train.shape[1:]) # 784 = 28*28 ``` ```python print(np.min(x_train), np.max(x_train)) # scaled 0-1 ``` -------------------------------- ### Configure loss function Source: https://github.com/adriangb/scikeras/blob/master/docs/source/migration.rst Explicitly define the loss function in the constructor to handle target encoding. ```python clf = KerasClassifier(loss="categorical_crossentropy") ``` -------------------------------- ### Route Positional Arguments Source: https://github.com/adriangb/scikeras/blob/master/docs/source/advanced.rst Use __0 syntax to pass parameters as positional arguments instead of keyword arguments. ```python import keras class Schedule: """Exponential decay lr scheduler. """ def __init__(self, wait_until_epoch: int = 10, coef: float = 0.1) -> None: self.wait_until_epoch = wait_until_epoch self.coef = coef def __call__(self, epoch: int, lr: float): if epoch < self.wait_until_epoch: return lr return lr * tf.math.exp(-self.coef) clf = KerasClassifier( ..., callbacks=keras.callbacks.LearningRateScheduler, callbacks__0=Schedule, # __0 indicates this should be passed as an arg; LearningRateScheduler does not accept kwargs callbacks__0__coef=0.2, ) ``` -------------------------------- ### Route Parameters to Metrics Source: https://context7.com/adriangb/scikeras/llms.txt Configure Keras metrics and their parameters via SciKeras routing. This allows for dynamic adjustment of metric thresholds or other metric-specific settings. ```python import keras from scikeras.wrappers import KerasClassifier def get_model(meta): model = keras.models.Sequential([ keras.layers.Input(shape=(meta["n_features_in_"],)), keras.layers.Dense(100, activation="relu"), keras.layers.Dense(1, activation="sigmoid") ]) return model # Route parameters to metrics clf = KerasClassifier( model=get_model, loss="binary_crossentropy", metrics=[keras.metrics.BinaryAccuracy, keras.metrics.AUC], metrics__0__threshold=0.65, # BinaryAccuracy threshold optimizer="adam", verbose=0, ) ``` -------------------------------- ### Calculate Compression Ratio of Autoencoder Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/AutoEncoders.md Calculates and prints the size of the original test data and the encoded representation in megabytes, along with the compression ratio. Assumes `autoencoder` and `x_test` are available. ```python encoded_imgs = autoencoder.transform(x_test) print(f"x_test size (in MB): {x_test.nbytes/1024**2:.2f}") print(f"encoded_imgs size (in MB): {encoded_imgs.nbytes/1024**2:.2f}") cr = round((encoded_imgs.nbytes/x_test.nbytes), 2) print(f"Compression ratio: 1/{1/cr:.0f}") ``` -------------------------------- ### Create dense and sparse pipelines Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/sparse.md Constructs two Scikit-Learn pipelines using OneHotEncoder and KerasRegressor. One forces dense output, the other uses sparse output. ```python dense_pipeline = Pipeline( [ ("encoder", OneHotEncoder(sparse_output=False)), ("model", KerasRegressor(get_clf, loss="mse", epochs=5, verbose=False)) ] ) sparse_pipeline = Pipeline( [ ("encoder", OneHotEncoder(sparse_output=True)), ("model", KerasRegressor(get_clf, loss="mse", epochs=5, verbose=False)) ] ) ``` -------------------------------- ### Benchmark runtime for sparse pipeline Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/sparse.md Measures the average execution time for training the sparse pipeline. ```python %timeit sparse_pipeline.fit(X, y) ``` -------------------------------- ### Import Scikit-Learn Base Classes Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/DataTransformers.md Required imports for creating custom transformers. ```python from sklearn.base import BaseEstimator, TransformerMixin ``` -------------------------------- ### Visualize Image Reconstruction with Matplotlib Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/AutoEncoders.md Displays original and lossy decoded images side-by-side for comparison. Ensure `x_test` and `roundtrip_imgs` are defined and populated. ```python import matplotlib.pyplot as plt n = 10 # How many digits we will display plt.figure(figsize=(20, 4)) for i in range(n): # Display original ax = plt.subplot(2, n, i + 1) plt.imshow(x_test[i].reshape(28, 28)) plt.gray() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) # Display reconstruction ax = plt.subplot(2, n, i + 1 + n) plt.imshow(roundtrip_imgs[i].reshape(28, 28)) plt.gray() ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.show() ``` -------------------------------- ### Initialize SciKeras Classifier Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/Benchmarks.md Initializes a KerasClassifier with the model getter function, a fixed random state for reproducibility, and training arguments. ```python clf = KerasClassifier( model=get_model, random_state=0, **fit_kwargs ) ``` -------------------------------- ### Test KerasClassifier with Sample Data Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/MLPClassifier_MLPRegressor.md Generate sample classification data and test the fitted KerasClassifier by printing its score. This verifies the basic functionality of the wrapped model. ```python from sklearn.datasets import make_classification X, y = make_classification() # check that fit works clf.fit(X, y) # check score print(clf.score(X, y)) ``` -------------------------------- ### Configure Multiple Callbacks with Dict Syntax Source: https://context7.com/adriangb/scikeras/llms.txt Use a dictionary to pass multiple callbacks to KerasClassifier, with parameters mapped via the double-underscore syntax. ```python clf = KerasClassifier( model=get_model, loss="binary_crossentropy", callbacks={ "es": keras.callbacks.EarlyStopping, "lr": keras.callbacks.ReduceLROnPlateau, }, callbacks__es__monitor="val_loss", callbacks__es__patience=5, callbacks__lr__monitor="val_loss", callbacks__lr__factor=0.5, validation_split=0.2, epochs=100, verbose=0, ) ``` -------------------------------- ### Wrap Keras Model with KerasClassifier Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/MLPClassifier_MLPRegressor.md Instantiate KerasClassifier by passing a model-building function and configuring parameters like optimizer and learning rate. User-defined parameters must be keyword arguments with default values. ```python clf = KerasClassifier( model=get_clf_model, hidden_layer_sizes=(100, ), optimizer="adam", optimizer__learning_rate=0.001, epochs=50, verbose=0, ) ``` -------------------------------- ### Load and preprocess MNIST data Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/AutoEncoders.md Loads the MNIST dataset, normalizes pixel values, and flattens the images for autoencoder input. ```python from keras.datasets import mnist import numpy as np (x_train, _), (x_test, _) = mnist.load_data() x_train = x_train.astype('float32') / 255. x_test = x_test.astype('float32') / 255. x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:]))) x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:]))) print(x_train.shape) print(x_test.shape) ``` -------------------------------- ### Configure TensorFlow Logging Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/sparse.md Sets up TensorFlow logging to suppress verbose output and filters specific warnings related to random state. ```python import warnings os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' from tensorflow import get_logger get_logger().setLevel('ERROR') warnings.filterwarnings("ignore", message="Setting the random state for TF") ``` -------------------------------- ### Generate Regression Data Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/Basic_Usage.md Create a synthetic dataset for a regression task using make_regression from scikit-learn. ```python from sklearn.datasets import make_regression X_regr, y_regr = make_regression(1000, 20, n_informative=10, random_state=0) X_regr.shape, y_regr.shape, y_regr.min(), y_regr.max() ``` -------------------------------- ### Generate Toy Binary Classification Data Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/Basic_Usage.md Creates a synthetic dataset for binary classification using scikit-learn's make_classification function. This is useful for testing classification models. ```python import numpy as np from sklearn.datasets import make_classification X, y = make_classification(1000, 20, n_informative=10, random_state=0) X.shape, y.shape, y.mean() ``` -------------------------------- ### Save and Load Model using Pickle Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/Basic_Usage.md Serialize and deserialize the entire SciKeras regressor object using Python's pickle module. The loaded model retains its trained state. ```python import pickle bytes_model = pickle.dumps(reg) new_reg = pickle.loads(bytes_model) new_reg.predict(X_regr[:5]) # model is still trained ``` -------------------------------- ### SciKeras Wrappers API Source: https://github.com/adriangb/scikeras/blob/master/docs/source/api.rst Documentation for the wrapper classes provided by SciKeras, used to integrate Keras models with scikit-learn. ```APIDOC ## SciKeras Wrappers API ### Description This section details the wrapper classes available in SciKeras, which are designed to make Keras models compatible with the scikit-learn API. ### Classes #### scikeras.wrappers.BaseWrapper **Description:** Base class for Keras wrappers. #### scikeras.wrappers.KerasClassifier **Description:** Keras classifier wrapper for scikit-learn. #### scikeras.wrappers.KerasRegressor **Description:** Keras regressor wrapper for scikit-learn. ``` -------------------------------- ### Evaluate Ensemble Performance Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/Meta_Estimators.md Trains and scores the single classifier and the AdaBoost ensemble on a toy dataset. ```python from sklearn.datasets import make_moons X, y = make_moons() single_score = clf.fit(X, y).score(X, y) adaboost_score = adaboost.fit(X, y).score(X, y) print(f"Single score: {single_score:.2f}") print(f"AdaBoost score: {adaboost_score:.2f}") ``` -------------------------------- ### Test MultiOutputClassifier Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/DataTransformers.md Prepares data and executes the classifier fit and score methods. ```python from sklearn.preprocessing import StandardScaler # Use labels as features, just to make sure we can learn correctly X = y_sklearn X = StandardScaler().fit_transform(X) ``` ```python clf = MultiOutputClassifier(model=get_clf_model, verbose=0, random_state=0) clf.fit(X, y_sklearn).score(X, y_sklearn) ``` -------------------------------- ### Create sparse pipeline with uint8 dtype Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/sparse.md Creates a Scikit-Learn pipeline that uses OneHotEncoder with sparse output and specifies uint8 as the data type for memory optimization. ```python sparse_pipline_uint8 = Pipeline( [ ("encoder", OneHotEncoder(sparse_output=True, dtype=np.uint8)), ("model", KerasRegressor(get_clf, loss="mse", epochs=5, verbose=False)) ] ) ``` -------------------------------- ### Test Subclassed MLPClassifier Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/MLPClassifier_MLPRegressor.md Instantiate the custom MLPClassifier and test its fit and score methods with sample data. This confirms that the subclassed estimator functions correctly. ```python clf = MLPClassifier(epochs=20) # for notebook execution time # check score print(clf.fit(X, y).score(X, y)) ``` -------------------------------- ### Benchmark runtime for dense pipeline Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/sparse.md Measures the average execution time for training the dense pipeline. ```python %timeit dense_pipeline.fit(X, y) ``` -------------------------------- ### Compile Model with Custom Loss Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/MLPClassifier_MLPRegressor.md Manually compiles the model within the builder function to implement custom logic for loss function selection. ```python def get_clf_model(hidden_layer_sizes: Iterable[int], meta: Dict[str, Any]): model = keras.Sequential() inp = keras.layers.Input(shape=(meta["n_features_in_"],)) model.add(inp) for hidden_layer_size in hidden_layer_sizes: layer = keras.layers.Dense(hidden_layer_size, activation="relu") model.add(layer) if meta["target_type_"] == "binary": n_output_units = 1 output_activation = "sigmoid" loss = "binary_crossentropy" elif meta["target_type_"] == "multiclass": n_output_units = meta["n_classes_"] output_activation = "softmax" loss = "sparse_categorical_crossentropy" else: raise NotImplementedError(f"Unsupported task type: {meta['target_type_']}") out = keras.layers.Dense(n_output_units, activation=output_activation) model.add(out) model.compile(loss=loss) return model ``` -------------------------------- ### Save Keras Model to Disk Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/Basic_Usage.md Save the underlying Keras model (including weights and configuration) to disk using Keras' save method. This is recommended for sharing models or when pickle is not suitable. ```python # Save to disk pred_old = reg.predict(X_regr) reg.model_.save("/tmp/my_model.keras") # saves just the Keras model ``` -------------------------------- ### Test MultiOutputTransformer Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/DataTransformers.md Verifies the transformer functionality using fit_transform, inverse_transform, and metadata retrieval. ```python tf = MultiOutputTransformer() y_sklearn = np.column_stack(y) y_keras = tf.fit_transform(y_sklearn) print("`y`, as will be passed to Keras:") print([y_keras[0][:4], y_keras[1][:4]]) ``` ```python y_pred_sklearn = tf.inverse_transform(y_pred) print("`y_pred`, as will be returned to sklearn:") y_pred_sklearn[:5] ``` ```python print(f"metadata = {tf.get_metadata()}") ``` -------------------------------- ### Configure Optimizer via compile_kwargs Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/MLPClassifier_MLPRegressor.md Accepts compile_kwargs to allow external tuning of the optimizer during model compilation. ```python def get_clf_model(hidden_layer_sizes: Iterable[int], meta: Dict[str, Any], compile_kwargs: Dict[str, Any]): model = keras.Sequential() inp = keras.layers.Input(shape=(meta["n_features_in_"],)) model.add(inp) for hidden_layer_size in hidden_layer_sizes: layer = keras.layers.Dense(hidden_layer_size, activation="relu") model.add(layer) if meta["target_type_"] == "binary": n_output_units = 1 output_activation = "sigmoid" loss = "binary_crossentropy" elif meta["target_type_"] == "multiclass": n_output_units = meta["n_classes_"] output_activation = "softmax" loss = "sparse_categorical_crossentropy" else: raise NotImplementedError(f"Unsupported task type: {meta['target_type_']}") out = keras.layers.Dense(n_output_units, activation=output_activation) model.add(out) model.compile(loss=loss, optimizer=compile_kwargs["optimizer"]) return model ``` -------------------------------- ### Define Custom Callback Source: https://github.com/adriangb/scikeras/blob/master/docs/source/advanced.rst Create a custom callback by extending keras.callbacks.Callback. ```python import keras class MyCallback(keras.callbacks.Callback): def on_train_begin(self, epoch, logs=None): print("Started training from a `fit` or `partial_fit` call!") def on_test_begin(self, epoch, logs=None): print("Started testing/evaluation from a `fit` call!") def on_predict_begin(self, epoch, logs=None): print("Started prediction from a `predict` or `predict_proba` call!") ``` -------------------------------- ### Rename build_fn to model Source: https://github.com/adriangb/scikeras/blob/master/docs/source/migration.rst Update the constructor argument from build_fn to model to ensure future compatibility. ```diff - clf = KerasClassifier(build_fn=...) + clf = KerasClassifier(model=...) ``` -------------------------------- ### Inspect Prediction Outputs Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/DataTransformers.md Prints the first two rows of the prediction arrays for both model outputs. ```python print(y_pred[0][:2, :]) ``` ```python print(y_pred[1][:2, :]) ``` -------------------------------- ### Perform Incremental Learning with partial_fit Source: https://context7.com/adriangb/scikeras/llms.txt Utilize partial_fit to train models on streaming data batches while preserving the model's state across calls. ```python import numpy as np import keras from scikeras.wrappers import KerasClassifier def get_model(meta): model = keras.models.Sequential([ keras.layers.Input(shape=(meta["n_features_in_"],)), keras.layers.Dense(50, activation="relu"), keras.layers.Dense(1, activation="sigmoid") ]) return model clf = KerasClassifier( model=get_model, loss="binary_crossentropy", verbose=0, ) # Simulate streaming data np.random.seed(0) classes = np.array([0, 1]) for batch in range(5): X_batch = np.random.random((100, 10)) y_batch = np.random.randint(0, 2, 100) # partial_fit trains for 1 epoch, preserving model state clf.partial_fit(X_batch, y_batch, classes=classes) print(f"Batch {batch + 1}, Epoch {clf.current_epoch}") # Output: # Batch 1, Epoch 1 # Batch 2, Epoch 2 # ... ``` -------------------------------- ### Compare AutoEncoder and Deep AutoEncoder Performance Source: https://github.com/adriangb/scikeras/blob/master/docs/source/notebooks/AutoEncoders.md Scores both a simple `autoencoder` and a `deep` autoencoder on the test set `x_test` using the `score` method and prints the results. Assumes both models and `x_test` are defined. ```python print("1-MSE for training set (higher is better)\n") score = autoencoder.score(X=x_test) print(f"AutoEncoder: {score:.4f}") score = deep.score(X=x_test) print(f"Deep AutoEncoder: {score:.4f}") ```