Tensorflow.js - optimizer iterations failing to update weights and biases

I've been working on a logistic regression model using TensorFlow.js, focusing only on the core API. I'm generating synthetic data and then running a training function that contains all the necessary logic.

Here's a snippet of the code:

const tf = require('@tensorflow/tfjs-core');

const NUM_OF_CLASSES = 1;
const NUM_OF_EXAMPLES = 1000;
const NUM_OF_VARIABLES = 300;
const NUM_EPOCHS = 10;

function calculate_X(N, D) {
  return tf.randomNormal([N, D], 0.0, 1.0);
}

function calculate_y(X) {
  const stepData = tf.tidy(
    () => tf.step(tf.slice2d(X, [0,0], [X.shape[0],1])).reshape([-1])
  );
  return stepData;
}

const X = calculate_X(NUM_OF_EXAMPLES, NUM_OF_VARIABLES);
const y = calculate_y(X);

function train(X, y) {

  const w = tf.variable(tf.zeros([NUM_OF_VARIABLES, NUM_OF_CLASSES]));
  const b = tf.variable(tf.zeros([NUM_OF_CLASSES]))

  const model = x =>
    x.matMul(w)
      .add(b)
      .softmax()
      .as1D();

  const optimizer = tf.train.adam(0.1 /* learningRate */);

  for (let epoch = 0; epoch < NUM_EPOCHS; epoch++) {
    optimizer.minimize(() => {
      const predYs = model(X);
      predYs.data().then(d => console.log('predYs', d));
      y.data().then(d => console.log('y', d));
      const loss = tf.losses.meanSquaredError(y, predYs);
      loss.data().then(l => console.log('Loss', l));
      return loss;
    }, true, [b, w]);
  }
}

train(X, y);

Despite the iterations running smoothly, the variables b & w are not updating in each run.

Answer №1

I updated the model function to incorporate the sigmoid() function and everything is functioning properly!

I'm still a bit unclear on why utilizing the softmax() function caused the model to struggle with optimizing the weights and biases.

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

Discover the importance of Node.js integration with HTML

I am trying to display a Node-js value in an HTML file using the pug engine. In my app.js file: const express=require('express'); const app=express(); var bodyParser = require('body-parser'); app.set('views','views&apo ...

Is there a way to simulate a KeyboardEvent (DOM_VK_UP) that the browser will process as if it were actually pressed by the user?

Take a look at this code snippet inspired by this solution. <head> <meta charset="UTF-8"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> </head> <body> <script> $(this). ...

Showcase images from a MongoDb database in an HTML document

Currently, I am utilizing Node.js with mongoose and EJS as the view engine. I have successfully created a simple send command to retrieve and send images on the server side by following these helpful guides: Setup file uploading in an Express.js applicat ...

Is it feasible to package shared modules into individual files using Browserify?

In my web app, I am using Browserify, Babel, and Gulp to bundle my scripts into a single file. However, when I checked the file size, it was over 3MB which seems excessive to me. Although I'm not entirely sure how Babel and Browserify modify my sourc ...

Is it possible to modify the express static directory path depending on the route being accessed?

I am trying to dynamically change the static path based on the route. Here is an example of what I have tried: const app = express(); const appRouter = express.Router(); const adminRouter = express.Router(); appRouter.use(express.static('/path/to/ap ...

Incorporating telepat-io into a Java Struts enterprise solution

Our J2EE enterprise application, built on Java Struts, has a static front end. Our team's architect has opted to enhance the user authentication by incorporating Ajax and JavaScript functionalities using telepat-io. The project is currently managed w ...

Not able to scroll to top in Angular 2 when changing routes

I need help figuring out how to automatically scroll to the top of my Angular 2 website when the route changes. I've attempted the code below, but unfortunately, it's not working as expected. When transitioning from one page to another, the page ...

Dealing with universal Ajax error handling in AngularJS

Previously on my website when it was 100% jQuery, I employed this method: $.ajaxSetup({ global: true, error: function(xhr, status, err) { if (xhr.status == 401) { window.location = "./index.html"; } } }); to establi ...

Using a JavaScript loop to modify the color of the final character in a word

I am curious to find out how I can dynamically change the color of the last character of each word within a <p> tag using a Javascript loop. For example, I would like to alter the color of the "n" in "John", the "s" in "Jacques", the "r" in "Peter" ...

Troubleshooting issues with Jquery, Ajax, and PHP integration

Currently, I am in the process of refreshing my knowledge on jQuery and AJAX. In jQuery in 8 hours, there is a code snippet that caught my attention: <!DOCTYPE html> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <title>A Sample ...

Looping through properties of objects with the help of angularJS ng-repeat is known as using objects['propertyname&#

What is the best way to iterate over an object with property names like this? $scope.myobjects = [ { 'property1': { id: 0, name: 'someone' } }, { 'property2': { id: 1, name: ' ...

What is the process for executing a GET/POST request and receiving a JSON response on a webpage?

I am working on a page that has a JSON result, with both get and post methods in the controller. There are two submit buttons - one that redirects to the Post method and another that goes to the JsonResult method (named AddTableData). How can I set this up ...

Tips on how to customize an Ajax modal appearance

I need to customize the CSS styling of a modal for editing purposes. Can anyone provide guidance on how to set the width, height, and other properties using the code snippet below? // Open modal in AJAX callback $("modal").dialog({ modal: true, minH ...

Troubleshooting problem with Joomla YJK2Slider effects

I recently purchased a Joomla! extension from YouJoomla, but their support forum has been less than helpful. That's why I'm reaching out to you guys for assistance. The main issue I'm encountering seems to be related to an Fx problem or pot ...

Tips for saving data in a database using Ajax with ExtJS?

In my current project, I have a set of items displayed in a row-wise order in the view using JavaScript. My goal is to implement an auto-save feature that will save the details of the clicked rows into a database using AJAX within ExtJS. ...

The value of a variable undergoes a transformation following its insertion into an

During my coding tests, I came across this snippet of code: <script> var newData = {}, graphs = [] for(var j=0; j<2; j++){ newData["name"] = 'value '+ j console.log(newData["name"]); graphs.push(newData); console.log ...

Is it appropriate to include JavaScript and CSS in views?

In my ASP.NET MVC project, I have set up a _Layout, a controller, and several views. Certain pieces of code are clearly global in nature, such as the CSS included in the _Layout file and other styles that are consistent throughout the site. However, when ...

What is the best way to spy on a property being called within a function?

I am facing an issue where the 'offsetWidth' value is undefined and I need to spyOn it. The function getCurrentPage retrieves an element based on the id currentpage. Although spying on getCurrentPage works, I have been unable to declare the offs ...

I am experiencing a problem where my webpage does not load properly when trying to access it

After setting up a route triggered by a specific click event using AJAX, I noticed that although my route is being called, the page content is not rendered as expected. Here is my AJAX function: function showProfile(user_id) { console.log("show pro:" + u ...

Encountering a 403 error from AWS S3 bucket when trying to access a static folder path within a Django project

I have been successfully hosting static files in an S3 bucket for my Django web app using the Appwork custom admin template. The files render perfectly locally, but I am facing an issue with rendering from the S3 bucket. I suspect the problem lies in the s ...