Skip to content

add relu function #1795

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 1 commit into from
Mar 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions maths/relu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
This script demonstrates the implementation of the ReLU function.

It's a kind of activation function defined as the positive part of its argument in the context of neural network.
The function takes a vector of K real numbers as input and then argmax(x, 0).
After through ReLU, the element of the vector always 0 or real number.

Script inspired from its corresponding Wikipedia article
https://en.wikipedia.org/wiki/Rectifier_(neural_networks)
"""

import numpy as np
from typing import List


def relu(vector: List[float]):
"""
Implements the relu function

Parameters:
vector (np.array,list,tuple): A numpy array of shape (1,n)
consisting of real values or a similar list,tuple


Returns:
relu_vec (np.array): The input numpy array, after applying
relu.

>>> vec = np.array([-1, 0, 5])
>>> relu(vec)
array([0, 0, 5])
"""

# compare two arrays and then return element-wise maxima.
return np.maximum(0, vector)


if __name__ == "__main__":
print(np.array(relu([-1, 0, 5]))) # --> [0, 0, 5]