MCPcopy Create free account
hub / github.com/tensorflow/tfjs-examples / run

Function run

date-conversion-attention/train.js:177–240  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

175}
176
177async function run() {
178 const args = parseArguments();
179 let tfn;
180 if (args.gpu) {
181 console.log('Using GPU');
182 tfn = require('@tensorflow/tfjs-node-gpu');
183 } else {
184 console.log('Using CPU');
185 tfn = require('@tensorflow/tfjs-node');
186 }
187
188 const model = createModel(
189 dateFormat.INPUT_VOCAB.length, dateFormat.OUTPUT_VOCAB.length,
190 dateFormat.INPUT_LENGTH, dateFormat.OUTPUT_LENGTH);
191 model.summary();
192
193 const {
194 trainEncoderInput,
195 trainDecoderInput,
196 trainDecoderOutput,
197 valEncoderInput,
198 valDecoderInput,
199 valDecoderOutput,
200 testDateTuples
201 } = generateDataForTraining(args.trainSplit, args.valSplit);
202
203 await model.fit(
204 [trainEncoderInput, trainDecoderInput], trainDecoderOutput, {
205 epochs: args.epochs,
206 batchSize: args.batchSize,
207 shuffle: true,
208 validationData: [[valEncoderInput, valDecoderInput], valDecoderOutput],
209 callbacks: args.logDir == null ? null :
210 tfn.node.tensorBoard(args.logDir, {updateFreq: args.logUpdateFreq})
211 });
212
213 // Save the model.
214 if (args.savePath != null && args.savePath.length) {
215 if (!fs.existsSync(args.savePath)) {
216 shelljs.mkdir('-p', args.savePath);
217 }
218 const saveURL = `file://${args.savePath}`
219 await model.save(saveURL);
220 console.log(`Saved model to ${saveURL}`);
221 }
222
223 // Run seq2seq inference tests and print the results to console.
224 const numTests = 10;
225 for (let n = 0; n < numTests; ++n) {
226 for (const testInputFn of dateFormat.INPUT_FNS) {
227 const inputStr = testInputFn(testDateTuples[n]);
228 console.log('\n-----------------------');
229 console.log(`Input string: ${inputStr}`);
230 const correctAnswer =
231 dateFormat.dateTupleToYYYYDashMMDashDD(testDateTuples[n]);
232 console.log(`Correct answer: ${correctAnswer}`);
233
234 const {outputStr} = await runSeq2SeqInference(model, inputStr);

Callers 1

train.jsFile · 0.70

Calls 4

createModelFunction · 0.90
runSeq2SeqInferenceFunction · 0.90
generateDataForTrainingFunction · 0.85
parseArgumentsFunction · 0.70

Tested by

no test coverage detected