Hi all,

I want to use ‘optimiser = optim.LBFGS(model.parameters(), lr=1e-4)’ instead of ‘optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)’

but I didn’t know how to introduce ‘def closure():’

can someone please explain to me how to modify the following code to use optim.LBFGS

here is the code:

```
def train_model(model, trainDataset, valDataset, number_epochs):
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
criterion = nn.MSELoss().to(device)
hist = dict(train=[], val=[])
best_loss = 25.0
best_m_wts = copy.deepcopy(model.state_dict())
for epoch in range(1, number_epochs + 1):
model = model.train()
train_losses = []
for sequence_true in trainDataset:
optimizer.zero_grad()
sequence_true = sequence_true.to(device)
sequence_pred = model(sequence_true)
loss = criterion(sequence_pred, sequence_true)
loss.backward()
optimizer.step()
train_losses.append(loss.item())
val_losses = []
model = model.eval()
with torch.no_grad():
for sequence_true in valDataset:
sequence_true = sequence_true.to(device)
sequence_pred = model(sequence_true)
loss = criterion(sequence_pred, sequence_true)
val_losses.append(loss.item())
train_loss = np.mean(train_losses)
val_loss = np.mean(val_losses)
hist['train'].append(train_loss)
hist['val'].append(val_loss)
if val_loss < best_loss:
best_loss = val_loss
best_m_wts = copy.deepcopy(model.state_dict())
print(f'Epoch {epoch}: train loss {train_loss} val loss {val_loss}')
model.load_state_dict(best_m_wts)
return model.eval(), hist
```

Thanks a lot for your help