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

**URL:** <https://discourse.julialang.org/t/ive-written-softmax-function-but-i-dont-get-the-proper-answer/33610>\
**Category:** Machine Learning\
**Created:** [January 21, 2020, 7:03am UTC](https://discourse.julialang.org/t/ive-written-softmax-function-but-i-dont-get-the-proper-answer/33610 "2020-01-21T07:03:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dialektike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dialektike/32/12141_2.png) [@dialektike](https://discourse.julialang.org/u/dialektike)\
**Post date:** [January 21, 2020, 7:03am UTC](https://discourse.julialang.org/t/ive-written-softmax-function-but-i-dont-get-the-proper-answer/33610/1 "2020-01-21T07:03:25Z")

</div>

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

```julia
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.

```julia
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:

```julia
1.0000000000000002

```

What is the problem?

---

<div class="post-metadata">

**Author:** ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Post date:** [January 21, 2020, 7:33am UTC](https://discourse.julialang.org/t/ive-written-softmax-function-but-i-dont-get-the-proper-answer/33610/2 "2020-01-21T07:33:25Z")

</div>

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/](https://floating-point-gui.de/)

---

<div class="post-metadata">

**Author:** ![dialektike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dialektike/32/12141_2.png) [@dialektike](https://discourse.julialang.org/u/dialektike)\
**Post date:** [January 22, 2020, 4:36am UTC](https://discourse.julialang.org/t/ive-written-softmax-function-but-i-dont-get-the-proper-answer/33610/3 "2020-01-22T04:36:32Z")

</div>

Thank you! Understand.
