3

In my training set, my classes are not represented equally. Therefore, I'm trying to use the classWeight option of the model.fit function. Quote from the docs:

classWeight ({[classIndex: string]: number}) Optional dictionary mapping class indices (integers) to a weight (float) to apply to the model's loss for the samples from this class during training. This can be useful to tell the model to "pay more attention" to samples from an under-represented class.

So this seems to be exactly what I'm looking for, but I'm not able to figure out how to use it. The classIndex is nowhere else present in the documentation and I don't understand how a class can be written as an index. As my code is one-hot encoded I even tried to use the index of that (0 for [1,0,0], etc.), but it did not work (see below).

Here is a minimal code sample:

const xs = tf.tensor2d([ [1,2,3,4,5,6], /* ... */ ]); // shape: [1000,6]
const ys = tf.tensor2d([ [1,0,0], /* ... */ ]);       // shape: [1000,3] (one-hot encoded classes)

const model = tf.sequential();
// ... some layers

await model.fit(xs, ys, {
    /* ... */
    classWeight: {
        0: 10000, // doesn't do anything
        1: 1,
        2: 1,
    },
});

I would expect my model to pay "more attention" to my first class ([1,0,0]) and therefore predict it more often. But it seems Tensorflow.js is just ignoring the parameter.

Thomas Dondorf
  • 23,416
  • 6
  • 84
  • 105
  • I have the same problem, did you manage to find a solution ? – Sebastien Apr 26 '20 at 03:19
  • @Sebastien Unfortunately not. – Thomas Dondorf Apr 26 '20 at 07:22
  • I'm trying to train a NN for a "one vs rest" kind of problem (anomalies detections). The classWeight didn't work for me, but neither did manual undersampling or oversampling techniques. The NN quickly converge to a state where every prediction is the "catchall" class. I'm starting to think that it means that maybe the provided data isn't enough to be able to predict anomalies correctly. Maybe you are (were) in the same kind of situation? – Sebastien Apr 26 '20 at 20:56

0 Answers0