# Abnormal Loss curve after cosing annealing

**URL:** <https://discuss.pytorch.org/t/abnormal-loss-curve-after-cosing-annealing/82821>\
**Category:** Uncategorized\
**Created:** [May 25, 2020, 1:20pm UTC](https://discuss.pytorch.org/t/abnormal-loss-curve-after-cosing-annealing/82821 "2020-05-25T13:20:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rohan\_Kumar](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/rohan_kumar/32/10228_2.png) [@Rohan\_Kumar](https://discuss.pytorch.org/u/Rohan_Kumar)\
**Post date:** [May 25, 2020, 1:20pm UTC](https://discuss.pytorch.org/t/abnormal-loss-curve-after-cosing-annealing/82821/1 "2020-05-25T13:20:31Z")

</div>

I used cosine annealing on my transformer network and got this curve

 ![image](https://discuss.pytorch.org/uploads/default/original/3X/b/9/b93ef33df4052c66ac32bc767a9f2980770cdab0.jpeg)

I have used Cross Entropy as my loss function and this is my parameters for the scheduler with max lr = 1e-3

```auto
sch = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, len(train_data) , eta_min=0, last_epoch=-1)

```

And this is my training code, what can i do to improve this loss curve?

```auto
for epoch in tqdm(range(epochs)):
        model.train()
        epoch_loss = 0
        for batch in tqdm(train_iterator):
            src_inp = batch['src_sentence'].permute(1,0).cuda()
            trg_inp = batch['trg_sentence'].permute(1,0).cuda()
            in_tgt = trg_inp[:-1, :]
            exp_tgt = trg_inp[1:, :]
            optimizer.zero_grad()
            output= model(src_inp, in_tgt)

            output = output.view(-1, output.shape[-1])

            trg = exp_tgt.reshape(-1)
            
            loss = criterion1(output, trg)
            loss.backward()

            torch.nn.utils.clip_grad_norm_(model.parameters(), 0.25)
            for p in model.parameters():
                p.data.add_(-lr, p.grad)
            optimizer.step()
            sch.step()
            epoch_loss += loss.item()
        y.append(epoch_loss/len(train_iterator))
        epoch_loss = 0 
        model.eval()
        for batch in tqdm(val_iterator):
            src_inp = batch['src_sentence'].permute(1,0).cuda()
            trg_inp = batch['trg_sentence'].permute(1,0).cuda()
            in_tgt = trg_inp[:-1, :]
            exp_tgt = trg_inp[1:, :]
            optimizer.zero_grad()
            output= model(src_inp, in_tgt)

            output = output.view(-1, output.shape[-1])

            trg = exp_tgt.reshape(-1)

            loss = criterion1(output, trg)

            epoch_loss += loss.item()
            if(math.isnan(loss.item())):
                print(src_inp)
        y_val.append(epoch_loss/len(val_iterator))

```

---

<div class="post-metadata">

**Author:** ![Kushaj](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kushaj/32/58838_2.png) [@Kushaj](https://discuss.pytorch.org/u/Kushaj)\
**Post date:** [May 25, 2020, 6:14pm UTC](https://discuss.pytorch.org/t/abnormal-loss-curve-after-cosing-annealing/82821/2 "2020-05-25T18:14:51Z")

</div>

Maybe batch size and validation set size is small.
