Why I am getting n predictions instead of one for single sample?

I wonder why my neural network give me n predictions for single sample. I was expecting to get only one prediction if I pass only one sample with size of input of neural network input layer, but I am always getting as many predictions as my input vector length. For example, if I have a neural-network which input layer size is 3, and I pass a vector with 3 values, I will get 3 predictions (each with size of my output layer size). Why is it like that, and what should I change to get only one prediction?

function createNetwork(inputSize, outputSize) {
  const model = tf.sequential();
  model.add(
    tf.layers.dense({
      units: inputSize,
      inputShape: [1],
      activation: 'relu'
    })
  );
  model.add(
    tf.layers.dense({
      units: 16,
      activation: 'relu'
    })
  );
  model.add(
    tf.layers.dense({
      units: outputSize
    })
  );
  model.compile({
    loss: 'meanSquaredError',
    optimizer: 'adam'
  });
  return model;
}

const network = createNetwork(3, 2);
const sample = tf.tensor1d([1, 2, 3]);
const prediction = network.model.predict(sample).arraySync();
console.log(prediction)

In this example, I have created a neural network with input layer of size 3 and output layer of size 2 then I have passed a vector with 3 variables and get 3 predictions (3×2 matrix insted 1x2 matrix)

Live demo

Hi,

So, as far as I understood your code,

you defined an input of 1 variable into a dense layer with 3 neurons. The output is 2 values as your last dense layer has 2 neurons.
If you give an input with 3 values, for each one you get 2 values, hence the 3x2 result

to change that, you need to change input_shape to 3, this will make your model understand the 3 values as one input and the output will be 1x2

model = keras.Sequential(
    [
        layers.InputLayer(input_shape=(3,)),
        layers.Dense(3, activation="relu", name="layer1"),
        layers.Dense(16, activation="relu", name="layer2"),
        layers.Dense(2, name="layer3"),
    ]
)
# Call model on a test input
model.summary()
x = tf.ones((1, 3))
y = model(x)
print(y)

sorry for the non-JS code

1 Like

Hi @lgusm ,

Thanks for your answer. As you said, 'you defined an input of 1 variable ', and that was my main issue. After I read this tutorial, TensorFlow.js — Making Predictions from 2D Data (google.com). I understand that units(size of the weight’s matrix for the layer) and inputShape(how many values we want to have on the input) is sth a bit different from I thought.
So, I had to do 2 things:

  • change the input shape of my neural network
  • pass my input as Tensor2D with defined shape

Still do not know why I cannot use Tensor1D. I thought this will be treated as a single input, but it looks like every value of Tensor1D is a single input data for neural network, and you will always get as many outputs as long as your Tensor1D input is. Do you know why this work like that?

Code should look like that:

function createNetwork(inputSize, outputSize) {
  const model = tf.sequential();
  model.add(
    tf.layers.dense({
      units: 1,
      inputShape: [inputSize],
      activation: 'relu'
    })
  );
  model.add(
    tf.layers.dense({
      units: 16,
      activation: 'relu'
    })
  );
  model.add(
    tf.layers.dense({
      units: outputSize
    })
  );
  model.compile({
    loss: 'meanSquaredError',
    optimizer: 'adam'
  });
  return model;
}

const network = createNetwork(3, 2);
const input = [[1, 2, 3]];
const sample = tf.tensor2d([1, 2, 3], [input.length, input[0].length]);
const prediction = network.model.predict(sample).arraySync();
console.log(prediction);

Live demo

I think it’s because of the BATCH size witch you didn’t define so it can be anything, meaning that your model will accept any number of instances of the predefined input.

From the documentation:

Input shape:

N-D tensor with shape: (batch_size, ..., input_dim). The most common situation would be a 2D input with shape (batch_size, input_dim)

1 Like

OK, now I see it will be more ‘flexible’ if I do not define this batch size. Now I have all my answers, thx :smiley:

1 Like