Calculating Cross Entropy in TensorFlow
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2
--
Chapters
00:00 Calculating Cross Entropy In Tensorflow
00:59 Accepted Answer Score 14
02:17 Answer 2 Score 20
02:56 Answer 3 Score 2
03:20 Thank you
--
Full question
https://stackoverflow.com/questions/4252...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #machinelearning #tensorflow #crossentropy
#avk47
ANSWER 1
Score 20
In addition to Don's answer (+1), this answer written by mrry may interest you, as it gives the formula to calculate the cross entropy in TensorFlow:
An alternative way to write:
xent = tf.nn.softmax_cross_entropy_with_logits(logits, labels)...would be:
softmax = tf.nn.softmax(logits) xent = -tf.reduce_sum(labels * tf.log(softmax), 1)However, this alternative would be (i) less numerically stable (since the softmax may compute much larger values) and (ii) less efficient (since some redundant computation would happen in the backprop). For real uses, we recommend that you use
tf.nn.softmax_cross_entropy_with_logits().
ACCEPTED ANSWER
Score 14
Like they say, you can't spell "softmax_cross_entropy_with_logits" without "softmax". Softmax of [0.45] is [1], and log(1) is 0.
Measures the probability error in discrete classification tasks in which the classes are mutually exclusive (each entry is in exactly one class). For example, each CIFAR-10 image is labeled with one and only one label: an image can be a dog or a truck, but not both.
NOTE: While the classes are mutually exclusive, their probabilities need not be. All that is required is that each row of
labelsis a valid probability distribution. If they are not, the computation of the gradient will be incorrect.If using exclusive
labels(wherein one and only one class is true at a time), seesparse_softmax_cross_entropy_with_logits.WARNING: This op expects unscaled logits, since it performs a
softmaxonlogitsinternally for efficiency. Do not call this op with the output ofsoftmax, as it will produce incorrect results.
logitsandlabelsmust have the same shape[batch_size, num_classes]and the same dtype (eitherfloat16,float32, orfloat64).
ANSWER 3
Score 2
Here is an implementation in Tensorflow 2.0 in case somebody else (me probably) needs it in the future.
@tf.function
def cross_entropy(x, y, epsilon = 1e-9):
    return -2 * tf.reduce_mean(y * tf.math.log(x + epsilon), -1) / tf.math.log(2.)
x = tf.constant([
    [1.0,0],
    [0.5,0.5],
    [.75,.25]
    ]
,dtype=tf.float32)
with tf.GradientTape() as tape:
    tape.watch(x)
    y = entropy(x, x)
tf.print(y)
tf.print(tape.gradient(y, x))
Output
[-0 1 0.811278105]
[[-1.44269502 29.8973541]
 [-0.442695022 -0.442695022]
 [-1.02765751 0.557305]]