forked from tensorflow/tfjs-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
197 lines (175 loc) · 6.54 KB
/
index.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
/**
* @license
* Copyright 2018 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 tfvis from '@tensorflow/tfjs-vis';
import {BostonHousingDataset, featureDescriptions} from './data';
import * as normalization from './normalization';
import * as ui from './ui';
// Some hyperparameters for model training.
const NUM_EPOCHS = 200;
const BATCH_SIZE = 40;
const LEARNING_RATE = 0.01;
const bostonData = new BostonHousingDataset();
const tensors = {};
// Convert loaded data into tensors and creates normalized versions of the
// features.
export function arraysToTensors() {
tensors.rawTrainFeatures = tf.tensor2d(bostonData.trainFeatures);
tensors.trainTarget = tf.tensor2d(bostonData.trainTarget);
tensors.rawTestFeatures = tf.tensor2d(bostonData.testFeatures);
tensors.testTarget = tf.tensor2d(bostonData.testTarget);
// Normalize mean and standard deviation of data.
let {dataMean, dataStd} =
normalization.determineMeanAndStddev(tensors.rawTrainFeatures);
tensors.trainFeatures = normalization.normalizeTensor(
tensors.rawTrainFeatures, dataMean, dataStd);
tensors.testFeatures =
normalization.normalizeTensor(tensors.rawTestFeatures, dataMean, dataStd);
};
/**
* Builds and returns Linear Regression Model.
*
* @returns {tf.Sequential} The linear regression model.
*/
export function linearRegressionModel() {
const model = tf.sequential();
model.add(tf.layers.dense({inputShape: [bostonData.numFeatures], units: 1}));
model.summary();
return model;
};
/**
* Builds and returns Multi Layer Perceptron Regression Model
* with 1 hidden layers, each with 10 units activated by sigmoid.
*
* @returns {tf.Sequential} The multi layer perceptron regression model.
*/
export function multiLayerPerceptronRegressionModel1Hidden() {
const model = tf.sequential();
model.add(tf.layers.dense({
inputShape: [bostonData.numFeatures],
units: 50,
activation: 'sigmoid',
kernelInitializer: 'leCunNormal'
}));
model.add(tf.layers.dense({units: 1}));
model.summary();
return model;
};
/**
* Builds and returns Multi Layer Perceptron Regression Model
* with 2 hidden layers, each with 10 units activated by sigmoid.
*
* @returns {tf.Sequential} The multi layer perceptron regression mode l.
*/
export function multiLayerPerceptronRegressionModel2Hidden() {
const model = tf.sequential();
model.add(tf.layers.dense({
inputShape: [bostonData.numFeatures],
units: 50,
activation: 'sigmoid',
kernelInitializer: 'leCunNormal'
}));
model.add(tf.layers.dense(
{units: 50, activation: 'sigmoid', kernelInitializer: 'leCunNormal'}));
model.add(tf.layers.dense({units: 1}));
model.summary();
return model;
};
/**
* Describe the current linear weights for a human to read.
*
* @param {Array} kernel Array of floats of length 12. One value per feature.
* @returns {List} List of objects, each with a string feature name, and value
* feature weight.
*/
export function describeKernelElements(kernel) {
tf.util.assert(
kernel.length == 12,
`kernel must be a array of length 12, got ${kernel.length}`);
const outList = [];
for (let idx = 0; idx < kernel.length; idx++) {
outList.push({description: featureDescriptions[idx], value: kernel[idx]});
}
return outList;
}
/**
* Compiles `model` and trains it using the train data and runs model against
* test data. Issues a callback to update the UI after each epcoh.
*
* @param {tf.Sequential} model Model to be trained.
* @param {boolean} weightsIllustration Whether to print info about the learned
* weights.
*/
export async function run(model, modelName, weightsIllustration) {
model.compile(
{optimizer: tf.train.sgd(LEARNING_RATE), loss: 'meanSquaredError'});
let trainLogs = [];
const container = document.querySelector(`#${modelName} .chart`);
ui.updateStatus('Starting training process...');
await model.fit(tensors.trainFeatures, tensors.trainTarget, {
batchSize: BATCH_SIZE,
epochs: NUM_EPOCHS,
validationSplit: 0.2,
callbacks: {
onEpochEnd: async (epoch, logs) => {
await ui.updateModelStatus(
`Epoch ${epoch + 1} of ${NUM_EPOCHS} completed.`, modelName);
trainLogs.push(logs);
tfvis.show.history(container, trainLogs, ['loss', 'val_loss'])
if (weightsIllustration) {
model.layers[0].getWeights()[0].data().then(kernelAsArr => {
const weightsList = describeKernelElements(kernelAsArr);
ui.updateWeightDescription(weightsList);
});
}
}
}
});
ui.updateStatus('Running on test data...');
const result = model.evaluate(
tensors.testFeatures, tensors.testTarget, {batchSize: BATCH_SIZE});
const testLoss = result.dataSync()[0];
const trainLoss = trainLogs[trainLogs.length - 1].loss;
const valLoss = trainLogs[trainLogs.length - 1].val_loss;
await ui.updateModelStatus(
`Final train-set loss: ${trainLoss.toFixed(4)}\n` +
`Final validation-set loss: ${valLoss.toFixed(4)}\n` +
`Test-set loss: ${testLoss.toFixed(4)}`,
modelName);
};
export function computeBaseline() {
const avgPrice = tf.mean(tensors.trainTarget);
console.log(`Average price: ${avgPrice.dataSync()}`);
const baseline = tf.mean(tf.pow(tf.sub(tensors.testTarget, avgPrice), 2));
console.log(`Baseline loss: ${baseline.dataSync()}`);
const baselineMsg = `Baseline loss (meanSquaredError) is ${
baseline.dataSync()[0].toFixed(2)}`;
ui.updateBaselineStatus(baselineMsg);
};
document.addEventListener('DOMContentLoaded', async () => {
await bostonData.loadData();
ui.updateStatus('Data loaded, converting to tensors');
arraysToTensors();
ui.updateStatus(
'Data is now available as tensors.\n' +
'Click a train button to begin.');
// TODO Explain what baseline loss is. How it is being computed in this
// Instance
ui.updateBaselineStatus('Estimating baseline loss');
computeBaseline();
await ui.setup();
}, false);