fix softmax activation for large values

This commit is contained in:
Leon Chen
2016-10-10 22:24:32 -04:00
parent 2195fe394a
commit b2764cf337
4 changed files with 68 additions and 3 deletions
+16
View File
@@ -52,6 +52,22 @@ describe('activations', function () {
assert.deepEqual(t.tensor.shape, shapeExpected)
assert.isTrue(approxEquals(t.tensor, dataExpected))
})
it('[activations.softmax.2] should work for very large values', function () {
const key = 'activations.softmax.2'
console.log(`\n%c[${key}] 1D, large values`, styles.h3)
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
activations.softmax(t)
const endTime = performance.now()
console.log('%cout', styles.h4, stringifyCondensed(t.tensor))
logTime(startTime, endTime)
const dataExpected = new Float32Array(TEST_DATA[key].expected.data)
const shapeExpected = TEST_DATA[key].expected.shape
assert.deepEqual(t.tensor.shape, shapeExpected)
assert.isTrue(approxEquals(t.tensor, dataExpected))
})
})
/*********************************************************
+4
View File
@@ -12,6 +12,10 @@
input: { data: [0, 0.2, 0.5, -0.1, 1, 2, -0.03, 0.3, 0, 0.8, -0.3, 1], shape: [2, 6] },
expected: { data: [0.067194, 0.082071, 0.110784, 0.0608, 0.182652, 0.4965, 0.107768, 0.149902, 0.11105, 0.247147, 0.082268, 0.301865], shape: [2, 6] }
},
'activations.softmax.2': {
input: { data: [0, 0.2, 0.5, -0.1, 1, 90], shape: [6] },
expected: { data: [0.0, 0.0, 0.0, 0.0, 0.0, 1.0], shape: [6] }
},
'activations.softplus.0': {
input: { data: [0, 0.2, 0.5, -0.1, 1, 2], shape: [6] },
expected: { data: [0.693147, 0.798139, 0.974077, 0.644397, 1.313262, 2.126928], shape: [6] }