keras
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseKeras
Keras
Keras 3 is a game changer: it is now multi-backend. You can write Keras code and run it on top of JAX, PyTorch, or TensorFlow.
Keras 3 是一项颠覆性的更新:它现在支持多后端。你可以编写Keras代码,然后在JAX、PyTorch或TensorFlow上运行。
When to Use
适用场景
- Portability: Write once, run on any framework.
- Simplicity: is still the cleanest API in the industry.
model.fit() - XLA: Keras 3 enables XLA compilation on all backends by default.
- 可移植性: 一次编写,可在任意框架上运行。
- 简洁性: 仍是业内最简洁的API。
model.fit() - XLA支持: Keras 3默认在所有后端上启用XLA编译。
Core Concepts
核心概念
Backend Agnostic
后端无关性
The Model is just a blueprint. You choose the engine at runtime.
os.environ["KERAS_BACKEND"] = "jax"模型只是一个蓝图,你可以在运行时选择引擎。
os.environ["KERAS_BACKEND"] = "jax"Functional API
函数式API
Defining models as a graph of layers: .
x = Dense()(inputs)将模型定义为层的图结构:。
x = Dense()(inputs)Keras Core (keras.ops
)
keras.opsKeras Core (keras.ops
)
keras.opsA numpy-like API that works across all frameworks (differentiable numpy).
类Numpy的API,可在所有框架上运行(可微分的numpy)。
Best Practices (2025)
2025年最佳实践
Do:
- Use Keras 3: Migrate from .
tf.keras - Use JAX backend: For fastest training on TPUs/GPUs.
- Use PyTorch backend: If you need to integrate into a larger PyTorch codebase.
Don't:
- Don't mix ops: Use
tf.*to remain framework-agnostic.keras.ops.*
建议做法:
- 使用Keras 3: 从迁移。
tf.keras - 使用JAX后端: 在TPU/GPU上实现最快训练速度。
- 使用PyTorch后端: 如果你需要集成到更大的PyTorch代码库中。
不建议做法:
- 不要混用操作: 使用
tf.*以保持框架无关性。keras.ops.*