In the forward function of Unet you are using several variables (x1,x2,…) to the store intermediate results of layers which remain in the memory rather i would use only one variable like x as in your other class.