Membuat Neural Network Regression sangat mudah dengan Keras.
import tensorflow as tf
import numpy as np
# 1. Siapkan Data
X = np.array([1, 2, 3, 4, 5], dtype=float)
y = np.array([50, 60, 70, 80, 90], dtype=float)
# 2. Buat Model Arsitektur
model = tf.keras.Sequential([
# Hidden Layer (Bisa menangkap pola kompleks)
tf.keras.layers.Dense(units=10, activation='relu', input_shape=[1]),
# Output Layer (1 Unit, Tanpa Aktivasi/Linear)
# PENTING: Jangan pakai sigmoid/softmax di sini untuk regresi!
tf.keras.layers.Dense(units=1)
])
# 3. Compile (Tentukan Optimizer & Loss)
model.compile(optimizer='adam', loss='mean_squared_error')
# 4. Training (Belajar)
model.fit(X, y, epochs=500)
# 5. Prediksi
hasil = model.predict([3.5])
print(f"Prediksi untuk 3.5 jam: {hasil[0][0]:.2f}")