Skip to content

Commit

Permalink
Add softmax test cases
Browse files Browse the repository at this point in the history
- the sum of the softmax output should be 1
- the softmax output should be between 0 and 1
  • Loading branch information
kitsuyui committed Dec 27, 2024
1 parent af1ddc2 commit 11903c9
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/mymath/src/softmax/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { describe, expect, it, jest } from '@jest/globals'

import { sum } from '../array'
import { softmax, softmaxWithTemperature } from './index'

describe('softmaxWithTemperature', () => {
it('should return the correct result', () => {
const weights = [1, 2, 3, 4, 5]
const temperature = 1
const result = softmaxWithTemperature(weights, temperature)
expect(softmaxWithTemperature(weights, temperature)).toEqual([
0.011656230956039605, 0.03168492079612427, 0.0861285444362687,
0.23412165725273662, 0.6364086465588308,
])
expect(sum(result)).toBeCloseTo(1)
for (const probability of result) {
expect(probability).toBeGreaterThanOrEqual(0)
expect(probability).toBeLessThanOrEqual(1)
}
})

it('should return an empty array when the input is an empty array', () => {
Expand All @@ -22,10 +29,16 @@ describe('softmaxWithTemperature', () => {
describe('softmax', () => {
it('should return the correct result', () => {
const weights = [1, 2, 3, 4, 5]
expect(softmax(weights)).toEqual([
const result = softmax(weights)
expect(result).toEqual([
0.011656230956039605, 0.03168492079612427, 0.0861285444362687,
0.23412165725273662, 0.6364086465588308,
])
expect(sum(result)).toBeCloseTo(1)
for (const probability of result) {
expect(probability).toBeGreaterThanOrEqual(0)
expect(probability).toBeLessThanOrEqual(1)
}
})

it('should return an empty array when the input is an empty array', () => {
Expand Down

0 comments on commit 11903c9

Please sign in to comment.