Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions neural_network/back_propagation_neural_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

"""

from collections.abc import Callable

import numpy as np
from matplotlib import pyplot as plt

Expand All @@ -32,18 +34,29 @@ class DenseLayer:
"""

def __init__(
self, units, activation=None, learning_rate=None, is_input_layer=False
self,
units,
activation: Callable[[np.ndarray], np.ndarray] | None = None,
learning_rate=None,
is_input_layer=False,
) -> None:
"""
common connected layer of bp network
:param units: numbers of neural units
:param activation: activation function
:param activation: activation function, sigmoid if not given
:param learning_rate: learning rate for paras
:param is_input_layer: whether it is input layer or not

>>> DenseLayer(3).activation is sigmoid
True
>>> DenseLayer(3, activation=np.tanh).activation is np.tanh
True
"""
self.units = units
self.weight = None
self.bias = None
if activation is None:
activation = sigmoid
self.activation = activation
if learning_rate is None:
learning_rate = 0.3
Expand All @@ -54,8 +67,6 @@ def initializer(self, back_units) -> None:
rng = np.random.default_rng()
self.weight = np.asmatrix(rng.normal(0, 0.5, (self.units, back_units)))
self.bias = np.asmatrix(rng.normal(0, 0.5, self.units)).T
if self.activation is None:
self.activation = sigmoid

def cal_gradient(self):
# activation function may be sigmoid or linear
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ skip = """\
# assumptions consistent with the CI job in .github/workflows/ty.yml, where the
# free-threaded 3.15t test venv would otherwise leave several stubs unresolved.
environment.python-version = "3.15"
rules.call-non-callable = "ignore"
rules.invalid-argument-type = "ignore"
rules.invalid-return-type = "ignore"
rules.no-matching-overload = "ignore"
Expand Down
Loading