Added Softmax to Maths folder (#516)

This commit is contained in:
Fergus McDonald
2020-10-30 23:50:29 -07:00
committed by GitHub
parent 2190b92e1b
commit 9c9640c1c3
3 changed files with 27 additions and 0 deletions

View File

@ -119,6 +119,7 @@
* [PrimeCheck](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PrimeCheck.js) * [PrimeCheck](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PrimeCheck.js)
* [ReversePolishNotation](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/ReversePolishNotation.js) * [ReversePolishNotation](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/ReversePolishNotation.js)
* [SieveOfEratosthenes](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/SieveOfEratosthenes.js) * [SieveOfEratosthenes](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/SieveOfEratosthenes.js)
* [Softmax](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Softmax.js)
* test * test
* [Abs](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/Abs.test.js) * [Abs](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/Abs.test.js)
* [Area](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/Area.test.js) * [Area](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/Area.test.js)
@ -145,6 +146,7 @@
* [PrimeCheck](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/PrimeCheck.test.js) * [PrimeCheck](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/PrimeCheck.test.js)
* [ReversePolishNotation](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/ReversePolishNotation.test.js) * [ReversePolishNotation](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/ReversePolishNotation.test.js)
* [SieveOfEratosthenes](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/SieveOfEratosthenes.test.js) * [SieveOfEratosthenes](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/SieveOfEratosthenes.test.js)
* [Softmax](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/Softmax.test.js)
## Navigation ## Navigation
* [Haversine](https://github.com/TheAlgorithms/Javascript/blob/master/Navigation/Haversine.js) * [Haversine](https://github.com/TheAlgorithms/Javascript/blob/master/Navigation/Haversine.js)

13
Maths/Softmax.js Normal file
View File

@ -0,0 +1,13 @@
// Wikipedia: https://en.wikipedia.org/wiki/Softmax_function
const Softmax = (inputs) => {
const eulerExpOfAllInputs = inputs.map(input => Math.exp(input))
const sumOfEulerExpOfAllInputs = eulerExpOfAllInputs.reduce((a, b) => a + b)
return inputs.map((input) => {
const eulerExpInputs = Math.exp(input)
return eulerExpInputs / sumOfEulerExpOfAllInputs
})
}
export { Softmax }

View File

@ -0,0 +1,12 @@
import { Softmax } from '../Softmax'
describe('Softmax', () => {
it('should return equal distribution of 1 for equal input values', () => {
expect(Softmax([1, 1])).toEqual([0.5, 0.5])
expect(Softmax([1, 1, 1, 1])).toEqual([0.25, 0.25, 0.25, 0.25])
})
it('should return values which sum to the value of 1', () => {
expect(Softmax([1, 2, 3, 4]).reduce((a, b) => a + b, 0)).toEqual(1)
})
})