MCPcopy Index your code
hub / github.com/tensorflow/tfjs-examples / renderModelPredictions

Function renderModelPredictions

polynomial-regression/index.js:192–223  ·  view source on GitHub ↗
(
    canvas, order, model, xPowerMeans, xPowerStddevs, yMean, yStddev)

Source from the content-addressed store, hash-verified

190
191// Render the predictions made by the model.
192function renderModelPredictions(
193 canvas, order, model, xPowerMeans, xPowerStddevs, yMean, yStddev) {
194 const ctx = canvas.getContext('2d');
195 const width = canvas.width;
196 let x = -0.5 * width;
197 const xStep = 0.02 * width;
198 const xs = [];
199 const xPowers = [];
200 let n = 0;
201 while (x < 0.5 * width) {
202 xs.push(x);
203 let d = 1;
204 for (let j = 0; j < order + 1; ++j) {
205 xPowers.push(
206 j === 0 ? d : ((d - xPowerMeans[j - 1]) / xPowerStddevs[j - 1]));
207 d *= x;
208 }
209 x += xStep;
210 n++;
211 }
212
213 const predictOut = model.predict(tf.tensor2d(xPowers, [n, order + 1]));
214 const normalizedYs = predictOut.dataSync();
215 ctx.beginPath();
216 let canvasXY = world2canvas(canvas, xs[0], normalizedYs[0] * yStddev + yMean);
217 ctx.moveTo(canvasXY[0], canvasXY[1]);
218 for (let i = 1; i < n; ++i) {
219 canvasXY = world2canvas(canvas, xs[i], normalizedYs[i] * yStddev + yMean);
220 ctx.lineTo(canvasXY[0], canvasXY[1]);
221 ctx.stroke();
222 }
223}
224
225// Generate x-y data based on the size of the canvas.
226function generateXYData(canvas, coeffs) {

Callers 1

fitAndRenderFunction · 0.85

Calls 2

world2canvasFunction · 0.85
predictMethod · 0.45

Tested by

no test coverage detected