Spaces:
Build error
Build error
Jensen Holm commited on
Commit ·
ff1254a
1
Parent(s): 9249567
adding unit tests for activation functions (using pytest)
Browse filesFiles changed (5)
123from .loss import *from .activation import *from .nn import *12345678910from .loss import *from .activation import *from .nn import *ACTIVATIONS: dict[str, Activation] = {"Relu": Relu(),"Sigmoid": Sigmoid(),"TanH": TanH(),"SoftMax": SoftMax(),}
38 unmodified lines39404142434445464748495051525354555638 unmodified linesclass SoftMax(Activation):def forward(self, X: np.ndarray) -> np.ndarray:exps = np.exp(X - np.max(X, axis=1, keepdims=True)) # Avoid numerical instabilityreturn exps / np.sum(exps, axis=1, keepdims=True)def backward(self, X: np.ndarray) -> np.ndarray:return XACTIVATIONS: dict[str, Activation] = {"Relu": Relu(),"Sigmoid": Sigmoid(),"TanH": TanH(),"SoftMax": SoftMax(),}38 unmodified lines394041424344454647484938 unmodified linesclass SoftMax(Activation):def forward(self, X: np.ndarray) -> np.ndarray:ax = 1 if X.ndim > 1 else 0exps = np.exp(X - np.max(X, axis=ax, keepdims=True)) # Avoid numerical instabilityreturn exps / np.sum(exps, axis=ax, keepdims=True)def backward(self, X: np.ndarray) -> np.ndarray:return X
1234567gradio==4.27.0matplotlib==3.8.4numpy==1.26.4plotly==5.21.0scikit_learn==1.4.2setuptools==69.5.1tqdm==4.66.212345678gradio==4.39.0matplotlib==3.6.3numpy==2.0.1plotly==5.22.0pytest==7.4.4scikit_learn==1.5.1setuptools==68.1.2tqdm==4.66.4
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566import osimport sysimport pytest
sys.path.append(os.path.abspath(".."))
import randomimport numpy as npfrom numpyneuron import ( TanH, Sigmoid, Relu, SoftMax, Sigmoid,)
# these functions are meant to work with np.ndarray# objects, but they will also work with numbers which# makes testing a little bit simpler
def test_tanh() -> None: """ tanh(1) =~ 0.76 tanh'(1) =~ sech^2(1) =~ 0.419 """ tanh = TanH() assert tanh.forward(1) == pytest.approx(np.tanh(1)) assert tanh.forward(1) == pytest.approx(0.7615941559557649) assert tanh.backward(1) == pytest.approx(0.41997434161402614)
def test_sigmoid() -> None: """ sigmoid(1) =~ 0.73105 sigmoid'(1) =~ 0.1966 """ sigmoid = Sigmoid() assert sigmoid.forward(1) == pytest.approx(0.7310585786300049) assert sigmoid.backward(1) == pytest.approx(0.4621171572600098)
def test_relu() -> None: """ relu(n > 0) = n relu(n < 0) = 0 relu'(n > 0) = 1 relu'(n < 0) = 0 """ relu = Relu() random_n = random.randint(1, 100) assert relu.forward(random_n) == random_n assert relu.backward(random_n) == 1
def test_softmax() -> None: """ softmax([1, 2, 3]) = [0.090031, 0.244728, 0.665241] """ softmax = SoftMax() vec = np.array([1, 2, 3]) assert np.allclose( softmax.forward(vec), np.array([0.090031, 0.244728, 0.665241]), ) assert np.allclose(softmax.backward(vec), vec)numpyneuron/__init__.py+7
numpyneuron/activation.py+3-10
requirements.txt+8-7
test/__init__.pyAdded
test/test_activation.pyAdded+66