使用 tf.data 加载 NumPy 数据

在 Tensorflow.org 上查看 在 Google Colab 运行 在 Github 上查看源代码 下载此 notebook

本教程提供了一个将数据从 NumPy 数组加载到 tf.data.Dataset 中的示例。

此示例从 .npz 文件加载 MNIST 数据集。但是,NumPy 数组的来源并不重要。

安装

import numpy as np
import tensorflow as tf
import tensorflow_datasets as tfds

.npz 文件中加载

DATA_URL = 'https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz'

path = tf.keras.utils.get_file('mnist.npz', DATA_URL)
with np.load(path) as data:
  train_examples = data['x_train']
  train_labels = data['y_train']
  test_examples = data['x_test']
  test_labels = data['y_test']

使用 tf.data.Dataset 加载 NumPy 数组

假设您有一个示例数组和相应的标签数组,请将两个数组作为元组传递给 tf.data.Dataset.from_tensor_slices 以创建 tf.data.Dataset

train_dataset = tf.data.Dataset.from_tensor_slices((train_examples, train_labels))
test_dataset = tf.data.Dataset.from_tensor_slices((test_examples, test_labels))
2021-08-13 23:48:26.031255: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-08-13 23:48:26.039463: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-08-13 23:48:26.040417: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-08-13 23:48:26.042270: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 AVX512F FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-08-13 23:48:26.042867: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-08-13 23:48:26.043752: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-08-13 23:48:26.044583: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-08-13 23:48:26.660908: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-08-13 23:48:26.662062: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-08-13 23:48:26.662958: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2021-08-13 23:48:26.663809: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 14648 MB memory:  -> device: 0, name: Tesla V100-SXM2-16GB, pci bus id: 0000:00:05.0, compute capability: 7.0

使用该数据集

打乱和批次化数据集

BATCH_SIZE = 64
SHUFFLE_BUFFER_SIZE = 100

train_dataset = train_dataset.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE)
test_dataset = test_dataset.batch(BATCH_SIZE)

建立和训练模型

model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer=tf.keras.optimizers.RMSprop(),
                loss=tf.keras.losses.SparseCategoricalCrossentropy(),
                metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])
model.fit(train_dataset, epochs=10)
Epoch 1/10
2021-08-13 23:48:27.436689: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:185] None of the MLIR Optimization Passes are enabled (registered 2)
938/938 [==============================] - 3s 2ms/step - loss: 3.0270 - sparse_categorical_accuracy: 0.8735
Epoch 2/10
938/938 [==============================] - 2s 2ms/step - loss: 0.5187 - sparse_categorical_accuracy: 0.9259
Epoch 3/10
938/938 [==============================] - 2s 2ms/step - loss: 0.3706 - sparse_categorical_accuracy: 0.9442
Epoch 4/10
938/938 [==============================] - 2s 2ms/step - loss: 0.3168 - sparse_categorical_accuracy: 0.9516
Epoch 5/10
938/938 [==============================] - 2s 2ms/step - loss: 0.2831 - sparse_categorical_accuracy: 0.9584
Epoch 6/10
938/938 [==============================] - 2s 2ms/step - loss: 0.2564 - sparse_categorical_accuracy: 0.9633
Epoch 7/10
938/938 [==============================] - 2s 2ms/step - loss: 0.2424 - sparse_categorical_accuracy: 0.9650
Epoch 8/10
938/938 [==============================] - 2s 2ms/step - loss: 0.2315 - sparse_categorical_accuracy: 0.9686
Epoch 9/10
938/938 [==============================] - 2s 2ms/step - loss: 0.2124 - sparse_categorical_accuracy: 0.9715
Epoch 10/10
938/938 [==============================] - 2s 2ms/step - loss: 0.1988 - sparse_categorical_accuracy: 0.9723
<keras.callbacks.History at 0x7fdd50446f90>
model.evaluate(test_dataset)
157/157 [==============================] - 0s 2ms/step - loss: 0.6166 - sparse_categorical_accuracy: 0.9525
[0.616615891456604, 0.9524999856948853]