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

Function fitModel

polynomial-regression/index.js:165–189  ·  view source on GitHub ↗
(xyData, epochs, learningRate)

Source from the content-addressed store, hash-verified

163// yMean: Arithmetic mean of y.
164// yStddev: Standard deviation of y.
165async function fitModel(xyData, epochs, learningRate) {
166 const batchSize = xyData.length;
167 const outputs = toNormalizedTensors(xyData, order);
168 const xPowerMeans = outputs[0];
169 const xPowerStddevs = outputs[1];
170 const xData = outputs[2];
171 const yMean = outputs[3];
172 const yStddev = outputs[4];
173 const yData = outputs[5];
174 const input = tf.input({shape: [order + 1]});
175 const linearLayer =
176 tf.layers.dense({units: 1, kernelInitializer: 'Zeros', useBias: false});
177 const output = linearLayer.apply(input);
178 const model = tf.model({inputs: input, outputs: output});
179 const sgd = tf.train.sgd(learningRate);
180 model.compile({optimizer: sgd, loss: 'meanSquaredError'});
181 await model.fit(xData, yData, {
182 batchSize: batchSize,
183 epochs: epochs,
184 });
185 console.log(
186 'Model weights (normalized):',
187 model.trainableWeights[0].read().dataSync());
188 return [model, xPowerMeans, xPowerStddevs, yMean, yStddev];
189}
190
191// Render the predictions made by the model.
192function renderModelPredictions(

Callers 1

fitAndRenderFunction · 0.70

Calls 2

toNormalizedTensorsFunction · 0.85
applyMethod · 0.45

Tested by

no test coverage detected