Answers.Body: <blockquote>
    <p>How do I convert this to a number between 0-1 without having access to the original car data?</p>
  </blockquote>
  
  <p>You save the normalisation parameters (typically an offset and a multiplier for each column), and consider that part of the model. Typically you do this when you originally scale training data.</p>
  
  <p>When you want to re-use the model, as well as loading the neural network architecture and weights, you need to load the normalisation parameters in order to re-use those too and scale your inputs.</p>
  
  <p>When tutorials present a self-contained neural network that loads training data, builds a model, then tests that model, all in the same process, then often this step is not shown. However, saving the normalisation data is important, and basically should be considered part of the model, even though it is not directly part of the neural network parameters or hyper-parameters.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-01T13:08:18.273Z
Answers.Text: OK, thanks for clearing that up. So do I save that data manually, in a separate file, or should it be part of the saved model?
Answers.UserDisplayName: Kokodoko
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-01T13:18:17.897Z
Answers.Text: @Kokodoko: It's up to you as the developer which you prefer, and may depend on your use case. For instance if you always use the same training data, scaling it the same way and want to move different NNs around for testing, then separate files will be slightly easier and quicker to develop. But a self-contained file with supporting data for the model might make more sense in other situations
Answers.UserDisplayName: Neil Slater
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-01T14:55:17.977Z
Answers.Text: But if I use `model.save`, is there an option to include that data? Can I add it manually before saving? For example, `model.min = 100; model.max = 200`.
Answers.UserDisplayName: Kokodoko
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-01T14:56:59.887Z
Answers.Text: @Kokodoko: I don't know, that depends on how function `save` is written
Answers.UserDisplayName: Neil Slater
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-01T16:56:33.960Z
Answers.Text: I'm using the Tensorflow JS API: https://js.tensorflow.org/api/latest/#tf.LayersModel.save 
  It seems to have a `config` option but this is not further explained... :(
Answers.UserDisplayName: Kokodoko
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-01T17:14:31.350Z
Answers.Text: From the description it does not look like it will accept added data, so you will need to arrange to store the normalisation parameters separately
Answers.UserDisplayName: Neil Slater
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-01T13:02:27.730Z
Answers.LastActivityDate: 2019-04-01T13:02:27.730Z
Answers.UserDisplayName: Neil Slater
Body: <p>I am following <a href="https://codelabs.developers.google.com/codelabs/tfjs-training-classfication/index.html#6" rel="nofollow noreferrer">this TensorFlow JS tutorial</a> where you load car data. The data looks like this:</p>
  
  <pre><code>[{x:100, y:20}, {x:80, y:33}]
  </code></pre>
  
  <p>X is the horsepower of a car, Y is the expected miles per gallon usage. After creating the model I save it locally using:</p>
  
  <pre><code>async function saveModel(){
      await model.save('downloads://cars-model');
  }
  </code></pre>
  
  <p>Next, I load the model in a separate project, to make predictions without needing the original data. </p>
  
  <p>NEW PROJECT</p>
  
  <pre><code>async function app(){
      let model = await tf.loadLayersModel('./cars-model.json');
      console.log("car model is loaded!");
  }
  </code></pre>
  
  <p>I expect to be able to run <code>predict</code> here, on a single number (say, 120)</p>
  
  <pre><code>model.predict(tf.tensor2d([120], [1, 1]))
  </code></pre>
  
  <p><strong>QUESTION</strong></p>
  
  <p>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 <code>inputMin, inputMax, labelMin, labelMax</code> values from the loaded model?</p>
  
  <p>To un-normalise the prediction (in this case 0.6) I also need those original values. </p>
  
  <p>How do I normalise/un-normalise data when loading a model?</p>
  
  <p><em>original prediction code uses label and input values from the original data</em></p>
  
  <pre><code>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(() =&gt; {
  
          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) =&gt; {
          return { x: val, y: preds[i] }
      });
  
  }
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-09-16T13:48:06.490Z
Comments.Text: Hi @Kokodoko did you work out a solution? I've hit the same road block, pulling in the data for the original min/max as a JSON but its giving me troubles. I have been googling for days but can't find any tutorials that load separate and this is the only reference to it I can see? weird eh? How is everyone else doing it :)
Comments.UserDisplayName: laurencefay
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-09-22T07:56:45.210Z
Comments.Text: @laurencefay According to the answer below you just have to save the original min/max values in a separate JSON file. Then you can load that JSON file later to retrieve those values and un-normalize the results
Comments.UserDisplayName: Kokodoko
Tags: <tensorflow><models><javascript>
Title: How do I normalise/un-normalise data when loading a model?
UserDisplayName: Kokodoko
ThreadText: # How do I normalise/un-normalise data when loading a model?
  
  (Asked by: username_0 on 2019-03-31)
  
  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] }
      });
  
  }
  
  **Tags:** tensorflow, models, javascript
  > **Comments:**
  > **username_1**: Hi @Kokodoko did you work out a solution? I've hit the same road block, pulling in the data for the original min/max as a JSON but its giving me troubles. I have been googling for days but can't find any tutorials that load separate and this is the only reference to it I can see? weird eh? How is everyone else doing it :)  
  > **username_0**: @laurencefay According to the answer below you just have to save the original min/max values in a separate JSON file. Then you can load that JSON file later to retrieve those values and un-normalize the results  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-04-01. Score: 1 | Accepted answer
  
  > How do I convert this to a number between 0-1 without having access to the original car data?
  
  You save the normalisation parameters (typically an offset and a multiplier for each column), and consider that part of the model. Typically you do this when you originally scale training data.
  
  When you want to re-use the model, as well as loading the neural network architecture and weights, you need to load the normalisation parameters in order to re-use those too and scale your inputs.
  
  When tutorials present a self-contained neural network that loads training data, builds a model, then tests that model, all in the same process, then often this step is not shown. However, saving the normalisation data is important, and basically should be considered part of the model, even though it is not directly part of the neural network parameters or hyper-parameters.
  
  > **Comments:**
  > **username_0**: OK, thanks for clearing that up. So do I save that data manually, in a separate file, or should it be part of the saved model?  
  > **username_2**: @Kokodoko: It's up to you as the developer which you prefer, and may depend on your use case. For instance if you always use the same training data, scaling it the same way and want to move different NNs around for testing, then separate files will be slightly easier and quicker to develop. But a self-contained file with supporting data for the model might make more sense in other situations  
  > **username_0**: But if I use `model.save`, is there an option to include that data? Can I add it manually before saving? For example, `model.min = 100; model.max = 200`.  
  > **username_2**: @Kokodoko: I don't know, that depends on how function `save` is written  
  > **username_0**: I'm using the Tensorflow JS API: https://js.tensorflow.org/api/latest/#tf.LayersModel.save  It seems to have a `config` option but this is not further explained... :(  
  > **username_2**: From the description it does not look like it will accept added data, so you will need to arrange to store the normalisation parameters separately  
  
  ---
  
  