Skip to content

Commit

Permalink
refactor!: support only Multibackend Keras.
Browse files Browse the repository at this point in the history
Also:
 * Add types
 * Improve metrics tracking file.
  • Loading branch information
Mah Neh committed Sep 23, 2024
1 parent 8dea894 commit f132537
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 276 deletions.
25 changes: 0 additions & 25 deletions keras_tuner/backend/__init__.py
Original file line number Diff line number Diff line change
@@ -1,25 +0,0 @@
# Copyright 2019 The KerasTuner Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Keras backend module.
This module adds a temporarily Keras API surface that is fully under KerasTuner
control. This allows us to switch between Keras 3 and `tf.keras`, as well
as add shims to support older version of `tf.keras`.
- `config`: check which backend is being run.
- `keras`: The full `keras` API (via `keras` 3 or `tf.keras`).
- `ops`: `keras.ops`, always tf backed if using `tf.keras`.
"""

from keras_tuner.backend import config, io, keras, ops, random
33 changes: 0 additions & 33 deletions keras_tuner/backend/config.py

This file was deleted.

36 changes: 22 additions & 14 deletions keras_tuner/backend/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,57 @@
# limitations under the License.


import glob as built_in_glob
import os
import shutil
from pathlib import Path
from typing import cast

from keras_tuner.backend import config
from _typeshed import OpenTextMode
from keras.api.backend import backend

if config.backend() == "tensorflow":
if backend() == "tensorflow":
import tensorflow as tf
else:
tf = None

# the tf options are for non-locking thread.

def exists(path):

def exists(path: str) -> bool:
"""Check if file exists at path."""
if tf is None:
return os.path.exists(path)
return Path(path).exists()
return tf.io.gfile.exists(path)


def rmtree(path):
def rmtree(path: str) -> None:
"""Remove the whole directory tree under path."""
if tf is None:
return shutil.rmtree(path)
return tf.io.gfile.rmtree(path)


def File(filename, mode):
def File(filename: str, mode: OpenTextMode):
if tf is None:
file = open(filename, mode)
file = Path(filename).open(mode)
else:
file = tf.io.gfile.GFile(filename, mode)

return file


def glob(path):
def glob(path: str) -> list[str]:
"""Return a list globs from pattern."""
if tf is None:
return built_in_glob.glob(path)
return cast(list[str], list(Path().glob(path)))
return tf.io.gfile.glob(path)


def makedirs(path):
def makedirs(path: str) -> None:
"""Create directry and parents at path, unless it exists."""
if tf is None:
return os.makedirs(path, exist_ok=True)
return tf.io.gfile.makedirs(path)
Path.mkdir(Path(path), exist_ok=True)
else:
tf.io.gfile.makedirs(path)


# The following code is forked from Keras:
Expand Down
37 changes: 0 additions & 37 deletions keras_tuner/backend/keras.py

This file was deleted.

38 changes: 0 additions & 38 deletions keras_tuner/backend/ops.py

This file was deleted.

20 changes: 0 additions & 20 deletions keras_tuner/backend/random.py

This file was deleted.

Loading

0 comments on commit f132537

Please sign in to comment.