0

I was trying to write a method to compute the SoftMax activation function that takes either a matrix or an array as input and apply the softmax function to each rows.

Here is what I tried:

import numpy as np
def softmaxSingle(x):
    e_x = np.exp(x - np.max(x))
    return e_x / e_x.sum()    

def softmax( x):
    if np.shape(x)[0]>1:
        result=[[]]*np.shape(x)[0]
        for i in range(len(result)):
            result[i]=list(softmaxSingle(x[i]))
        return list(result)
    e_x = np.exp(x - np.max(x))
    return e_x / e_x.sum()

When I tried SoftMax(x) where x is a matrix, It runs(although I don't know if it produces correct answer). When x is just a list, it doesn't work

1 Answer 1

1

You can simply make a list to np.array conversion:

import numpy as np

def softmax(x):
    """Compute softmax values for each sets of scores in x."""
    if isinstance(x, list):
        x = np.array(x)
    e_x = np.exp(x - np.max(x))
    return e_x / e_x.sum()
Sign up to request clarification or add additional context in comments.

4 Comments

But 'x-np.max(x)' method doesn't substract each row by the max of each row, it substract each row by the max of all entries, Is that ok? Try this for example: a=[[1,2,3],[4,5,6]] print(a-np.max(a)) and the output of that is : [[-5 -4 -3] [-2 -1 0]]
@user42493 Can you give me sample input? And yes, it needs to subtract every element by the max of that matrix, don't you think? If row-wise is a requirement then you can still work it that way
Ok, what about e_x.sum() ? that gives the sum of all entries in the matrix, but we need to divide each row of e_x by the sum of each row e_x?
From the docs: Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array. If axis is negative it counts from the last to the first axis.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.