MCPcopy
hub / github.com/tensorflow/tfjs-examples / setUpUI

Function setUpUI

cart-pole/ui.js:235–373  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

233}
234
235export async function setUpUI() {
236 const cartPole = new CartPole(true);
237
238 if (await SaveablePolicyNetwork.checkStoredModelStatus() != null) {
239 policyNet = await SaveablePolicyNetwork.loadModel();
240 logStatus('Loaded policy network from IndexedDB.');
241 hiddenLayerSizesInput.value = policyNet.hiddenLayerSizes();
242 }
243 await updateUIControlState();
244
245 renderDuringTrainingCheckbox.addEventListener('change', () => {
246 renderDuringTraining = renderDuringTrainingCheckbox.checked;
247 });
248
249 createModelButton.addEventListener('click', async () => {
250 try {
251 const hiddenLayerSizes =
252 hiddenLayerSizesInput.value.trim().split(',').map(v => {
253 const num = Number.parseInt(v.trim());
254 if (!(num > 0)) {
255 throw new Error(
256 `Invalid hidden layer sizes string: ` +
257 `${hiddenLayerSizesInput.value}`);
258 }
259 return num;
260 });
261 policyNet = new SaveablePolicyNetwork(hiddenLayerSizes);
262 console.log('DONE constructing new instance of SaveablePolicyNetwork');
263 await updateUIControlState();
264 } catch (err) {
265 logStatus(`ERROR: ${err.message}`);
266 }
267 });
268
269 deleteStoredModelButton.addEventListener('click', async () => {
270 if (confirm(`Are you sure you want to delete the locally-stored model?`)) {
271 await policyNet.removeModel();
272 policyNet = null;
273 await updateUIControlState();
274 }
275 });
276
277 trainButton.addEventListener('click', async () => {
278 if (trainButton.textContent === 'Stop') {
279 stopRequested = true;
280 } else {
281 disableModelControls();
282
283 try {
284 const trainIterations = Number.parseInt(numIterationsInput.value);
285 if (!(trainIterations > 0)) {
286 throw new Error(`Invalid number of iterations: ${trainIterations}`);
287 }
288 const gamesPerIteration = Number.parseInt(gamesPerIterationInput.value);
289 if (!(gamesPerIteration > 0)) {
290 throw new Error(
291 `Invalid # of games per iterations: ${gamesPerIteration}`);
292 }

Callers 1

index.jsFile · 0.90

Calls 15

setRandomStateMethod · 0.95
getStateTensorMethod · 0.95
updateMethod · 0.95
sumFunction · 0.90
meanFunction · 0.90
updateUIControlStateFunction · 0.85
disableModelControlsFunction · 0.85
onIterationEndFunction · 0.85
plotStepsFunction · 0.85
enableModelControlsFunction · 0.85
renderCartPoleFunction · 0.85
hiddenLayerSizesMethod · 0.80

Tested by

no test coverage detected