tf.nn.top_k() tf.nn.in_top_k()
来源:互联网 发布:即时通讯app源码下载 编辑:程序博客网 时间:2024/04/29 20:24
1..中文
tf.nn.top_k(input, k, name=None)
解释:这个函数的作用是返回 input 中每行最大的 k 个数,并且返回它们所在位置的索引。
#!/usr/bin/env python# -*- coding: utf-8 -*-import tensorflow as tfimport numpy as npinput = tf.constant(np.random.rand(3,4))k = 2output = tf.nn.top_k(input, k)with tf.Session() as sess: print(sess.run(input)) print(sess.run(output))
[[ 0.98925872 0.15743092 0.76471106 0.5949957 ] [ 0.95766488 0.67846336 0.21058844 0.2644312 ] [ 0.65531991 0.61445187 0.65372938 0.88111084]]TopKV2(values=array([[ 0.98925872, 0.76471106], [ 0.95766488, 0.67846336], [ 0.88111084, 0.65531991]]), indices=array([[0, 2], [0, 1], [3, 0]]))
输入参数:
- input: 一个张量,数据类型必须是以下之一:float32、float64、int32、int64、uint8、int16、int8。数据维度是 batch_size 乘上 x 个类别。
- k: 一个整型,必须 >= 1。在每行中,查找最大的 k 个值。
- name: 为这个操作取个名字。
输出参数:
一个元组 Tensor ,数据元素是 (values, indices),具体如下:
values: 一个张量,数据类型和 input 相同。数据维度是 batch_size 乘上 k 个最大值。
indices: 一个张量,数据类型是 int32 。每个最大值在 input 中的索引位置。
tf.nn.in_top_k(predictions, targets, k, name=None)
解释:这个函数的作用是返回一个布尔向量,说明目标值是否存在于预测值之中。
输出数据是一个 targets 长度的布尔向量,如果目标值存在于预测值之中,那么 out[i] = true。
注意:targets 是predictions中的索引位,并不是 predictions 中具体的值。
#!/usr/bin/env python# -*- coding: utf-8 -*-import tensorflow as tfimport numpy as npinput = tf.constant(np.random.rand(3,4), tf.float32)k = 2 #targets对应的索引是否在最大的前k(2)个数据中output = tf.nn.in_top_k(input, [3,3,3], k)with tf.Session() as sess: print(sess.run(input)) print(sess.run(output))
[[ 0.43401602 0.29302254 0.40603295 0.21894781] [ 0.77089119 0.95353228 0.04788217 0.37489092] [ 0.83710146 0.2505011 0.28791779 0.97788286]][False False True]
2. 英文:
tf.nn.top_k(input, k, name=None)
Returns the values and indices of the k largest elements for each row.
represents the j-th largest element in .
gives the column index of the corresponding element,such that . If twoelements are equal, the lower-index element appears first.
Args:
input
: ATensor
. Must be one of the following types:float32
,float64
,int32
,int64
,uint8
,int16
,int8
. A batch_size x classes tensork
: Anint
that is>= 1
. Number of top elements to look for within each rowname
: A name for the operation (optional).
Returns:
A tuple of Tensor
objects (values, indices).
values
: ATensor
. Has the same type asinput
. A batch_size x k tensor with the k largest elements for each row, sorted in descending orderindices
: ATensor
of typeint32
. A batch_size x k tensor with the index of each value within each row
tf.nn.in_top_k(predictions, targets, k, name=None)
Says whether the targets are in the top K predictions.
This outputs a batch_size bool array, an entry out[i] is true if theprediction for the target class is among the top k predictions amongall predictions for example i. Note that the behavior of InTopK differsfrom the TopK op in its handling of ties; if multiple classes have thesame prediction value and straddle the top-k boundary, all of thoseclasses are considered to be in the top k.
More formally, let
be the predictions for all classes for example i, be the target class for example i, be the output for example i,
Args:
predictions
: ATensor
of typefloat32
. A batch_size x classes tensortargets
: ATensor
of typeint32
. A batch_size vector of class idsk
: Anint
. Number of top elements to look at for computing precisionname
: A name for the operation (optional).
Returns:
A Tensor
of type bool
. Computed Precision at k as a bool Tensor
- tf.nn.top_k() tf.nn.in_top_k()
- tf.nn.top_k() tf.nn.in_top_k()
- tf.nn.in_top_k()
- tf.nn.in_top_k的用法
- TensorFlow函数:tf.nn.in_top_k()
- tf.nn.in_top_k()的用法
- tf.nn
- tf.nn.conv2d 实例
- tf.nn.max_pool 实例
- tf.nn.atrous_conv2d 实例
- tf.nn.conv2d()
- tf.nn.ctc_loss
- tf.nn.embedding_lookup理解
- tf.nn.conv2d理解
- tf.nn.embedding_lookup()笔记
- tf.nn.softmax_cross_entropy_with_logits
- tf.nn.conv2d
- tf.nn.max_pool
- CodeForces 580A Kefa and First Steps
- 大话数据结构
- 2017WHCTF
- 技术大牛总结的Github与华为软件开发云完整对比
- 用e=1/1!+1/2!+1/3!+……公式求e的近似值,直到发现某一项的值小于10^(-5)为止;
- tf.nn.top_k() tf.nn.in_top_k()
- window系统如何恢复/查看swp文件
- mybatis3通过provider注解结合动态sql实现增删改查,不再依赖xml成为可能
- kubernetes简介
- 操作系统-知识点总结-JeRome_JLU
- React学习之坑(二)- 基础入门
- 大话数据结构
- python + opencv 实现人脸检测
- Java建造者模式以及常见误区错误分析纠正