Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Derivative of softmax function in Python

Below is the softmax activation function for a neural network. What is the derivative of this function?

def softmax(z):
   e = np.exp(z)
   return e / np.sum(e, axis=1)
like image 550
Krutika Parekh Avatar asked Dec 01 '25 02:12

Krutika Parekh


1 Answers

Iterative version for softmax derivative

import numpy as np

def softmax_grad(s): 
    # Take the derivative of softmax element w.r.t the each logit which is usually Wi * X
    # input s is softmax value of the original input x. 
    # s.shape = (1, n) 
    # i.e. s = np.array([0.3, 0.7]), x = np.array([0, 1])

    # initialize the 2-D jacobian matrix.
    jacobian_m = np.diag(s)

    for i in range(len(jacobian_m)):
        for j in range(len(jacobian_m)):
            if i == j:
                jacobian_m[i][j] = s[i] * (1-s[i])
            else: 
                jacobian_m[i][j] = -s[i]*s[j]
    return jacobian_m

Vectorized version

def softmax_grad(softmax):
    # Reshape the 1-d softmax to 2-d so that np.dot will do the matrix multiplication
    s = softmax.reshape(-1,1)
    return np.diagflat(s) - np.dot(s, s.T)

Reference: https://medium.com/@aerinykim/how-to-implement-the-softmax-derivative-independently-from-any-loss-function-ae6d44363a9d

like image 197
bumblebee Avatar answered Dec 02 '25 16:12

bumblebee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!