Vladislav Ladenkov - 1 year ago 186

Python Question

I need to study TF in the express way and i cant understant this part:

`cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))`

It's explained with this: First, tf.log computes the logarithm of each element of y. Next, we multiply each element of y_ with the corresponding element of tf.log(y).

Why it does this manipulations, which are marked bold? why do wee need another dimensiom? Thanks

Answer Source

There are two dimensions because cross_entropy computes values for a batch of training examples. Therefore, the dimension 0 is for a batch, and dimension 1 is for different classes of a specific example. For example, if there are 3 possible classes and batch size is 2, then y is a 2D tensor of size (2, 3).