# Plotting loss with LBFGS

**URL:** <https://discuss.pytorch.org/t/plotting-loss-with-lbfgs/50400>\
**Category:** Uncategorized\
**Created:** [July 11, 2019, 10:20pm UTC](https://discuss.pytorch.org/t/plotting-loss-with-lbfgs/50400 "2019-07-11T22:20:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kekday](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kekday/32/11990_2.png) [@kekday](https://discuss.pytorch.org/u/kekday)\
**Post date:** [July 11, 2019, 10:20pm UTC](https://discuss.pytorch.org/t/plotting-loss-with-lbfgs/50400/1 "2019-07-11T22:20:56Z")

</div>

Hi,  
Looking at the example for how LBFGS needs a closure() function to be used ([https://github.com/pytorch/examples/blob/master/time\_sequence\_prediction/train.py](https://github.com/pytorch/examples/blob/master/time_sequence_prediction/train.py)), is there a way I could plot the loss as well? Simply appending the loss calculated inside closure() to a list initialized outside closure doesn’t seem to work.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [July 11, 2019, 10:28pm UTC](https://discuss.pytorch.org/t/plotting-loss-with-lbfgs/50400/2 "2019-07-11T22:28:38Z")

</div>

> [@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:

```python
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)

```

---

<div class="post-metadata">

**Author:** ![kekday](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kekday/32/11990_2.png) [@kekday](https://discuss.pytorch.org/u/kekday)\
**Post date:** [July 11, 2019, 10:48pm UTC](https://discuss.pytorch.org/t/plotting-loss-with-lbfgs/50400/3 "2019-07-11T22:48:24Z")

</div>

My bad, it does seem to work indeed. Not quite sure what I missed last time since I tried the exact same thing and the list just stayed empty. Thanks a lot!
