@@ -933,12 +933,12 @@ Let's look at an example:
933
933
``` python
934
934
import tensorflow as tf
935
935
936
- def non_differentiable_entropy (logits ):
936
+ def non_differentiable_softmax_entropy (logits ):
937
937
probs = tf.nn.softmax(logits)
938
938
return tf.nn.softmax_cross_entropy_with_logits(labels = probs, logits = logits)
939
939
940
940
w = tf.get_variable(" w" , shape = [5 ])
941
- y = - non_differentiable_entropy (w)
941
+ y = - non_differentiable_softmax_entropy (w)
942
942
943
943
opt = tf.train.AdamOptimizer()
944
944
train_op = opt.minimize(y)
@@ -970,13 +970,12 @@ Now let's fix our function with a differentiable version of the entropy and chec
970
970
import tensorflow as tf
971
971
import numpy as np
972
972
973
- def entropy (logits , dim = - 1 ):
974
- probs = tf.nn.softmax(logits, dim)
975
- nplogp = probs * (tf.reduce_logsumexp(logits, dim, keep_dims = True ) - logits)
976
- return tf.reduce_sum(nplogp, dim)
973
+ def softmax_entropy (logits , dim = - 1 ):
974
+ plogp = tf.nn.softmax(logits, dim) * tf.nn.log_softmax(logits, dim)
975
+ return - tf.reduce_sum(nplogp, dim)
977
976
978
977
w = tf.get_variable(" w" , shape = [5 ])
979
- y = - entropy (w)
978
+ y = - softmax_entropy (w)
980
979
981
980
print (w.get_shape())
982
981
print (y.get_shape())
0 commit comments