跑模型的时候遇到loss为nan的情况,图里面有对softmax归一化后的值取对数的操作,担心是这里算出来0。一般softmax的计算会减去序列的最大值。即
tf.exp(logits - tf.reduce_max(logits)) / tf.reduce_sum(tf.exp(logits - tf.reduce_max(logits)))
但是看tf源码没看懂哪里在做这个运算,但是注释文档里写的这样算的:
def softmax(logits, dim=-1, name=None):
'''
Computes softmax activations.
This function performs the equivalent of
softmax = tf.exp(logits) / tf.reduce_sum(tf.exp(logits), dim)
'''
不敢相信,毕竟减序列最大值是常规操作,所以测了一下看到底是怎么算的。
(Pdb) x = [1.0,1.0,