@@ -311,18 +311,14 @@ y_ = tf.placeholder(tf.float32, [None, 10])
311311Then we can implement the cross-entropy, \\ (-\sum y'\log(y)\\ ):
312312
313313``` python
314- cross_entropy = - tf.reduce_sum(y_* tf.log(y))
314+ cross_entropy = tf.reduce_mean( - tf.reduce_sum(y_ * tf.log(y), reduction_indices = [ 1 ] ))
315315```
316316
317317First, ` tf.log ` computes the logarithm of each element of ` y ` . Next, we multiply
318- each element of ` y_ ` with the corresponding element of ` tf.log(y) ` . Finally,
319- ` tf.reduce_sum ` adds all the elements of the tensor.
320-
321- Note that this isn't just the cross-entropy of the truth with a single
322- prediction, but the sum of the cross-entropies for all the images we looked at.
323- In this example, we have 100 images in each batch: how well we are doing on 100
324- data points is a much better description of how good our model is than a single
325- data point.
318+ each element of ` y_ ` with the corresponding element of ` tf.log(y) ` . Then
319+ ` tf.reduce_sum ` adds the elements in the second dimension of y, due to the
320+ ` reduction_indices=[1] ` parameter. Finally, ` tf.reduce_mean ` computes the mean
321+ over all the examples in the batch.
326322
327323Now that we know what we want our model to do, it's very easy to have TensorFlow
328324train it to do so.
@@ -334,11 +330,11 @@ minimize. Then it can apply your choice of optimization algorithm to modify the
334330variables and reduce the cost.
335331
336332``` python
337- train_step = tf.train.GradientDescentOptimizer(0.01 ).minimize(cross_entropy)
333+ train_step = tf.train.GradientDescentOptimizer(0.5 ).minimize(cross_entropy)
338334```
339335
340336In this case, we ask TensorFlow to minimize ` cross_entropy ` using the gradient
341- descent algorithm with a learning rate of 0.01 . Gradient descent is a simple
337+ descent algorithm with a learning rate of 0.5 . Gradient descent is a simple
342338procedure, where TensorFlow simply shifts each variable a little bit in the
343339direction that reduces the cost. But TensorFlow also provides
344340[ many other optimization algorithms]
@@ -415,7 +411,7 @@ Finally, we ask for our accuracy on our test data.
415411print (sess.run(accuracy, feed_dict = {x: mnist.test.images, y_: mnist.test.labels}))
416412```
417413
418- This should be about 91 %.
414+ This should be about 92 %.
419415
420416Is that good? Well, not really. In fact, it's pretty bad. This is because we're
421417using a very simple model. With some small changes, we can get to
0 commit comments