1

My model doesn´t learn.. It is supposed to do a softmax calculation in the end. I want as a result a classification (quit or no-quit). The model should predict if the customer will quit. I am giving the quit-column as label and have 196 input-features.

My visor says there is no learning at all. But then I am not certain, how the visor will get information, if my model learns. I am very new to javascript and would appreciate any help.

ngOnInit() {
  this.train();
}


async train(): Promise<any> {
  const csvUrl = '/assets/little.csv';
  const csvDataset = tf.data.csv(
    csvUrl,
    {
      columnConfigs: {
        quit: {
          isLabel: true
        }
      },
      delimiter:','
    });
  const numOfFeatures = (await csvDataset.columnNames()).length -1;      
  console.log(numOfFeatures);
  const flattenedDataset =
  csvDataset
  .map(({xs, ys}: any) =>
    {
      // Convert xs(features) and ys(labels) from object form (keyed by
      // column name) to array form.
      return {xs:Object.values(xs), ys:Object.values(ys)};
    }).batch(10);    
  console.log(flattenedDataset.toArray());      

  const model = tf.sequential({
    layers: [
      tf.layers.dense({inputShape: [196], units: 100, activation: 'relu'}),
      tf.layers.dense({units: 100, activation: 'relu'}),
      tf.layers.dense({units: 100, activation: 'relu'}),        
      tf.layers.dense({units: 1, activation: 'softmax'}),        
    ]
  }); 
  await trainModel(model, flattenedDataset);
  const surface = { name: 'Model Summary', tab: 'Model Inspection'};
  tfvis.show.modelSummary(surface, model);    
  console.log('Done Training');
}

async function trainModel(model, flattenedDataset) {
  // Prepare the model for training.  
  model.compile({
    optimizer: tf.train.adam(),
    loss: tf.losses.sigmoidCrossEntropy,
    metrics: ['accuracy']
  });

  const batchSize = 32;
  const epochs = 50;

  return await model.fitDataset(flattenedDataset, {
    batchSize,
    epochs,
    shuffle: true,
    callbacks: tfvis.show.fitCallbacks(
      { name: 'Training Performance' },
      ['loss'],
      { height: 200, callbacks: ['onEpochEnd'] }
    )
  });
}  
7
  • Did you try a different loss function such as categoricalCrossentropy ? And change the config to be this way: loss: 'categoricalCrossentropy' Commented May 14, 2020 at 10:05
  • With your change, the loss-Value stays at a much lower level, but is still a flat line. Before, the line was at around 0.75. With your change it stays at around 0.00005. But still the algorithm doesnt seem to learn.. Commented May 14, 2020 at 11:00
  • The softmax activation is for classification problem. Your model seems not to do a classification. Your last layer has a single unit which indicates that you are doing a regression. Your model is most likely not learning because of that Commented May 14, 2020 at 11:36
  • That would be a good explanation! I updated the question. Can you provide a suggestion, how I must change the code, so the model does classification correctly? I want the output to be a classification of quit/no-quit. Commented May 18, 2020 at 6:18
  • The number of units for the last layer is the number of categories. There are two categories in quit and no-quit. Additionnaly your labels should be one-hot encoded. More general answers on why a model is not learning can be found here Commented May 18, 2020 at 7:36

1 Answer 1

1

The number of units for the last layer is the number of categories. There are two categories quit and no-quit. Additionnaly your labels should be one-hot encoded. More general answers on why a model is not learning can be found here.

Sign up to request clarification or add additional context in comments.

1 Comment

That was the solution! I added a column non-quit which I defined to be 1 - quit (So I am one hot-encoded there). Then I changed the code to columnConfigs: { quit: { isLabel: true },noquit:{ isLabel: true } }, and the last layer to tf.layers.dense({units: 2, activation: 'softmax'}), Now it is learning !!! Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.