1

I'm trying to train a model and never pass the fit().

In the console doesn't show the loss result, it gets stuck there.

Already changed the async to a promise, but it's the same.

To see the entire code, click here!

function train() {
  trainModel().then(result => {
    console.log(result.history.loss[0]);
    setTimeout(train, 100);
  });
}

// entrena modelo~ params = train_xs(input) y train_ys(output)

async function trainModel() {

  //Create the input data

     for (let i = 0; i < 5; i++) {
        train_xs = tf.tensor2d(ins.pixels[i], [28, 28], 'int32');
        train_ys = tf.tensor2d(outs.coords[i], [3, 2], 'int32');

        const h = await model.fit(train_xs, train_ys, {
         epochs: 1

        });
        console.log("Loss after Epoch " + i + " : " + h.history.loss[0]);
      }
      console.log('end fitness model');
    }

//never shows end fitness model

no error messages, the console keeps just clean

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dinda
  • 11
  • 1

1 Answers1

0

There are a couple of issues (the console is clean because it was not logging out the errors):

  • the shape of xs and ys does not match the input and output of the model.ins.pixels[i]

  • xs and ys should have the same batch size. Since in all iteration of the for loop, only one feature and one label is used, therefore the batchsize is 1.

Here is a fix of the model

let model;

let xs;
let train_xs;
let train_ys; 
let inAndOut;

let resolution = 20;
let cols;
let rows;

var ins;
var outs;


function setup() {
  createCanvas(400, 400);
  /// visualization

  ins = new Inputs13(); // ins.pixels;
  outs = new Outputs13(); // outs.coords;
  inAndOut = new InputsAndOutputsToTest();



  ///crear modelo
  model = tf.sequential();

  let hidden = tf.layers.dense({
    inputShape: [784],
    units: 28,
    activation: 'sigmoid'
  });

  let output = tf.layers.dense({
    units: 6,
    activation: 'sigmoid'
  });

  model.add(hidden);
  model.add(output);

  const optimizer = tf.train.adam(0.1);
  model.compile({
    optimizer: optimizer,
    loss: 'meanSquaredError'
  })

  xs = tf.tensor2d(inAndOut.pixelsToTest[0],[28,28]);
  //console.log('xs');
  //console.log(xs);
  //xs.print();
  //entrena modelo
  setTimeout(train, 10);
}


//promesa, llama a entrenar modelo y muestra de losss
function train() {
  console.log("im in train!");
  trainModel().then(result => {
    console.log(result.history.loss[0]);
    setTimeout(train, 100);
  });

}

// entrena modelo~ params = train_xs(input) y train_ys(output)
async function trainModel() {
  let h;
    //Create the input data
  for (let i = 0; i < 5; i++) {
   train_xs = tf.tensor(ins.pixels[i], [1, 784]); //[tens], [shape]
   console.log('xs.shape', train_xs.shape)
   train_ys = tf.tensor(outs.coords[i]).reshape([1, 6]);
   console.log('ys.shape', train_ys.shape)
 /* console.log('train_xs');
   train_xs.print();
  console.log("train_ys");
  train_ys.print();*/

   h = await model.fit(train_xs, train_ys, {
   // shuffle: true,
    epochs: 1

  });
   console.log("Loss after Epoch " + i + " : " + h.history.loss[0]);
 }
  console.log('end fitness model');
  return h;
}


//muestra visual!
function draw() {
  background(220);

  //Get the predictions params xs = inputs para pruebas
  tf.tidy(() => {
    let ys = model.predict(xs);
    //console.log("ys");
    //console.log(ys);

    let y_values = ys.dataSync();
   //  console.log("y_values");
   // console.log(y_values);

  });

}

However, it is possible to use all the 13 features and 13 labels all at once. The for loop will no longer be useful.

   train_xs = tf.tensor(ins.pixels, [13, 784]);
   console.log('xs.shape', train_xs.shape)
   train_ys = tf.tensor(outs.coords).reshape([13, 6]);
edkeveked
  • 17,989
  • 10
  • 55
  • 93