Keras 3.0 介绍
Keras 3.0 升级是对 Keras 的全面重写,引入了一系列令人振奋的新特性,为深度学习领域带来了全新的可能性。
多框架支持
Keras 3.0 的最大亮点之一是支持多框架。Keras 3 实现了完整的 Keras API,并使其可用于 TensorFlow、JAX 和 PyTorch ------ 包括一百多个层、数十种度量标准、损失函数、优化器和回调函数,以及 Keras 的训练和评估循环,以及 Keras 的保存和序列化基础设施。所有您熟悉和喜爱的 API 都在这里。
大规模模型训练和部署
新版本的 Keras 为大规模模型训练和部署提供了全新的能力。借助优化的算法和性能改进,现在您可以处理更大规模、更复杂的深度学习模型,而无需担心性能问题。
使用任何来源的数据管道。
Keras 3 的 fit()
/evaluate()
/predict()
例程兼容 tf.data.Dataset
对象、PyTorch 的 DataLoader
对象、NumPy 数组和 Pandas 数据框,无论您使用的是哪个后端。您可以在 PyTorch 的 DataLoader
上训练 Keras 3 + TensorFlow 模型,或者在 tf.data.Dataset
上训练 Keras 3 + PyTorch 模型。
案例1:搭配Pytorch训练
-
导入环境
import os
This guide can only be run with the torch backend.
os.environ["KERAS_BACKEND"] = "torch"
import torch
import keras
from keras import layers
import numpy as np -
定义模型
在 train_step()
方法的主体中,实现了一个常规的训练更新,类似于您已经熟悉的内容。重要的是,我们通过 self.compute_loss()
计算损失,它包装了传递给 compile()
的损失函数。
class CustomModel(keras.Model):
def train_step(self, data):
# Unpack the data. Its structure depends on your model and
# on what you pass to `fit()`.
x, y = data
# Call torch.nn.Module.zero_grad() to clear the leftover gradients
# for the weights from the previous train step.
self.zero_grad()
# Compute loss
y_pred = self(x, training=True) # Forward pass
loss = self.compute_loss(y=y, y_pred=y_pred)
# Call torch.Tensor.backward() on the loss to compute gradients
# for the weights.
loss.backward()
trainable_weights = [v for v in self.trainable_weights]
gradients = [v.value.grad for v in trainable_weights]
# Update weights
with torch.no_grad():
self.optimizer.apply(gradients, trainable_weights)
# Update metrics (includes the metric that tracks the loss)
for metric in self.metrics:
if metric.name == "loss":
metric.update_state(loss)
else:
metric.update_state(y, y_pred)
# Return a dict mapping metric names to current value
# Note that it will include the loss (tracked in self.metrics).
return {m.name: m.result() for m in self.metrics}
-
训练模型
Construct and compile an instance of CustomModel
inputs = keras.Input(shape=(32,))
outputs = keras.layers.Dense(1)(inputs)
model = CustomModel(inputs, outputs)
model.compile(optimizer="adam", loss="mse", metrics=["mae"])Just use
fit
as usualx = np.random.random((1000, 32))
y = np.random.random((1000, 1))
model.fit(x, y, epochs=3)
案例2:自定义Pytorch流程
https://keras.io/guides/writing_a_custom_training_loop_in_torch/
-
导入环境
import os
This guide can only be run with the torch backend.
os.environ["KERAS_BACKEND"] = "torch"
import torch
import keras
from keras import layers
import numpy as np -
定义模型、加载数据集
Let's consider a simple MNIST model
def get_model():
inputs = keras.Input(shape=(784,), name="digits")
x1 = keras.layers.Dense(64, activation="relu")(inputs)
x2 = keras.layers.Dense(64, activation="relu")(x1)
outputs = keras.layers.Dense(10, name="predictions")(x2)
model = keras.Model(inputs=inputs, outputs=outputs)
return modelCreate load up the MNIST dataset and put it in a torch DataLoader
Prepare the training dataset.
batch_size = 32
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
x_train = np.reshape(x_train, (-1, 784)).astype("float32")
x_test = np.reshape(x_test, (-1, 784)).astype("float32")
y_train = keras.utils.to_categorical(y_train)
y_test = keras.utils.to_categorical(y_test)Reserve 10,000 samples for validation.
x_val = x_train[-10000:]
y_val = y_train[-10000:]
x_train = x_train[:-10000]
y_train = y_train[:-10000]Create torch Datasets
train_dataset = torch.utils.data.TensorDataset(
torch.from_numpy(x_train), torch.from_numpy(y_train)
)
val_dataset = torch.utils.data.TensorDataset(
torch.from_numpy(x_val), torch.from_numpy(y_val)
)Create DataLoaders for the Datasets
train_dataloader = torch.utils.data.DataLoader(
train_dataset, batch_size=batch_size, shuffle=True
)
val_dataloader = torch.utils.data.DataLoader(
val_dataset, batch_size=batch_size, shuffle=False
) -
定义优化器
Instantiate a torch optimizer
model = get_model()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)Instantiate a torch loss function
loss_fn = torch.nn.CrossEntropyLoss()
-
训练模型
epochs = 3
for epoch in range(epochs):
for step, (inputs, targets) in enumerate(train_dataloader):
# Forward pass
logits = model(inputs)
loss = loss_fn(logits, targets)# Backward pass model.zero_grad() loss.backward() # Optimizer variable updates optimizer.step() # Log every 100 batches. if step % 100 == 0: print( f"Training loss (for 1 batch) at step {step}: {loss.detach().numpy():.4f}" ) print(f"Seen so far: {(step + 1) * batch_size} samples")