You have returned e_x,n_x  in the model. However, when you call the model output_1 = model(images). Here output1 corresponds to e_x   (shape : 8,512,22,40) and not n_x, which is what you would have to give to your loss. Try returning the required value as _,output_1 = model(images).