ml_switcheroo.frameworks.keras

Keras (v3) Framework Adapter.

This module implements the adapter for the Keras 3 framework, supporting multi-backend translation (JAX/Torch/TensorFlow).

It handles: 1. Math: Mapping keras.ops.* (backend-agnostic math). 2. Layers: Mapping keras.layers.*. 3. Discovery: Runtime introspection of the Keras API surface. 4. Ghost Mode: Silent fallback when Keras is not installed. 5. Weight Migration: Loading/saving .h5 or .keras files via h5py.

Attributes

keras

Classes

KerasAdapter

Adapter for Keras v3 (Multi-backend).

Module Contents

ml_switcheroo.frameworks.keras.keras = None[source]
class ml_switcheroo.frameworks.keras.KerasAdapter[source]

Adapter for Keras v3 (Multi-backend).

Provides definitions for Keras Core Ops, Layers, and Models.

display_name: str = 'Keras'
inherits_from: str | None = None
ui_priority: int = 25
property search_modules: List[str]

Returns list of search modules.

If in Ghost Mode, returns empty list to prevent Scaffolder from attempting to import missing modules.

Returns:

Module names.

Return type:

List[str]

property unsafe_submodules: Set[str]

Submodules to exclude from recursive scans.

Returns:

Empty set for Keras properly scoped imports.

Return type:

Set[str]

property import_alias: Tuple[str, str]

Default import alias.

Returns:

(“keras”, “keras”).

Return type:

Tuple[str, str]

property import_namespaces: Dict[str, ml_switcheroo.frameworks.base.ImportConfig]

Namespace mapping for import fixer.

Returns:

Configuration for import injection.

Return type:

Dict[str, ImportConfig]

property discovery_heuristics: Dict[str, List[str]]

Regex patterns for scaffold categorization.

Returns:

Patterns mapping tiers to paths.

Return type:

Dict[str, List[str]]

property test_config: Dict[str, str]

Templates for test code generation.

Returns:

Test Harness values.

Return type:

Dict[str, str]

property harness_imports: List[str]

Imports for verification harness.

Returns:

Empty list.

Return type:

List[str]

get_harness_init_code() str[source]

Init code for verification harness.

Returns:

Empty string.

Return type:

str

get_to_numpy_code() str[source]

Returns code to convert Keras tensors to NumPy.

Returns:

Conversion logic checking for numpy property.

Return type:

str

property supported_tiers: List[ml_switcheroo.enums.SemanticTier]

Supported semantic tiers.

Returns:

[ARRAY_API, NEURAL, EXTRAS].

Return type:

List[SemanticTier]

property declared_magic_args: List[str]

List of framework-specific magic arguments.

Returns:

Empty list as Keras handles state implicitly.

Return type:

List[str]

property structural_traits: ml_switcheroo.frameworks.base.StructuralTraits

Structural transformation rules.

Returns:

Configuration object.

Return type:

StructuralTraits

property plugin_traits: Any

Plugin behavior flags.

Returns:

Object defining capabilities.

Return type:

PluginTraits

property definitions: Dict[str, ml_switcheroo.frameworks.base.StandardMap]

Static mappings for Keras. Loaded dynamically from frameworks/definitions/keras.json.

Returns:

Definitions.

Return type:

Dict[str, StandardMap]

property rng_seed_methods: List[str]

Global seed methods.

Returns:

Names like “set_random_seed”.

Return type:

List[str]

collect_api(category: ml_switcheroo.frameworks.base.StandardCategory) List[ml_switcheroo.core.ghost.GhostRef][source]

Collects API definitions via introspection or snapshot.

Parameters:

category (StandardCategory) – Category to scan.

Returns:

Found items.

Return type:

List[GhostRef]

convert(data: Any) Any[source]

Converts input data to Keras Tensor.

Parameters:

data (Any) – Input data.

Returns:

Keras Tensor or original data.

Return type:

Any

get_serialization_imports() List[str][source]

Imports for saving/loading.

Returns:

Imports.

Return type:

List[str]

get_serialization_syntax(op: str, file_arg: str, object_arg: str | None = None) str[source]

Syntax for saving/loading models.

Parameters:
  • op (str) – ‘save’ or ‘load’.

  • file_arg (str) – Path string.

  • object_arg (Optional[str]) – Object name.

Returns:

Generated python code.

Return type:

str

get_weight_conversion_imports() List[str][source]

Returns imports required for the generated weight migration script.

Returns:

List of import statements.

Return type:

List[str]

get_weight_load_code(path_var: str) str[source]

Returns python code to load a checkpoint. Stub implemented as Keras models contain structure + weights, making raw dict handling tricky.

get_tensor_to_numpy_expr(tensor_var: str) str[source]

Returns python expression string that converts tensor_var from Keras tensor to numpy array.

get_weight_save_code(state_var: str, path_var: str) str[source]

Returns Python code to save the dictionary state_var (mapping flat keys to numpy arrays) to path_var using h5py.

Parameters:
  • state_var (str) – Variable name of the state dictionary.

  • path_var (str) – Variable name of the output path.

Returns:

Generated Python code block.

Return type:

str

get_device_syntax(device_type: str, device_index: str | None = None) str[source]

Syntax for device scoping.

Parameters:
  • device_type (str) – ‘cuda’, ‘cpu’.

  • device_index (Optional[str]) – Index.

Returns:

Generated code.

Return type:

str

get_device_check_syntax() str[source]

Syntax for checking GPU availability.

Returns:

Logic expression.

Return type:

str

get_rng_split_syntax(rng_var: str, key_var: str) str[source]

Keras handles RNG state internally.

Returns:

“pass”.

Return type:

str

apply_wiring(snapshot: Dict[str, Any]) None[source]

Applies configuration wiring.

Parameters:

snapshot – Snapshot dict.

get_doc_url(api_name: str) str | None[source]

Provides a search URL for Keras documentation as direct API mapping is non-trivial.

Parameters:

api_name (str) – API path.

Returns:

URL.

Return type:

Optional[str]

get_tiered_examples() Dict[str, str][source]

Returns tiered examples.

Returns:

Map of tiers to examples.

Return type:

Dict[str, str]