138 lines
4.5 KiB
Python
138 lines
4.5 KiB
Python
import sys
|
|
import os
|
|
import time
|
|
import pandas as pd
|
|
import matplotlib.pyplot as plt
|
|
|
|
# Tambahkan root directory ke path agar dapat import dari src
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")))
|
|
|
|
from src.data_loader import load_mnist_for_mlp
|
|
from src.models import create_mlp_model
|
|
|
|
|
|
def main():
|
|
print("=" * 60)
|
|
print("TUGAS 1: MEMUAT DATASET MNIST & EKSPERIMEN MLP")
|
|
print("=" * 60)
|
|
|
|
# 1. Load Data
|
|
(x_train, y_train), (x_test, y_test) = load_mnist_for_mlp()
|
|
print(f"Data training : {x_train.shape}")
|
|
print(f"Data testing : {x_test.shape}")
|
|
|
|
# 2. Konfigurasi 3 Model MLP
|
|
models_config = {
|
|
"Model A - 1 Hidden Layer": [64],
|
|
"Model B - 2 Hidden Layer": [128, 64],
|
|
"Model C - 3 Hidden Layer": [256, 128, 64]
|
|
}
|
|
|
|
results = []
|
|
EPOCHS = 10
|
|
BATCH_SIZE = 128
|
|
|
|
# 3. Training & Evaluasi
|
|
for model_name, architecture in models_config.items():
|
|
print("\n" + "=" * 60)
|
|
print(model_name)
|
|
print("=" * 60)
|
|
|
|
model = create_mlp_model(architecture)
|
|
model.summary()
|
|
|
|
print("\nMemulai training...")
|
|
start_time = time.time()
|
|
|
|
model.fit(
|
|
x_train,
|
|
y_train,
|
|
epochs=EPOCHS,
|
|
batch_size=BATCH_SIZE,
|
|
validation_split=0.1,
|
|
verbose=1
|
|
)
|
|
|
|
total_time = time.time() - start_time
|
|
average_time = total_time / EPOCHS
|
|
|
|
test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)
|
|
total_parameters = model.count_params()
|
|
|
|
results.append({
|
|
"model": model_name,
|
|
"parameters": total_parameters,
|
|
"accuracy": test_accuracy * 100,
|
|
"time_per_epoch": average_time
|
|
})
|
|
|
|
print("\nHasil:")
|
|
print(f"Test Accuracy : {test_accuracy * 100:.2f}%")
|
|
print(f"Jumlah Parameter : {total_parameters:,}")
|
|
print(f"Waktu/Epoch : {average_time:.2f} detik")
|
|
|
|
# 4. Ringkasan Perbandingan
|
|
print("\n\n" + "=" * 80)
|
|
print("HASIL PERBANDINGAN 3 MODEL MLP")
|
|
print("=" * 80)
|
|
print(f"{'Model':<30}{'Parameter':>15}{'Accuracy':>15}{'Time/Epoch':>15}")
|
|
print("-" * 80)
|
|
|
|
for res in results:
|
|
print(
|
|
f"{res['model']:<30}"
|
|
f"{res['parameters']:>15,}"
|
|
f"{res['accuracy']:>14.2f}%"
|
|
f"{res['time_per_epoch']:>14.2f}s"
|
|
)
|
|
|
|
best_model = max(results, key=lambda x: x["accuracy"])
|
|
print("\n" + "=" * 60)
|
|
print("MODEL DENGAN TEST ACCURACY TERTINGGI")
|
|
print("=" * 60)
|
|
print(f"Model : {best_model['model']}")
|
|
print(f"Test Accuracy : {best_model['accuracy']:.2f}%")
|
|
print(f"Jumlah Parameter : {best_model['parameters']:,}")
|
|
print(f"Waktu Training/Epoch: {best_model['time_per_epoch']:.2f} detik")
|
|
|
|
# 5. Simpan Hasil ke Folder results/01_mlp
|
|
REPORTS_DIR = os.path.join("results", "01_mlp", "reports")
|
|
FIGURES_DIR = os.path.join("results", "01_mlp", "figures")
|
|
os.makedirs(REPORTS_DIR, exist_ok=True)
|
|
os.makedirs(FIGURES_DIR, exist_ok=True)
|
|
|
|
df_results = pd.DataFrame(results)
|
|
csv_path = os.path.join(REPORTS_DIR, "perbandingan_mlp.csv")
|
|
df_results.to_csv(csv_path, index=False)
|
|
print(f"\n[OK] Ringkasan CSV disimpan ke : {csv_path}")
|
|
|
|
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
|
|
names = ["Model A\n(1 Layer)", "Model B\n(2 Layer)", "Model C\n(3 Layer)"]
|
|
accs = [r["accuracy"] for r in results]
|
|
bars1 = ax1.bar(names, accs, color=["#4C72B0", "#55A868", "#C44E52"])
|
|
ax1.set_title("Perbandingan Test Accuracy (%)", fontsize=12, fontweight="bold")
|
|
ax1.set_ylabel("Accuracy (%)")
|
|
ax1.set_ylim([max(0, min(accs) - 2), 100])
|
|
for bar in bars1:
|
|
yval = bar.get_height()
|
|
ax1.text(bar.get_x() + bar.get_width()/2, yval + 0.2, f"{yval:.2f}%", ha='center', va='bottom')
|
|
|
|
times = [r["time_per_epoch"] for r in results]
|
|
bars2 = ax2.bar(names, times, color=["#4C72B0", "#55A868", "#C44E52"])
|
|
ax2.set_title("Waktu Training per Epoch (detik)", fontsize=12, fontweight="bold")
|
|
ax2.set_ylabel("Detik / Epoch")
|
|
for bar in bars2:
|
|
yval = bar.get_height()
|
|
ax2.text(bar.get_x() + bar.get_width()/2, yval + 0.05, f"{yval:.2f}s", ha='center', va='bottom')
|
|
|
|
plt.tight_layout()
|
|
fig_path = os.path.join(FIGURES_DIR, "perbandingan_3_model_mlp.png")
|
|
plt.savefig(fig_path, dpi=130)
|
|
plt.close()
|
|
print(f"[OK] Grafik perbandingan disimpan ke: {fig_path}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|