From d4892e1ba51cfbd883d45071803a69d204650e91 Mon Sep 17 00:00:00 2001 From: Leon Chen Date: Sun, 21 Aug 2016 21:48:05 -0400 Subject: [PATCH] setup test files --- index.html | 27 +++++++++ test/activations.js | 136 ++++++++++++++++++++++++++++++++++++++++++++ test/test-utils.js | 34 +++++++++++ 3 files changed, 197 insertions(+) create mode 100644 index.html create mode 100644 test/activations.js create mode 100644 test/test-utils.js diff --git a/index.html b/index.html new file mode 100644 index 0000000..ffafd62 --- /dev/null +++ b/index.html @@ -0,0 +1,27 @@ + + + + Keras-js | Mocha Tests + + + +
+ + + + + + + + + + + + + + + diff --git a/test/activations.js b/test/activations.js new file mode 100644 index 0000000..bb13f9b --- /dev/null +++ b/test/activations.js @@ -0,0 +1,136 @@ +/* eslint-env browser, mocha */ + +const assert = chai.assert +const activations = KerasJS.activations + +const styles = testUtils.styles +const approxEquals = testUtils.approxEquals +const logTime = testUtils.logTime + +describe('activations', function () { + describe('softmax', function () { + it('should work for 1D tensor', function () { + console.log('\n%cactivations', styles.h1) + console.log('\n%csoftmax', styles.h2) + console.log('\n%c1D', styles.h3) + let t = new KerasJS.Tensor([0, 0.2, 0.5, -0.1, 1, 2], [6]) + console.log('%cin', styles.h4, t) + const startTime = performance.now() + activations.softmax(t) + const endTime = performance.now() + console.log('%cout', styles.h4, t) + logTime(startTime, endTime) + const dataOut = t.tensor.data + const shapeOut = t.tensor.shape + const dataExpected = new Float32Array([0.067194, 0.082071, 0.110784, 0.0608, 0.182652, 0.4965]) + const shapeExpected = [6] + assert.deepEqual(shapeOut, shapeExpected) + assert.isTrue(approxEquals(dataOut, dataExpected)) + }) + + it('should work for 2D tensor', function () { + console.log('\n%c2D', styles.h3) + let t = new KerasJS.Tensor([0, 0.2, 0.5, -0.1, 1, 2, -0.03, 0.3, 0, 0.8, -0.3, 1], [2, 6]) + console.log('%cin', styles.h4, t) + const startTime = performance.now() + activations.softmax(t) + const endTime = performance.now() + console.log('%cout', styles.h4, t) + logTime(startTime, endTime) + const dataOut = t.tensor.data + const shapeOut = t.tensor.shape + const dataExpected = new Float32Array([0.067194, 0.082071, 0.110784, 0.0608, 0.182652, 0.4965, 0.107768, 0.149902, 0.11105, 0.247147, 0.082268, 0.301865]) + const shapeExpected = [2, 6] + assert.deepEqual(shapeOut, shapeExpected) + assert.isTrue(approxEquals(dataOut, dataExpected)) + }) + }) + + describe('relu', function () { + it('should work for 1D tensor', function () { + console.log('\n%crelu', styles.h2) + console.log('\n%c1D', styles.h3) + let t = new KerasJS.Tensor([0, 0.2, 0.5, -0.1, 1, 2], [6]) + console.log('%cin', styles.h4, t) + const startTime = performance.now() + activations.relu(t) + const endTime = performance.now() + console.log('%cout', styles.h4, t) + logTime(startTime, endTime) + const dataOut = t.tensor.data + const shapeOut = t.tensor.shape + const dataExpected = new Float32Array([0.0, 0.2, 0.5, 0.0, 1.0, 2.0]) + const shapeExpected = [6] + assert.deepEqual(shapeOut, shapeExpected) + assert.isTrue(approxEquals(dataOut, dataExpected)) + }) + + it('should work for 2D tensor', function () { + console.log('\n%c2D', styles.h3) + let t = new KerasJS.Tensor([0, 0.2, 0.5, -0.1, 1, 2, -0.03, 0.3, 0, 0.8, -0.3, 1], [2, 6]) + console.log('%cin', styles.h4, t) + const startTime = performance.now() + activations.relu(t) + const endTime = performance.now() + console.log('%cout', styles.h4, t) + logTime(startTime, endTime) + const dataOut = t.tensor.data + const shapeOut = t.tensor.shape + const dataExpected = new Float32Array([0.0, 0.2, 0.5, 0.0, 1.0, 2.0, 0.0, 0.3, 0.0, 0.8, 0.0, 1.0]) + const shapeExpected = [2, 6] + assert.deepEqual(shapeOut, shapeExpected) + assert.isTrue(approxEquals(dataOut, dataExpected)) + }) + + it('should work for 3D tensor', function () { + console.log('\n%c3D', styles.h3) + let t = new KerasJS.Tensor([0, 0.2, -0.5, -0.1, 1, 2, -0.03, 2.3, 0, 0.8, -0.3, 1], [2, 2, 3]) + console.log('%cin', styles.h4, t) + const startTime = performance.now() + activations.relu(t) + const endTime = performance.now() + console.log('%cout', styles.h4, t) + logTime(startTime, endTime) + const dataOut = t.tensor.data + const shapeOut = t.tensor.shape + const dataExpected = new Float32Array([0.0, 0.2, 0.0, 0.0, 1.0, 2.0, 0.0, 2.3, 0.0, 0.8, 0.0, 1.0]) + const shapeExpected = [2, 2, 3] + assert.deepEqual(shapeOut, shapeExpected) + assert.isTrue(approxEquals(dataOut, dataExpected)) + }) + + it('should work with maxValue', function () { + console.log('\n%c3D, maxValue=0.5', styles.h3) + let t = new KerasJS.Tensor([0, 0.2, -0.5, -0.1, 1, 2, -0.03, 2.3, 0, 0.8, -0.3, 1], [2, 2, 3]) + console.log('%cin', styles.h4, t) + const startTime = performance.now() + activations.relu(t, { maxValue: 0.5 }) + const endTime = performance.now() + console.log('%cout', styles.h4, t) + logTime(startTime, endTime) + const dataOut = t.tensor.data + const shapeOut = t.tensor.shape + const dataExpected = new Float32Array([0.0, 0.2, 0.0, 0.0, 0.5, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5]) + const shapeExpected = [2, 2, 3] + assert.deepEqual(shapeOut, shapeExpected) + assert.isTrue(approxEquals(dataOut, dataExpected)) + }) + + it('should work with alpha (slope of negative portion)', function () { + console.log('\n%c3D, alpha=0.3', styles.h3) + let t = new KerasJS.Tensor([0, 0.2, -0.5, -0.1, 1, 2, -0.03, 2.3, 0, 0.8, -0.3, 1], [2, 2, 3]) + console.log('%cin', styles.h4, t) + const startTime = performance.now() + activations.relu(t, { alpha: 0.3 }) + const endTime = performance.now() + console.log('%cout', styles.h4, t) + logTime(startTime, endTime) + const dataOut = t.tensor.data + const shapeOut = t.tensor.shape + const dataExpected = new Float32Array([0.0, 0.2, -0.15, -0.03, 1.0, 2.0, -0.009, 2.3, 0.0, 0.8, -0.09, 1.0]) + const shapeExpected = [2, 2, 3] + assert.deepEqual(shapeOut, shapeExpected) + assert.isTrue(approxEquals(dataOut, dataExpected)) + }) + }) +}) diff --git a/test/test-utils.js b/test/test-utils.js new file mode 100644 index 0000000..cea2dc2 --- /dev/null +++ b/test/test-utils.js @@ -0,0 +1,34 @@ +(function () { + 'use strict' + + const styles = { + h1: 'color:#001f3f;font-weight:bold;font-size:160%;', + h2: 'color:#0074D9;font-weight:bold;font-size:130%;', + h3: 'color:#FF4136;font-weight:bold;font-size:110%;', + h4: 'color:#AAAAAA;font-size:100%;', + time: 'color:#2ECC40;font-weight:bold;font-size:100%;' + } + + function approxEquals (a, b, tol = 1e-6) { + if (a.length !== b.length) return false + for (let i = 0; i < a.length; i++) { + if ( + a[i] < (b[i] - tol) || + a[i] > (b[i] + tol) + ) { + return false + } + } + return true + } + + function logTime (startTime, endTime) { + console.log(`%c>>>> exec: ${Math.round(100 * (endTime - startTime)) / 100} ms`, styles.time) + } + + window.testUtils = { + styles, + approxEquals, + logTime + } +})()