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

Function getNormalizedDatasets

quantization/data_housing.js:116–176  ·  view source on GitHub ↗
(
    count, featureMeans, featureStddevs, labelMean, labelStddev,
    validationSplit, evaluationSplit)

Source from the content-addressed store, hash-verified

114 * evalYs {tf.Tensor} evaluation label tensor.
115 */
116export async function getNormalizedDatasets(
117 count, featureMeans, featureStddevs, labelMean, labelStddev,
118 validationSplit, evaluationSplit) {
119 tf.util.assert(
120 validationSplit > 0 && validationSplit < 1,
121 () => `validationSplit is expected to be >0 and <1, ` +
122 `but got ${validationSplit}`);
123 tf.util.assert(
124 evaluationSplit > 0 && evaluationSplit < 1,
125 () => `evaluationSplit is expected to be >0 and <1, ` +
126 `but got ${evaluationSplit}`);
127 tf.util.assert(
128 validationSplit + evaluationSplit < 1,
129 () => `The sum of validationSplit and evaluationSplit exceeds 1`);
130
131 const dataset = tf.data.csv(HOUSING_CSV_URL, {
132 columnConfigs: {
133 [labelColumn]: {
134 isLabel: true
135 }
136 }
137 });
138
139 const featureValues = [];
140 const labelValues = [];
141 const indices = [];
142 const iterator = await dataset.iterator();
143 for (let i = 0; i < count; ++i) {
144 const {value, done} = await iterator.next();
145 if (done) {
146 break;
147 }
148 featureColumns.map(feature => {
149 featureValues.push(
150 (value.xs[feature] - featureMeans[feature]) /
151 featureStddevs[feature]);
152 });
153 labelValues.push((value.ys[labelColumn] - labelMean) / labelStddev);
154 indices.push(i);
155 }
156
157 const xs = tf.tensor2d(featureValues, [count, featureColumns.length]);
158 const ys = tf.tensor2d(labelValues, [count, 1]);
159
160 // Set random seed to fix shuffling order and therefore to fix the
161 // training, validation, and evaluation splits.
162 Math.seedrandom('1337');
163 tf.util.shuffle(indices);
164
165 const numTrain = Math.round(count * (1 - validationSplit - evaluationSplit));
166 const numVal = Math.round(count * validationSplit);
167 const trainXs = xs.gather(indices.slice(0, numTrain));
168 const trainYs = ys.gather(indices.slice(0, numTrain));
169 const valXs = xs.gather(indices.slice(numTrain, numTrain + numVal));
170 const valYs = ys.gather(indices.slice(numTrain, numTrain + numVal));
171 const evalXs = xs.gather(indices.slice(numTrain + numVal));
172 const evalYs = ys.gather(indices.slice(numTrain + numVal));
173

Callers 2

mainFunction · 0.90
mainFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected