TensorFlow.org에서 보기 | Google Colab에서 실행하기 | GitHub에서소스 보기 | 노트북 다운로드하기 |
이 튜토리얼은 NumPy 배열에서 tf.data.Dataset
으로 데이터를 로드하는 예제를 제공합니다.
이 예제에서는 .npz
파일에서 MNIST 데이터세트를 로드합니다. 그러나 NumPy 배열의 소스는 중요하지 않습니다.
설정
import numpy as np
import tensorflow as tf
2022-12-14 21:10:19.807402: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer.so.7'; dlerror: libnvinfer.so.7: cannot open shared object file: No such file or directory 2022-12-14 21:10:19.807509: W tensorflow/compiler/xla/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libnvinfer_plugin.so.7'; dlerror: libnvinfer_plugin.so.7: cannot open shared object file: No such file or directory 2022-12-14 21:10:19.807519: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Cannot dlopen some TensorRT libraries. If you would like to use Nvidia GPU with TensorRT, please make sure the missing libraries mentioned above are installed properly.
.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))
데이터세트 사용하기
데이터세트 셔플 및 배치
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)
])
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.6223 - sparse_categorical_accuracy: 0.8792 Epoch 2/10 938/938 [==============================] - 2s 2ms/step - loss: 0.5732 - sparse_categorical_accuracy: 0.9266 Epoch 3/10 938/938 [==============================] - 2s 2ms/step - loss: 0.3837 - sparse_categorical_accuracy: 0.9460 Epoch 4/10 938/938 [==============================] - 2s 2ms/step - loss: 0.3161 - sparse_categorical_accuracy: 0.9568 Epoch 5/10 938/938 [==============================] - 2s 2ms/step - loss: 0.2733 - sparse_categorical_accuracy: 0.9628 Epoch 6/10 938/938 [==============================] - 2s 2ms/step - loss: 0.2418 - sparse_categorical_accuracy: 0.9673 Epoch 7/10 938/938 [==============================] - 2s 2ms/step - loss: 0.2175 - sparse_categorical_accuracy: 0.9707 Epoch 8/10 938/938 [==============================] - 2s 2ms/step - loss: 0.2040 - sparse_categorical_accuracy: 0.9726 Epoch 9/10 938/938 [==============================] - 2s 2ms/step - loss: 0.1928 - sparse_categorical_accuracy: 0.9743 Epoch 10/10 938/938 [==============================] - 2s 2ms/step - loss: 0.1807 - sparse_categorical_accuracy: 0.9773 <keras.callbacks.History at 0x7f42daeda730>
model.evaluate(test_dataset)
157/157 [==============================] - 0s 2ms/step - loss: 0.5589 - sparse_categorical_accuracy: 0.9606 [0.5589265823364258, 0.9606000185012817]