C2W1 Assignment: model not compatible with data

In the Cats vs. Dogs Assignment in Week 1 of the course Convoluational Neural Networks in Tensorflow, I get an error although all the preceding unit tests are run without any problem.

When fitting the model, I get the error message: “ValueError: Arguments target and output must have the same rank (ndim). Received: target.shape=(None, 1), output.shape=(None, 17, 17, 1)”

Earlier, I get the warning: “Your model is not compatible with the dataset you defined earlier. Check that the loss function, last layer and label_mode are compatible with one another.”

I am using ‘binary_crossentropy’ as loss, the last layer is “tf.keras.layers.Dense(1, activation=‘sigmoid’)” and the label_mode=‘binary’.

you are using incorrect dense unit for the last layer and incorrect activation, remember your label_mode is binary, so dense unit need to be 2 and as the class mode are specific to its dataset samples using binarycrossentropy as loss is incorrect, all are interconnected issues.

Use correct dense layer unit for last layer.
Change the activation remember label mode being binary doesn’t just specify that sigmoid would be right choice. if your model architecture input shape is using more than 2 features, sigmoid might or might not be correct choice, so when there are more features, what activation does one use? (I can only provide you hints)

But your error feedback just isn’t pointing on these specific errors.

Can I know what is your Input shape?
Remember input shape here is expecting 3D image, so make sure you have added number of channels too to the input shape.

Thank you for the hints!

The input shape in the model architecture definition is tf.keras.Input(shape=(150, 150, 3)). When reading in the files I used image_size=(150, 150) in the image_dataset_from_directory()-method.

Shape of batch of images: (128, 150, 150, 3)
Shape of batch of labels: (128, 1)

change the unit, activation and loss as I mentioned before. let me know if you are still encountering the issue.

remember a class specific model requires one to use Sparsecategoricalcrossentropy loss.

I used tf.keras.layers.Dense(2, activation=‘softmax’) now along with loss=tf.keras.losses.SparseCategoricalCrossentropy(). The warning “Your model is not compatible with the dataset you defined earlier. Check that the loss function, last layer and label_mode are compatible with one another” still comes up.

The error is now different: “ValueError: Argument output must have rank (ndim) target.ndim - 1. Received: target.shape=(None, 1), output.shape=(None, 17, 17, 2)”.

In my understanding, we are still doing a binary classification task (dogs vs. cats), not a multi-class classification (as with the fashion dataset). That’s why I thought sigmoid would be the correct loss function. I suspect the error to be in the way the data is read in.

I found my mistake. I have forgotten to include a layer flattening the input data before the final dense layer, that’s why the dimensions did not match.

1 Like