Install & Compatibility
Where this runs
tested against v1.0.5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
84MB installed
● package 84MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
kerastuner
✓ import keras_tuner as kerastuner
✗ import kerastuner
This quickstart demonstrates how to import and initialize a Keras Tuner `RandomSearch` tuner using the `kerastuner` legacy import provided by the `kt-legacy` package. It defines a simple hypermodel and initializes the tuner, ready for a hyperparameter search.
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)
Debug
Known issues
deprecatedThe `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.fixFor new code, replace `import kerastuner` with `import keras_tuner` and update references accordingly. For existing code, ensure `kt-legacy` is installed alongside `keras-tuner`.
affects: < 2.0.0 of `keras-tuner` for direct `kerastuner` usage, `kt-legacy` is designed for all versions.
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.fixEnsure both `keras-tuner` and `kt-legacy` are installed: `pip install keras-tuner kt-legacy`.
affects: All versions
Upgrade
Version history
1.0.5latest on PyPI · released Apr 12, 2023
Audit
Dependencies
keras-tunerrequired`kt-legacy` provides backward-compatible import names for the `keras-tuner` library. It will not function correctly without `keras-tuner` installed.