Closed riverarodrigoa closed 5 years ago
Hi @riverarodrigoa ,
I do not know exactly how works LBFGS
with closures, but with ignite it could be probably used like that:
from ignite.engine import Engine
model = ...
optimizer = torch.optim.LBFGS(model.parameters(), lr=1)
criterion =
def update_fn(engine, batch):
model.train()
x, y = batch
# pass to device if needed as here: https://github.com/pytorch/ignite/blob/40d815930d7801b21acfecfa21cd2641a5a50249/ignite/engine/__init__.py#L45
def closure():
y_pred = model(x)
loss = criterion(y_pred, y)
optimizer.zero_grad()
loss.backward()
return loss
optimizer.step(closure)
trainer = Engine(update_fn)
# everything else is the same
Let me know if this solves the problem.
PS. Please read Concepts for more details
Close the issue as answered. Feel free to reopen if needed
Hi all,
I started using Ignite recently and i found it very interesting. I would like to train a model using as an optimizer the LBFGS algorithm from the
torch.optim
module.This is my code:
And the error that raises is:
TypeError: step() missing 1 required positional argument: 'closure'
I know that is required to define a closure for the implementation of LBFGS, so my question is how can I do it using ignite? or is there another approach for doing this?
Thank you