0

Im working on a machine learning project and since I don't know the amount of categories in the dependend variables I don't want to hardcode it. In my project now I have 3 different categories but I keep getting the error:

Error: Invalid TF_Status: 3Message: Input to reshape is a tensor with 9 values, but the requested shape has 3

This is part of my code:

const outcomeOptions = data.Outcome_options;
console.log("Outcome options ", outcomeOptions); //logs [ 'good', 'bad', 'neutral']
const uniqueOptionsSet = new Set(outcomeOptions);
console.log("options set: ", uniqueOptionsSet); //logs [ 'good', 'bad', 'neutral']
const numUniqueOptions = uniqueOptionsSet.size; 
console.log("options size: ", numUniqueOptions); // logs 3
const numericalLabels = outcomeOptions.map(option => [...uniqueOptionsSet].indexOf(option));
console.log("labels ", numericalLabels); // logs [0,1,2]
const oneHotEncodedLabels = tf.oneHot(tf.tensor1d(numericalLabels, 'int32'), numUniqueOptions, 1);
console.log("encodedlabels: ", oneHotEncodedLabels); //logs given below the code

ys = oneHotEncodedLabels.reshape([numUniqueOptions, 1]);
console.log("ys:", ys) // doesn't log because the error occured.

The encodedlabels logs:

encodedlabels:  Tensor {kept: false, isDisposedInternal: false, shape: [ 3, 3 ], dtype: 'int32', size: 9, strides: [ 3 ], dataId: {}, id: 14, rankType: '2', scopeId: 2}

Does anyone know what I do wrong?

edit: clarification: I have a dependent variable with 3 unique options ['good', 'bad', 'neutral'] so I need a tenser with shape 3 but I get a tensor with 9 values, does anyone know why it gets the shape of 9, and how do I reshape it back to the required shape of 3.

5
  • The error is quite straightforward: your tensor has a shape [3,3] and you're trying to reshape it to [3,1], which obviously does not work. The question I have is what are you trying to accomplish? Can you describe the goal of your processing here?
    – Lescurel
    Commented Apr 30 at 7:28
  • Yes I try to train a machine learning model where the dependent variable has three options ([ 'good', 'bad', 'neutral']), but I don't understand why the tensor gets a shape of [3,3] because it needs to have a shape [3,1] right? I train the model later with: await model.fit(xs, ys, { epochs: 10 });. Do you have any idea why the tenser gets a [3,3] shape?
    – chrispsv
    Commented Apr 30 at 9:14
  • Well, that's the result of tf.oneHot. It one hot encodes your tensor, so that [0,1,2] becomes [[1,0,0],[0,1,0],[0,0,1]]. The required shape of your labels will depend on the output shape of your model and the loss function you want to use.
    – Lescurel
    Commented Apr 30 at 9:26
  • Ahh okay I misunderstood that function. Would you recommend using that function? or is there a way for tenserflow to then use the new 3*3 tensor? because since there are three options, tenserflow needs a 3*1 tenser, or is that not true?
    – chrispsv
    Commented Apr 30 at 9:40
  • 1
    What you're asking is way beyond the scope of this question, and there's no definitive answer to it. It depends on your model, what task you're working on, etc. TensorFlow does not "need" as 3x1 tensor, what it needs is a label in a shape that is compatible with the loss function you want to use. Have a look at the tutorials, that should clear some stuff up. Given that you're working on classification, I'd recommend a tutorial on that, like "Handwritten digit recognition with CNNs"
    – Lescurel
    Commented Apr 30 at 9:52

0