forked from tensorflow/tfjs-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
251 lines (234 loc) · 8.49 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
* @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 tfvis from '@tensorflow/tfjs-vis';
import * as game from './game';
import * as ui from './ui';
/**
* Returns a dataset which will yield unlimited plays of the game.
*/
export const GAME_GENERATOR_DATASET = tf.data.generator(function* gen() {
while (true) {
yield game.generateOnePlay();
}
});
/**
* Module global boolean to indicate whether the model should stop training at
* the end of this epoch.
*/
let STOP_REQUESTED = false;
/**
* Holds game state of most recent simulation to allow for re-calculation
* of feature representation.
*/
let SAMPLE_GAME_STATE;
/**
* Holds the model to be trained & evaluated.
*/
let GLOBAL_MODEL;
/**
* Takes the state of one complete game and returns features suitable for
* training. Returns an object containing features = player1's hand represented
* using oneHot encoding, and label = whether player 1 won.
* @param {*} gameState
*/
function gameToFeaturesAndLabel(gameState) {
return tf.tidy(() => {
const player1Hand = tf.tensor1d(gameState.player1Hand, 'int32');
const handOneHot = tf.oneHot(
tf.sub(player1Hand, tf.scalar(1, 'int32')),
game.GAME_STATE.max_card_value);
const features = tf.sum(handOneHot, 0);
const label = tf.tensor1d([gameState.player1Win]);
return {xs: features, ys: label};
});
}
/**
* Collects one random play of the game. Processes the sample to generate
* features and labels representation of the play. Calls a UI method to render
* the sample and the processed sample.
* @param {bool} wantNewGame : If true, a new game is generated.
*/
async function simulateGameHandler(wantNewGame) {
if (wantNewGame) {
SAMPLE_GAME_STATE = game.generateOnePlay();
}
const featuresAndLabel = gameToFeaturesAndLabel(SAMPLE_GAME_STATE);
ui.displaySimulation(SAMPLE_GAME_STATE, featuresAndLabel);
ui.displayNumSimulationsSoFar();
}
/**
* This is pulled into a separate function to isolate the async code.
* @see datasetToArrayHandler
*/
async function datasetToArray() {
return GAME_GENERATOR_DATASET.map(gameToFeaturesAndLabel)
.batch(ui.getBatchSize())
.take(ui.getTake())
.toArray();
}
/**
* Creates a dataset pipeline from GAME_GENERATOR_DATASET by:
* 1) Applying the function gameToFeaturesAndlabel
* 2) Taking the first N samples of the dataset
* 3) Batching the dataset to batches of size B
*
* It then executes the dataset by filling an array. Finally, it passes this
* array to the UI to render in a table.
*/
async function datasetToArrayHandler() {
const arr = await datasetToArray();
ui.displayBatches(arr);
ui.displayNumSimulationsSoFar();
}
/**
* Returns a three layer sequential model suitable for predicting win state from
* feature representation. The input shape depends on whether oneHot
* representation is used.
*/
function createDNNModel() {
GLOBAL_MODEL = tf.sequential();
GLOBAL_MODEL.add(tf.layers.dense({
inputShape: [game.GAME_STATE.max_card_value],
units: 20,
activation: 'relu'
}));
GLOBAL_MODEL.add(tf.layers.dense({units: 20, activation: 'relu'}));
GLOBAL_MODEL.add(tf.layers.dense({units: 1, activation: 'sigmoid'}));
return GLOBAL_MODEL;
}
/**
* Trains a the provided model on the provided dataset using model.fitDataset.
* Schedules a callback at the end of every epoch to update the UI with
* graphs showing loss and accuracy, as well as training speed and the current
* prediction for the manually entered hand.
* @param {tf.Model} model
* @param {tf.data.Dataset} dataset
*/
async function trainModelUsingFitDataset(model, dataset) {
const trainLogs = [];
const beginMs = performance.now();
const fitDatasetArgs = {
batchesPerEpoch: ui.getBatchesPerEpoch(),
epochs: ui.getEpochsToTrain(),
validationData: dataset,
validationBatches: 10,
callbacks: {
onEpochEnd: async (epoch, logs) => {
// Plot the loss and accuracy values at the end of every training epoch.
const secPerEpoch =
(performance.now() - beginMs) / (1000 * (epoch + 1));
ui.displayTrainLogMessage(
`Training model... Approximately ` +
`${secPerEpoch.toFixed(4)} seconds per epoch`);
trainLogs.push(logs);
tfvis.show.history(
ui.lossContainerElement, trainLogs, ['loss', 'val_loss'])
tfvis.show.history(
ui.accuracyContainerElement, trainLogs, ['acc', 'val_acc'],
{zoomToFitAccuracy: true})
ui.displayNumSimulationsSoFar();
// Update the prediction.
predictHandler();
// Stop the training if stop requested.
if (STOP_REQUESTED) {
model.stopTraining = true;
}
},
}
};
ui.disableTrainButton();
ui.enableStopButton();
ui.enablePredictButton();
await model.fitDataset(dataset, fitDatasetArgs);
ui.enableTrainButton();
ui.disableStopButton();
}
/**
* Constructs a new model and trains it on a dataset pipeline built off of
* GAME_GENERATOR_DATASET. The dataset pipeline performs feature calculation
* and batching.
* @see trainModelUsingFitDataset for training details.
*/
async function trainModelUsingFitDatasetHandler() {
STOP_REQUESTED = false;
const model = createDNNModel();
model.compile({
optimizer: 'rmsprop',
loss: 'binaryCrossentropy',
metrics: ['accuracy'],
});
const dataset = GAME_GENERATOR_DATASET.map(gameToFeaturesAndLabel)
.batch(ui.getBatchSize());
trainModelUsingFitDataset(model, dataset);
}
/**
* Applies the model to the manually entered hand value and updates the UI with
* the model's prediction.
*/
function predictHandler() {
const cards = ui.getInputCards();
const features =
gameToFeaturesAndLabel({player1Hand: cards, player1Win: 1}).xs;
const output = GLOBAL_MODEL.predict(features.expandDims(0));
ui.displayPrediction(`${output.dataSync()[0].toFixed(3)}`);
}
/**
* Updates the game constant controlling the number of cards per hand and
* clears UI.
*/
function selectCardsPerHandHandler() {
game.GAME_STATE.num_cards_per_hand =
Number.parseInt(document.getElementById('select-cards-per-hand').value);
simulateGameHandler(true);
ui.updatePredictionInputs();
ui.displayBatches([]);
ui.disablePredictButton();
ui.displayPrediction('New model needs to be trained');
}
/** Sets up handlers for the user affordences, including all buttons. */
document.addEventListener('DOMContentLoaded', async () => {
console.log('content loaded... connecting buttons.');
document.getElementById('select-cards-per-hand')
.addEventListener('change', selectCardsPerHandHandler, false);
document.getElementById('simulate-game')
.addEventListener('click', () => simulateGameHandler(true), false);
document.getElementById('dataset-to-array')
.addEventListener('click', datasetToArrayHandler, false);
document.getElementById('dataset-to-array')
.addEventListener('click', datasetToArrayHandler, false);
document.getElementById('train-model-using-fit-dataset')
.addEventListener('click', trainModelUsingFitDatasetHandler, false);
document.getElementById('stop-training')
.addEventListener('click', () => STOP_REQUESTED = true);
document.getElementById('generator-batch').addEventListener('change', () => {
ui.displayExpectedSimulations();
ui.displayBatches([]);
}, false);
document.getElementById('generator-take').addEventListener('change', () => {
ui.displayBatches([]);
}, false);
document.getElementById('batches-per-epoch')
.addEventListener('change', ui.displayExpectedSimulations, false);
document.getElementById('epochs-to-train')
.addEventListener('change', ui.displayExpectedSimulations, false);
document.getElementById('predict').addEventListener(
'click', predictHandler, false);
ui.displayNumSimulationsSoFar();
ui.displayExpectedSimulations();
ui.updatePredictionInputs();
});