* 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
(model, dataset)
| 133 | * @param {tf.data.Dataset} dataset |
| 134 | */ |
| 135 | async function trainModelUsingFitDataset(model, dataset) { |
| 136 | const trainLogs = []; |
| 137 | const beginMs = performance.now(); |
| 138 | const fitDatasetArgs = { |
| 139 | batchesPerEpoch: ui.getBatchesPerEpoch(), |
| 140 | epochs: ui.getEpochsToTrain(), |
| 141 | validationData: dataset, |
| 142 | validationBatches: 10, |
| 143 | callbacks: { |
| 144 | onEpochEnd: async (epoch, logs) => { |
| 145 | // Plot the loss and accuracy values at the end of every training epoch. |
| 146 | const secPerEpoch = |
| 147 | (performance.now() - beginMs) / (1000 * (epoch + 1)); |
| 148 | ui.displayTrainLogMessage( |
| 149 | `Training model... Approximately ` + |
| 150 | `${secPerEpoch.toFixed(4)} seconds per epoch`); |
| 151 | trainLogs.push(logs); |
| 152 | tfvis.show.history( |
| 153 | ui.lossContainerElement, trainLogs, ['loss', 'val_loss']) |
| 154 | tfvis.show.history( |
| 155 | ui.accuracyContainerElement, trainLogs, ['acc', 'val_acc'], |
| 156 | {zoomToFitAccuracy: true}) |
| 157 | ui.displayNumSimulationsSoFar(); |
| 158 | // Update the prediction. |
| 159 | predictHandler(); |
| 160 | // Stop the training if stop requested. |
| 161 | if (STOP_REQUESTED) { |
| 162 | model.stopTraining = true; |
| 163 | } |
| 164 | }, |
| 165 | } |
| 166 | }; |
| 167 | ui.disableTrainButton(); |
| 168 | ui.enableStopButton(); |
| 169 | ui.enablePredictButton(); |
| 170 | await model.fitDataset(dataset, fitDatasetArgs); |
| 171 | ui.enableTrainButton(); |
| 172 | ui.disableStopButton(); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Constructs a new model and trains it on a dataset pipeline built off of |
no test coverage detected