Compare commits

...

3 Commits

Author SHA1 Message Date
bytewizdl-dot
7492704c8e Push task 2 2026-09-20 10:33:27 +07:00
bytewizdl-dot
fbde479958 Task 2 update push 2026-09-20 10:30:51 +07:00
bytewizdl-dot
e6e31080a1 Task 2 - push 2026-09-20 10:23:20 +07:00
5 changed files with 350 additions and 0 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/Scripts/python.exe"
}

BIN
task-2-Regulation/image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

View File

@ -0,0 +1,123 @@
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import matplotlib.pyplot as plt
# 1. LOAD & PREPROCESS MNIST DATASET
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0
x_train = x_train.reshape(-1, 784)
x_test = x_test.reshape(-1, 784)
# Model C Architecture from Task 1
BEST_ARCHITECTURE = [256, 128, 64]
EPOCHS = 20 # 20 epochs gives enough room to observe overfitting
BATCH_SIZE = 128
# ============================================================
# MODEL 1: WITHOUT REGULARIZATION (Baseline Model C from Task 1)
# ============================================================
model_base = keras.Sequential([
layers.Input(shape=(784,)),
layers.Dense(256, activation="relu"),
layers.Dense(128, activation="relu"),
layers.Dense(64, activation="relu"),
layers.Dense(10, activation="softmax")
])
model_base.compile(
optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"]
)
print("=" * 60)
print("TRAINING BASELINE MODEL (WITHOUT REGULARIZATION)")
print("=" * 60)
history_base = model_base.fit(
x_train, y_train,
epochs=EPOCHS,
batch_size=BATCH_SIZE,
validation_split=0.1,
verbose=1
)
# ============================================================
# MODEL 2: WITH REGULARIZATION (Batch Normalization + Dropout 0.3)
# ============================================================
model_reg = keras.Sequential([
layers.Input(shape=(784,)),
layers.Dense(256),
layers.BatchNormalization(),
layers.Activation("relu"),
layers.Dropout(0.3),
layers.Dense(128),
layers.BatchNormalization(),
layers.Activation("relu"),
layers.Dropout(0.3),
layers.Dense(64),
layers.BatchNormalization(),
layers.Activation("relu"),
layers.Dropout(0.3),
layers.Dense(10, activation="softmax")
])
model_reg.compile(
optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"]
)
print("\n" + "=" * 60)
print("TRAINING REGULARIZED MODEL (WITH BATCHNORM + DROPOUT)")
print("=" * 60)
history_reg = model_reg.fit(
x_train, y_train,
epochs=EPOCHS,
batch_size=BATCH_SIZE,
validation_split=0.1,
verbose=1
)
# Optional: Save the trained regularized model to disk
model_reg.save("model_c_regularized.keras")
print("\n[INFO] Regularized model saved as 'model_c_regularized.keras' in your project directory.")
# ============================================================
# PLOTTING LOSS CURVES FOR COMPARISON
# ============================================================
plt.figure(figsize=(14, 5))
# Plot 1: Baseline (Without Regularization)
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)
# Plot 2: Regularized (With BatchNorm + Dropout)
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)
plt.tight_layout()
plt.show()

Binary file not shown.

224
task1-models/codingan.py Normal file
View File

@ -0,0 +1,224 @@
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import time
# ============================================================
# 1. LOAD DATASET MNIST
# ============================================================
print("=" * 60)
print("MEMUAT DATASET MNIST")
print("=" * 60)
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
print(f"Data training : {x_train.shape}")
print(f"Data testing : {x_test.shape}")
# ============================================================
# 2. PREPROCESSING
# ============================================================
# Normalisasi nilai pixel dari 0-255 menjadi 0-1
x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0
# Mengubah gambar 28x28 menjadi 784 fitur
x_train = x_train.reshape(-1, 784)
x_test = x_test.reshape(-1, 784)
print(f"Training setelah preprocessing: {x_train.shape}")
print(f"Testing setelah preprocessing : {x_test.shape}")
# ============================================================
# 3. FUNGSI MEMBUAT MODEL
# ============================================================
def create_model(hidden_layers):
model = keras.Sequential()
# Input layer
model.add(layers.Input(shape=(784,)))
# Hidden layers
for neurons in hidden_layers:
model.add(
layers.Dense(
neurons,
activation="relu"
)
)
# Output layer
model.add(
layers.Dense(
10,
activation="softmax"
)
)
model.compile(
optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"]
)
return model
# ============================================================
# 4. KONFIGURASI 3 MODEL
# ============================================================
models_config = {
"Model A - 1 Hidden Layer": [64],
"Model B - 2 Hidden Layer": [128, 64],
"Model C - 3 Hidden Layer": [256, 128, 64]
}
# ============================================================
# 5. TRAINING DAN EVALUASI
# ============================================================
results = []
EPOCHS = 10
BATCH_SIZE = 128
for model_name, architecture in models_config.items():
print("\n")
print("=" * 60)
print(model_name)
print("=" * 60)
# Membuat model
model = create_model(architecture)
# Menampilkan arsitektur
model.summary()
# Training
print("\nMemulai training...")
start_time = time.time()
history = model.fit(
x_train,
y_train,
epochs=EPOCHS,
batch_size=BATCH_SIZE,
validation_split=0.1,
verbose=1
)
end_time = time.time()
# Total waktu training
total_time = end_time - start_time
# Rata-rata waktu per epoch
average_time = total_time / EPOCHS
# Evaluasi menggunakan test data
test_loss, test_accuracy = model.evaluate(
x_test,
y_test,
verbose=0
)
# Jumlah parameter
total_parameters = model.count_params()
# Menyimpan hasil
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")
# ============================================================
# 6. HASIL PERBANDINGAN
# ============================================================
print("\n\n")
print("=" * 80)
print("HASIL PERBANDINGAN 3 MODEL MLP")
print("=" * 80)
print(
f"{'Model':<30}"
f"{'Parameter':>15}"
f"{'Accuracy':>15}"
f"{'Time/Epoch':>15}"
)
print("-" * 80)
for result in results:
print(
f"{result['model']:<30}"
f"{result['parameters']:>15,}"
f"{result['accuracy']:>14.2f}%"
f"{result['time_per_epoch']:>14.2f}s"
)
# ============================================================
# 7. MENENTUKAN MODEL DENGAN ACCURACY TERTINGGI
# ============================================================
best_model = max(
results,
key=lambda x: x["accuracy"]
)
print("\n")
print("=" * 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")
# ============================================================
# 8. KESIMPULAN SINGKAT
# ============================================================
print("\n")
print("=" * 60)
print("KESIMPULAN")
print("=" * 60)
print(
"Model yang lebih dalam memiliki jumlah parameter "
"yang lebih banyak dan umumnya membutuhkan waktu "
"training yang lebih besar."
)
print(
"Namun, model yang lebih dalam tidak selalu menghasilkan "
"peningkatan test accuracy yang signifikan."
)
print(
"Pemilihan arsitektur sebaiknya mempertimbangkan "
"accuracy, jumlah parameter, dan waktu training."
)