Memuat data NumPy

Lihat di TensorFlow.org Jalankan di Google Colab Lihat source di GitHub Unduh notebook

Tutorial ini menunjukkan sebuah contoh bagaimana memuat data dari array NumPy menjadi sebuah tf.data.Dataset.

Conthon ini memuat dataset MNIST dari sebuah file .npz. Akan tetapi, sumber dari array NumPy tidak begitu penting.

Setup

try:
  # %tensorflow_version hanya tersedia di Colab.
  %tensorflow_version 2.x
except Exception:
  pass

from __future__ import absolute_import, division, print_function, unicode_literals

import numpy as np
import tensorflow as tf

Muat dari file .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']

Memuat array NumPy dengan tf.data.Dataset

Dengan asumsi Anda memiliki array dan label yang berkorespondensi dengan array tersebut, buat array dan label tersebut menjadi sebuah tuple dengan menggunakan tf.data.Dataset.from_tensor_slices untuk membuat sebuah 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))

Menggunakan dataset

Menggunakan shuffle dan batch terhadap dataset

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)

Membangun dan melatih 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, 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
938/938 [==============================] - 2s 2ms/step - loss: 3.5820 - sparse_categorical_accuracy: 0.8820
Epoch 2/10
938/938 [==============================] - 2s 2ms/step - loss: 0.5401 - sparse_categorical_accuracy: 0.9298
Epoch 3/10
938/938 [==============================] - 2s 2ms/step - loss: 0.3974 - sparse_categorical_accuracy: 0.9448
Epoch 4/10
938/938 [==============================] - 2s 2ms/step - loss: 0.3283 - sparse_categorical_accuracy: 0.9543
Epoch 5/10
938/938 [==============================] - 2s 2ms/step - loss: 0.2881 - sparse_categorical_accuracy: 0.9602
Epoch 6/10
938/938 [==============================] - 2s 2ms/step - loss: 0.2648 - sparse_categorical_accuracy: 0.9647
Epoch 7/10
938/938 [==============================] - 2s 2ms/step - loss: 0.2489 - sparse_categorical_accuracy: 0.9678
Epoch 8/10
938/938 [==============================] - 2s 2ms/step - loss: 0.2296 - sparse_categorical_accuracy: 0.9711
Epoch 9/10
938/938 [==============================] - 2s 2ms/step - loss: 0.2249 - sparse_categorical_accuracy: 0.9725
Epoch 10/10
938/938 [==============================] - 2s 2ms/step - loss: 0.2045 - sparse_categorical_accuracy: 0.9739

<tensorflow.python.keras.callbacks.History at 0x7fe0e83da7b8>
model.evaluate(test_dataset)
157/157 [==============================] - 0s 2ms/step - loss: 0.6514 - sparse_categorical_accuracy: 0.9532

[0.6513878703117371, 0.9531999826431274]