View on TensorFlow.org
|
Run in Google Colab
|
View source on GitHub
|
Download notebook
|
This tutorial provides an example of loading data from NumPy arrays into a tf.data.Dataset.
This example loads the MNIST dataset from a .npz file. However, the source of the NumPy arrays is not important.
Setup
import numpy as np
import tensorflow as tf
2025-12-01 08:37:36.241605: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:467] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered WARNING: All log messages before absl::InitializeLog() is called are written to STDERR E0000 00:00:1764578256.266252 382952 cuda_dnn.cc:8579] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered E0000 00:00:1764578256.273818 382952 cuda_blas.cc:1407] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered W0000 00:00:1764578256.292643 382952 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once. W0000 00:00:1764578256.292669 382952 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once. W0000 00:00:1764578256.292671 382952 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once. W0000 00:00:1764578256.292674 382952 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.
Load from .npz file
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']
Load NumPy arrays with tf.data.Dataset
Assuming you have an array of examples and a corresponding array of labels, pass the two arrays as a tuple into tf.data.Dataset.from_tensor_slices to create a 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))
I0000 00:00:1764578260.988159 382952 gpu_device.cc:2019] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 13638 MB memory: -> device: 0, name: Tesla T4, pci bus id: 0000:00:05.0, compute capability: 7.5 I0000 00:00:1764578260.990477 382952 gpu_device.cc:2019] Created device /job:localhost/replica:0/task:0/device:GPU:1 with 13756 MB memory: -> device: 1, name: Tesla T4, pci bus id: 0000:00:06.0, compute capability: 7.5 I0000 00:00:1764578260.992652 382952 gpu_device.cc:2019] Created device /job:localhost/replica:0/task:0/device:GPU:2 with 13756 MB memory: -> device: 2, name: Tesla T4, pci bus id: 0000:00:07.0, compute capability: 7.5 I0000 00:00:1764578260.995006 382952 gpu_device.cc:2019] Created device /job:localhost/replica:0/task:0/device:GPU:3 with 13756 MB memory: -> device: 3, name: Tesla T4, pci bus id: 0000:00:08.0, compute capability: 7.5
Use the datasets
Shuffle and batch the datasets
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)
Build and train a model
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10)
])
model.compile(optimizer=tf.keras.optimizers.RMSprop(),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['sparse_categorical_accuracy'])
/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)
model.fit(train_dataset, epochs=10)
Epoch 1/10 WARNING: All log messages before absl::InitializeLog() is called are written to STDERR I0000 00:00:1764578262.725289 383118 service.cc:152] XLA service 0x7efa64005330 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices: I0000 00:00:1764578262.725320 383118 service.cc:160] StreamExecutor device (0): Tesla T4, Compute Capability 7.5 I0000 00:00:1764578262.725324 383118 service.cc:160] StreamExecutor device (1): Tesla T4, Compute Capability 7.5 I0000 00:00:1764578262.725326 383118 service.cc:160] StreamExecutor device (2): Tesla T4, Compute Capability 7.5 I0000 00:00:1764578262.725335 383118 service.cc:160] StreamExecutor device (3): Tesla T4, Compute Capability 7.5 I0000 00:00:1764578262.810544 383118 cuda_dnn.cc:529] Loaded cuDNN version 91600 80/938 ━━━━━━━━━━━━━━━━━━━━ 1s 2ms/step - loss: 33.2599 - sparse_categorical_accuracy: 0.6180 I0000 00:00:1764578263.427021 383118 device_compiler.h:188] Compiled cluster using XLA! This line is logged at most once for the lifetime of the process. 938/938 ━━━━━━━━━━━━━━━━━━━━ 4s 3ms/step - loss: 8.3610 - sparse_categorical_accuracy: 0.8231 Epoch 2/10 938/938 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - loss: 0.5359 - sparse_categorical_accuracy: 0.9176 Epoch 3/10 938/938 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - loss: 0.3971 - sparse_categorical_accuracy: 0.9380 Epoch 4/10 938/938 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - loss: 0.3160 - sparse_categorical_accuracy: 0.9518 Epoch 5/10 938/938 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - loss: 0.2804 - sparse_categorical_accuracy: 0.9571 Epoch 6/10 938/938 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - loss: 0.2402 - sparse_categorical_accuracy: 0.9613 Epoch 7/10 938/938 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - loss: 0.2256 - sparse_categorical_accuracy: 0.9659 Epoch 8/10 938/938 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - loss: 0.2261 - sparse_categorical_accuracy: 0.9674 Epoch 9/10 938/938 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - loss: 0.2035 - sparse_categorical_accuracy: 0.9708 Epoch 10/10 938/938 ━━━━━━━━━━━━━━━━━━━━ 2s 2ms/step - loss: 0.1763 - sparse_categorical_accuracy: 0.9735 <keras.src.callbacks.history.History at 0x7efd6809a1c0>
model.evaluate(test_dataset)
157/157 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - loss: 0.5675 - sparse_categorical_accuracy: 0.9495 [0.527810275554657, 0.953499972820282]
View on TensorFlow.org
Run in Google Colab
View source on GitHub
Download notebook