Based on the screenshot it seems you are manually assigning dy.unsqueeze(0).clone() to param.grad, which is causing the error. Check the shape of param. param.grad, and dy.unsqueeze(0) before the assignment and make sure all have the same shape.