Python Tensorflow softmax_cross_熵_与t f.reduce_均值(-t f.reduce_和(y*t.log(yhat),reduce_指数=1))

Python Tensorflow softmax_cross_熵_与t f.reduce_均值(-t f.reduce_和(y*t.log(yhat),reduce_指数=1)),python,machine-learning,tensorflow,Python,Machine Learning,Tensorflow,下面是多层感知器的例子:我对函数tf.nn.softmax\u cross\u entropy\u与\u logits以及它与tf.nn.relu和reduce\u sum的关系感到困惑。假设我声明了一个具有以下内容的网络: x = tf.placeholder('float',[None,24**2]) y = tf.placeholder('float',[None,10]) w1 = tf.Variable(random_normal([24**2,12]) w2 = tf.V

下面是多层感知器的例子:我对函数
tf.nn.softmax\u cross\u entropy\u与\u logits
以及它与
tf.nn.relu
reduce\u 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\u v2=tf.reduce\u mean(-tf.reduce\u sum(y*tf.log(tf.nn.softmax(pred)),1))

在第一批中,它被命名为
nan
,因为
pred
实际上在softmax之后包含了相当多的零。我猜交叉熵忽略了零,只是根据以下公式求和: