""" # -*- coding: utf-8 -*- # # Copyright 2021 Michael Büsch # # Licensed under the Apache License version 2.0 # or the MIT license, at your option. # SPDX-License-Identifier: Apache-2.0 OR MIT # """ from mlplib.activation import * import numpy as np def test_sigmoid(): sigmoid = Sigmoid() assert np.allclose(sigmoid.fn(np.array([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5])), np.array([0.00669285, 0.01798621, 0.04742587, 0.11920292, 0.26894142, 0.5, 0.73105858, 0.88079708, 0.95257413, 0.98201379, 0.99330715])) def test_sigmoid_d(): sigmoid = Sigmoid() assert np.allclose(sigmoid.fn_d(np.array([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5])), np.array([0.00664806, 0.01766271, 0.04517666, 0.10499359, 0.19661193, 0.25, 0.19661193, 0.10499359, 0.04517666, 0.01766271, 0.00664806])) def test_relu(): relu = ReLU() assert np.all(relu.fn(np.array([1, 2, -1, -2])) == np.array([1, 2, 0, 0])) def test_relu_d(): relu = ReLU() assert np.all(relu.fn_d(np.array([1, 2, -1, -2])) == np.array([1, 1, 0, 0])) def test_lrelu(): lrelu = LReLU(alpha=0.1) assert np.all(lrelu.fn(np.array([1, 2, -1, -2])) == np.array([1, 2, -0.1, -0.2])) def test_lrelu_d(): lrelu = LReLU(alpha=0.1) assert np.all(lrelu.fn_d(np.array([1, 2, -1, -2])) == np.array([1, 1, 0.1, 0.1])) def test_tanh(): tanh = Tanh() assert np.allclose(tanh.fn(np.array([-3, -2, -1, -0.5, 0, 0.5, 1, 2, 3])), np.array([-0.99505475, -0.96402758, -0.76159416, -0.46211716, 0, 0.46211716, 0.76159416, 0.96402758, 0.99505475])) def test_tanh_d(): tanh = Tanh() assert np.allclose(tanh.fn_d(np.array([-3, -2, -1, -0.5, 0, 0.5, 1, 2, 3])), np.array([0.00986604, 0.07065082, 0.41997434, 0.78644773, 1, 0.78644773, 0.41997434, 0.07065082, 0.00986604])) def test_softmax(): # Test data Copyright 2008-2021, The SciPy community. inp = np.array([[1, 0.5, 0.2, 3], [1, -1, 7, 3], [2, 12, 13, 3]]) out = Softmax().fn(inp) assert np.allclose(np.sum(out, axis=1), np.array([1.0, 1.0, 1.0])) assert np.allclose(out, np.array([[1.05877e-1, 6.42177e-2, 4.75736e-2, 7.82332e-1], [2.42746e-3, 3.28521e-4, 9.79307e-1, 1.79366e-2], [1.22094e-5, 2.68929e-1, 7.31025e-1, 3.31885e-5]])) if __name__ == "__main__": import matplotlib.pyplot as plt fig, axes = plt.subplots(5, 2) fig.tight_layout() step = 0.01 sigmoid = Sigmoid() ax = axes[0][0] ax.set_title("sigmoid") ax.set_ylim(-0.1, 1.1) x = np.arange(-5, 5, step) ax.plot(x, sigmoid.fn(x)) ax = axes[0][1] ax.set_title("sigmoid derivative") ax.set_ylim(-0.1, 1.1) ax.plot(x, sigmoid.fn_d(x)) relu = ReLU() ax = axes[1][0] ax.set_title("relu") ax.set_ylim(-0.1, 1.1) x = np.arange(-5, 5, step) ax.plot(x, relu.fn(x)) ax = axes[1][1] ax.set_title("relu derivative") ax.set_ylim(-0.1, 1.1) ax.plot(x, relu.fn_d(x)) lrelu = LReLU(0.1) ax = axes[2][0] ax.set_title("leaky relu") ax.set_ylim(-1.1, 1.1) x = np.arange(-5, 5, step) ax.plot(x, lrelu.fn(x)) ax = axes[2][1] ax.set_title("leaky relu derivative") ax.set_ylim(-0.1, 1.1) ax.plot(x, lrelu.fn_d(x)) tanh = Tanh() ax = axes[3][0] ax.set_title("tanh") ax.set_ylim(-1.1, 1.1) x = np.arange(-2, 2, step) ax.plot(x, tanh.fn(x)) ax = axes[3][1] ax.set_title("tanh derivative") ax.set_ylim(-1.1, 1.1) ax.plot(x, tanh.fn_d(x)) softmax = Softmax() ax = axes[4][0] ax.set_title("softmax") ax.set_ylim(-0.1, 1.1) x = np.array([[0.0, 0.1, 0.2, 0.4, 2.5, 0.4, 0.2, 0.1, 0.0]]) xs = softmax.fn(x) ax.plot(xs.flatten()) ax.plot(x.flatten()) ax = axes[4][1] ax.set_title("softmax derivative") ax.set_ylim(-0.1, 1.1) xd = softmax.fn_d(x) ax.plot(xd.flatten()) plt.show() # vim: ts=4 sw=4 expandtab