Skip to content

Commit 6ffcbad

Browse files
saahil-mahatopre-commit-ci[bot]tianyizheng02
authored andcommitted
Add binary step activation function (TheAlgorithms#10030)
* Add binary step activation function * fix: ruff line too long error * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * refactor: add link to directory * revert: add link to directory * fix: algorithm bug and docs * Update neural_network/activation_functions/binary_step.py * fix: ruff line too long error --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Tianyi Zheng <[email protected]>
1 parent 5fe4d6d commit 6ffcbad

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Diff for: neural_network/activation_functions/binary_step.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
This script demonstrates the implementation of the Binary Step function.
3+
4+
It's an activation function in which the neuron is activated if the input is positive
5+
or 0, else it is deactivated
6+
7+
It's a simple activation function which is mentioned in this wikipedia article:
8+
https://en.wikipedia.org/wiki/Activation_function
9+
"""
10+
11+
12+
import numpy as np
13+
14+
15+
def binary_step(vector: np.ndarray) -> np.ndarray:
16+
"""
17+
Implements the binary step function
18+
19+
Parameters:
20+
vector (ndarray): A vector that consists of numeric values
21+
22+
Returns:
23+
vector (ndarray): Input vector after applying binary step function
24+
25+
>>> vector = np.array([-1.2, 0, 2, 1.45, -3.7, 0.3])
26+
>>> binary_step(vector)
27+
array([0, 1, 1, 1, 0, 1])
28+
"""
29+
30+
return np.where(vector >= 0, 1, 0)
31+
32+
33+
if __name__ == "__main__":
34+
import doctest
35+
36+
doctest.testmod()

0 commit comments

Comments
 (0)