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

23,004

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.

Share:
23,004
mprachar
Author by

mprachar

Data Science, Bioinformatics graduate. Immunoinformatics research. Python, PyTorch, R.

Updated on June 15, 2020

Comments

  • mprachar
    mprachar almost 4 years

    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.

  • mprachar
    mprachar almost 5 years
    Thank you for the suggestions. I tried following: changed the last layer_dense to 1 used a "sigmoid" activation with binary_crossentropy and instead of one-hot encoding just used a vector of binary encoded values (num [1:1062] 1 1 1 1 1 1 0 1 1 1 ...) and I'm getting another error: Error in py_call_impl(callable, dots$args, dots$keywords) : UnboundLocalError: local variable 'a' referenced before assignment
  • Dr. Snoopy
    Dr. Snoopy almost 5 years
    @MarekPrachař This looks like incompatible versions of tensorflow or keras, you should try the latest for both (tf 1.13 and keras 2.2.4).
  • mprachar
    mprachar almost 5 years
    Okay so it seems there was a problem with TensorFlow version 1.13, which can be solved by: install_tensorflow(version = "1.12") After that it's working! Thank you