Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support lstm and lstmCell operations #227

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/nn/ops/lstm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export class Lstm extends Operation {
undefined);
currentPeepholeWeight.push(peepholeWeight ?
(tf.squeeze(
tf.slice(peepholeWeight, [dir, 0], [1, 4 * hiddenSize]), [0])) :
tf.slice(peepholeWeight, [dir, 0], [1, 3 * hiddenSize]), [0])) :
undefined);
}

Expand Down Expand Up @@ -359,14 +359,16 @@ export class LstmCell extends Operation {
const zero = tf.scalar(0);
const starts = layout === MLLstmWeightLayout.iofg ?
{i: 0, o: hiddenSize, f: 2 * hiddenSize, g: 3 * hiddenSize} :
/*ifog*/ {i: 0, f: hiddenSize, o: 2 * hiddenSize, g: 3 * hiddenSize};
/*ifgo*/ {i: 0, f: hiddenSize, g: 2 * hiddenSize, 0: 3 * hiddenSize};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does 0: mean? I didn't know that key names were even allowed to start with digits :b (I figured they followed the same rules as other identifiers where they may contain digits but must start with a letter).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does 0: mean?

It's a typo error, I've fixed it with last commit.


// input gate (i)
const i = activation0.runOp(tf.add(
tf.mul(
cellState,
// The pack ordering of the weight vectors is for the
// input (i), output (o), and forget (f) gate respectively.
(peepholeWeight ?
tf.slice(peepholeWeight, [starts.i], [hiddenSize]) : zero)),
tf.slice(peepholeWeight, [0], [hiddenSize]) : zero)),
tf.add(
tf.add(
(bias ? tf.slice(bias, [starts.i], [hiddenSize]) : zero),
Expand All @@ -388,7 +390,7 @@ export class LstmCell extends Operation {
tf.mul(
cellState,
(peepholeWeight ?
tf.slice(peepholeWeight, [starts.f], [hiddenSize]) :
tf.slice(peepholeWeight, [2 * hiddenSize], [hiddenSize]) :
zero)),
tf.add(
tf.add(
Expand Down Expand Up @@ -430,7 +432,7 @@ export class LstmCell extends Operation {
tf.mul(
cellState,
(peepholeWeight ?
tf.slice(peepholeWeight, [starts.o], [hiddenSize]) :
tf.slice(peepholeWeight, [hiddenSize], [hiddenSize]) :
zero)),
tf.add(
tf.add(
Expand Down
12 changes: 6 additions & 6 deletions test/ops/lstm.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ describe('test lstm', () => {
1, 2, 1, 2, 1, 2, 1, 2,
]));
const peepholeWeight = builder.constant(
{type: 'float32', dimensions: [4 * hiddenSize]},
new Float32Array(4 * hiddenSize).fill(0));
{type: 'float32', dimensions: [3 * hiddenSize]},
new Float32Array(3 * hiddenSize).fill(0));
const activations = [
builder.relu(),
builder.relu(),
Expand Down Expand Up @@ -112,8 +112,8 @@ describe('test lstm', () => {
1, 2, 1, 2, 1, 2, 1, 2,
]));
const peepholeWeight = builder.constant(
{type: 'float32', dimensions: [numDirections, 4 * hiddenSize]},
new Float32Array(4 * hiddenSize).fill(0));
{type: 'float32', dimensions: [numDirections, 3 * hiddenSize]},
new Float32Array(3 * hiddenSize).fill(0));
const initialHiddenState = builder.constant(
{type: 'float32', dimensions: [numDirections, batchSize, hiddenSize]},
new Float32Array(batchSize * hiddenSize).fill(0));
Expand Down Expand Up @@ -193,8 +193,8 @@ describe('test lstm', () => {
1, 2, 1, 2, 1, 2, 1, 2,
]));
const peepholeWeight = builder.constant(
{type: 'float32', dimensions: [numDirections, 4 * hiddenSize]},
new Float32Array(4 * hiddenSize).fill(0));
{type: 'float32', dimensions: [numDirections, 3 * hiddenSize]},
new Float32Array(3 * hiddenSize).fill(0));
const initialHiddenState = builder.constant(
{type: 'float32', dimensions: [numDirections, batchSize, hiddenSize]},
new Float32Array(batchSize * hiddenSize).fill(0));
Expand Down