mirror of
https://github.com/wassname/keras-js.git
synced 2026-09-12 12:33:40 +08:00
setup test files
This commit is contained in:
+27
@@ -0,0 +1,27 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Keras-js | Mocha Tests</title>
|
||||
<link href="https://cdn.rawgit.com/mochajs/mocha/v3.0.2/mocha.css" rel="stylesheet" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="mocha"></div>
|
||||
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.0.2/mocha.min.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.min.js"></script>
|
||||
|
||||
<script src="/lib/gpu.min.js"></script>
|
||||
<script src="/lib/weblas.js"></script>
|
||||
<script src="/dist/keras.js"></script>
|
||||
|
||||
<script>mocha.setup('bdd')</script>
|
||||
<script src="/test/test-utils.js"></script>
|
||||
<script src="/test/activations.js"></script>
|
||||
<script>
|
||||
mocha.checkLeaks();
|
||||
mocha.globals(['jQuery']);
|
||||
mocha.run();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -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))
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -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
|
||||
}
|
||||
})()
|
||||
Reference in New Issue
Block a user