In [1]:
import numpy as np
import matplotlib.pyplot as plt
In [2]:
def sigmoid(x: np.ndarray) -> np.ndarray:
    return 1 / (1 + np.exp(-x))

x = np.linspace(-10, 10, 10000)
y = sigmoid(x)
dy_dx = np.gradient(y, x)

plt.figure(figsize=(10, 6))
plt.plot(x, y, label="Sigmoid")
plt.plot(x, dy_dx, label="Derivative")
plt.title("Sigmoid and its Derivative \n")
plt.legend()
plt.tight_layout()
plt.grid(True)
plt.show()
Non Linearity — plot 1
In [3]:
def relu(x: np.ndarray) -> np.ndarray:
    return np.where(x > 0, x, 0)

x = np.linspace(-5, 5, 10000)
y = relu(x)
dy_dx = np.gradient(y, x)

plt.figure(figsize=(10, 6))
plt.plot(x, y, label="ReLU")
plt.plot(x, dy_dx, label="Derivative")
plt.title("ReLU and its Derivative \n")
plt.legend()
plt.tight_layout()
plt.grid(True)
plt.show()
Non Linearity — plot 2
In [4]:
def leaky_relu(x: np.ndarray, alpha: float = 0.01) -> np.ndarray:
    return np.where(x > 0, x, x * alpha)

x = np.linspace(-5, 5, 10000)
alpha = 0.1
y = leaky_relu(x, alpha)
dy_dx = np.gradient(y, x)

plt.figure(figsize=(10, 6))
plt.plot(x, y, label=f"LeakyReLU wlth alpha: {alpha}")
plt.plot(x, dy_dx, label="Derivative ")
plt.title("LeakyReLU and its Derivative \n")
plt.legend()
plt.tight_layout()
plt.grid(True)
plt.show()
Non Linearity — plot 3
In [5]:
def tanh(x: np.ndarray) -> np.ndarray:
    return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))

x = np.linspace(-5, 5, 10000)
y = tanh(x)
dy_dx = np.gradient(y, x)

plt.figure(figsize=(10, 6))
plt.plot(x, y, label="Tanh")
plt.plot(x, dy_dx, label="Derivative")
plt.title("Tanh and its Derivative \n")
plt.legend()
plt.tight_layout()
plt.grid(True)
plt.show()
Non Linearity — plot 4