Home > front end >  Tensorflow: no errors when size of output layer doesn't match size of expected results
Tensorflow: no errors when size of output layer doesn't match size of expected results

Time:08-24

This is more of a question than a problem.
Let's say we have the following code/model:

model = Sequential()
model.add(layers.Dense(10, input_shape=train_x.shape[1:], activation=activations.tanh))
model.add(layers.Dense(1, activation=activations.linear))
model.compile(optimizer=Adam(learning_rate=1e-3, decay=1e-5), loss=MSE)

model.fit(x=train_x, y=train_y, epochs=10, validation_data=(test_x, test_y))

The shape of train_x is (100,3).
The shape of train_y is (100,2).
The shape of test_x is (20,3).
The shape of test_y is (20,2).

I would expect an error that the shape of the output layer doesn't match the shape of train_y elements - output layer has 1 neuron and the elements in train_y have 2 features (or whatever they are called). There is no error, just the loss doesn't change much.
So my questions are:
How does this work?
Is it a bug or is there a reason for it to work like that?
Am I doing something wrong?

I am using tensorflow 2.9.1 and python 3.8 by the way.

CodePudding user response:

It is not a bug. It is just simple broadcasting:

import tensorflow as tf

x1 = tf.random.normal((20, 2))
x2 = tf.random.normal((20, 1))

tf.keras.losses.MSE(x1, x2) # works with the help of broadcasting
  • Related