refactor: restrukturisasi folder eksperimen ke tasks modular

This commit is contained in:
matthewflrnt
2026-09-27 18:50:08 +07:00
parent e827aa0e96
commit fe63a50997
9 changed files with 112 additions and 60 deletions
+137
View File
@@ -0,0 +1,137 @@
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()
+93
View File
@@ -0,0 +1,93 @@
import sys
import os
import matplotlib
matplotlib.use("Agg")
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, create_regularized_mlp_model
from src.utils import ensure_dir
def main():
print("=" * 60)
print("TUGAS 2: EKSPERIMEN REGULARISASI (BASELINE vs BATCHNORM + DROPOUT)")
print("=" * 60)
# 1. Load Data
(x_train, y_train), (x_test, y_test) = load_mnist_for_mlp()
EPOCHS = 20
BATCH_SIZE = 128
# 2. Baseline Model (Without Regularization)
print("\n" + "=" * 60)
print("TRAINING BASELINE MODEL (WITHOUT REGULARIZATION)")
print("=" * 60)
model_base = create_mlp_model([256, 128, 64])
history_base = model_base.fit(
x_train, y_train,
epochs=EPOCHS,
batch_size=BATCH_SIZE,
validation_split=0.1,
verbose=1
)
# 3. Regularized Model (BatchNorm + Dropout 0.3)
print("\n" + "=" * 60)
print("TRAINING REGULARIZED MODEL (WITH BATCHNORM + DROPOUT)")
print("=" * 60)
model_reg = create_regularized_mlp_model(hidden_layers=[256, 128, 64], dropout_rate=0.3)
history_reg = model_reg.fit(
x_train, y_train,
epochs=EPOCHS,
batch_size=BATCH_SIZE,
validation_split=0.1,
verbose=1
)
# 4. Save Model Artifact
results_model_dir = os.path.join("results", "01_mlp", "models")
ensure_dir(results_model_dir)
model_reg_path = os.path.join(results_model_dir, "model_c_regularized.keras")
model_reg.save(model_reg_path)
print(f"\n[INFO] Model teregularisasi disimpan di: {model_reg_path}")
# 5. Plot Comparison Curves
results_fig_dir = os.path.join("results", "01_mlp", "figures")
ensure_dir(results_fig_dir)
fig_path = os.path.join(results_fig_dir, "loss_regularization.png")
plt.figure(figsize=(14, 5))
# Subplot 1: Baseline
plt.subplot(1, 2, 1)
plt.plot(history_base.history['loss'], label='Training Loss', color='blue')
plt.plot(history_base.history['val_loss'], label='Validation Loss', color='orange', linestyle='--')
plt.title('Model C Without Regularization')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.grid(True, alpha=0.3)
# Subplot 2: Regularized
plt.subplot(1, 2, 2)
plt.plot(history_reg.history['loss'], label='Training Loss', color='blue')
plt.plot(history_reg.history['val_loss'], label='Validation Loss', color='green', linestyle='--')
plt.title('Model C With BatchNorm + Dropout(0.3)')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig(fig_path, dpi=130)
print(f"[INFO] Grafik perbandingan loss disimpan di: {fig_path}")
if __name__ == "__main__":
main()
+67
View File
@@ -0,0 +1,67 @@
"""
Tugas 3: Contoh Implementasi Berbagai Arsitektur Model Sesuai Karakteristik Data
"""
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Conv2D, Dense, Embedding, Flatten, LSTM, MaxPooling2D
from tensorflow.keras.models import Sequential
from xgboost import XGBClassifier
def build_models():
print("=" * 60)
print("TUGAS 3: CONTOH ARSITEKTUR MODEL SELECTION")
print("=" * 60)
# (a) Prediksi Stok Barang dari Deret Waktu (LSTM)
# Input shape: (samples, time_steps=12, features=1)
model_a = Sequential([
LSTM(32, input_shape=(12, 1)),
Dense(1)
], name="LSTM_Stock_Forecasting")
model_a.compile(optimizer='adam', loss='mse')
print("\n[Case A] Model LSTM untuk Prediksi Stok Barang (Time Series):")
model_a.summary()
# (b) Klasifikasi Foto Produk Rusak vs Normal (CNN)
# Input shape contoh citra RGB 128x128
model_b = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(128, 128, 3)),
MaxPooling2D((2, 2)),
Flatten(),
Dense(64, activation='relu'),
Dense(1, activation='sigmoid') # binary: rusak vs normal
], name="CNN_Defect_Classification")
model_b.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
print("\n[Case B] Model CNN untuk Klasifikasi Foto Produk (Computer Vision):")
model_b.summary()
# (c) Analisis Sentimen Ulasan Play Store (LSTM + Embedding)
vocab_size = 5000
max_length = 100
model_c = Sequential([
Embedding(input_dim=vocab_size, output_dim=64, input_length=max_length),
LSTM(64),
Dense(32, activation='relu'),
Dense(1, activation='sigmoid') # binary: positif vs negatif
], name="LSTM_Sentiment_Analysis")
model_c.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
print("\n[Case C] Model LSTM + Embedding untuk Analisis Sentimen (NLP):")
model_c.summary()
# (d) Prediksi Churn Pelanggan Tabular (ML Klasik - XGBoost)
# X_train memiliki dimensi (n_samples, 15)
model_d = XGBClassifier(
n_estimators=100,
max_depth=4,
learning_rate=0.1,
random_state=42
)
print("\n[Case D] Model XGBoost untuk Prediksi Churn (Tabular 15 fitur):")
print(model_d)
if __name__ == "__main__":
build_models()