Mô tả
Load model đã lưu, đánh giá (evaluate) trên test set, làm prediction, so sánh với model mới train từ đầu.
Code ví dụ hoàn chỉnh
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np
# Chuẩn bị data mẫu (MNIST subset)
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
x_train = x_train.reshape(-1, 784).astype('float32') / 255.0
x_test = x_test.reshape(-1, 784).astype('float32') / 255.0
y_train = y_train.astype('int32')
y_test = y_test.astype('int32')
# Chỉ dùng subset để demo nhanh
x_train, y_train = x_train[:1000], y_train[:1000]
x_test, y_test = x_test[:1000], y_test[:1000]
def create_model():
model = keras.Sequential([
layers.Dense(64, activation='relu', input_shape=(784,)),
layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
# 1. TẠO VÀ TRAIN MODEL GỐC
model = create_model()
model.fit(x_train, y_train, epochs=3, verbose=0)
# 2. LƯU MODEL
model.save('saved_model.h5')
# 3. LOAD MODEL (RESTORED)
restored_model = keras.models.load_model('saved_model.h5')
# 4. ĐÁNH GIÁ MODEL RESTORED
print("Evaluating restored model...")
loss, acc = restored_model.evaluate(x_test, y_test, verbose=0)
print(f"Restored model - Loss: {loss:.4f}, Accuracy: {acc:.4f} ({acc*100:.2f}%)")
# 5. PREDICTION
predictions = restored_model.predict(x_test[:5], verbose=0)
print(f"Predictions shape: {predictions.shape}") # (5, 10)
print(f"Predicted classes: {np.argmax(predictions, axis=1)}")
print(f"True labels: {y_test[:5]}")
# 6. SO SÁNH VỚI MODEL MỚI TRAIN TỪ ĐẦU (UNTRANINED)
new_model = create_model()
loss_untrained, acc_untrained = new_model.evaluate(x_test, y_test, verbose=0)
print(f"\nUntrained model - Loss: {loss_untrained:.4f}, Accuracy: {acc_untrained:.4f} ({acc_untrained*100:.2f}%)")
# 7. TRAIN MODEL MỚI
new_model.fit(x_train, y_train, epochs=3, verbose=0)
loss_retrained, acc_retrained = new_model.evaluate(x_test, y_test, verbose=0)
print(f"Retrained model - Loss: {loss_retrained:.4f}, Accuracy: {acc_retrained:.4f} ({acc_retrained*100:.2f}%)")
Đầu ra mẫu
Evaluating restored model... Restored model - Loss: 0.2134, Accuracy: 0.9450 (94.50%) Predictions shape: (5, 10) Predicted classes: [7 2 1 0 4] True labels: [7 2 1 0 4] Untrained model - Loss: 2.3026, Accuracy: 0.0980 (9.80%) Retrained model - Loss: 0.2012, Accuracy: 0.9470 (94.70%)
Giải thích các bước
- create_model(): Hàm factory tạo model architecture.
- model.fit(): Train model gốc.
- model.save(): Lưu toàn bộ (architecture + weights + optimizer).
- load_model(): Khôi phục hoàn toàn model.
- evaluate(): Tính loss & metrics trên test set (không update weights).
- predict(): Forward pass, trả về probabilities/logits.
- So sánh: Restored model ≈ Retrained model (độ chênh do random seed). Untrained model ≈ random guess (10% accuracy cho 10 classes).
Evaluate vs Predict
| Phương thức | Mục đích | Trả về | Cập nhật weights |
|---|---|---|---|
evaluate() | Đo performance | Loss, metrics | Không |
predict() | Inference | Predictions (probs) | Không |
fit() | Training | History | Có |