Zobacz na TensorFlow.org | Uruchom w Google Colab | Wyświetl źródło na GitHub | Pobierz notatnik |
Ten samouczek zawiera przykład ładowania danych z tablic NumPy do tf.data.Dataset
.
Ten przykład ładuje zestaw danych MNIST z pliku .npz
. Jednak źródło tablic NumPy nie jest ważne.
Ustawiać
import numpy as np
import tensorflow as tf
Załaduj z pliku .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']
Załaduj tablice NumPy za pomocą tf.data.Dataset
Zakładając, że masz tablicę przykładów i odpowiednią tablicę etykiet, przekaż dwie tablice jako krotkę do tf.data.Dataset.from_tensor_slices
, aby utworzyć 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))
Użyj zbiorów danych
Tasuj i grupuj zbiory danych
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)
Zbuduj i wytrenuj 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'])
model.fit(train_dataset, epochs=10)
Epoch 1/10 938/938 [==============================] - 3s 2ms/step - loss: 3.5318 - sparse_categorical_accuracy: 0.8762 Epoch 2/10 938/938 [==============================] - 2s 2ms/step - loss: 0.5408 - sparse_categorical_accuracy: 0.9289 Epoch 3/10 938/938 [==============================] - 2s 2ms/step - loss: 0.3770 - sparse_categorical_accuracy: 0.9473 Epoch 4/10 938/938 [==============================] - 2s 2ms/step - loss: 0.3281 - sparse_categorical_accuracy: 0.9566 Epoch 5/10 938/938 [==============================] - 2s 2ms/step - loss: 0.2940 - sparse_categorical_accuracy: 0.9621 Epoch 6/10 938/938 [==============================] - 2s 2ms/step - loss: 0.2622 - sparse_categorical_accuracy: 0.9657 Epoch 7/10 938/938 [==============================] - 2s 2ms/step - loss: 0.2446 - sparse_categorical_accuracy: 0.9698 Epoch 8/10 938/938 [==============================] - 2s 2ms/step - loss: 0.2147 - sparse_categorical_accuracy: 0.9739 Epoch 9/10 938/938 [==============================] - 2s 2ms/step - loss: 0.1956 - sparse_categorical_accuracy: 0.9750 Epoch 10/10 938/938 [==============================] - 2s 2ms/step - loss: 0.1964 - sparse_categorical_accuracy: 0.9759 <keras.callbacks.History at 0x7fc7a80beb50>
model.evaluate(test_dataset)
157/157 [==============================] - 0s 2ms/step - loss: 0.7089 - sparse_categorical_accuracy: 0.9572 [0.7088937163352966, 0.9571999907493591]