TensorFlow 2 初学者快速入门

在 TensorFlow.org 上查看 在 Google Colab 中运行 在 GitHub 上查看源码 下载笔记本

这份简短的介绍使用了 Keras

  1. 加载预构建的数据集。
  2. 构建一个用于图像分类的神经网络机器学习模型。
  3. 训练该神经网络。
  4. 评估模型的准确率。

本教程是一个 Google Colaboratory 笔记本。Python 程序直接在浏览器中运行——这是学习和使用 TensorFlow 的绝佳方式。要学习本教程,请点击本页顶部的按钮在 Google Colab 中运行该笔记本。

  1. 在 Colab 中,连接到 Python 运行时:在菜单栏的右上方,选择 连接 (CONNECT)
  2. 要运行笔记本中的所有代码,请选择 运行时 (Runtime) > 全部运行 (Run all)。要逐个运行代码单元,请将鼠标悬停在每个单元上,然后选择 运行单元 (Run cell) 图标。

Run cell icon

设置 TensorFlow

将 TensorFlow 导入您的程序以开始使用

import tensorflow as tf
print("TensorFlow version:", tf.__version__)
2024-08-16 07:45:15.387747: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2024-08-16 07:45:15.408731: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2024-08-16 07:45:15.415209: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
TensorFlow version: 2.17.0

如果您是在自己的开发环境中学习,而不是使用 Colab,请参阅安装指南以设置 TensorFlow 进行开发。

加载数据集

加载并准备 MNIST 数据集。图像的像素值范围为 0 到 255。通过将这些值除以 255.0,将其缩放到 0 到 1 的范围。这也将样本数据从整数转换为浮点数。

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

构建机器学习模型

构建一个 tf.keras.Sequential 模型

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10)
])
/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/keras/src/layers/reshaping/flatten.py:37: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.
  super().__init__(**kwargs)
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1723794318.490455  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.494342  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.497584  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.501312  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.512702  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.516197  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.519187  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.522647  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.526047  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.529503  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.532428  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794318.535893  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.771712  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.773840  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.775826  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.777872  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.779874  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.781821  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.783693  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.785644  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.787540  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.789499  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.791369  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.793317  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.831749  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.833814  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.835738  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.837736  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.839701  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.841655  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.843526  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.845500  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.847443  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.849923  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.852250  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
I0000 00:00:1723794319.854736  241277 cuda_executor.cc:1015] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355

Sequential 对于堆叠各层非常有用,其中每一层都有一个输入 张量 (tensor) 和一个输出张量。层是具有已知数学结构的函数,可以重用并拥有可训练的变量。大多数 TensorFlow 模型都是由层组成的。此模型使用了 FlattenDenseDropout 层。

对于每个示例,模型都会返回一个 logits(逻辑值)或 log-odds 分数向量,每个类别对应一个分数。

predictions = model(x_train[:1]).numpy()
predictions
array([[ 0.68130803, -0.03935227, -0.53304887,  0.22200397, -0.3079031 ,
        -0.6267688 ,  0.43393654,  0.5691322 ,  0.31098977,  0.32141146]],
      dtype=float32)

tf.nn.softmax 函数将这些逻辑值转换为每个类别的 概率

tf.nn.softmax(predictions).numpy()
array([[0.16339162, 0.07947874, 0.04851112, 0.10321827, 0.06076043,
        0.0441712 , 0.12758444, 0.14605366, 0.11282429, 0.11400625]],
      dtype=float32)

使用 losses.SparseCategoricalCrossentropy 定义用于训练的损失函数

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)

损失函数接收一个真实值向量和一个逻辑值向量,并为每个示例返回一个标量损失。此损失等于真实类别的负对数概率:如果模型确定正确的类别,则损失为零。

这个未训练的模型给出的概率接近随机(每个类别 1/10),因此初始损失应该接近 -tf.math.log(1/10) ~= 2.3

loss_fn(y_train[:1], predictions).numpy()
3.1196823

在开始训练之前,使用 Keras Model.compile 配置并编译模型。将 optimizer 类设置为 adam,将 loss 设置为您之前定义的 loss_fn 函数,并通过将 metrics 参数设置为 accuracy 来指定要评估模型的指标。

