我正在关注此处的多层感知器示例:https://github.com/aymericdamien/TensorFlow-Examples我对函数 tf.nn.softmax_cross_entropy_with_logits 以及它与 tf.nn.relu 和 reduce_sum 的关系感到困惑。假设我声明一个网络:
x = tf.placeholder('float',[None,24**2])
y = tf.placeholder('float',[None,10])
w1 = tf.Variable(random_normal([24**2,12])
w2 = tf.Variable(random_normal([12,10])
h = tf.nn.relu(tf.matmul(x,w1))
yhat= tf.matmul(h, w2)
'''
cost function
'''
cost = tf.reduce_mean(tf.nn.softmax_corss_entropy_with_logits(logits=yhat, labels=y))
上面的不应该是一样的吗:
x = tf.placeholder('float',[None,24**2])
y = tf.placeholder('float',[None,10])
w1 = tf.Variable(random_normal([24**2,12])
w2 = tf.Variable(random_normal([12,10])
h = tf.nn.relu(tf.matmul(x,w1))
yhat= tf.nn.softmax(tf.matmul(h, w2))
'''
cost function
'''
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(_hat),reduction_indices=1))
但是当我使用第一种结构进行训练时,我的准确率约为 95%,第二种方法产生 1% 的准确率,所以显然它不仅仅是“数值不稳定性” “对吗?
请您参考如下方法:
做了一些快速研究。我在 multilayer_peceptron.py 文件的第 62 行上方添加了这个,并在第 87 行打印了它
cost_v2 = tf.reduce_mean(-tf.reduce_sum(y*tf.log(tf.nn.softmax(pred)),1))
在第一批中,它以 nan 出现,因为 pred 实际上在 softmax 之后包含相当多的零。我猜交叉熵忽略了零,只是基于此进行求和:https://datascience.stackexchange.com/questions/9302/the-cross-entropy-error-function-in-neural-networks
