Skip to content

Get NAN loss after 35k steps #4

Open

Description

@StevenLOL

Anyone got NAN ?
selection_059

Activity

  1. abisee commented on May 5, 2017

    @abisee
    Owner

    @StevenLOL I see this happen sometimes too -- seems to be a very common problem with Tensorflow training in general.

  2. hate5six commented on May 7, 2017

    @hate5six

    I've been having this problem and I decreased the learning rate as per various discussions on SO and that seemed to work. After a while I tried increasing it by 0.01 and started getting NaN's again. I've tried restoring the checkpoint and re-running with the lower learning rate, but I'm still seeing NaN. Does this mean my checkpoint is useless?

  3. aar-gee commented on May 8, 2017

    @aar-gee

    I am also getting NaN. Found out the culprit to be
    line #227: log_dists = [tf.log(dist) for dist in final_dists]
    in model.py

  4. hate5six commented on May 8, 2017

    @hate5six

    @Rahul-Iisc Is your workaround to filter out cases where dist == 0, for example:

    log_dists = [tf.log(dist) for dist in final_dists if dist != 0] ?

  5. aar-gee commented on May 8, 2017

    @aar-gee

    @hate5six I'm still thinking about an appropriate solution. Each dist is a tensor shape (batch_size, extended_vsize), so I am not sure if dist != 0 will work. Also, I want the log_dists length to be the same as final_dists

  6. aar-gee commented on May 8, 2017

    @aar-gee

    Trying to convert NaNs to 0 for now. Need to further look for the cause of 0s coming up in the distribution. @abisee @StevenLOL reopen the issue?

    def _change_nan_to_number(self, tensor, number=1):
        return tf.where(tf.logical_not(tf.is_finite(tensor)), tf.ones_like(tensor) * number, tensor)
    
    log_dists = [tf.log(self._change_nan_to_number(dist)) for dist in final_dists]
    

    UPDATE: This didn't work. Loss ended up 0 instead of NaN.

  7. reopened this on May 8, 2017
  8. aar-gee commented on May 9, 2017

    @aar-gee

    The below change worked for me. Add the below code to def _calc_final_dist(self, vocab_dists, attn_dists). Info is in comments.

          # OOV part of vocab is max_art_oov long. Not all the sequences in a batch will have max_art_oov tokens.
          # That will cause some entries to be 0 in the distribution, which will result in NaN when calulating log_dists
          # Add a very small number to prevent that.
    
          def add_epsilon(dist, epsilon=sys.float_info.epsilon):
            epsilon_mask = tf.ones_like(dist) * epsilon
            return dist + epsilon_mask
    
          final_dists = [add_epsilon(dist) for dist in final_dists]
          
          return final_dists
    
  9. lizaigaoge550 commented on May 9, 2017

    @lizaigaoge550

    final_dists = [tf.clip_by_value(dist,1e-10,1.) for dist in final_dists]

  10. jamesposhtiger commented on May 25, 2017

    @jamesposhtiger

    @lizaigaoge550 did this work for you?
    final_dists = [tf.clip_by_value(dist,1e-10,1.) for dist in final_dists]
    Could you let me know which line you put this at?
    Many thanks

  11. lizaigaoge550 commented on May 27, 2017

    @lizaigaoge550

    @jamesposhtiger
    after finishing the final_dists

  12. added 2 commits that reference this issue on May 30, 2017
  13. apoorv001 commented on Jun 1, 2017

    @apoorv001

    can we restore the already trained model after it starts getting NaN ?

  14. 7 remaining items

  15. abisee commented on Aug 5, 2017

    @abisee
    Owner

    I've looked further into this and still don't understand where the NaNs are coming from. I changed the code to detect when a NaN occurs, then dump the attention distribution, vocabulary distribution, final distribution and some other stuff to file.

    Looking at the dump file, I find that attn_dists and vocab_dists are both all NaN, on every decoder step, for every example in the batch and across the encoder timesteps (for attn_dists) and across the vocabulary (for vocab_dists). Consequently final_dists contains NaNs, therefore log_dists does and the final loss is NaN too.

    This is different than what I was expecting. I was expecting to find zero values in final_dists and therefore NaNs in log_dists, but it seems that the problem occurs earlier, somehow causing NaNs everywhere in attn_dists and vocab_dists.

    Given this information, I don't see why adding epsilon to final_dists works as a solution, because if attn_dists and vocab_dists contain NaNs, it shouldn't work.

  16. bwang482 commented on Aug 7, 2017

    @bwang482

    Thanks for your update! The strange thing is, it does work @abisee. By adding epsilon I have not encountered NaNs again. But it does affect the training convergence a bit. By how much, I dunno.

  17. shahbazsyed commented on Aug 10, 2017

    @shahbazsyed

    @bluemonk482 I tried adding epsilon as mentioned in the previous discussions above. I still get the NaN loss after one day of training. Can you tell me what was the learning rate you used for your experiments? I tried using 0.1.

  18. bwang482 commented on Aug 10, 2017

    @bwang482

    @shahbazsyed Well I used as big a learning rate as 0.15, and had no NaN error after adding epsilon. How did you add the epsilon? Like this (as @Rahul-Iisc has suggested):

          def add_epsilon(dist, epsilon=sys.float_info.epsilon):
            epsilon_mask = tf.ones_like(dist) * epsilon
            return dist + epsilon_mask
    
          final_dists = [add_epsilon(dist) for dist in final_dists]
    
  19. shahbazsyed commented on Aug 12, 2017

    @shahbazsyed

    @bluemonk482 Yes, I added epsilon just as @Rahul-Iisc suggested. I tried with a higher learning rate of 0.2 , but am still getting the same error. Did you change any other parameters ? Mine are the following :

    hidden_dim = 300
    emb_dim = 256
    coverage = true
    lr = 0.2
    
  20. bwang482 commented on Aug 12, 2017

    @bwang482

    @shahbazsyed I see you have changed the default parameter setting. I used 128 for emb_dim. And why did you increase your learning rate to 0.2 when you had NaN error at 0.1? You can try a smaller learning rate.

  21. shahbazsyed commented on Aug 14, 2017

    @shahbazsyed

    @bluemonk482 Still got the NaN with lower learning rate after 42000 steps. I tried decoding with the model trained so far, it was just a bunch of UNKs.

  22. aar-gee commented on Aug 14, 2017

    @aar-gee

    Use tfdbg.

  23. abisee commented on Aug 16, 2017

    @abisee
    Owner

    Hello everyone, and thanks for your patience. We've made a few changes that help with the NaN issue.

    • We changed the way the log of the final distribution is calculated. This seemed to result in many fewer NaNs (but we still encountered some).
    • We provide a script to let you directly inspect the checkpoint file, to see whether it's corrupted by NaNs or not.
    • New flag to allow you to restore a best model from the eval directory.
    • Train job now halts when it encounters non-finite loss.
    • The train job now keeps 3 checkpoints at a time -- useful for restoring after NaN.
    • New flag to run Tensorflow Debugger.

    The README also contains a section about NaNs.

    Edit: We've also provided a pretrained model, which we trained using this new version of the code. See the README.

  24. eduOS commented on Jan 11, 2018

    @eduOS

    I encountered the same problem and found that it is because of the zeros examples in the batch. If the training corpus contains $k * batch_size - a$ where $a<k$ examples/samples there would be $k - a$ zero examples. (single pass)

    I just add a line next to line 329 in batcher:

    if len(b) != self._hps.batch_size:
        continue

    May this help a little.

  25. mahnazkoupaee commented on Feb 1, 2018

    @mahnazkoupaee

    There might be another cause for NANs. There are some stories in the dataset which only contain highlights and not the article itself. That causes a sequence of [PAD] tokens with attention distribution of all zeros which causes the probability of [PAD] to be zero in final distribution (because of scatter function). And since the only target for that sequence is [PAD] with zero probability it will generate NAN. I removed those articles and it seems to be working.

  26. ygorg commented on Feb 6, 2019

    @ygorg

    I also encounter NaNs when pgen is 1 or 0. This means that either vocab_dists or attn_dists will be 0-filled and final_dists will have 0s in it (for words only in vocabulary or only in input). This will cause the loss to be inf (and the backprop will put NaNs in layers) if a word in reference has a 0 probability.
    I will add espilon if pgen is 0 and remove epsilon if pgen is 1.
    But as @tianjianjiang, i am concerned that this will bias the model.

  27. xiongma commented on May 8, 2019

    @xiongma

    when I add this model to transformer, I get nan, when I train model, I try many ways to solve this problem, but it still nan, if anyone interested in it, we can discuss.

  28. GaneshDoosa commented on Mar 29, 2020

    @GaneshDoosa

    when I add this model to transformer, I get nan, when I train model, I try many ways to solve this problem, but it still nan, if anyone interested in it, we can discuss.

    Please I'm trying your code of pointer generator built on LCSTS dataset. I changed the dataset with amazon reviews for my academic work purpose; I'm getting all UNKs in output..Please help if you have solved it

  29. tohidarehman1988 commented on Dec 12, 2020

    @tohidarehman1988

    Anyone got NAN ?
    selection_059

    can you please help me how you resolved this? how you print this loss graph?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions