MCPcopy Create free account
hub / github.com/tensorflow/tfjs-examples / train

Function train

polynomial-regression-core/index.js:92–112  ·  view source on GitHub ↗
(xs, ys, numIterations)

Source from the content-addressed store, hash-verified

90 * ys — training data y values
91 */
92async 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
114async function learnCoefficients() {
115 const trueCoefficients = {a: -.8, b: -.2, c: .9, d: .5};

Callers 1

learnCoefficientsFunction · 0.70

Calls 2

predictFunction · 0.70
lossFunction · 0.70

Tested by

no test coverage detected