Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust demo scripts to be Keras 3 compatible #6761

Merged
merged 1 commit into from
Feb 16, 2024

Conversation

bmd3k
Copy link
Contributor

@bmd3k bmd3k commented Feb 16, 2024

This adjusts *_demo.py files to work with Keras 3.

The hparams_demo is fully backward compatible with Keras 2 and forward compatible with Keras 3.

Unfortunately the graphs_demo is not backward compatible with Keras 2. Users attempting to run it with Keras 2 will get the following error:

  File "/usr/local/google/home/bdubois/.cache/bazel/_bazel_bdubois/079646a57be11faea0b2bfefccb2a81a/execroot/org_tensorflow_tensorboard/bazel-out/k8-fastbuild/bin/tensorboard/plugins/graph/graphs_demo.runfiles/org_tensorflow_tensorboard/tensorboard/plugins/graph/graphs_demo.py", line 128, in profile
    tf.summary.trace_on(profiler=True, profiler_outdir=logdir)
TypeError: trace_on() got an unexpected keyword argument 'profiler_outdir'

Amazingly, though, the graph that the demo generates with Keras 3 can be successfully loaded in the Graph dashboard. This makes me optimistic to get the Graph plugin fully Keras 3 compatible after addressing the user-reported error in #6686.

Old Keras 2 Graph:
image
New Keras 3 Graph:
image

@bmd3k bmd3k changed the title Adjust demo and colab scripts to be keras3-compatible Adjust demo scripts to be keras3-compatible Feb 16, 2024
@bmd3k bmd3k changed the title Adjust demo scripts to be keras3-compatible Adjust demo scripts to be Keras 3 compatible Feb 16, 2024
model.add(tf.keras.layers.Dropout(hparams[HP_DROPOUT], seed=rng.random()))
model.add(
tf.keras.layers.Dropout(
hparams[HP_DROPOUT], seed=rng.randrange(1 << 32)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addresses this error:

  File "/usr/local/google/home/bdubois/virtualenv/20240215-tensorboard-git-without-keras/lib/python3.11/site-packages/keras/src/layers/regularization/dropout.py", line 53, in __init__
    self.seed_generator = backend.random.SeedGenerator(seed)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/google/home/bdubois/virtualenv/20240215-tensorboard-git-without-keras/lib/python3.11/site-packages/keras/src/random/seed_generator.py", line 61, in __init__
    raise ValueError(
ValueError: Argument `seed` must be an integer. Received: seed=0.3616904771377427

@@ -125,11 +125,9 @@ def g(i):
for step in range(3):
# Suppress the profiler deprecation warnings from tf.summary.trace_*.
with _silence_deprecation_warnings():
tf.summary.trace_on(profiler=True)
tf.summary.trace_on(profiler=True, profiler_outdir=logdir)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addresses this error:

WARNING:tensorflow:No `profiler_outdir` passed to trace_on(). Profiler won't be enabled.
0
WARNING:tensorflow:Ignoring `profiler_outdir` passed to trace_export(). Please pass it to trace_on() instead.

<snip>

  File "/usr/local/google/home/bdubois/virtualenv/20240215-tensorboard-git-without-keras/lib/python3.11/site-packages/tensorflow/python/profiler/profiler_v2.py", line 144, in stop
    raise errors.UnavailableError(
tensorflow.python.framework.errors_impl.UnavailableError: Cannot export profiling results. No profiler is running.

@@ -90,7 +90,7 @@ def keras():
model = tf.keras.models.Sequential(layers)
model.compile(
loss=tf.keras.losses.mean_squared_error,
optimizer=tf.keras.optimizers.SGD(lr=0.2),
optimizer=tf.keras.optimizers.SGD(learning_rate=0.2),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addresses this error:

  File "/usr/local/google/home/bdubois/virtualenv/20240215-tensorboard-git-without-keras/lib/python3.11/site-packages/keras/src/optimizers/base_optimizer.py", line 38, in __init__
    raise ValueError(f"Argument(s) not recognized: {kwargs}")
ValueError: Argument(s) not recognized: {'lr': 0.2}

@bmd3k bmd3k requested a review from yatbear February 16, 2024 16:45
Copy link
Member

@yatbear yatbear left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing!

@bmd3k bmd3k merged commit 4c004d4 into tensorflow:master Feb 16, 2024
13 checks passed
rileyajones added a commit that referenced this pull request Feb 20, 2024
## Motivation for features / changes
This test behaves differently internally and externally. The changes
made in #6761 succeed in getting the test to pass externally, but it
will then fail when imported Googlers see (cl/608667862).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants