0

So I'm trying to write an ELU activation function and the input into the function would be a 1D array of shape (20,1) and the output should be a similar array with the activation applied for each element in the array. The problem is that although I've done this with sigmoid or tanh and so on, for ELU we have a conditional check for the value at a given index.

def elu(x): 
    d=np.zeros_like(x, dtype=float)  
    d[x<=0]=alpha * (np.exp(x) - 1)
    d[x>0]=x
    return d

This is what I have so far, but it doesn't work, since I'm calling the np.exp function on an array itself, so the output would be an array, but I'm trying to assign that array value into a single element. The question is, how would I access the element that fulfilled such condition and replace it with the value of that expression, namely alpha*(np.exp(x)-1).

The alternative would be making a for loop to cycle through each element one by one to update the array or make the method work on a single scalar and then when calling the activation, cycle through the inputs and call the activation individually each time for each element (and then do the same when calling its derivative).

1 Answer 1

1

You get dimension mismatch because you are trying to assign arrays of different dimension. You need to filter also x, namely:

def elu(x): 
    d=np.zeros_like(x, dtype=float)  
    d[x<=0]=alpha * (np.exp(x[x<=0]) - 1)
    d[x>0]=x[x>0]
    return d
Sign up to request clarification or add additional context in comments.

1 Comment

That worked great, thank you! So it was a matter of applying the condition to the other side as well...

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.