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