Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion keras/src/utils/backend_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ def set_backend(backend):
Example:

```python
import keras

keras.config.set_backend("jax")

del keras
import keras
Comment on lines +128 to +133
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The example is good, but it could be more illustrative by showing the effect of changing the backend. A slightly more verbose example that prints the backend name before and after the change would make it clearer to users why re-importing keras is necessary and what the function accomplishes. This aligns with the style guide's emphasis on showing how to use the API to solve problems.1

Suggested change
import keras
keras.config.set_backend("jax")
del keras
import keras
import keras
print(f"Backend before: {keras.backend.backend()}")
keras.config.set_backend("jax")
del keras
import keras
print(f"Backend after: {keras.backend.backend()}")

Style Guide References

Footnotes

  1. The style guide recommends that documentation should show code examples for end-to-end workflows to demonstrate how to use the API, rather than just talking about how it works. A more illustrative example better serves this purpose.

```

⚠️ WARNING ⚠️: Using this function is dangerous and should be done
Copy link

Choose a reason for hiding this comment

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

Perhaps it would good to print/display some of this warning text when set_backend() is run, otherwise it looks like a silent failure (when a set_backend() is called but the backend doesn't actually change).

If concerned about spamming text, perhaps there can be a flag to suppress the warning (but default to printing something, since this is such a "dangerous" function)

Expand All @@ -138,7 +143,7 @@ def set_backend(backend):

This includes any function or class instance that uses any Keras
functionality. All such code needs to be re-executed after calling
`set_backend()`.
`set_backend()` and re-importing the `keras` module.
"""
os.environ["KERAS_BACKEND"] = backend
# Clear module cache.
Expand Down