Activation Functions in Neural Networks

рдЗрд╕ рдмреНрд▓реЙрдЧ рдореЗрдВ рд╣рдо Activation Functions рдХреА рдЧрд╣рд░рд╛рдИ рд╕реЗ рд╕рдордЭ рд▓реЗрдВрдЧреЗред Activation Functions Neural Network рдХрд╛ рджрд┐рдорд╛рдЧ рд╣реИрдВ рдЬреЛ рдпрд╣ рддрдп рдХрд░рддреЗ рд╣реИрдВ рдХрд┐ рдХреМрди рд╕рд╛ neuron fire рд╣реЛрдЧрд╛ рдФрд░ network complex patterns рд╕реАрдЦ рдкрд╛рдПрдЧрд╛ рдпрд╛ рдирд╣реАрдВред рдЗрд╕рдореЗрдВ рд╣рдо Step, Sigmoid, Tanh, ReLU, Leaky ReLU, S

тЪб Activation Functions in Neural Networks

Neural Networks рдореЗрдВ activation function рдХрд╛ role decision making рдХрд╛ рд╣реЛрддрд╛ рд╣реИред рдмрд┐рдирд╛ activation functions рдХреЗ neural network рд╕рд┐рд░реНрдл linear transformations рд╣реА рдХрд░ рдкрд╛рддрд╛ред рд▓реЗрдХрд┐рди real-world problems (рдЬреИрд╕реЗ image recognition, NLP, speech processing) non-linear рд╣реЛрддреА рд╣реИрдВред рдЗрд╕реАрд▓рд┐рдП рд╣рдореЗрдВ activation functions рдХреА рдЬрд╝рд░реВрд░рдд рд╣реЛрддреА рд╣реИред

тЭУ Activation Function рдХреА рдЬрд╝рд░реВрд░рдд рдХреНрдпреЛрдВ?

  • Non-linearity introduce рдХрд░рддрд╛ рд╣реИ
  • Complex decision boundaries рдмрдирд╛рддрд╛ рд╣реИ
  • Gradient Descent рдФрд░ Backpropagation рдХреЛ рд╕рдВрднрд╡ рдмрдирд╛рддрд╛ рд╣реИ
  • Neurons рдХреЛ relevant signal pass рдХрд░рдиреЗ рдФрд░ irrelevant ignore рдХрд░рдиреЗ рдореЗрдВ рдорджрдж рдХрд░рддрд╛ рд╣реИ

ЁЯУМ Types of Activation Functions

1я╕ПтГг Step Function

рд╕рдмрд╕реЗ basic activation function рд╣реИред Input > 0 рд╣реЛрдиреЗ рдкрд░ 1 return рдХрд░рддрд╛ рд╣реИ, рд╡рд░рдирд╛ 0ред рдпрд╣ simple binary classification рдХреЗ рд▓рд┐рдП рдЙрдкрдпреЛрдЧреА рд╣реИ рд▓реЗрдХрд┐рди gradient-based training рдореЗрдВ рдХрд╛рдо рдирд╣реАрдВ рдЖрддрд╛ред


import numpy as np

def step_function(x):
    return np.where(x >= 0, 1, 0)

print(step_function(np.array([-2, -1, 0, 1, 2])))
    

2я╕ПтГг Sigmoid Function

Sigmoid рд╣рд░ input рдХреЛ (0,1) range рдореЗрдВ compress рдХрд░рддрд╛ рд╣реИред Logistic regression рдФрд░ binary classification рдореЗрдВ use рд╣реЛрддрд╛ рд╣реИред рд▓реЗрдХрд┐рди рдмрдбрд╝реЗ positive рдпрд╛ negative inputs рдкрд░ gradient vanish рд╣реЛ рдЬрд╛рддрд╛ рд╣реИред


import numpy as np

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

print(sigmoid(np.array([-5, 0, 5])))
    

3я╕ПтГг Tanh Function

