Week 2 - Python Basics, softmax error

I am a bit stuck on Exercise 7 - softmax. The output of the grader is as follows:

Error: Wrong output
 2  Tests passed
 1  Tests failed

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-36-589a35b2026d> in <module>
      3 print("softmax(x) = " + str(softmax(t_x)))
      4 
----> 5 softmax_test(softmax)

~/work/release/W2A1/public_tests.py in softmax_test(target)
    202     ]
    203 
--> 204     test(test_cases, target)
    205 
    206 

~/work/release/W2A1/test_utils.py in test(test_cases, target)
     24         print('\033[92m', success," Tests passed")
     25         print('\033[91m', len(test_cases) - success, " Tests failed")
---> 26         raise AssertionError("Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.".format(target.__name__))

AssertionError: Not all tests were passed for softmax. Check your equations and avoid using global variables inside the function.

I don’t think I can post any code I wrote, but in summary, I have 3 lines of code, first one computes np.exp, second uses np.sum (I ensure I have axis=1 and keepdims=True). Lastly, I simply divide these two quantities and assign result to s.

Could someone let me know what I might be missing?

Hi @Richard_Wessels

Make sure that you are not using any global variables as mentioned in the error message. Also, check your division and other operations by debugging and printing values in each stage so you can find the source of the problem!

Feel free to ask if you need any further assistance.

1 Like

That tells you are using a global variable in your codes which is causing the assertion error.

So next

I suppose you applied this numpy to the argument call x which is A numpy matrix of shape (m,n), then it is correct

then

your axis and keep dims is correct, but while applying the np.sum, you make sure you apply this to the previously recall code line which should not be just “x” but x_exp. Make sure you recalled this numpy sum code lines as x_sum.

this is again correct, but again depends how you recalled the two quantities.

If all of these were recalled as stated then the error might be in previously exercise which needs to be looked upon.

Regards
DP

In addition, be sure you’re using the correct variable inside the softmax() function.
It’s listed in the function definition:

Thank you, I was applying np.sum to x instead of x_exp. Thank you all for the help :slight_smile:

2 Likes