|
| 1 | +Keras and Tensorflow |
| 2 | +==================== |
| 3 | + |
| 4 | +The package SciKeras_ brings a Scikit-learn API to Keras. This allows Dask-ML |
| 5 | +to be used seamlessly with Keras models. |
| 6 | + |
| 7 | +Installation |
| 8 | +------------ |
| 9 | + |
| 10 | +Following the `Tensorflow install directions`_ and `SciKeras install guide`_, |
| 11 | +these packages need to be installed: |
| 12 | + |
| 13 | +.. code-block:: bash |
| 14 | +
|
| 15 | + $ pip install tensorflow>=2.3.0 |
| 16 | + $ pip install scikeras>=0.1.8 |
| 17 | +
|
| 18 | +These are the minimum versions that Dask-ML requires to use Tensorflow/Keras. |
| 19 | + |
| 20 | +.. _Tensorflow install directions: https://www.tensorflow.org/install |
| 21 | +.. _SciKeras install guide: https://github.com/adriangb/scikeras#installation |
| 22 | + |
| 23 | +Usage |
| 24 | +----- |
| 25 | + |
| 26 | +First, let's start by defining normal function to create our model. This is the |
| 27 | +normal way to create a `Keras Sequential model`_ |
| 28 | + |
| 29 | +.. _Keras Sequential model: https://keras.io/api/models/sequential/ |
| 30 | + |
| 31 | +.. code-block:: python |
| 32 | +
|
| 33 | + import tensorflow as tf |
| 34 | + from tensorflow.keras.layers import Dense |
| 35 | + from tensorflow.keras.models import Sequential |
| 36 | +
|
| 37 | + def build_model(lr=0.01, momentum=0.9): |
| 38 | + layers = [Dense(512, input_shape=(784,), activation="relu"), |
| 39 | + Dense(10, input_shape=(512,), activation="softmax")] |
| 40 | + model = Sequential(layers) |
| 41 | +
|
| 42 | + opt = tf.keras.optimizers.SGD( |
| 43 | + learning_rate=lr, momentum=momentum, nesterov=True, |
| 44 | + ) |
| 45 | + model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) |
| 46 | + return model |
| 47 | +
|
| 48 | +Now, we can use the SciKeras to create a Scikit-learn compatible model: |
| 49 | + |
| 50 | +.. code-block:: python |
| 51 | +
|
| 52 | + from scikeras.wrappers import KerasClassifier |
| 53 | + niceties = dict(verbose=False) |
| 54 | + model = KerasClassifier(build_fn=build_model, lr=0.1, momentum=0.9, **niceties) |
| 55 | +
|
| 56 | +This model will work with all of Dask-ML: it can use NumPy arrays as inputs and |
| 57 | +obeys the Scikit-learn API. For example, it's possible to use Dask-ML to do the |
| 58 | +following: |
| 59 | + |
| 60 | +* Use Keras with Dask-ML's model selection, including |
| 61 | + :class:`~dask_ml.model_selection.HyperbandSearchCV`. |
| 62 | +* Use Keras with Dask-ML's :class:`~dask_ml.wrappers.Incremental`. |
| 63 | + |
| 64 | +If we want to tune ``lr`` and ``momentum``, SciKeras requires that we pass |
| 65 | +``lr`` and ``momentum`` at initialization: |
| 66 | + |
| 67 | +.. code-block:: |
| 68 | +
|
| 69 | + model = KerasClassifier(build_fn=build_model, lr=None, momentum=None, **niceties) |
| 70 | +
|
| 71 | +.. _SciKeras: https://github.com/adriangb/scikeras |
| 72 | + |
| 73 | +SciKeras supports more model creation methods, including some that are |
| 74 | +backwards-compatible with Tensorflow. Refer to their documentation for details. |
| 75 | + |
| 76 | +Example: Hyperparameter Optimization |
| 77 | +------------------------------------ |
| 78 | + |
| 79 | +If we wanted to, we could use the model above with |
| 80 | +:class:`~dask_ml.model_selection.HyperbandSearchCV`. Let's tune this model on |
| 81 | +the MNIST dataset: |
| 82 | + |
| 83 | +.. code-block:: python |
| 84 | +
|
| 85 | + from tensorflow.keras.datasets import mnist |
| 86 | + from tensorflow.keras.utils import to_categorical |
| 87 | + import numpy as np |
| 88 | + from typing import Tuple |
| 89 | +
|
| 90 | + def get_mnist() -> Tuple[np.ndarray, np.ndarray]: |
| 91 | + (X_train, y_train), _ = mnist.load_data() |
| 92 | + X_train = X_train.reshape(X_train.shape[0], 784) |
| 93 | + X_train = X_train.astype("float32") |
| 94 | + X_train /= 255 |
| 95 | + return X_train, y_train |
| 96 | +
|
| 97 | +And let's perform the basic task of tuning our SGD implementation: |
| 98 | + |
| 99 | +.. code-block:: python |
| 100 | +
|
| 101 | + from scipy.stats import loguniform, uniform |
| 102 | + params = {"lr": loguniform(1e-3, 1e-1), "momentum": uniform(0, 1)} |
| 103 | + X, y = get_mnist() |
| 104 | +
|
| 105 | +Now, the search can be run: |
| 106 | + |
| 107 | +.. code-block:: python |
| 108 | +
|
| 109 | + from dask.distributed import Client |
| 110 | + client = Client() |
| 111 | +
|
| 112 | + from dask_ml.model_selection import HyperbandSearchCV |
| 113 | + search = HyperbandSearchCV(model, params, max_iter=27) |
| 114 | + search.fit(X, y) |
0 commit comments