(xs, ys, numIterations)
| 90 | * ys — training data y values |
| 91 | */ |
| 92 | async function train(xs, ys, numIterations) { |
| 93 | for (let iter = 0; iter < numIterations; iter++) { |
| 94 | // optimizer.minimize is where the training happens. |
| 95 | |
| 96 | // The function it takes must return a numerical estimate (i.e. loss) |
| 97 | // of how well we are doing using the current state of |
| 98 | // the variables we created at the start. |
| 99 | |
| 100 | // This optimizer does the 'backward' step of our training process |
| 101 | // updating variables defined previously in order to minimize the |
| 102 | // loss. |
| 103 | optimizer.minimize(() => { |
| 104 | // Feed the examples into the model |
| 105 | const pred = predict(xs); |
| 106 | return loss(pred, ys); |
| 107 | }); |
| 108 | |
| 109 | // Use tf.nextFrame to not block the browser. |
| 110 | await tf.nextFrame(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | async function learnCoefficients() { |
| 115 | const trueCoefficients = {a: -.8, b: -.2, c: .9, d: .5}; |
no test coverage detected