这是一个笔记本文件。 Python程序可以直接在浏览器中运行,这是学习 Tensorflow 的绝佳方式。
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
载入并准备好 MNIST 数据集。将样本从整数转换为浮点数: Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz 11493376/11490434 [==============================] - 0s 0us/step 11501568/11490434 [==============================] - 0s 0us/step
将模型的各层堆叠起来,以搭建 tf.keras.Sequential 模型。为训练选择优化器和损失函数:
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
2022-09-19 12:15:32.369186: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2022-09-19 12:15:32.474845: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2022-09-19 12:15:32.475981: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2022-09-19 12:15:32.478199: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 AVX512F FMA To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2022-09-19 12:15:32.478543: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2022-09-19 12:15:32.479547: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2022-09-19 12:15:32.480623: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2022-09-19 12:15:34.953752: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2022-09-19 12:15:34.954559: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2022-09-19 12:15:34.955209: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:937] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2022-09-19 12:15:34.955812: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 15401 MB memory: -> device: 0, name: Tesla P100-PCIE-16GB, pci bus id: 0000:00:04.0, compute capability: 6.0
训练并验证模型:
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=20, validation_split=0.3, batch_size=128)
model.evaluate(x_test, y_test)
2022-09-19 12:15:35.888721: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:185] None of the MLIR Optimization Passes are enabled (registered 2)
Epoch 1/20 329/329 [==============================] - 3s 5ms/step - loss: 0.4615 - accuracy: 0.8692 - val_loss: 0.2390 - val_accuracy: 0.9338 Epoch 2/20 329/329 [==============================] - 1s 3ms/step - loss: 0.2256 - accuracy: 0.9351 - val_loss: 0.1851 - val_accuracy: 0.9473 Epoch 3/20 329/329 [==============================] - 1s 3ms/step - loss: 0.1717 - accuracy: 0.9502 - val_loss: 0.1538 - val_accuracy: 0.9542 Epoch 4/20 329/329 [==============================] - 1s 3ms/step - loss: 0.1404 - accuracy: 0.9585 - val_loss: 0.1330 - val_accuracy: 0.9608 Epoch 5/20 329/329 [==============================] - 1s 3ms/step - loss: 0.1210 - accuracy: 0.9639 - val_loss: 0.1206 - val_accuracy: 0.9642 Epoch 6/20 329/329 [==============================] - 1s 3ms/step - loss: 0.1032 - accuracy: 0.9695 - val_loss: 0.1149 - val_accuracy: 0.9663
···
Epoch 18/20 329/329 [==============================] - 1s 3ms/step - loss: 0.0383 - accuracy: 0.9884 - val_loss: 0.0920 - val_accuracy: 0.9758 Epoch 19/20 329/329 [==============================] - 1s 3ms/step - loss: 0.0328 - accuracy: 0.9900 - val_loss: 0.0927 - val_accuracy: 0.9747 Epoch 20/20 329/329 [==============================] - 1s 3ms/step - loss: 0.0305 - accuracy: 0.9900 - val_loss: 0.0911 - val_accuracy: 0.9761
313/313 [==============================] - 1s 2ms/step - loss: 0.0725 - accuracy: 0.9788 [0.07254102826118469, 0.9787999987602234]