Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input shape in keras (This loss expects targets to have the same shape as the output)

this is my first time using keras, I'm trying to follow a tutorial I've found online and fit my own data to it. I have a matrix and binary labels.

> str(d_train)
 num [1:1062, 1:180] -0.04748 0.04607 -0.05429 -0.0126 -0.00219 ...
> str(trainlabels)
 num [1:1062, 1:2] 0 0 0 0 0 0 1 0 0 0 ...

my code:

model = keras_model_sequential()
model %>%
  layer_dense(units = 8, activation = 'relu', input_shape = c(180)) %>%
  layer_dense(units = 3, activation = "softmax")
summary(model)
## Compile
model %>%
  compile(loss = "binary_crossentropy",
          optimizer = "adam",
          metrics = "accuracy")
## Fit model
history = model %>%
  fit(d_train,
      trainlabels,
      epoch=200,
      batch_size=32,
      validation_split=0.2)

I can't seem to fit the model, I'm getting this error message:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: A target array with shape (1062, 2) was passed for an output of shape (None, 3) while using as loss `binary_crossentropy`. This loss expects targets to have the same shape as the output.

Based on the error message, asking for different shape of my input array, I tried to change the dimensions around with no luck.

like image 941
mprachar Avatar asked Jun 14 '19 13:06

mprachar


1 Answers

I am not an R expert, but here:

layer_dense(units = 3, activation = "softmax")

You are telling Keras that the output of your network has three classes. Your labels have shape (1062, 2) which suggest it has two classes, hence there is an inconsistency.

You could just change units = 2 in your last dense and it should work. Also note that you are using the softmax activation, and in that case you should prefer to use the categorical_crossentropy loss.

To use binary_crossentropy for binary classification, you should have units = 1, sigmoid activation, and labels should be (1062, 1) or (1062,), which means they are 0-1 encoded.

like image 194
Dr. Snoopy Avatar answered Oct 25 '22 04:10

Dr. Snoopy