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.
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.