From 002e709d621b98ceb4aef7fb174943eb6ccaeb94 Mon Sep 17 00:00:00 2001 From: Fergus McDonald Date: Sun, 25 Oct 2020 15:21:46 -0700 Subject: [PATCH] Added Softmax to Maths folder --- DIRECTORY.md | 2 ++ Maths/Softmax.js | 13 +++++++++++++ Maths/test/Softmax.test.js | 12 ++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 Maths/Softmax.js create mode 100644 Maths/test/Softmax.test.js diff --git a/DIRECTORY.md b/DIRECTORY.md index b2cf2520f4..79f79d9423 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -116,6 +116,7 @@ * [PrimeCheck](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PrimeCheck.js) * [ReversePolishNotation](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/ReversePolishNotation.js) * [SieveOfEratosthenes](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/SieveOfEratosthenes.js) + * [Softmax](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Softmax.js) * test * [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) @@ -141,6 +142,7 @@ * [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) * [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 * [Haversine](https://github.com/TheAlgorithms/Javascript/blob/master/Navigation/Haversine.js) diff --git a/Maths/Softmax.js b/Maths/Softmax.js new file mode 100644 index 0000000000..baeb9fad36 --- /dev/null +++ b/Maths/Softmax.js @@ -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 } diff --git a/Maths/test/Softmax.test.js b/Maths/test/Softmax.test.js new file mode 100644 index 0000000000..179697bcf7 --- /dev/null +++ b/Maths/test/Softmax.test.js @@ -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) + }) +}) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy