tf.data加载NumPy
代码
"""
Created on 2020/11/20 16:33
@Author: CY
@email: 5844104706@qq.com
"""
import numpy as np
import tensorflow as tf
import tensorflow_datasets as tfds
print("#1. 加载数据")
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']
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)
print("2.建立和训练模型")
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)
model.evaluate(test_dataset)
结果
#1. 加载数据
2.建立和训练模型
Epoch 1/10
938/938 [==============================] - 1s 1ms/step - loss: 3.4181 - sparse_categorical_accuracy: 0.8768
Epoch 2/10
938/938 [==============================] - 1s 1ms/step - loss: 0.5796 - sparse_categorical_accuracy: 0.9224
Epoch 3/10
938/938 [==============================] - 1s 1ms/step - loss: 0.4212 - sparse_categorical_accuracy: 0.9424
Epoch 4/10
938/938 [==============================] - 1s 1ms/step - loss: 0.3490 - sparse_categorical_accuracy: 0.9521
Epoch 5/10
938/938 [==============================] - 1s 1ms/step - loss: 0.2969 - sparse_categorical_accuracy: 0.9582
Epoch 6/10
938/938 [==============================] - 1s 1ms/step - loss: 0.2884 - sparse_categorical_accuracy: 0.9623
Epoch 7/10
938/938 [==============================] - 1s 1ms/step - loss: 0.2572 - sparse_categorical_accuracy: 0.9669
Epoch 8/10
938/938 [==============================] - 1s 1ms/step - loss: 0.2216 - sparse_categorical_accuracy: 0.9708
Epoch 9/10
938/938 [==============================] - 1s 1ms/step - loss: 0.2193 - sparse_categorical_accuracy: 0.9723
Epoch 10/10
938/938 [==============================] - 1s 1ms/step - loss: 0.2087 - sparse_categorical_accuracy: 0.9740
157/157 [==============================] - 0s 597us/step - loss: 0.6257 - sparse_categorical_accuracy: 0.9610
Process finished with exit code 0