TensorFlow framework ML của Google. Keras API deep learning Python trên TensorFlow. Functional API xây dựng đồ thị lớp DAG.
Sử dụng Google Colab. Code đầy đủ quy trình: tải MNIST, tiền xử lý, biên dịch, huấn luyện, đánh giá:
Ví dụ
print("Tải dữ liệu MNIST")
print("Chia dữ liệu huấn luyện và kiểm tra")
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
print("Reshape dữ liệu cho huấn luyện tốt hơn")
x_train = x_train.reshape(60000, 784).astype("float32") / 255
x_test = x_test.reshape(10000, 784).astype("float32") / 255
print("Biên dịch mô hình")
model.compile(
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
optimizer=keras.optimizers.RMSprop(),
metrics=["accuracy"],
)
print("Huấn luyện mô hình")
history = model.fit(x_train, y_train, batch_size=64, epochs=2, validation_split=0.2)
test_scores = model.evaluate(x_test, y_test, verbose=2)
print("Loss của mô hình:", test_scores[0])
print("Accuracy của mô hình:", test_scores[1])
Nguồn: TensorFlow Guide
Đầu ra
Tải dữ liệu MNIST Chia dữ liệu huấn luyện và kiểm tra Reshape dữ liệu cho huấn luyện tốt hơn Biên dịch mô hình Huấn luyện mô hình Epoch 1/2 750/750 [==============================] - 3s 3ms/step - loss: 0.5768 - accuracy: 0.8394 - val_loss: 0.2015 - val_accuracy: 0.9405 Epoch 2/2 750/750 [==============================] - 2s 3ms/step - loss: 0.1720 - accuracy: 0.9495 - val_loss: 0.1462 - val_accuracy: 0.9580 313/313 - 0s - loss: 0.1433 - accuracy: 0.9584 Loss của mô hình: 0.14328785240650177 Accuracy của mô hình: 0.9584000110626221
Giải thích
- Tải dữ liệu MNIST.
- Chia tập train/test.
- Reshape và chuẩn hóa dữ liệu.
- Biên dịch mô hình (loss, optimizer, metrics).
- Huấn luyện với
fit. - Đánh giá trên tập test với
evaluate, in loss và accuracy.