Your model works correctly using random input shapes, so I guess the shape mismatch is caused in the loss calculation, in which case you would have to check the shapes of the model output and target tensor and make sure they have the expected shapes.
I don’t know which criterion you are using, but the docs explain the expected shapes for them.