Keras Tuner Legacy Imports
The `kt-legacy` library provides backward-compatible import names for Keras Tuner. It allows users to import Keras Tuner components using the `kerastuner` namespace, which was the original import path, instead of the current `keras_tuner` namespace. This is particularly useful for migrating or maintaining compatibility with older codebases that expect the `kerastuner` module.
Common errors
-
ModuleNotFoundError: No module named 'kerastuner'
cause This error occurs when old code attempts to `import kerastuner` but the `kt-legacy` package is not installed, or `keras-tuner` itself is not installed. Modern `keras-tuner` installations use `keras_tuner` as the primary import path.fixInstall `kt-legacy` using `pip install kt-legacy`. Also ensure `keras-tuner` is installed via `pip install keras-tuner`. -
AttributeError: module 'kerastuner' has no attribute 'RandomSearch'
cause This usually indicates that `kt-legacy` might be installed, but the underlying `keras-tuner` library is either missing or an incompatible version, preventing the `kerastuner` alias from correctly resolving to the actual Keras Tuner classes.fixVerify that `keras-tuner` is installed and up-to-date (`pip install --upgrade keras-tuner`). If the problem persists, try reinstalling both: `pip install --upgrade keras-tuner kt-legacy`.
Warnings
- deprecated The `kerastuner` import path is deprecated in favor of `keras_tuner` within the main Keras Tuner library. While `kt-legacy` provides compatibility, new projects should use `keras_tuner` directly to align with current best practices and avoid potential future breakage.
- gotcha `kt-legacy` only provides the legacy import alias. It does not include the Keras Tuner functionality itself. `keras-tuner` must be installed separately for the aliased imports to function.
Install
-
pip install kt-legacy -
pip install keras-tuner kt-legacy
Imports
- kerastuner
import kerastuner as kt
import kerastuner as kt
Quickstart
import keras
import kerastuner as kt
def build_model(hp):
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(
units=hp.Int('units', min_value=32, max_value=512, step=32),
activation='relu'
),
keras.layers.Dense(10, activation='softmax')
])
model.compile(
optimizer=keras.optimizers.Adam(learning_rate=hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])),
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
return model
# Example of using a tuner with the legacy import
tuner = kt.RandomSearch(
hypermodel=build_model,
objective='val_accuracy',
max_trials=2, # For quick demonstration
executions_per_trial=1,
directory='my_dir', project_name='intro_to_kt_legacy'
)
# Note: To run search, you would typically need training data, e.g.,
# (img_train, label_train), (img_test, label_test) = keras.datasets.fashion_mnist.load_data()
# img_train = img_train.astype('float32') / 255.0
# label_train = label_train[:100] # Subset for quick example
# img_train = img_train[:100]
# tuner.search(img_train, label_train, epochs=2, validation_split=0.2)
print("Keras Tuner (legacy import) initialized successfully.")
# print(tuner.get_best_hyperparameters()[0].values)