{"id":9062,"library":"kt-legacy","title":"Keras Tuner Legacy Imports","description":"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.","status":"active","version":"1.0.5","language":"en","source_language":"en","source_url":"https://github.com/haifeng-jin/kt-legacy","tags":["keras","tensorflow","hyperparameter tuning","legacy","compatibility","migration"],"install":[{"cmd":"pip install kt-legacy","lang":"bash","label":"Install `kt-legacy`"},{"cmd":"pip install keras-tuner kt-legacy","lang":"bash","label":"Install `keras-tuner` and `kt-legacy` (recommended)"}],"dependencies":[{"reason":"`kt-legacy` provides backward-compatible import names for the `keras-tuner` library. It will not function correctly without `keras-tuner` installed.","package":"keras-tuner","optional":false}],"imports":[{"note":"The `kt-legacy` package enables `import kerastuner` for projects written with older Keras Tuner import paths. For new development, it's recommended to `import keras_tuner as kt` directly.","wrong":"import kerastuner as kt","symbol":"kerastuner","correct":"import kerastuner as kt"}],"quickstart":{"code":"import keras\nimport kerastuner as kt\n\ndef build_model(hp):\n    model = keras.Sequential([\n        keras.layers.Flatten(input_shape=(28, 28)),\n        keras.layers.Dense(\n            units=hp.Int('units', min_value=32, max_value=512, step=32),\n            activation='relu'\n        ),\n        keras.layers.Dense(10, activation='softmax')\n    ])\n    model.compile(\n        optimizer=keras.optimizers.Adam(learning_rate=hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])),\n        loss='sparse_categorical_crossentropy',\n        metrics=['accuracy']\n    )\n    return model\n\n# Example of using a tuner with the legacy import\ntuner = kt.RandomSearch(\n    hypermodel=build_model,\n    objective='val_accuracy',\n    max_trials=2, # For quick demonstration\n    executions_per_trial=1,\n    directory='my_dir', project_name='intro_to_kt_legacy'\n)\n\n# Note: To run search, you would typically need training data, e.g.,\n# (img_train, label_train), (img_test, label_test) = keras.datasets.fashion_mnist.load_data()\n# img_train = img_train.astype('float32') / 255.0\n# label_train = label_train[:100] # Subset for quick example\n# img_train = img_train[:100]\n# tuner.search(img_train, label_train, epochs=2, validation_split=0.2)\n\nprint(\"Keras Tuner (legacy import) initialized successfully.\")\n# print(tuner.get_best_hyperparameters()[0].values)","lang":"python","description":"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."},"warnings":[{"fix":"For new code, replace `import kerastuner` with `import keras_tuner` and update references accordingly. For existing code, ensure `kt-legacy` is installed alongside `keras-tuner`.","message":"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.","severity":"deprecated","affected_versions":"< 2.0.0 of `keras-tuner` for direct `kerastuner` usage, `kt-legacy` is designed for all versions."},{"fix":"Ensure both `keras-tuner` and `kt-legacy` are installed: `pip install keras-tuner kt-legacy`.","message":"`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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Install `kt-legacy` using `pip install kt-legacy`. Also ensure `keras-tuner` is installed via `pip install keras-tuner`.","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.","error":"ModuleNotFoundError: No module named 'kerastuner'"},{"fix":"Verify 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`.","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.","error":"AttributeError: module 'kerastuner' has no attribute 'RandomSearch'"}]}