If this is done inside an nn.Module, your should create y as self.y = nn.Parameter(x)  which is basically a Variable that requires grad. But then the module is aware that this is a learnable parameter.