From 94f4db7dc700c5eb42a43543639775222ceaabd2 Mon Sep 17 00:00:00 2001 From: Albert Ho Date: Fri, 2 Feb 2024 15:11:40 -0800 Subject: [PATCH] update test - loss fn --- binding/nodejs/test/index.test.ts | 40 +++++++++++++------------------ 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/binding/nodejs/test/index.test.ts b/binding/nodejs/test/index.test.ts index d44df29..4829fb0 100644 --- a/binding/nodejs/test/index.test.ts +++ b/binding/nodejs/test/index.test.ts @@ -19,22 +19,6 @@ import { getSystemLibraryPath } from '../src/platforms'; import { getAudioFile } from './test_utils'; const WAV_PATH = 'sample.wav'; -const EXPECTED_VOICE_PROBABILITY_SCORES = [ - 0.00350041501224041, 0.017226237803697586, 0.022328782826662064, - 0.024964014068245888, 0.017900168895721436, 0.028114058077335358, - 0.1203354075551033, 0.11426965147256851, 0.0626743733882904, - 0.028843538835644722, 0.9569169282913208, 0.9879112839698792, - 0.9837538003921509, 0.9286680221557617, 0.9479621648788452, - 0.9499486684799194, 0.9634374976158142, 0.9786311388015747, - 0.9836912155151367, 0.9827406406402588, 0.9849273562431335, - 0.9784052968025208, 0.9812748432159424, 0.9742639660835266, - 0.9664738178253174, 0.9518184661865234, 0.9407353401184082, 0.814495861530304, - 0.118283212184906, 0.01206541433930397, 0.014447852969169617, - 0.015830140560865402, 0.012734980322420597, 0.01062167901545763, - 0.010828903876245022, 0.00736008258536458, 0.014871003106236458, - 0.00925009697675705, 0.009878940880298615, 0.008195844478905201, - 0.008736720308661461, 0.01084984466433525, -]; const libraryPath = getSystemLibraryPath(); @@ -76,14 +60,24 @@ describe('successful processes', () => { it('testing process', () => { let cobraEngine = new Cobra(ACCESS_KEY); - let res = cobraProcessWaveFile(cobraEngine, WAV_PATH); + let probs = cobraProcessWaveFile(cobraEngine, WAV_PATH); + const labels = [ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ]; - res.forEach((voiceProbabilityScore, i) => { - expect(voiceProbabilityScore).toBeCloseTo( - EXPECTED_VOICE_PROBABILITY_SCORES[i], - 3 - ); - }); + expect(labels.length).toBe(probs.length); + + let error = 0; + + for (let i = 0; i < probs.length; i++) { + error -= + labels[i] * Math.log(probs[i]) + + (1 - labels[i]) * Math.log(1 - probs[i]); + } + + error /= probs.length; + expect(error).toBeLessThan(0.1); cobraEngine.release(); });