Skip to content

Commit b9be76a

Browse files
committed
Convert Keras Core to Keras 3.
1 parent 10d0349 commit b9be76a

File tree

700 files changed

+5823
-5823
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

700 files changed

+5823
-5823
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
[![](https://github.com/keras-team/keras-core/workflows/Tests/badge.svg?branch=main)](https://github.com/keras-team/keras-core/actions?query=workflow%3ATests+branch%3Amain)
2-
[![](https://badge.fury.io/py/keras-core.svg)](https://badge.fury.io/py/keras-core)
1+
[![](https://github.com/keras-team/keras/workflows/Tests/badge.svg?branch=main)](https://github.com/keras-team/keras/actions?query=workflow%3ATests+branch%3Amain)
2+
[![](https://badge.fury.io/py/keras.svg)](https://badge.fury.io/py/keras)
33

4-
# Keras Core: A new multi-backend Keras
4+
# Keras 3: A new multi-backend Keras
55

6-
Keras Core is a new multi-backend implementation of the Keras API, with support for TensorFlow, JAX, and PyTorch.
6+
Keras 3 is a new multi-backend implementation of the Keras API, with support for TensorFlow, JAX, and PyTorch.
77

88
**WARNING:** At this time, this package is experimental.
99
It has rough edges and not everything might work as expected.
@@ -13,7 +13,7 @@ Once ready, this package will become Keras 3.0 and subsume `tf.keras`.
1313

1414
## Local installation
1515

16-
Keras Core is compatible with Linux and MacOS systems. To install a local development version:
16+
Keras 3 is compatible with Linux and MacOS systems. To install a local development version:
1717

1818
1. Install dependencies:
1919

@@ -28,7 +28,7 @@ python pip_build.py --install
2828
```
2929

3030
You should also install your backend of choice: `tensorflow`, `jax`, or `torch`.
31-
Note that `tensorflow` is required for using certain Keras Core features: certain preprocessing layers as
31+
Note that `tensorflow` is required for using certain Keras 3 features: certain preprocessing layers as
3232
well as `tf.data` pipelines.
3333

3434
## Configuring your backend
@@ -46,16 +46,16 @@ In Colab, you can do:
4646
import os
4747
os.environ["KERAS_BACKEND"] = "jax"
4848

49-
import keras_core as keras
49+
import keras as keras
5050
```
5151

52-
**Note:** The backend must be configured before importing `keras_core`, and the backend cannot be changed after
52+
**Note:** The backend must be configured before importing `keras`, and the backend cannot be changed after
5353
the package has been imported.
5454

5555
## Backwards compatibility
5656

57-
Keras Core is intended to work as a drop-in replacement for `tf.keras` (when using the TensorFlow backend). Just take your
58-
existing `tf.keras` code, change the `keras` imports to `keras_core`, make sure that your calls to `model.save()` are using
57+
Keras 3 is intended to work as a drop-in replacement for `tf.keras` (when using the TensorFlow backend). Just take your
58+
existing `tf.keras` code, change the `keras` imports to `keras`, make sure that your calls to `model.save()` are using
5959
the up-to-date `.keras` format, and you're done.
6060

6161
If your `tf.keras` model does not include custom components, you can start running it on top of JAX or PyTorch immediately.
@@ -66,7 +66,7 @@ to a backend-agnostic implementation in just a few minutes.
6666
In addition, Keras models can consume datasets in any format, regardless of the backend you're using:
6767
you can train your models with your existing `tf.data.Dataset` pipelines or PyTorch `DataLoaders`.
6868

69-
## Why use Keras Core?
69+
## Why use Keras 3?
7070

7171
- Run your high-level Keras workflows on top of any framework -- benefiting at will from the advantages of each framework,
7272
e.g. the scalability and performance of JAX or the production ecosystem options of TensorFlow.

benchmarks/layer_benchmark/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Benchmark the layer performance
22

33
This directory contains benchmarks to compare the performance of
4-
`keras_core.layers.XXX` and `tf.keras.layers.XXX`. We compare the performance of
4+
`keras.layers.XXX` and `tf.keras.layers.XXX`. We compare the performance of
55
both the forward pass and train step (forward & backward pass).
66

77
To run the benchmark, use the command below and change the flags according to

benchmarks/layer_benchmark/base_benchmark.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import tensorflow as tf
55
from absl import flags
66

7-
import keras_core
7+
import keras
88

99
FLAGS = flags.FLAGS
1010

@@ -66,7 +66,7 @@ def on_predict_batch_end(self, batch, logs=None):
6666
self.state["throughput"] = throughput
6767

6868

69-
class KerasCoreBenchmarkMetricsCallback(keras_core.callbacks.Callback):
69+
class KerasCoreBenchmarkMetricsCallback(keras.callbacks.Callback):
7070
def __init__(self, start_batch=1, stop_batch=None):
7171
self._callback = BenchmarkMetricsCallback(start_batch, stop_batch)
7272

@@ -108,36 +108,36 @@ def __init__(
108108
input_shape,
109109
flat_call_inputs=True,
110110
jit_compile=True,
111-
keras_core_layer=None,
111+
keras_layer=None,
112112
tf_keras_layer=None,
113113
):
114114
self.layer_name = layer_name
115-
_keras_core_layer_class = getattr(keras_core.layers, layer_name)
115+
_keras_layer_class = getattr(keras.layers, layer_name)
116116
_tf_keras_layer_class = getattr(tf.keras.layers, layer_name)
117117

118-
if keras_core_layer is None:
119-
# Sometimes you want to initialize the keras_core layer and tf_keras
118+
if keras_layer is None:
119+
# Sometimes you want to initialize the keras layer and tf_keras
120120
# layer in a different way. For example, `Bidirectional` layer,
121-
# which takes in `keras_core.layers.Layer` and
121+
# which takes in `keras.layers.Layer` and
122122
# `tf.keras.layer.Layer` separately.
123-
self._keras_core_layer = _keras_core_layer_class(**init_args)
123+
self._keras_layer = _keras_layer_class(**init_args)
124124
else:
125-
self._keras_core_layer = keras_core_layer
125+
self._keras_layer = keras_layer
126126

127127
if tf_keras_layer is None:
128128
self._tf_keras_layer = _tf_keras_layer_class(**init_args)
129129
else:
130130
self._tf_keras_layer = tf_keras_layer
131131

132132
self.input_shape = input_shape
133-
self._keras_core_model = self._build_keras_core_model(
133+
self._keras_model = self._build_keras_model(
134134
input_shape, flat_call_inputs
135135
)
136136
self._tf_keras_model = self._build_tf_keras_model(
137137
input_shape, flat_call_inputs
138138
)
139139

140-
self._keras_core_model.compile(
140+
self._keras_model.compile(
141141
loss="mse", optimizer="sgd", jit_compile=jit_compile
142142
)
143143
self._tf_keras_model.compile(
@@ -148,19 +148,19 @@ def __init__(
148148
self.jit_compile = jit_compile
149149
self.input_shape = input_shape
150150

151-
def _build_keras_core_model(self, input_shape, flat_call_inputs=True):
151+
def _build_keras_model(self, input_shape, flat_call_inputs=True):
152152
inputs = []
153153
if not isinstance(input_shape[0], (tuple, list)):
154154
input_shape = [input_shape]
155155

156156
for shape in input_shape:
157-
inputs.append(keras_core.Input(shape=shape))
157+
inputs.append(keras.Input(shape=shape))
158158

159159
if flat_call_inputs:
160-
outputs = self._keras_core_layer(*inputs)
160+
outputs = self._keras_layer(*inputs)
161161
else:
162-
outputs = self._keras_core_layer(inputs)
163-
return keras_core.Model(inputs=inputs, outputs=outputs)
162+
outputs = self._keras_layer(inputs)
163+
return keras.Model(inputs=inputs, outputs=outputs)
164164

165165
def _build_tf_keras_model(self, input_shape, flat_call_inputs=True):
166166
inputs = []
@@ -195,7 +195,7 @@ def benchmark_predict(self, num_samples, batch_size, data=None):
195195
stop_batch=num_iterations
196196
)
197197

198-
self._keras_core_model.predict(
198+
self._keras_model.predict(
199199
data,
200200
batch_size=batch_size,
201201
callbacks=[callback],
@@ -207,15 +207,15 @@ def benchmark_predict(self, num_samples, batch_size, data=None):
207207
callbacks=[tf_keras_callback],
208208
)
209209

210-
keras_core_throughput = (
210+
keras_throughput = (
211211
callback._callback.state["throughput"] * batch_size
212212
)
213213
tf_keras_throughput = (
214214
tf_keras_callback._callback.state["throughput"] * batch_size
215215
)
216216
print(
217-
f"Keras Core throughput of forward pass of {self.layer_name}: "
218-
f"{keras_core_throughput:.2f} samples/sec."
217+
f"Keras 3 throughput of forward pass of {self.layer_name}: "
218+
f"{keras_throughput:.2f} samples/sec."
219219
)
220220
print(
221221
f"TF Keras throughput of forward pass of {self.layer_name}: "
@@ -240,15 +240,15 @@ def benchmark_train(self, num_samples, batch_size, data=None, label=None):
240240
if self.flat_call_inputs:
241241
# Scale by a small factor to avoid zero gradients.
242242
label = (
243-
keras_core.backend.convert_to_numpy(
244-
self._keras_core_layer(*data)
243+
keras.backend.convert_to_numpy(
244+
self._keras_layer(*data)
245245
)
246246
* 1.001
247247
)
248248
else:
249249
label = (
250-
keras_core.backend.convert_to_numpy(
251-
self._keras_core_layer(data)
250+
keras.backend.convert_to_numpy(
251+
self._keras_layer(data)
252252
)
253253
* 1.001
254254
)
@@ -259,7 +259,7 @@ def benchmark_train(self, num_samples, batch_size, data=None, label=None):
259259
stop_batch=num_iterations
260260
)
261261

262-
self._keras_core_model.fit(
262+
self._keras_model.fit(
263263
data,
264264
label,
265265
batch_size=batch_size,
@@ -272,15 +272,15 @@ def benchmark_train(self, num_samples, batch_size, data=None, label=None):
272272
callbacks=[tf_keras_callback],
273273
)
274274

275-
keras_core_throughput = (
275+
keras_throughput = (
276276
callback._callback.state["throughput"] * batch_size
277277
)
278278
tf_keras_throughput = (
279279
tf_keras_callback._callback.state["throughput"] * batch_size
280280
)
281281
print(
282-
f"Keras Core throughput of forward & backward pass of "
283-
f"{self.layer_name}: {keras_core_throughput:.2f} samples/sec."
282+
f"Keras 3 throughput of forward & backward pass of "
283+
f"{self.layer_name}: {keras_throughput:.2f} samples/sec."
284284
)
285285
print(
286286
f"TF Keras throughput of forward & backward pass of "

benchmarks/layer_benchmark/rnn_benchmark.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from absl import app
1717
from absl import flags
1818

19-
import keras_core
19+
import keras
2020
from benchmarks.layer_benchmark.base_benchmark import LayerBenchmark
2121

2222
FLAGS = flags.FLAGS
@@ -194,16 +194,16 @@ def benchmark_bidirectional(
194194
):
195195
layer_name = "Bidirectional"
196196
init_args = {}
197-
keras_core_layer = keras_core.layers.Bidirectional(
198-
keras_core.layers.LSTM(32)
197+
keras_layer = keras.layers.Bidirectional(
198+
keras.layers.LSTM(32)
199199
)
200200
tf_keras_layer = tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32))
201201
benchmark = LayerBenchmark(
202202
layer_name,
203203
init_args,
204204
input_shape=[256, 256],
205205
jit_compile=jit_compile,
206-
keras_core_layer=keras_core_layer,
206+
keras_layer=keras_layer,
207207
tf_keras_layer=tf_keras_layer,
208208
)
209209

@@ -225,8 +225,8 @@ def benchmark_time_distributed(
225225
):
226226
layer_name = "TimeDistributed"
227227
init_args = {}
228-
keras_core_layer = keras_core.layers.TimeDistributed(
229-
keras_core.layers.Conv2D(16, (3, 3))
228+
keras_layer = keras.layers.TimeDistributed(
229+
keras.layers.Conv2D(16, (3, 3))
230230
)
231231
tf_keras_layer = tf.keras.layers.TimeDistributed(
232232
tf.keras.layers.Conv2D(16, (3, 3))
@@ -236,7 +236,7 @@ def benchmark_time_distributed(
236236
init_args,
237237
input_shape=[10, 32, 32, 3],
238238
jit_compile=jit_compile,
239-
keras_core_layer=keras_core_layer,
239+
keras_layer=keras_layer,
240240
tf_keras_layer=tf_keras_layer,
241241
)
242242

benchmarks/model_benchmark/benchmark_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import time
22

3-
import keras_core
3+
import keras
44

55

6-
class BenchmarkMetricsCallback(keras_core.callbacks.Callback):
6+
class BenchmarkMetricsCallback(keras.callbacks.Callback):
77
def __init__(self, start_batch=1, stop_batch=None):
88
self.start_batch = start_batch
99
self.stop_batch = stop_batch

benchmarks/model_benchmark/bert_benchmark.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from absl import logging
2222
from model_benchmark.benchmark_utils import BenchmarkMetricsCallback
2323

24-
import keras_core as keras
24+
import keras as keras
2525

2626
flags.DEFINE_string("model_size", "small", "The size of model to benchmark.")
2727
flags.DEFINE_string(

benchmarks/model_benchmark/image_classification_benchmark.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from absl import logging
2828
from model_benchmark.benchmark_utils import BenchmarkMetricsCallback
2929

30-
import keras_core as keras
30+
import keras as keras
3131

3232
flags.DEFINE_string("model", "EfficientNetV2B0", "The model to benchmark.")
3333
flags.DEFINE_integer("epochs", 1, "The number of epochs.")

benchmarks/torch_ctl_benchmark/conv_model_benchmark.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import torch.nn as nn
1010
import torch.optim as optim
1111

12-
import keras_core
12+
import keras
1313
from benchmarks.torch_ctl_benchmark.benchmark_utils import train_loop
14-
from keras_core import layers
14+
from keras import layers
1515

1616
num_classes = 2
1717
input_shape = (3, 256, 256)
@@ -55,8 +55,8 @@ def forward(self, x):
5555
return x
5656

5757

58-
def run_keras_core_custom_training_loop():
59-
keras_model = keras_core.Sequential(
58+
def run_keras_custom_training_loop():
59+
keras_model = keras.Sequential(
6060
[
6161
layers.Input(shape=input_shape),
6262
layers.Conv2D(32, kernel_size=(3, 3), activation="relu"),
@@ -74,7 +74,7 @@ def run_keras_core_custom_training_loop():
7474
num_epochs=num_epochs,
7575
optimizer=optimizer,
7676
loss_fn=loss_fn,
77-
framework="keras_core",
77+
framework="keras",
7878
)
7979

8080

@@ -93,5 +93,5 @@ def run_torch_custom_training_loop():
9393

9494

9595
if __name__ == "__main__":
96-
run_keras_core_custom_training_loop()
96+
run_keras_custom_training_loop()
9797
run_torch_custom_training_loop()

benchmarks/torch_ctl_benchmark/dense_model_benchmark.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import torch.nn as nn
1010
import torch.optim as optim
1111

12-
import keras_core
12+
import keras
1313
from benchmarks.torch_ctl_benchmark.benchmark_utils import train_loop
14-
from keras_core import layers
14+
from keras import layers
1515

1616
num_classes = 2
1717
input_shape = (8192,)
@@ -55,8 +55,8 @@ def forward(self, x):
5555
return x
5656

5757

58-
def run_keras_core_custom_training_loop():
59-
keras_model = keras_core.Sequential(
58+
def run_keras_custom_training_loop():
59+
keras_model = keras.Sequential(
6060
[
6161
layers.Input(shape=input_shape),
6262
layers.Dense(64, activation="relu"),
@@ -73,7 +73,7 @@ def run_keras_core_custom_training_loop():
7373
num_epochs=num_epochs,
7474
optimizer=optimizer,
7575
loss_fn=loss_fn,
76-
framework="keras_core",
76+
framework="keras",
7777
)
7878

7979

@@ -92,5 +92,5 @@ def run_torch_custom_training_loop():
9292

9393

9494
if __name__ == "__main__":
95-
run_keras_core_custom_training_loop()
95+
run_keras_custom_training_loop()
9696
run_torch_custom_training_loop()

0 commit comments

Comments
 (0)