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

Function toNormalizedTensors

polynomial-regression/index.js:116–149  ·  view source on GitHub ↗
(xyData, order)

Source from the content-addressed store, hash-verified

114// yStddev: Standard deviation of y.
115// Normalized powers of y: an Tensor2D of shape [batchSize, 1].
116function toNormalizedTensors(xyData, order) {
117 const batchSize = xyData.length;
118 const xData = xyData.map(xy => xy[0]);
119 const yData = xyData.map(xy => xy[1]);
120 const yMean = mean(yData);
121 const yStddev = stddev(yData);
122 const yNormalized = normalizeVector(yData, yMean, yStddev);
123 const normalizedXPowers = [];
124 const xPowerMeans = [];
125 const xPowerStddevs = [];
126 for (let i = 0; i < order; ++i) {
127 const xPower = xData.map(x => Math.pow(x, i + 1));
128 const xPowerMean = mean(xPower);
129 xPowerMeans.push(xPowerMean);
130 const xPowerStddev = stddev(xPower);
131 xPowerStddevs.push(xPowerStddev);
132 const normalizedXPower = normalizeVector(xPower, xPowerMean, xPowerStddev);
133 normalizedXPowers.push(normalizedXPower);
134 }
135 const xArrayData = [];
136 for (let i = 0; i < xData.length; ++i) {
137 for (let j = 0; j < order + 1; ++j) {
138 if (j === 0) {
139 xArrayData.push(1);
140 } else {
141 xArrayData.push(normalizedXPowers[j - 1][i]);
142 }
143 }
144 }
145 return [
146 xPowerMeans, xPowerStddevs, tf.tensor2d(xArrayData, [batchSize, order + 1]),
147 yMean, yStddev, tf.tensor2d(yNormalized, [batchSize, 1])
148 ];
149}
150
151// Fit a model for polynomial regression.
152//

Callers 1

fitModelFunction · 0.85

Calls 3

meanFunction · 0.70
stddevFunction · 0.70
normalizeVectorFunction · 0.70

Tested by

no test coverage detected