Почему Reduce_Man применяется к выводу sparse_softmax_cross_entropy_with_logits? - PullRequest
0 голосов
/ 30 мая 2018

Есть несколько обучающих программ, которые применили reduce_mean к выводу sparse_softmax_cross_entropy_with_logits.Например,

cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv))

или

cross_entropy = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
                               labels=tf.cast(y_, dtype=tf.int32), logits=y_conv))

Почему reduce_mean применяется к выходу sparse_softmax_cross_entropy_with_logits?Это потому, что мы используем мини-партии, и поэтому мы хотим рассчитать (используя reduce_mean) средние потери по всем образцам мини-партии?

Ответы [ 2 ]

0 голосов
/ 30 мая 2018

Причина в том, чтобы получить среднюю потерю за партию.

Как правило, вы обучаете нейронную сеть с входными пакетами размером> 1, каждый элемент в пакете будет давать значение потерь, поэтому самый простой способ объединить их в одно значение - это усреднить.

0 голосов
/ 30 мая 2018

Сначала я нахожу что-то интересное ~

, давайте определим sparse_vector как

sparse_vector = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=tf.cast(y_, dtype=tf.int32), logits=y_conv)

sparse_vector является вектором , и мы должны вычислить его сумму, поэтому мы должны использовать redux_mean .

import numpy as np
import tensorflow as tf

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.InteractiveSession(config=config)

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=False)

print(mnist.test.labels.shape)
print(mnist.train.labels.shape)

with tf.name_scope('inputs'):
    X_ = tf.placeholder(tf.float32, [None, 784])
    y_ = tf.placeholder(tf.int64, [None])

X = tf.reshape(X_, [-1, 28, 28, 1])
h_conv1 = tf.layers.conv2d(X, filters=32, kernel_size=5, strides=1, 
                           padding='same', activation=tf.nn.relu, name='conv1')
h_pool1 = tf.layers.max_pooling2d(h_conv1, pool_size=2, strides=2,
                                  padding='same', name='pool1')

h_conv2 = tf.layers.conv2d(h_pool1, filters=64, kernel_size=5, strides=1, 
                           padding='same',activation=tf.nn.relu, name='conv2')
h_pool2 = tf.layers.max_pooling2d(h_conv2, pool_size=2, strides=2, 
                                  padding='same', name='pool2')

# flatten
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.layers.dense(h_pool2_flat, 1024, name='fc1', activation=tf.nn.relu)


keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, 0.5)  

h_fc2 = tf.layers.dense(h_fc1_drop, units=10, name='fc2')
# y_conv = tf.nn.softmax(h_fc2)
y_conv = h_fc2
# print('Finished building network.')

# cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
sparse_vector = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=tf.cast(y_, dtype=tf.int32), logits=y_conv)
cross_entropy = tf.reduce_mean(sparse_vector)
sess.run(tf.global_variables_initializer())
# print(sparse_vector)
# print(cross_entropy)
# Tensor("SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits:0", shape=(?,), dtype=float32)
# Tensor("Mean:0", shape=(), dtype=float32)

batch = mnist.train.next_batch(10)
sparse_vector,cross_entropy = sess.run(
    [sparse_vector,cross_entropy],
    feed_dict={X_: batch[0], y_: batch[1]})

print(sparse_vector)
print(cross_entropy)

, на выходе будет [2.2213464 2.2676413 2.3555744 2.3196406 2.0794516 2.394274 2.266591 2.31397182.345526 2.3952296]

2.2959247

...