kekday:

Simply appending the loss calculated inside closure() to a list initialized outside closure doesn’t seem to work.


Do you get any error or why is it not working?
Simply appending loss.item() to a list seems to work:
losses = []
def closure():
    optimizer.zero_grad()
    out = seq(input)
    loss = criterion(out, target)
    print('loss:', loss.item())
    losses.append(loss.item())
    loss.backward()
    return loss
optimizer.step(closure)