tensorflow_softmax模型

# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""A very simple MNIST classifier.

See extensive documentation at
http://tensorflow.org/tutorials/mnist/beginners/index.md
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

# Import data
from tensorflow.examples.tutorials.mnist import input_data

import tensorflow as tf

flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_string('data_dir', '/tmp/data/', 'Directory for storing data')

mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)

sess = tf.InteractiveSession()

# Create the model
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)

# Define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

# Train
tf.initialize_all_variables().run()
for i in range(1000):
  batch_xs, batch_ys = mnist.train.next_batch(100)
  train_step.run({x: batch_xs, y_: batch_ys})

# Test trained model
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}))

 

转载于:https://my.oschina.net/hounLeft/blog/720733

`log_softmax NLLLoss`是深度学习领域中用于计算交叉熵损失的一种常见组合技术,尤其在分类任务中非常流行。 ### log_softmax 首先,我们解释一下`log_softmax`函数的作用。给定输入向量 `x`,log_softmax 函数会先应用 softmax 函数将每个元素转换成概率分布形式: \[ \text{softmax}(x)_i = \frac{\exp(x_i)}{\sum_{j=1}^{n} \exp(x_j)} \] 然后,对得到的概率分布取自然对数: \[ \text{log\_softmax}(x)_i = \ln(\text{softmax}(x)_i) \] 这个操作不仅简化了指数运算和归一化步骤,同时在数值稳定性方面也更优。尤其是在反向传播过程中,避免了直接对较大的指数值求对数可能会产生的数值溢出问题。 ### NLLLoss (Negative Log Likelihood Loss) 接下来说说 NLLLoss 的含义。NLLLoss,即负对数似然损失(Negative Log Likelihood Loss),是一种常用的损失函数,用于评估模型预测结果与实际标签之间的差距。在分类任务中,其目标是最小化模型预测的类别概率与真实标签之间的负对数似然度之和。 公式表示如下: \[ L(y, p) = -\log(p_y) \] 其中,\(y\) 表示真实标签,而 \(p_y\) 是模型预测属于该标签的概率。 ### 将二者结合使用 在实践中,我们将两个组件结合使用是因为它们各自的特点使其相得益彰: 1. **数值稳定性**:通过在计算过程中使用 `log_softmax` 可以提高数值稳定性和防止数值溢出的问题。 2. **简化梯度计算**:`log_softmax` 计算的导数可以直接与 NLLLoss 的导数关联起来,减少了梯度传播过程中的复杂性。 3. **提升训练效率**:这种组合使得梯度更新更为直接,从而加速了模型的训练过程,并有助于优化性能。 因此,在深度学习框架如 PyTorch 和 TensorFlow 中,`LogSoftmax + NLLLoss` 这种组合经常被推荐用于监督学习的任务特别是分类任务中。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值