TensorFlow JS: Storing minimum and maximum values with the model, then retrieving them for prediction data comparison

Currently, I am delving into the realm of developing an ML model with TensorFlow JS. Although I am fairly new to both JavaScript and ML, I have managed to create a functional model that provides satisfactory predictions. However, I encountered an issue when attempting to save the model and load it into a client-side UI - the min/max values used for normalization also need to match in order to ensure consistent predictions (at least, that's what I believe is causing the discrepancy). I tried various methods such as retrieving the min/max data as individual tensor values or extracting the entire tensor to iterate through and identify the min/max values. Additionally, I experimented with hardcoding the min/max as either a number or an object.

Despite being able to view the tensor data, I faced difficulty accessing the min/max values which resulted in a NaN error during prediction. Given my limited experience in this field, I assume there must be a glaringly obvious oversight on my part. Any assistance would be greatly appreciated as I find myself growing increasingly frustrated trying to pinpoint where I might have gone wrong.


//saving tensor normalisedFeature for future retrieval of min/max

function downloadJ() {
        let values = {
            normalisedFeature
        };
        let json = JSON.stringify(values);
        //Convert JSON string to BLOB.
        json = [json];
        let blob1 = new Blob(json, { type: "text/json;charset=utf-8" }); 

        let url = window.URL || window.webkitURL;
        link = url.createObjectURL(blob1);
        let a = document.createElement("a");
        a.download = "tValues.json";
        a.href = link;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
}

//loading tensor saved values
let normalisedFeatureJ = {};
$.ajax({
    url: "model/tValues.json",
    async: false,
    dataType: 'json',
    success: function(data) {
        normalisedFeatureJ = (data);
    }
});
console.log(Object.values(normalisedFeatureJ));

//tried dataSync(), looping, parsing etc. Unable to access min/max values

//JSON file structure:
{"normalisedFeature":
    {"tensor": {"isDisposedInternal":false,"shape":[10000,17],"dtype":"float32","size":170000,"strides":[17],"dataId":{},"id":28,"rankType":"2"},
            "min":{"isDisposedInternal":false,"shape":[],"dtype":"float32","size":1,"strides":[],"dataId":{},"id":6,"rankType":"0"},
                "max":{"isDisposedInternal":false,"shape":[],"dtype":"float32","size":1,"strides":[],"dataId":{},"id":16,"rankType":"0"}}}
          

While attempting to work out the calculations without leveraging tensor operations, things quickly descended into chaos :)

Answer №1

Your JSON file includes the tensor metadata, but not the actual data itself. To include the data in downloadJ, modify it to define values as follows:

let values = {
  tensor: {
    shape: normalizedFeature.tensor.shape,
    data: normalizedFeature.tensor.dataSync()
  },
  min: normalizedFeature.min.dataSync()[0],
  max: normalizedFeature.max.dataSync()[0]
};


The resulting JSON structure will be like:

{
  "tensor": {
    "shape": [
      10000,
      17
    ],
    "data": {
      "0": 0.6050498485565186,
      ...
      "169999": 0.055848438292741776
    }
  },
  "min": -43.01580047607422,
  "max": 727.2080078125
}

Make sure to retrieve the min and max values from this JSON when loading the model.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Steps to efficiently execute multiple AJAX requests inside a single AJAX function

