(xyData, order)
| 114 | // yStddev: Standard deviation of y. |
| 115 | // Normalized powers of y: an Tensor2D of shape [batchSize, 1]. |
| 116 | function 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 | // |
no test coverage detected