I have a weighted sparse_categorical_crossentropy loss which is not working after updating to Keras 2.1.1. The previous version I was using was 2.0.6, and it was working fine then.
There are 2 classes (0 and 1) in the Output Layer. I pass the flattened 1D array with an extra dimension as the label.
label = label.flatten().astype(np.int16)
label = np.expand_dims(label, axis=-1)
If I call sparse_categorical_crossentropy directly then there are no errors shown and the training starts. To verify that there was nothing wrong in the way custom weighted loss function should be called, I copied the sparse_categorical_crossentropy from keras/backend/tensorflow_backend.py, renamed it to scc and assigned it to the loss function.
// default
net.model.compile(optimizer=Adam(lr=0.001), loss=sparse_categorical_crossentropy)
// scc is just a copy of sparse_categorical_crossentropy from tensorflow_backend
net.model.compile(optimizer=Adam(lr=0.001), loss=scc)
This gives the following error:
ValueError: Error when checking target: expected mask to have shape (None, 4096, 2) but got array with shape (8, 4096, 1)
If I change the label to One-Hot encoding format to correspond to the 2 labels, then tensorflow throws an error as shown below:
label = np.hstack((label, (~label.astype(bool)).astype(int)))
W tensorflow/core/framework/op_kernel.cc:1192] Invalid argument: logits and labels must have the same first dimension, got logits shape [32768,2] and labels shape [65536]
Any idea what I am doing wrong ?