I am currently working on a project that involves using AJAX, jQuery, PHP, and SQL to manipulate data. function fetchComments(commentOnId, commentOn){ if(!isNaN(commentOnId) && commentOnId >=0){ $.ajax({ type: 'POST', ...

Creating formGroups dynamically for each object in an array and then updating the values with the object data

What I am aiming to accomplish: My goal is to dynamically generate a new formGroup for each recipe received from the backend (stored in this.selectedRecipe.ingredients) and then update the value of each formControl within the newly created formGroup with t ...

Error 505: Python 3.4 encountered an HTTP issue while trying to retrieve JSON data from

Attempting to establish a connection with a web page that receives parameters and returns JSON data in Python 3.4 using the urllib module. The goal is to extract this JSON data and save it into a CSV file. Here is what has been attempted: import json imp ...

What are the steps to position this bootstrap drop-down menu above a fixed page header?

Could someone please advise on what changes need to be made in the code snippet below from http://jsfiddle.net/ufkkdbja/? I am looking to ensure that when the hamburger menu is clicked, the entire menu appears without being cut off by the fixed page header ...

How to access and retrieve data from a USB flash drive using Javascript

I am looking to print PDF files from a USB flash drive. I have decided to use Mozilla Firefox and the R-kiosk plugin along with the open library PDF.js, but I am facing an issue. How can I read folders and files to create a tree structure without using ...

What is the best way to perform an atomic update on an embedded document within an array using mongoose? The operation should only update the latest document and then return the updated

Currently, I am delving into node.js and MongoDB with mongoose. //Document in mongoDB { name: 'first', _id: '1000' phases: [{ _id: 1, phaseName: 'phase1', stories: [{ _id: s1, sname: ...

An uncomplicated React component for mapping and selection

When trying to fetch 4 categories from my database, I encounter an issue. After successfully retrieving the category data, upon submitting the form I receive: [object%20Object],[object%20Object],[object%20Object],[object%20Object] Below is the code snipp ...

When working with Mongoose and TypeScript, encountering the 'connect' error from mongoose can be frustrating and disruptive to your

After attempting to start the server, an error message is displayed: this.mongo.connect('mongodb://localhost:27017/tsnode', { ^ TypeError: Cannot read property 'connect' of undefined import express from 'express&ap ...

Is it redundant to use flatMap in RXJS?

I recently came across an enlightening article on RXJS which delves into the concept of flatMap. After understanding its purpose - to flatten observable of observables into a single observable sequence (similar to SelectMany in C#) - I noticed an interes ...

I'm experiencing an issue with fullCalendar where the dayRender function is not functioning as expected

I have been using fullCalendar and I am looking to customize the color of specific days. I have successfully created an overlay that is displayed when a user clicks on a particular day. Everything works as expected with the overlay, but now I am encounte ...

Using jQuery Ajax to Send Values Between Functions and Replace Nulls

I need assistance with handling the value from a dropdownlist in my JavaScript function. The function works well if the value is not Null, but I want to replace a Null value with the static value "asc" before passing it to the next function. function g ...

Using JavaScript to save a file with a data URL in Internet Explorer

Need help with file conversion on different browsers. I developed an app that converts files, and everything was working perfectly in Chrome. However, when it comes to IE (10/11/Edge), I'm facing some challenges: 1) The HTML5 download attribute does ...

Create a visually engaging book reader with the power of jQuery and images

For hours, I've been attempting to develop an online Comic Book reader to load my images. Everything is functioning correctly, except for a counter using an increment method that just won't work - reducing the increments breaks the function. Is ...

What is the best way to sequentially process data column by column and incorporate them into a three-dimensional model using three.js?

Currently, I am in the process of creating a 3D model using three.js that will accurately display the position and orientation of a vehicle using accelerometer and gyroscope data. I have collected all the necessary information in an xlsx file, including th ...

Nuxt 2.5 and higher do not recognize the definition of Global

After utilizing Nuxt 2.1 in my app, I proceeded to upgrade it gradually and everything was fine until Nuxt 2.4. However, starting from version 2.5 and above, production builds are breaking with an error stating global is not defined. The location of the e ...

When Jquery JSON DOM fails to load data

I've been trying to implement this functionality on my website by following various tutorials, but I am facing an issue where the data is not displaying anywhere. Initially, it was showing the return value of json_encode, but now it's not even do ...

Encapsulate all ASP.NET JSON responses by returning an anonymous object

Exploring ASP.net (Visual Studio 2010, .NET 3.5) for the first time and hoping to achieve the following: Using OperationContracts to serve webservice data in JSON format. A mobile app built with angularJS is consuming these JSON responses. The goal is to ...

Utilizing the switch statement with objects in PHP

I need assistance with implementing a switch case or if else statement in an array of objects using PHP. Can anyone help me with this? Here is an example of the JSON objects I have: {"car":[{"colour":"black", "brand&quo ...

In a carousel slider, the height and width of divs are not set to specific dimensions

For a code snippet, you can visit this link: here The html: <html lang="en"> <head> <link href="https://fonts.googleapis.com/css?family=Lato:400,400i,700,700i|Merriweather:300,400,700" rel="stylesheet"> <link href="https://ma ...

Is there a way to access cached ManifestEntry JavaScript functions from within the service worker?

After generating a precache manifest, it looks something like this: self.__precacheManifest = [ { "revision": "ea92339a72de73748c35", "url": "/js/vendor.ea92339a.js" }, { "revision": "ce28c628eea246b643c9", "url": "/js/manifest.ce28c ...