68 lines
2.3 KiB
Python
68 lines
2.3 KiB
Python
"""
|
|
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()
|
|
|