-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
model.js
203 lines (184 loc) · 6.66 KB
/
model.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import * as tf from '@tensorflow/tfjs';
import * as dateFormat from './date_format';
/**
* A custom layer used to obtain the last time step of an RNN sequential
* output.
*/
class GetLastTimestepLayer extends tf.layers.Layer {
constructor(config) {
super(config || {});
this.supportMasking = true;
}
computeOutputShape(inputShape) {
const outputShape = inputShape.slice();
outputShape.splice(outputShape.length - 2, 1);
return outputShape;
}
call(input) {
if (Array.isArray(input)) {
input = input[0];
}
const inputRank = input.shape.length;
tf.util.assert(inputRank === 3, `Invalid input rank: ${inputRank}`);
return input.gather([input.shape[1] - 1], 1).squeeze([1]);
}
static get className() {
return 'GetLastTimestepLayer';
}
}
tf.serialization.registerClass(GetLastTimestepLayer);
/**
* Create an LSTM-based attention model for date conversion.
*
* @param {number} inputVocabSize Input vocabulary size. This includes
* the padding symbol. In the context of this model, "vocabulary" means
* the set of all unique characters that might appear in the input date
* string.
* @param {number} outputVocabSize Output vocabulary size. This includes
* the padding and starting symbols. In the context of this model,
* "vocabulary" means the set of all unique characters that might appear in
* the output date string.
* @param {number} inputLength Maximum input length (# of characters). Input
* sequences shorter than the length must be padded at the end.
* @param {number} outputLength Output length (# of characters).
* @return {tf.Model} A compiled model instance.
*/
export function createModel(
inputVocabSize, outputVocabSize, inputLength, outputLength) {
const embeddingDims = 64;
const lstmUnits = 64;
const encoderInput = tf.input({shape: [inputLength]});
const decoderInput = tf.input({shape: [outputLength]});
let encoder = tf.layers.embedding({
inputDim: inputVocabSize,
outputDim: embeddingDims,
inputLength,
maskZero: true
}).apply(encoderInput);
encoder = tf.layers.lstm({
units: lstmUnits,
returnSequences: true
}).apply(encoder);
const encoderLast = new GetLastTimestepLayer({
name: 'encoderLast'
}).apply(encoder);
let decoder = tf.layers.embedding({
inputDim: outputVocabSize,
outputDim: embeddingDims,
inputLength: outputLength,
maskZero: true
}).apply(decoderInput);
decoder = tf.layers.lstm({
units: lstmUnits,
returnSequences: true
}).apply(decoder, {initialState: [encoderLast, encoderLast]});
let attention = tf.layers.dot({axes: [2, 2]}).apply([decoder, encoder]);
attention = tf.layers.activation({
activation: 'softmax',
name: 'attention'
}).apply(attention);
const context = tf.layers.dot({
axes: [2, 1],
name: 'context'
}).apply([attention, encoder]);
const decoderCombinedContext =
tf.layers.concatenate().apply([context, decoder]);
let output = tf.layers.timeDistributed({
layer: tf.layers.dense({
units: lstmUnits,
activation: 'tanh'
})
}).apply(decoderCombinedContext);
output = tf.layers.timeDistributed({
layer: tf.layers.dense({
units: outputVocabSize,
activation: 'softmax'
})
}).apply(output);
const model = tf.model({
inputs: [encoderInput, decoderInput],
outputs: output
});
model.compile({
loss: 'categoricalCrossentropy',
optimizer: 'adam'
});
return model;
}
/**
* Perform sequence-to-sequence decoding for date conversion.
*
* @param {tf.Model} model The model to be used for the sequence-to-sequence
* decoding, with two inputs:
* 1. Encoder input of shape `[numExamples, inputLength]`
* 2. Decoder input of shape `[numExamples, outputLength]`
* and one output:
* 1. Decoder softmax probability output of shape
* `[numExamples, outputLength, outputVocabularySize]`
* @param {string} inputStr Input date string to be converted.
* @return {{outputStr: string, attention?: tf.Tensor}}
* - The `outputStr` field is the output date string.
* - If and only if `getAttention` is `true`, the `attention` field will
* be populated by attention matrix as a `tf.Tensor` of
* dtype `float32` and shape `[]`.
*/
export async function runSeq2SeqInference(
model, inputStr, getAttention = false) {
return tf.tidy(() => {
const encoderInput = dateFormat.encodeInputDateStrings([inputStr]);
const decoderInput = tf.buffer([1, dateFormat.OUTPUT_LENGTH]);
decoderInput.set(dateFormat.START_CODE, 0, 0);
for (let i = 1; i < dateFormat.OUTPUT_LENGTH; ++i) {
const predictOut = model.predict(
[encoderInput, decoderInput.toTensor()]);
const output = predictOut.argMax(2).dataSync()[i - 1];
predictOut.dispose();
decoderInput.set(output, 0, i);
}
const output = {outputStr: ''};
// The `tf.Model` instance used for the final time step varies depending on
// whether the attention matrix is requested or not.
let finalStepModel = model;
if (getAttention) {
// If the attention matrix is requested, construct a two-output model.
// - The 1st output is the original decoder output.
// - The 2nd output is the attention matrix.
finalStepModel = tf.model({
inputs: model.inputs,
outputs: model.outputs.concat([model.getLayer('attention').output])
});
}
const finalPredictOut = finalStepModel.predict(
[encoderInput, decoderInput.toTensor()]);
let decoderFinalOutput; // The decoder's final output.
if (getAttention) {
decoderFinalOutput = finalPredictOut[0];
output.attention = finalPredictOut[1];
} else {
decoderFinalOutput = finalPredictOut;
}
decoderFinalOutput =
decoderFinalOutput.argMax(2).dataSync()[dateFormat.OUTPUT_LENGTH - 1];
for (let i = 1; i < decoderInput.shape[1]; ++i) {
output.outputStr += dateFormat.OUTPUT_VOCAB[decoderInput.get(0, i)];
}
output.outputStr += dateFormat.OUTPUT_VOCAB[decoderFinalOutput];
return output;
});
}