|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2019 Google LLC. All Rights Reserved. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * ============================================================================= |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * Addition RNN example. |
| 20 | + * |
| 21 | + * Based on tfjs example: |
| 22 | + * https://github.com/tensorflow/tfjs-examples/tree/master/addition-rnn |
| 23 | + */ |
| 24 | + |
| 25 | +import * as tfvis from '@tensorflow/tfjs-vis'; |
| 26 | +const worker = new Worker('./worker.js'); |
| 27 | + |
| 28 | +async function runAdditionRNNDemo() { |
| 29 | + document.getElementById('trainModel').addEventListener('click', async () => { |
| 30 | + const digits = +(document.getElementById('digits')).value; |
| 31 | + const trainingSize = +(document.getElementById('trainingSize')).value; |
| 32 | + const rnnTypeSelect = document.getElementById('rnnType'); |
| 33 | + const rnnType = |
| 34 | + rnnTypeSelect.options[rnnTypeSelect.selectedIndex].getAttribute( |
| 35 | + 'value'); |
| 36 | + const layers = +(document.getElementById('rnnLayers')).value; |
| 37 | + const hiddenSize = +(document.getElementById('rnnLayerSize')).value; |
| 38 | + const batchSize = +(document.getElementById('batchSize')).value; |
| 39 | + const trainIterations = +(document.getElementById('trainIterations')).value; |
| 40 | + const numTestExamples = +(document.getElementById('numTestExamples')).value; |
| 41 | + |
| 42 | + // Do some checks on the user-specified parameters. |
| 43 | + const status = document.getElementById('trainStatus'); |
| 44 | + if (digits < 1 || digits > 5) { |
| 45 | + status.textContent = 'digits must be >= 1 and <= 5'; |
| 46 | + return; |
| 47 | + } |
| 48 | + const trainingSizeLimit = Math.pow(Math.pow(10, digits), 2); |
| 49 | + if (trainingSize > trainingSizeLimit) { |
| 50 | + status.textContent = |
| 51 | + `With digits = ${digits}, you cannot have more than ` + |
| 52 | + `${trainingSizeLimit} examples`; |
| 53 | + return; |
| 54 | + } |
| 55 | + worker.postMessage({ digits, trainingSize, rnnType, layers, hiddenSize, trainIterations, batchSize, numTestExamples }); |
| 56 | + worker.addEventListener('message', (e) => { |
| 57 | + if (e.data.isPredict) { |
| 58 | + const { i, iterations, modelFitTime, lossValues, accuracyValues } = e.data; |
| 59 | + document.getElementById('trainStatus').textContent = |
| 60 | + `Iteration ${i + 1} of ${iterations}: ` + |
| 61 | + `Time per iteration: ${modelFitTime.toFixed(3)} (seconds)`; |
| 62 | + const lossContainer = document.getElementById('lossChart'); |
| 63 | + tfvis.render.linechart( |
| 64 | + lossContainer, { values: lossValues, series: ['train', 'validation'] }, |
| 65 | + { |
| 66 | + width: 420, |
| 67 | + height: 300, |
| 68 | + xLabel: 'epoch', |
| 69 | + yLabel: 'loss', |
| 70 | + }); |
| 71 | + |
| 72 | + const accuracyContainer = document.getElementById('accuracyChart'); |
| 73 | + tfvis.render.linechart( |
| 74 | + accuracyContainer, |
| 75 | + { values: accuracyValues, series: ['train', 'validation'] }, { |
| 76 | + width: 420, |
| 77 | + height: 300, |
| 78 | + xLabel: 'epoch', |
| 79 | + yLabel: 'accuracy', |
| 80 | + }); |
| 81 | + } else { |
| 82 | + const { isCorrect, examples } = e.data; |
| 83 | + const examplesDiv = document.getElementById('testExamples'); |
| 84 | + const examplesContent = examples.map( |
| 85 | + (example, i) => |
| 86 | + `<div class="${ |
| 87 | + isCorrect[i] ? 'answer-correct' : 'answer-wrong'}">` + |
| 88 | + `${example}` + |
| 89 | + `</div>`); |
| 90 | + |
| 91 | + examplesDiv.innerHTML = examplesContent.join('\n'); |
| 92 | + } |
| 93 | + }); |
| 94 | + }); |
| 95 | +} |
| 96 | + |
| 97 | +runAdditionRNNDemo(); |
0 commit comments