Solved by rasbt in post #2 
              
              
                Well looks like your x is a 3D tensor, not a 4D tensor. I suspect there is no color channel (grayscale images) in your case, so 
B, C, H, W = x.shape

should be 
B, H, W = x.shape