0

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:

from ignite.engine import Events, Engine, create_supervised_trainer, create_supervised_evaluator
from ignite.metrics import RootMeanSquaredError, Loss
from ignite.handlers import EarlyStopping

 D_in, H, D_out = 5, 10, 1
 model = simpleNN(D_in, H, D_out) # a simple MLP with 1 Hidden Layer
 model.double()
 train_loader, val_loader = get_data_loaders(i)

 optimizer = torch.optim.LBFGS(model.parameters(), lr=1)
 loss_func = torch.nn.MSELoss()  
    
 #Ignite
 trainer = create_supervised_trainer(model, optimizer, loss_func)
 evaluator = create_supervised_evaluator(model, metrics={'RMSE': RootMeanSquaredError(),'LOSS': Loss(loss_func)})
    
 @trainer.on(Events.ITERATION_COMPLETED)
 def log_training_loss(engine):
     print("Epoch[{}] Loss: {:.5f}".format(engine.state.epoch, len(train_loader), engine.state.output))
    
def score_function(engine):
    val_loss = engine.state.metrics['RMSE']
    print("VAL_LOSS: {:.5f}".format(val_loss))
    return -val_loss
    
handler = EarlyStopping(patience=10, score_function=score_function, trainer=trainer)
evaluator.add_event_handler(Events.COMPLETED, handler)
    
trainer.run(train_loader, max_epochs=100)

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?

Riverarodrigoa
  • 554
  • 8
  • 21

2 Answers2

1

The way to do it is like this:

    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

Source

Riverarodrigoa
  • 554
  • 8
  • 21
0

You need to encapsulate all evaluating step with zero_grad and returning step in

    for batch in loader():
        def closure():
          ...
          return loss
        optim.step(closure)

Pytorch docs for 'closure'

Kobap Bopy
  • 17
  • 5