model.compile(optimizer='adam',
              loss=loss_fn,
              metrics=['accuracy'])

训练并评估您的模型

使用 Model.fit 方法来调整模型参数并最小化损失

model.fit(x_train, y_train, epochs=5)
Epoch 1/5
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1723794322.305243  241442 service.cc:146] XLA service 0x7effb8008d30 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
I0000 00:00:1723794322.305276  241442 service.cc:154]   StreamExecutor device (0): Tesla T4, Compute Capability 7.5
I0000 00:00:1723794322.305281  241442 service.cc:154]   StreamExecutor device (1): Tesla T4, Compute Capability 7.5
I0000 00:00:1723794322.305284  241442 service.cc:154]   StreamExecutor device (2): Tesla T4, Compute Capability 7.5
I0000 00:00:1723794322.305287  241442 service.cc:154]   StreamExecutor device (3): Tesla T4, Compute Capability 7.5
112/1875 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.6089 - loss: 1.3300
I0000 00:00:1723794323.392324  241442 device_compiler.h:188] Compiled cluster using XLA!  This line is logged at most once for the lifetime of the process.
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 4s 1ms/step - accuracy: 0.8622 - loss: 0.4811
Epoch 2/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9547 - loss: 0.1539
Epoch 3/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9676 - loss: 0.1107
Epoch 4/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9738 - loss: 0.0843
Epoch 5/5
1875/1875 ━━━━━━━━━━━━━━━━━━━━ 2s 1ms/step - accuracy: 0.9764 - loss: 0.0769
<keras.src.callbacks.history.History at 0x7f0184ec7490>

Model.evaluate 方法会检查模型的性能,通常是在 验证集测试集 上进行。

model.evaluate(x_test,  y_test, verbose=2)
313/313 - 1s - 3ms/step - accuracy: 0.9782 - loss: 0.0729
[0.07293704897165298, 0.9782000184059143]

现在,图像分类器已在此数据集上训练到约 98% 的准确率。要了解更多信息,请阅读 TensorFlow 教程

如果您希望模型返回概率,您可以封装训练好的模型,并为其附加一个 softmax 层。

probability_model = tf.keras.Sequential([
  model,
  tf.keras.layers.Softmax()
])
probability_model(x_test[:5])
<tf.Tensor: shape=(5, 10), dtype=float32, numpy=
array([[1.5427084e-07, 7.5027339e-11, 3.1343968e-06, 4.6326011e-05,
        8.9990645e-13, 1.5266414e-07, 2.0456495e-13, 9.9994934e-01,
        2.1858141e-07, 7.8530559e-07],
       [1.7771253e-08, 8.4947787e-05, 9.9989736e-01, 1.8331458e-06,
        8.3026415e-15, 3.4793761e-08, 6.2480517e-08, 7.9319728e-12,
        1.5733674e-05, 3.5440111e-15],
       [3.3602277e-07, 9.9804592e-01, 5.7737787e-05, 5.8099768e-06,
        6.3599517e-05, 2.3768812e-06, 2.3459031e-06, 1.6781164e-03,
        1.4260423e-04, 1.0617223e-06],
       [9.9997318e-01, 8.7561805e-11, 9.8983969e-07, 9.0878149e-10,
        1.0803159e-07, 3.3033965e-07, 2.3622524e-05, 6.7567669e-07,
        4.7765565e-09, 1.1131582e-06],
       [1.1404303e-05, 2.4895797e-09, 6.0792736e-06, 4.9114313e-08,
        9.9449867e-01, 5.9158310e-06, 2.9842497e-05, 4.8574508e-05,
        8.5193824e-06, 5.3910208e-03]], dtype=float32)>

结论

恭喜!您已经使用 Keras API 利用预构建数据集训练出了一个机器学习模型。

有关使用 Keras 的更多示例,请查看 教程。要了解有关使用 Keras 构建模型的更多信息,请阅读 指南。如果您想了解更多关于加载和准备数据的信息,请参阅关于 加载图像数据加载 CSV 数据 的教程。