Could you check the shapes of data, output, pred and target?
Based on the error message I guess you might accidentally view or reshape the data or an activation in a wrong way, which could result in such a shape mismatch (e.g. by flattening the activation and removing the batch dimension).