I am following this TensorFlow JS tutorial where you load car data. The data looks like this:
[{x:100, y:20}, {x:80, y:33}]
X is the horsepower of a car, Y is the expected miles per gallon usage. After creating the model I save it locally using:
async function saveModel(){
await model.save('downloads://cars-model');
}
Next, I load the model in a separate project, to make predictions without needing the original data.
NEW PROJECT
async function app(){
let model = await tf.loadLayersModel('./cars-model.json');
console.log("car model is loaded!");
}
I expect to be able to run predict
here, on a single number (say, 120)
model.predict(tf.tensor2d([120], [1, 1]))
QUESTION
I think the number 120 needs to be normalised to a number between 0-1, just like the training data was. But how do I know the inputMin, inputMax, labelMin, labelMax
values from the loaded model?
To un-normalise the prediction (in this case 0.6) I also need those original values.
How do I normalise/un-normalise data when loading a model?
original prediction code uses label and input values from the original data
function testModel(model, inputData, normalizationData) {
const { inputMax, inputMin, labelMin, labelMax } = normalizationData;
// Generate predictions for a uniform range of numbers between 0 and 1;
// We un-normalize the data by doing the inverse of the min-max scaling
// that we did earlier.
const [xs, preds] = tf.tidy(() => {
const xs = tf.linspace(0, 1, 100);
const preds = model.predict(xs.reshape([100, 1]));
const unNormXs = xs
.mul(inputMax.sub(inputMin))
.add(inputMin);
const unNormPreds = preds
.mul(labelMax.sub(labelMin))
.add(labelMin);
// Un-normalize the data
return [unNormXs.dataSync(), unNormPreds.dataSync()];
});
const predictedPoints = Array.from(xs).map((val, i) => {
return { x: val, y: preds[i] }
});
}