W1 assignment 2 sum(y.ravel()) >1

In the first part of the assignment with the character level name generation, there’s this piece:

y= softmax(z)

for the np.random.choice to work, i need to reduce the dimension of y from (27,1) to (27,)

    print(y.shape)                  (27,1)
    print(sum(y))                   [1.]
    print(y.ravel().shape)      (27,)
    print(sum(y.ravel()))        1.0000000000000002

can’t figure out why the sum of y.ravel not equal to 1

after a few hours, i figured out a piece of it.
The error message made me think it was the 1.000002 causing the choice() to break, but it actually accepted it.

The problem is from how x is setup. since wrong shape would lead to broadcasting. so a, z, y will end up having 100 columns. so prob will be 99.99 instead of 1.00

To within numerical accuaracy, I get both the sum of y and the sum of probs to be 1.00000. There is a residual of +/- a very small value.

1 Like