-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
334 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
'use strict'; | ||
|
||
import {concat} from './concat.js'; | ||
import {lstmCell} from './lstm_cell.js'; | ||
import {reshape} from './reshape.js'; | ||
import {sizeOfShape, Tensor} from './lib/tensor.js'; | ||
import {sigmoid} from './sigmoid.js'; | ||
import {slice} from './slice.js'; | ||
import {squeeze} from './squeeze.js'; | ||
import {tanh} from './tanh.js'; | ||
import {validateLstmParams} from './lib/validate-input.js'; | ||
|
||
/** | ||
*Long Short-Term Memory [LSTM] recurrent network uses an input, output, forget, | ||
*and cell gate to compute the output state that rolls into the output across the | ||
* temporal sequence of the network. | ||
* @param {Tensor} input | ||
* @param {Tensor} weight | ||
* @param {Tensor} recurrentWeight | ||
* @param {Number} steps | ||
* @param {Number} hiddenSize | ||
* @param {MLLstmOptions} options | ||
* @return {Array.<Tensor>} | ||
*/ | ||
|
||
export function lstm(input, weight, recurrentWeight, steps, hiddenSize, | ||
{bias, recurrentBias, peepholeWeight, initialHiddenState, | ||
initialCellState, returnSequence = false, direction = 'forward', layout = 'iofg', | ||
activations = [sigmoid, tanh, tanh]}={}) { | ||
validateLstmParams(...arguments); | ||
const numDirections = (direction == 'both' ? 2 : 1); | ||
const batchSize = input.shape[1]; | ||
const inputSize = input.shape[2]; | ||
|
||
let hiddenState; | ||
let cellState; | ||
if (initialHiddenState) { | ||
hiddenState = initialHiddenState; | ||
} else { | ||
const initialHiddenStateShape = [numDirections, batchSize, hiddenSize]; | ||
hiddenState = new Tensor( | ||
initialHiddenStateShape, new Array(sizeOfShape(initialHiddenStateShape)).fill(0)); | ||
} | ||
if (initialCellState) { | ||
cellState = initialCellState; | ||
} else { | ||
const initialCellState = [numDirections, batchSize, hiddenSize]; | ||
cellState = new Tensor( | ||
initialCellState, new Array(sizeOfShape(initialCellState)).fill(0)); | ||
} | ||
|
||
let sequence; | ||
const currentWeight = []; | ||
const currentRecurrentWeight = []; | ||
const currentBias = []; | ||
const currentRecurrentBias = []; | ||
const currentPeepholeWeight = []; | ||
|
||
for (let dir = 0; dir < numDirections; ++dir) { | ||
currentWeight.push(squeeze(slice(weight, [dir, 0, 0], [1, 4 * hiddenSize, inputSize]))); | ||
currentRecurrentWeight.push(squeeze(slice(recurrentWeight, | ||
[dir, 0, 0], [1, 4 * hiddenSize, hiddenSize]))); | ||
currentBias.push(bias ? (squeeze(slice(bias, [dir, 0], [1, 4 * hiddenSize]))) : null); | ||
currentRecurrentBias.push(recurrentBias ? | ||
(squeeze(slice(recurrentBias, [dir, 0], [1, 4 * hiddenSize]))) : null); | ||
currentPeepholeWeight.push(peepholeWeight ? | ||
(squeeze(slice(peepholeWeight, [dir, 0], [1, 3 * hiddenSize]))) : null); | ||
} | ||
|
||
for (let step = 0; step < steps; ++step) { | ||
const currentHidden = []; | ||
const currentCell = []; | ||
let nextHidden = null; | ||
let nextCell = null; | ||
|
||
for (let dir = 0; dir < numDirections; ++dir) { | ||
currentHidden.push(squeeze(slice(hiddenState, [dir, 0, 0], [1, batchSize, hiddenSize]))); | ||
currentCell.push(squeeze(slice(cellState, [dir, 0, 0], [1, batchSize, hiddenSize]))); | ||
} | ||
|
||
for (let dir = 0; dir < numDirections; ++dir) { | ||
const slice0 = (dir == 1 || direction == 'backward' ? steps - step - 1 : step); | ||
const currentInput = squeeze(slice(input, [slice0, 0, 0], [1, batchSize, inputSize])); | ||
|
||
const results = lstmCell( | ||
currentInput, currentWeight[dir], currentRecurrentWeight[dir], | ||
currentHidden[dir], currentCell[dir], hiddenSize, {bias: currentBias[dir], | ||
recurrentBias: currentRecurrentBias[dir], peepholeWeight: currentPeepholeWeight[dir], | ||
layout: layout, activations: activations}); | ||
|
||
const output = reshape(results[0], [1, null, hiddenSize]); | ||
const cell = reshape(results[1], [1, null, hiddenSize]); | ||
|
||
nextHidden = (nextHidden ? concat([nextHidden, output], 0) : output); | ||
nextCell = (nextCell ? concat([nextCell, cell], 0) : cell); | ||
} | ||
|
||
hiddenState = nextHidden; | ||
cellState = nextCell; | ||
|
||
if (returnSequence) { | ||
nextHidden = reshape(nextHidden, [1, numDirections, null, hiddenSize]); | ||
sequence = (sequence ? concat([sequence, nextHidden], 0) : nextHidden); | ||
} | ||
} | ||
|
||
return (sequence ? [hiddenState, cellState, sequence] : [hiddenState, cellState]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
'use strict'; | ||
|
||
import {lstm} from '../src/lstm.js'; | ||
import {relu} from '../src/relu.js'; | ||
import {Tensor} from '../src/lib/tensor.js'; | ||
import * as utils from './utils.js'; | ||
|
||
describe('test lstm', function() { | ||
it('lstm returnSequence=true ' + | ||
'activations=[relu, relu, relu]', function() { | ||
const steps = 1; | ||
const numDirections = 1; | ||
const batchSize = 2; | ||
const inputSize = 2; | ||
const hiddenSize = 2; | ||
const input = new Tensor([steps, batchSize, inputSize], new Float32Array([1, 2, 2, 1])); | ||
const weight = new Tensor([numDirections, 4 * hiddenSize, inputSize], | ||
new Float32Array([ | ||
1, -1, 2, -2, 1, -1, 2, -2, | ||
1, -1, 2, -2, 1, -1, 2, -2, | ||
])); | ||
const recurrentWeight = new Tensor([numDirections, 4 * hiddenSize, hiddenSize], | ||
new Array(4 * hiddenSize * hiddenSize).fill(0.1)); | ||
const bias = new Tensor([numDirections, 4 * hiddenSize], | ||
new Float32Array([ | ||
1, 2, 1, 2, 1, 2, 1, 2, | ||
])); | ||
const recurrentBias = new Tensor([numDirections, 4 * hiddenSize], | ||
new Float32Array([ | ||
1, 2, 1, 2, 1, 2, 1, 2, | ||
])); | ||
const peepholeWeight = new Tensor([numDirections, 3 * hiddenSize], | ||
new Float32Array(3 * hiddenSize).fill(0)); | ||
const initialHiddenState = new Tensor([numDirections, batchSize, hiddenSize], | ||
new Float32Array(batchSize * hiddenSize).fill(0)); | ||
const initialCellState = new Tensor([numDirections, batchSize, hiddenSize], | ||
new Float32Array(batchSize * hiddenSize).fill(0)); | ||
const returnSequence = true; | ||
const activations = [ | ||
relu, | ||
relu, | ||
relu, | ||
]; | ||
const outputs = lstm( | ||
input, weight, recurrentWeight, steps, hiddenSize, | ||
{bias, recurrentBias, peepholeWeight, initialHiddenState, | ||
initialCellState, returnSequence, activations}); | ||
console.log('outputs: ', outputs); | ||
utils.checkShape(outputs[0], [numDirections, batchSize, hiddenSize]); | ||
utils.checkShape(outputs[1], [numDirections, batchSize, hiddenSize]); | ||
utils.checkShape(outputs[2], [steps, numDirections, batchSize, hiddenSize]); | ||
const expected = [ | ||
[ | ||
1, 8, 27, 216, | ||
], | ||
[ | ||
1, 4, 9, 36, | ||
], | ||
[ | ||
1, 8, 27, 216, | ||
], | ||
]; | ||
for (let i = 0; i < expected.length; ++i) { | ||
utils.checkValue(outputs[i], expected[i]); | ||
} | ||
}); | ||
|
||
it('lstm steps=2 direction="backward" returnSequence=true' + | ||
'activations=[relu, relu, relu]', function() { | ||
const steps = 2; | ||
const numDirections = 1; | ||
const batchSize = 2; | ||
const inputSize = 2; | ||
const hiddenSize = 2; | ||
const input = new Tensor([steps, batchSize, inputSize], | ||
new Float32Array([1, 2, 2, 1, 3, 4, 1, 2])); | ||
const weight = new Tensor([numDirections, 4 * hiddenSize, inputSize], | ||
new Float32Array([ | ||
1, -1, 2, -2, 1, -1, 2, -2, | ||
1, -1, 2, -2, 1, -1, 2, -2, | ||
])); | ||
const recurrentWeight = new Tensor([numDirections, 4 * hiddenSize, hiddenSize], | ||
new Array(4 * hiddenSize * hiddenSize).fill(0.1)); | ||
const bias = new Tensor([numDirections, 4 * hiddenSize], | ||
new Float32Array([ | ||
1, 2, 1, 2, 1, 2, 1, 2, | ||
])); | ||
const recurrentBias = new Tensor([numDirections, 4 * hiddenSize], | ||
new Float32Array([ | ||
1, 2, 1, 2, 1, 2, 1, 2, | ||
])); | ||
const peepholeWeight = new Tensor([numDirections, 3 * hiddenSize], | ||
new Float32Array(3 * hiddenSize).fill(0)); | ||
const initialHiddenState = new Tensor([numDirections, batchSize, hiddenSize], | ||
new Float32Array(batchSize * hiddenSize).fill(0)); | ||
const initialCellState = new Tensor([numDirections, batchSize, hiddenSize], | ||
new Float32Array(batchSize * hiddenSize).fill(0)); | ||
const returnSequence = true; | ||
const direction = 'backward'; | ||
const activations = [ | ||
relu, | ||
relu, | ||
relu, | ||
]; | ||
const outputs = lstm( | ||
input, weight, recurrentWeight, steps, hiddenSize, | ||
{bias, recurrentBias, peepholeWeight, initialHiddenState, | ||
initialCellState, direction, returnSequence, activations}); | ||
console.log('outputs: ', outputs); | ||
utils.checkShape(outputs[0], [numDirections, batchSize, hiddenSize]); | ||
utils.checkShape(outputs[1], [numDirections, batchSize, hiddenSize]); | ||
utils.checkShape(outputs[2], [steps, numDirections, batchSize, hiddenSize]); | ||
const expected = [ | ||
[10.469, 58.02899999999999, 74.529, 518.9490000000001], | ||
[5.51, 20.009999999999998, 19.11, 75.21000000000001], | ||
[ | ||
1, | ||
8, | ||
1, | ||
8, | ||
10.469, | ||
58.02899999999999, | ||
74.529, | ||
518.9490000000001, | ||
], | ||
]; | ||
for (let i = 0; i < expected.length; ++i) { | ||
utils.checkValue(outputs[i], expected[i]); | ||
} | ||
}); | ||
}); |