常用交叉熵损失函数,常代替均方差损失函数用于分类
loss_function = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred),
reduction_indices=1))
但有时用softmax激活的上面的pred,使pred=0或很接近0,导致log(0)值为NaN,造成训练时数据不稳定。
改用结合softmax交叉熵损失函数
loss_function = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=forward,labels=y))
因为上面的forward并没有经过softmax激活。