Skip to content

Add binary step activation function #10030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 8, 2023
37 changes: 37 additions & 0 deletions neural_network/activation_functions/binary_step.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""
This script demonstrates the implementation of the Binary Step function.

It's an activation function in which if the input to the
activation function is greater than a threshold, the neuron is
activated, else it is deactivated

It's a simple activation function which is mentioned in this wikipedia article:
https://en.wikipedia.org/wiki/Activation_function
"""


import numpy as np


def binary_step(vector: np.ndarray) -> np.ndarray:
"""
Implements the binary step function

Parameters:
vector (ndarray): A vector that consitis of numeric values

Returns:
vector (ndarray): A vector that consitis of values 0 or 1

>>> vector = np.array([-1.2, 0, 2, 1.45, -3.7, 0.3])
>>> binary_step(vector)
array([0, 0, 1, 1, 0, 1])
"""

return np.where(vector > 0, 1, 0)


if __name__ == "__main__":
import doctest

doctest.testmod()