Keras 3.0发布:全面拥抱 PyTorch!

Keras 3.0 介绍

https://keras.io/keras_3/

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训练

https://keras.io/guides/custom_train_step_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

  • 定义模型

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 usual

    x = 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 model

    Create 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")
相关推荐
love530love8 小时前
根治 PyTorch CUDA `pynvml` 弃用警告:直接修改 `torch/cuda/__init__.py` 的实践记录
人工智能·pytorch·windows·python·深度学习·机器学习·pynvml
Kobebryant-Manba12 小时前
安装cuda
pytorch·python·深度学习·conda·numpy
盼小辉丶14 小时前
PyTorch强化学习实战(11)——N步DQN(N-step DQN)
pytorch·python·深度学习·强化学习
星越华夏15 小时前
深度学习项目实战:基于PyTorch的图像分类与目标检测(YOLOv8)
pytorch·深度学习·yolo·分类
imDwAaY15 小时前
从感知机到 Attention:我用 PyTorch 打穿 CS188 机器学习终章 CS188 Proj5 学习笔记
人工智能·pytorch·笔记·python·学习·机器学习
zlkingdom1 天前
Jetson Orin开发板,在conda环境中直接实现Pytorch的GPU加速
人工智能·pytorch·conda·随笔·jetson orin
月疯1 天前
PyTorch 中定义了一个 LeakyReLU 激活函数层
人工智能·pytorch·python
Y学院2 天前
PyTorch深度学习框架核心概念精讲
人工智能·pytorch·深度学习
zhangfeng11332 天前
联邦学习 合并权重 合并权重。导致内存溢出解决办法和类库 mergekit 包依赖版本
人工智能·pytorch·机器学习
nashane2 天前
HarmonyOS 6学习:应用无响应(AppFreeze)故障排查与性能优化指南
人工智能·pytorch·python