Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
class Priyo_NeuralNetwork:
|
2 |
+
def __init__(self, input_size, hidden_size, output_size):
|
3 |
+
# Inisialisasi bobot dan bias secara acak
|
4 |
+
self.weights1 = np.random.randn(input_size, hidden_size)
|
5 |
+
self.bias1 = np.zeros(hidden_size)
|
6 |
+
self.weights2 = np.random.randn(hidden_size, output_size)
|
7 |
+
self.bias2 = np.zeros(output_size)
|
8 |
+
|
9 |
+
def sigmoid(self, x):
|
10 |
+
clipped_x = np.clip(x, -5, 5) # Clip values between -5 and 5
|
11 |
+
return 1 / (1 + np.exp(-clipped_x))
|
12 |
+
|
13 |
+
def forward(self, X):
|
14 |
+
# Perhitungan forward propagation
|
15 |
+
self.z1 = np.dot(X, self.weights1) + self.bias1
|
16 |
+
self.a1 = self.sigmoid(self.z1)
|
17 |
+
self.z2 = np.dot(self.a1, self.weights2) + self.bias2
|
18 |
+
self.a2 = self.sigmoid(self.z2)
|
19 |
+
return self.a2
|
20 |
+
|
21 |
+
def backward(self, X, y, learning_rate):
|
22 |
+
m = X.shape[0]
|
23 |
+
dZ2 = self.a2 - y
|
24 |
+
dW2 = 1/m * np.dot(self.a1.T, dZ2)
|
25 |
+
db2 = 1/m * np.sum(dZ2, axis=0)
|
26 |
+
dZ1 = np.dot(dZ2, self.weights2.T) * (1 - self.a1) * self.a1
|
27 |
+
dW1 = 1/m * np.dot(X.T, dZ1)
|
28 |
+
db1 = 1/m * np.sum(dZ1, axis=0)
|
29 |
+
|
30 |
+
return dW1, db1, dW2, db2
|
31 |
+
|
32 |
+
def train(self, X, y, epochs, learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-8):
|
33 |
+
m = X.shape[0]
|
34 |
+
|
35 |
+
# Initialize moments for Adam
|
36 |
+
v_dw1, v_db1, v_dw2, v_db2 = np.zeros_like(self.weights1), np.zeros_like(self.bias1), np.zeros_like(self.weights2), np.zeros_like(self.bias2)
|
37 |
+
s_dw1, s_db1, s_dw2, s_db2 = np.zeros_like(self.weights1), np.zeros_like(self.bias1), np.zeros_like(self.weights2), np.zeros_like(self.bias2)
|
38 |
+
t = 0
|
39 |
+
|
40 |
+
for epoch in range(epochs):
|
41 |
+
self.forward(X)
|
42 |
+
dW1, db1, dW2, db2 = self.backward(X, y, learning_rate)
|
43 |
+
|
44 |
+
# Update weights and biases using Adam
|
45 |
+
t += 1
|
46 |
+
|
47 |
+
# Update biased first moment estimate
|
48 |
+
v_dw1 = beta1 * v_dw1 + (1 - beta1) * dW1
|
49 |
+
v_db1 = beta1 * v_db1 + (1 - beta1) * db1
|
50 |
+
v_dw2 = beta1 * v_dw2 + (1 - beta1) * dW2
|
51 |
+
v_db2 = beta1 * v_db2 + (1 - beta1) * db2
|
52 |
+
|
53 |
+
# Update biased second raw moment estimate
|
54 |
+
s_dw1 = beta2 * s_dw1 + (1 - beta2) * np.square(dW1)
|
55 |
+
s_db1 = beta2 * s_db1 + (1 - beta2) * np.square(db1)
|
56 |
+
s_dw2 = beta2 * s_dw2 + (1 - beta2) * np.square(dW2)
|
57 |
+
s_db2 = beta2 * s_db2 + (1 - beta2) * np.square(db2)
|
58 |
+
|
59 |
+
# Compute bias-corrected first moment estimate
|
60 |
+
v_dw1_corrected = v_dw1 / (1 - beta1**t)
|
61 |
+
v_db1_corrected = v_db1 / (1 - beta1**t)
|
62 |
+
v_dw2_corrected = v_dw2 / (1 - beta1**t)
|
63 |
+
v_db2_corrected = v_db2 / (1 - beta1**t)
|
64 |
+
|
65 |
+
# Compute bias-corrected second raw moment estimate
|
66 |
+
s_dw1_corrected = s_dw1 / (1 - beta2**t)
|
67 |
+
s_db1_corrected = s_db1 / (1 - beta2**t)
|
68 |
+
s_dw2_corrected = s_dw2 / (1 - beta2**t)
|
69 |
+
s_db2_corrected = s_db2 / (1 - beta2**t)
|
70 |
+
|
71 |
+
# Update weights and biases
|
72 |
+
self.weights1 -= learning_rate * v_dw1_corrected / (np.sqrt(s_dw1_corrected) + epsilon)
|
73 |
+
self.bias1 -= learning_rate * v_db1_corrected / (np.sqrt(s_db1_corrected) + epsilon)
|
74 |
+
self.weights2 -= learning_rate * v_dw2_corrected / (np.sqrt(s_dw2_corrected) + epsilon)
|
75 |
+
self.bias2 -= learning_rate * v_db2_corrected / (np.sqrt(s_db2_corrected) + epsilon)
|
76 |
+
|
77 |
+
if (epoch+1) % 100 == 0:
|
78 |
+
print(f'Epoch {epoch+1}/{epochs}, loss: {self.loss(y, self.a2)}')
|
79 |
+
|
80 |
+
def loss(self, y_true, y_pred):
|
81 |
+
# Fungsi loss (misalnya binary cross-entropy)
|
82 |
+
return -np.mean(y_true * np.log(y_pred) + (1 - y_true) * np.log(1 - y_pred))
|
83 |
+
|
84 |
+
def predict(self, X):
|
85 |
+
# Prediksi menggunakan forward propagation
|
86 |
+
y_pred = self.forward(X)
|
87 |
+
# Rounding untuk klasifikasi biner
|
88 |
+
y_pred = np.round(y_pred)
|
89 |
+
return y_pred
|
90 |
+
|
91 |
+
def accuracy(self, X, y):
|
92 |
+
# Hitung akurasi
|
93 |
+
y_pred = self.predict(X)
|
94 |
+
accuracy = np.mean(y_pred == y)
|
95 |
+
return accuracy
|