Skip to content

Commit 644ba59

Browse files
authored
Update multilayer_perceptron_classifier_from_scratch.py
1 parent 8988727 commit 644ba59

1 file changed

Lines changed: 10 additions & 12 deletions

File tree

machine_learning/multilayer_perceptron_classifier_from_scratch.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import numpy as np
22
from numpy.random import default_rng
3-
43
rng = default_rng(42)
5-
6-
74
class Dataloader:
85
"""
96
DataLoader class for handling dataset, including data shuffling,
@@ -139,12 +136,12 @@ class MLP:
139136
"""
140137

141138
def __init__(
142-
self,
143-
dataloader: Dataloader,
144-
epoch: int,
145-
learning_rate: float,
146-
gamma: float = 1.0,
147-
hidden_dim: int = 2,
139+
self,
140+
dataloader: Dataloader,
141+
epoch: int,
142+
learning_rate: float,
143+
gamma: float = 1.0,
144+
hidden_dim: int = 2,
148145
) -> None:
149146
self.learning_rate = learning_rate
150147
self.gamma = gamma # learning_rate decay hyperparameter gamma
@@ -195,7 +192,8 @@ def initialize(self) -> tuple[np.ndarray, np.ndarray]:
195192
"""
196193

197194
in_dim, out_dim = self.dataloader.get_inout_dim()
198-
w1 = rng.standard_normal((in_dim + 1, self.hidden_dim)) * np.sqrt(2.0 / in_dim)
195+
w1 = (rng.standard_normal((in_dim + 1, self.hidden_dim)) *
196+
np.sqrt(2.0 / in_dim))
199197
w2 = rng.standard_normal((self.hidden_dim, out_dim)) * np.sqrt(
200198
2.0 / self.hidden_dim
201199
)
@@ -404,7 +402,7 @@ def accuracy(label: np.ndarray, y_hat: np.ndarray) -> float:
404402
>>> label = np.array([[1, 0], [0, 1], [1, 0]])
405403
>>> y_hat = np.array([[0.9, 0.1], [0.2, 0.8], [0.6, 0.4]])
406404
>>> mlp.accuracy(label, y_hat)
407-
1.0
405+
np.float64(1.0)
408406
"""
409407
return (y_hat.argmax(axis=1) == label.argmax(axis=1)).mean()
410408

@@ -425,7 +423,7 @@ def loss(output: np.ndarray, label: np.ndarray) -> float:
425423
>>> output = np.array([[0.9, 0.1], [0.2, 0.8]])
426424
>>> label = np.array([[1.0, 0.0], [0.0, 1.0]])
427425
>>> round(mlp.loss(output, label), 3)
428-
0.025
426+
np.float64(0.025)
429427
"""
430428
return np.sum((output - label) ** 2) / (2 * label.shape[0])
431429

0 commit comments

Comments
 (0)