|
| 1 | +/* Copyright 2017 Google Inc. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +import {Array1D, Array2D, CheckpointLoader, NDArrayMathGPU, Scalar, |
| 17 | + util} from '../deeplearnjs'; |
| 18 | + |
| 19 | +// manifest.json lives in the same directory. |
| 20 | +const reader = new CheckpointLoader('.'); |
| 21 | +reader.getAllVariables().then(vars => { |
| 22 | + const primerData = 3; |
| 23 | + const expected = [1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4]; |
| 24 | + const math = new NDArrayMathGPU(); |
| 25 | + |
| 26 | + const lstmKernel1 = vars[ |
| 27 | + 'rnn/multi_rnn_cell/cell_0/basic_lstm_cell/kernel'] as Array2D; |
| 28 | + const lstmBias1 = vars[ |
| 29 | + 'rnn/multi_rnn_cell/cell_0/basic_lstm_cell/bias'] as Array1D; |
| 30 | + |
| 31 | + const lstmKernel2 = vars[ |
| 32 | + 'rnn/multi_rnn_cell/cell_1/basic_lstm_cell/kernel'] as Array2D; |
| 33 | + const lstmBias2 = vars[ |
| 34 | + 'rnn/multi_rnn_cell/cell_1/basic_lstm_cell/bias'] as Array1D; |
| 35 | + |
| 36 | + const fullyConnectedBiases = vars['fully_connected/biases'] as Array1D; |
| 37 | + const fullyConnectedWeights = vars['fully_connected/weights'] as Array2D; |
| 38 | + |
| 39 | + const results: number[] = []; |
| 40 | + |
| 41 | + math.scope((keep, track) => { |
| 42 | + const forgetBias = track(Scalar.new(1.0)); |
| 43 | + const lstm1 = math.basicLSTMCell.bind(math, forgetBias, lstmKernel1, |
| 44 | + lstmBias1); |
| 45 | + const lstm2 = math.basicLSTMCell.bind(math, forgetBias, lstmKernel2, |
| 46 | + lstmBias2); |
| 47 | + |
| 48 | + let c = [track(Array2D.zeros([1, lstmBias1.shape[0] / 4])), |
| 49 | + track(Array2D.zeros([1, lstmBias2.shape[0] / 4]))]; |
| 50 | + let h = [track(Array2D.zeros([1, lstmBias1.shape[0] / 4])), |
| 51 | + track(Array2D.zeros([1, lstmBias2.shape[0] / 4]))]; |
| 52 | + |
| 53 | + let input = primerData; |
| 54 | + for (let i = 0; i < expected.length; i++) { |
| 55 | + const onehot = track(Array2D.zeros([1, 10])); |
| 56 | + onehot.set(1.0, 0, input); |
| 57 | + |
| 58 | + const output = math.multiRNNCell([lstm1, lstm2], onehot, c, h); |
| 59 | + |
| 60 | + c = output[0]; |
| 61 | + h = output[1]; |
| 62 | + |
| 63 | + const outputH = h[1]; |
| 64 | + const weightedResult = math.matMul(outputH, fullyConnectedWeights); |
| 65 | + const logits = math.add( weightedResult, fullyConnectedBiases); |
| 66 | + |
| 67 | + const result = math.argMax(logits).get(); |
| 68 | + results.push(result); |
| 69 | + input = result; |
| 70 | + } |
| 71 | + }); |
| 72 | + document.getElementById('expected').innerHTML = '' + expected; |
| 73 | + document.getElementById('results').innerHTML = '' + results; |
| 74 | + if(util.arraysEqual(expected, results)) { |
| 75 | + document.getElementById('success').innerHTML = 'Success!'; |
| 76 | + } else { |
| 77 | + document.getElementById('success').innerHTML = 'Failure.'; |
| 78 | + } |
| 79 | +}); |
0 commit comments