I am using pytorch to optimize a certain function f(x) . SO, I created a function f , That gives me the gradient as following:
f  = lambda x: gradient(x)
"gradient (x) is a function to calculate the gradient"

Then, I try to use LBFGS to optimize this function as follwoing:
opt = optim.LBFGS([X],lr=.1)
for _ in range(1000):
    def closure():
        opt.zero_grad()
        X.grad = -f(X)
        return X
    opt.step(closure)

But, I got this error:
RuntimeError: assigned grad has data of a different size

How can I solve this issue, with the same setup.