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