Tanh рд╣рд░ input рдХреЛ (-1, 1) range рдореЗрдВ compress рдХрд░рддрд╛ рд╣реИред рдпрд╣ Sigmoid рд╕реЗ рдмреЗрд╣рддрд░ рд╣реИ рдХреНрдпреЛрдВрдХрд┐ рдЗрд╕рдХрд╛ mean zero рд╣реЛрддрд╛ рд╣реИред рд▓реЗрдХрд┐рди рдЗрд╕рдореЗрдВ рднреА gradient vanishing issue рд╣реЛрддрд╛ рд╣реИред


def tanh(x):
    return np.tanh(x)

print(tanh(np.array([-3, 0, 3])))
    

4я╕ПтГг ReLU (Rectified Linear Unit)

Deep Learning рдореЗрдВ рд╕рдмрд╕реЗ popular activation function рд╣реИред Negative values рдХреЛ 0 рдХрд░ рджреЗрддрд╛ рд╣реИ рдФрд░ positive values as it is pass рдХрд░ рджреЗрддрд╛ рд╣реИред рдпрд╣ vanishing gradient problem рдХреЛ рдХрд╛рдлреА рд╣рдж рддрдХ solve рдХрд░рддрд╛ рд╣реИред


def relu(x):
    return np.maximum(0, x)

print(relu(np.array([-5, -1, 0, 2, 5])))
    

5я╕ПтГг Leaky ReLU

ReLU рдХрд╛ improved version рд╣реИред рдЗрд╕рдореЗрдВ negative inputs рдХреЛ рдкреВрд░реА рддрд░рд╣ ignore рдирд╣реАрдВ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рдмрд▓реНрдХрд┐ рдЙрдиреНрд╣реЗрдВ small slope (0.01x) рджрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред рдЗрд╕рд╕реЗ dying ReLU problem solve рд╣реЛрддреА рд╣реИред


def leaky_relu(x, alpha=0.01):
    return np.where(x > 0, x, alpha * x)

print(leaky_relu(np.array([-5, -1, 0, 2, 5])))
    

6я╕ПтГг Softmax Function

Softmax multi-class classification problems рдореЗрдВ use рд╣реЛрддрд╛ рд╣реИред рдпрд╣ outputs рдХреЛ probability distribution рдореЗрдВ рдмрджрд▓ рджреЗрддрд╛ рд╣реИред рд╣рд░ class рдХреА probability (0,1) range рдореЗрдВ рд╣реЛрддреА рд╣реИ рдФрд░ sum = 1 рд╣реЛрддрд╛ рд╣реИред


def softmax(x):
    exp_x = np.exp(x - np.max(x))
    return exp_x / exp_x.sum()

print(softmax(np.array([2, 1, 0.1])))
    

ЁЯУК Comparison of Activation Functions

Function Range Pros Cons
Step 0 or 1 Simple, fast No gradient, no learning
Sigmoid (0,1) Probabilistic output Vanishing gradient
Tanh (-1,1) Zero-centered Vanishing gradient
ReLU [0, тИЮ) Efficient, avoids vanishing gradient Dying ReLU
Leaky ReLU (-тИЮ, тИЮ) Fixes dying ReLU Still not perfect
Softmax (0,1) Best for classification Expensive computation

ЁЯПЖ рдирд┐рд╖реНрдХрд░реНрд╖

Activation Functions neural network рдХрд╛ рд╕рдмрд╕реЗ рдорд╣рддреНрд╡рдкреВрд░реНрдг рд╣рд┐рд╕реНрд╕рд╛ рд╣реИрдВред рдЗрдирдХреЗ рдмрд┐рдирд╛ complex patterns рд╕реАрдЦрдирд╛ impossible рд╣реИред рд╕рд╣реА activation function рдХрд╛ рдЪреБрдирд╛рд╡ data рдФрд░ task рдкрд░ depend рдХрд░рддрд╛ рд╣реИред рдЖрдЬ industry рдореЗрдВ ReLU, Leaky ReLU рдФрд░ Softmax рд╕рдмрд╕реЗ рдЬрд╝реНрдпрд╛рджрд╛ use рд╣реЛ рд░рд╣реЗ рд╣реИрдВред рд▓реЗрдХрд┐рди research рдореЗрдВ рдирдП activation functions (Swish, GELU рдЖрджрд┐) рднреА рдЖ рд░рд╣реЗ рд╣реИрдВред