Softmax_test fails in "Python Basics with numpy" (Week 2)


The softmax_test fails for 1 our of 3 tests. Like this

Error: Wrong output
 2  Tests passed
 1  Tests failed
AssertionError: Not all tests were passed for softmax. Check your equations and avoid using global variables inside the function.

This is interesting since all three tests have the same input matrix into the softmax function which is the same as the demo function:

t_x = np.array([[9, 2, 5, 0, 0],
                [7, 5, 0, 0 ,0]])

Any idea what could be wrong?

Hi @whnr,
I am guessing if you are using an inverted signal.

It might be helpful to see the actual output you get when you run the test case in the notebook. I modified my code to print out the intermediate values for x_exp and x_sum. Here’s what I get when I run the test case in the notebook:

x_exp = [[8.10308393e+03 7.38905610e+00 1.48413159e+02 1.00000000e+00
 [1.09663316e+03 1.48413159e+02 1.00000000e+00 1.00000000e+00
x_sum = [[8260.88614278]
softmax(x) = [[9.80897665e-01 8.94462891e-04 1.79657674e-02 1.21052389e-04
 [8.78679856e-01 1.18916387e-01 8.01252314e-04 8.01252314e-04

How does that compare to what you get?

1 Like

@paulinpaloalto @carlosrl I found my mistake! I forgot to use x_exp in the sum and used x. Interesting that the other test cases still passed.


Hmmm, I’m not sure what you mean here. I tried making the mistake that you describe and both the local test case in the notebook and the grader fail, as I would have expected. Can you give more detail about the case in which you believe that wrong code passed the test cases?

I did the same thing, then reread the question and got it.

Here is the line that passed two of the three test cases

x_sum = np.sum(x, axis=1, keepdims=True) the x should be a x_exp to pass all test cases.

Thanks for the followup. Yes, that is ok: the tests check different things, e.g. the “shape” and “datatype” of your output will still be correct. But it fails the test for the actual value, meaning that the tests successfully detect your error, which is what I wanted to confirm.

1 Like