Source code for neural_network.functions.relu
from typing import List
from .abstract_function import AbstractFunction
[docs]
class ReLU(AbstractFunction):
"""Class to represent the ReLU function
"""
[docs]
def __init__(self, leak: float = 0.0):
"""Constructor method
Parameters
----------
leak : float
The parameter to be used if this is a LeakyReLU
"""
super().__init__()
self._leak = leak
[docs]
def __call__(self, x: float, w: List[float] = None) -> float:
"""Implementation of ReLU
Parameters
----------
x : float
Input to function
w : List[float]
Weights (not used here)
Returns
-------
float
Output to function
"""
return x if x >= 0 else x * self._leak
[docs]
def gradient(self, x: float, w: List[float] = None) -> float:
"""Gradient of ReLU
Parameters
----------
x : float
Input to function
w : List[float]
Not used for this class
Returns
-------
float
Gradient of ReLU
"""
return 1 if x >= 0 else self._leak