9、TensorFLow 中的决策函数

来源:互联网 发布:网络彩票推广员 编辑:程序博客网 时间:2024/05/18 00:21

一、分类问题的决策函数

1、多分类(包括二分类)

这里写图片描述
这里写图片描述

2、tf.nn.softmax(logits, dim=-1, name=None)

  • softmax = exp(logits) / reduce_sum(exp(logits), dim)
  • 神经网络最后一层经过 softmax 函数后,取得概率最大的元素的 index,即为我们预测的结果
# 神经网络的输出logits = tf.constant([3.0, 1.0, -3.0])  # 正确的标签y_ = tf.constant([1.0, 0.0, 0.0])# 神经网络的输出经过 softmax 变换得到每个类别的概率y = tf.nn.softmax(logits)>>> array([ 0.87887824,  0.11894324,  0.00217852], dtype=float32))

3、tf.nn.top_k(input, k, sorted=True, name=None)

  • Finds values and indices of the k largest entries for the last dimension
  • softmax 函数处理过的张量,取其中最大的 k 个元素的值和索引
# 神经网络的输出logits = tf.constant([3.0, 1.0, -3.0])  # 正确的标签y_ = tf.constant([1.0, 0.0, 0.0])# 神经网络的输出经过 softmax 变换得到每个类别的概率y = tf.nn.softmax(logits)>>> array([ 0.87887824,  0.11894324,  0.00217852], dtype=float32))# 取得其中最大的 2 个元素的值和索引y_top_2 = tf.nn.top_k(y, 2)>>> TopKV2(values=array([ 0.87887824,  0.11894324], dtype=float32), indices=array([0, 1], dtype=int32))y_top_2.values>>> array([ 0.87887824,  0.11894324], dtype=float32)y_top_2.indices>>> array([0, 1], dtype=int32)

4、tf.nn.in_top_k(predictions, targets, k, name=None)

  • 判断 targets(索引类标[0, 1, 2, …, n-1]) 是否在 top K predictions中,若在其中,则返回True,不在则返回 False
  • 返回一个 bool 类型的一维张量,shape 为 [batch_size]

这里写图片描述


二、回归问题的决策函数

回归问题的决策函数: 前向传播过程中最后一层神经网络的输出结果即为网络的预测结果

三、参考资料

1、https://www.tensorflow.org/api_docs/python/tf/nn/top_k
2、Tensorflow Python API 翻译(nn)

原创粉丝点击