Search code examples
javascripttensorflowmachine-learningartificial-intelligencetensorflow.js

TensorFlow.js train using multiple inputs one output


I´m currently trying to write a system that can classify specific number sequence to action. Trying to build it with tensorflow.js has worked fine so far, but now i´m running into some issues.

I´m trying train the model using an input like

[
    [0,1,2,3,4,5,6,7,8,9],
    [0,1,2,3,4,5,6,7,8,9],
    [0,1,2,3,4,5,6,7,8,9],
    [0,1,2,3,4,5,6,7,8,9],
    [0,1,2,3,4,5,6,7,8,9],
]

that for example should result in an output [1]

made a tensor as follows

let t = tf.tensor2d([...Array.from({ length: (5 * 25) }, (v, i) => i)], [5, 25])

//Tensor
//   [[0  , 1  , 2  , ..., 22 , 23 , 24 ],
//     [25 , 26 , 27 , ..., 47 , 48 , 49 ],
//     [50 , 51 , 52 , ..., 72 , 73 , 74 ],
//     [75 , 76 , 77 , ..., 97 , 98 , 99 ],
//     [100, 101, 102, ..., 122, 123, 124]]

Now this looks like i wanted it, now t.shape gives me [ 5, 25 ] which i thought i would use as an inputShape for my model, upon research I found out that i need to define it as batchInputShape instead of inputShape, which eliminated one more error.

loadedModel.add(tf.layers.dense({ units: 256, activation: 'relu', batchInputShape: [5, 25] }));

Now I´m running into the issue that the model treats the tensor as multiple values, thus looking for multiple outputs, but i´m trying to have it only give one output

Upon running await loadedModel.fit(t, tf.tensor([0]), { epochs: 5 })

I get Input Tensors should have the same number of samples as target Tensors. Found 5 input sample(s) and 1 target sample(s).

Which I think indicates what I wrote above.

Now my final question is, how should I setup the inputShape (if that is what i´m doing wrong) so that only one output is expected.

Kind Regards


Solution

  • Found out that if I just flat down the array of data, it should work out aswell