node.jsneural-networktensortensorflow.js

Correct tensor shape for a single input of 1d array for Tensorflow.js model?


i am implementing simple MNIST digits recognition using Tensorflow.js.

Here is very primitive (for that example) model

const tf = require('@tensorflow/tfjs-node');

const model = tf.sequential();
model.add(
  tf.layers.dense({
    inputShape: [28 * 28],
    units: 128,
    activation: 'relu',
  })
);
model.add(
  tf.layers.dense({
    units: 10,
    activation: 'softmax',
  })
);
model.compile({
  loss: tf.losses.meanSquaredError,
  optimizer: 'adam',
  metrics: ['accuracy'],
});

As you can see, i want to pass plain 1d array of 784 numbers as input. Let's create it, filled with random, it doesn't matter for the question.

const arr = Array.from({ length: 784 }, () => Math.floor(Math.random()));

Tensorflow model should accept Tensor as input. I described inputShape as [784]. I have an array.

Let's transform it into 1 dimensional tensor, right?

const tensorInput = tf.tensor1d(arr);

Let's try to get prediction with that input:

const result = model.predict(tensorInput);
console.log('result:', result.dataSync());

It throws an error with not matching tensor dimentions:

ValueError: Error when checking : expected dense_Dense1_input to have shape [null,784] but got array with shape [784,1].

Why it does consider my tensor as 784 rows with 1 column?

I see it working fine if I create tensorInput as

const tensorInput = tf.tensor2d(arr, [1, 784]);

Is my assumption correct that Null in first place in shape ( [null, 784] ) means something like variable batch size?

Network by default expect data as batches, not a single row?

Am I doing everything correct or this is something not normal? It just causes some confusion, the fact that my single dimensional array should be created as 2d tensor in Tensorflow.

Thanks in advance.


Solution

  • Yes, it is just how Tensorflow works...