feat: add bias to nn parameters

This commit is contained in:
Lenoctambule
2026-03-27 04:35:22 +01:00
parent 439a11a828
commit 1e8a27ddaa
2 changed files with 11 additions and 7 deletions

View File

@@ -10,6 +10,7 @@ class Encoder:
lr: float, lr: float,
activation_func: types.FunctionType): activation_func: types.FunctionType):
self.W = np.random.uniform(-1, 1, (in_size, out_size)) self.W = np.random.uniform(-1, 1, (in_size, out_size))
self.B = np.zeros((out_size))
self.lr = lr self.lr = lr
self.last_input = None self.last_input = None
self.last_output = None self.last_output = None
@@ -17,13 +18,14 @@ class Encoder:
def forward(self, V: np.ndarray) -> np.ndarray: def forward(self, V: np.ndarray) -> np.ndarray:
self.last_input = V self.last_input = V
z = V @ self.W res = V @ self.W + self.B
self.last_output = regularize(self.activation_func(z)) self.last_output = regularize(self.activation_func(res))
return self.last_output return self.last_output
def backprop(self, error: np.ndarray): def backprop(self, error: np.ndarray):
dW = np.outer(self.last_input, error) dW = np.outer(self.last_input, error)
self.W -= self.lr * dW self.W -= self.lr * dW
self.B -= self.lr * error
return error @ self.W.T return error @ self.W.T
@@ -34,6 +36,7 @@ class Decoder:
lr: float, lr: float,
activation_func): activation_func):
self.W = np.random.uniform(-1, 1, (in_size, out_size)) self.W = np.random.uniform(-1, 1, (in_size, out_size))
self.B = np.zeros((out_size))
self.lr = lr self.lr = lr
self.last_input = None self.last_input = None
self.last_output = None self.last_output = None
@@ -41,14 +44,15 @@ class Decoder:
def forward(self, V: np.ndarray) -> np.ndarray: def forward(self, V: np.ndarray) -> np.ndarray:
self.last_input = V self.last_input = V
z = V @ self.W res = V @ self.W + self.B
self.last_output = regularize(self.activation_func(z)) self.last_output = regularize(self.activation_func(res))
return self.last_output return self.last_output
def backprop(self, target: np.ndarray): def backprop(self, target: np.ndarray):
error = self.last_output - target error = self.last_output - target
dW = np.outer(self.last_input, error) dW = np.outer(self.last_input, error)
self.W -= self.lr * dW self.W -= self.lr * dW
self.B -= self.lr * error
return error @ self.W.T return error @ self.W.T

View File

@@ -18,7 +18,7 @@ def mnist_embed():
prev_error = float('inf') prev_error = float('inf')
losses = [] losses = []
epoch = 0 epoch = 0
x_train = x_train[:1_000] x_train = x_train[:]
while True: while True:
error = 0 error = 0
for x in x_train: for x in x_train:
@@ -30,10 +30,10 @@ def mnist_embed():
prev_error = error prev_error = error
losses.append(error) losses.append(error)
dynamic_loss_plot_update(ax, line, losses) dynamic_loss_plot_update(ax, line, losses)
if NO_IMPROV > 10: if NO_IMPROV > 5:
print('Done !') print('Done !')
break break
if epoch > 200: if epoch > 500:
break break
epoch += 1 epoch += 1
dynamic_loss_plot_finish(ax, line) dynamic_loss_plot_finish(ax, line)