Commit eb173500 authored by narugo1992's avatar narugo1992
Browse files

dev(narugo): add test and docs for sigmoid function

parent fac816fb
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
imgutils.utils.func
====================================

.. currentmodule:: imgutils.utils.func

.. automodule:: imgutils.utils.func


sigmoid
-------------------------

.. autofunction:: sigmoid

+1 −0
Original line number Diff line number Diff line
@@ -9,4 +9,5 @@ imgutils.utils
.. toctree::
    :maxdepth: 3

    func
    onnxruntime
+35 −0
Original line number Diff line number Diff line
"""
This module provides mathematical functions related to neural networks.

It includes the sigmoid activation function, which is commonly used in various
machine learning and deep learning models. The sigmoid function maps any input
value to a value between 0 and 1, making it useful for binary classification
problems and as an activation function in neural network layers.

Usage:
    >>> from imgutils.utils import sigmoid
    >>> result = sigmoid(input_value)
"""

import numpy as np

__all__ = ['sigmoid']


def sigmoid(x):
    """
    Compute the sigmoid function for the input.

    The sigmoid function is defined as:
    :math:`f\\left(x\\right) = \\frac{1}{1 + e^{-x}}`

    This function applies the sigmoid activation to either a single number
    or an array of numbers using NumPy for efficient computation.

    :param x: Input value or array of values.
    :type x: float or numpy.ndarray

    :return: Sigmoid of the input.
    :rtype: float or numpy.ndarray

    :example:
        >>> import numpy as np
        >>> sigmoid(0)
        0.5
        >>> sigmoid(np.array([-1, 0, 1]))
        array([0.26894142, 0.5       , 0.73105858])
    """
    return 1 / (1 + np.exp(-x))

test/utils/__init__.py

0 → 100644
+0 −0

Empty file added.

+42 −0
Original line number Diff line number Diff line
import numpy as np
import pytest

from imgutils.utils import sigmoid


@pytest.fixture
def sample_input():
    return np.array([-1, 0, 1])


@pytest.fixture
def expected_output():
    return np.array([0.26894142, 0.5, 0.73105858])


@pytest.mark.unittest
class TestUtilsFuncSigmoid:
    def test_sigmoid_scalar(self):
        assert np.isclose(sigmoid(0), 0.5)

    def test_sigmoid_array(self, sample_input, expected_output):
        result = sigmoid(sample_input)
        np.testing.assert_array_almost_equal(result, expected_output)

    def test_sigmoid_large_positive(self):
        assert np.isclose(sigmoid(100), 1.0)

    def test_sigmoid_large_negative(self):
        assert np.isclose(sigmoid(-100), 0.0)

    def test_sigmoid_zero(self):
        assert sigmoid(0) == 0.5

    def test_sigmoid_type(self):
        assert isinstance(sigmoid(1), float)
        assert isinstance(sigmoid(np.array([1])), np.ndarray)

    def test_sigmoid_shape(self):
        input_array = np.array([[1, 2], [3, 4]])
        result = sigmoid(input_array)
        assert result.shape == input_array.shape