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

Fix LSTM training continuity for cloned nets #951

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions src/recurrent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,15 @@ export class Recurrent<
}
return null;
}

fromJSON(json: any): void {
super.fromJSON(json);
this._layerSets = json.layerSets.map((layerSet: any) =>
layerSet.map((layer: any) => {
const newLayer = new (layer.constructor as any)();
newLayer.fromJSON(layer);
return newLayer;
})
);
}
}
47 changes: 47 additions & 0 deletions src/recurrent/lstm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,51 @@ describe('LSTM', () => {
expect(net.run([transactionTypes.other])).toBe('other');
});
});

describe('cloned LSTM net training', () => {
it('continues evolving from the point where the original stopped', () => {
const net = new LSTM({ hiddenLayers: [60, 60] });
net.maxPredictionLength = 100;

const trainData = [
'doe, a deer, a female deer',
'ray, a drop of golden sun',
'me, a name I call myself',
];

// First train
net.train(trainData, {
iterations: 5000,
log: true,
logPeriod: 500,
learningRate: 0.2,
});

// Clone the net:
const net2 = new LSTM({ hiddenLayers: [60, 60] });
net2.fromJSON(net.toJSON());

// Both output the same text:
expect(net.run('ray')).toBe(net2.run('ray'));

// More training, start from the last error rate:
net.train(trainData, {
iterations: 30,
log: true,
logPeriod: 10,
learningRate: 0.2,
});

// More training to the clone:
net2.train(trainData, {
iterations: 30,
log: true,
logPeriod: 10,
learningRate: 0.2,
});

// The first reduced the quality, but the second is crazy:
expect(net.run('ray')).not.toBe(net2.run('ray'));
});
});
});