I've written softmax() function, but I don't get the proper answer

In order to study ML, I’m making my own softmax () function. I created a softmax () function as follows:

function softmax(a)
    c = maximum(a)
    exp.(a .- c) / sum(exp.(a .- c))
end

The sum of the values in the output matrix of this function should be 1.

temp =[0.03599090056107662, -0.01742189223257631, 0.03347773422745233, 0.04595474713958682, 0.13597367330428853, -0.06639136007573368, -0.0034873745788529358, -0.029754161131846667, 0.056507619128728516, 0.04165990829472141]

sum(softmax(temp))

If you run the above function, you get:

1.0000000000000002

What is the problem?

1 Like

By problem, you’re referring to the sum not being exactly 1? This tiny error is a result of how floating point representation and math works in all programming languages.

See https://floating-point-gui.de/

6 Likes

Thank you! Understand.