import tensorflow as tf

a = tf.random.shuffle(tf.range(5))
a
tf.sort(a, direction='DESCENDING')
# 返回索引
tf.argsort(a, direction='DESCENDING')
idx = tf.argsort(a, direction='DESCENDING')
tf.gather(a, idx)
idx = tf.argsort(a, direction='DESCENDING')
tf.gather(a, idx)
a = tf.random.uniform([3, 3], maxval=10, dtype=tf.int32)
a
tf.sort(a)
tf.sort(a, direction='DESCENDING')
idx = tf.argsort(a)
idx
# 返回前2个值
res = tf.math.top_k(a, 2)
res
res.values
res.indices
prob = tf.constant([[0.1, 0.2, 0.7], [0.2, 0.7, 0.1]])
target = tf.constant([2, 0])
# 概率最大的索引在最前面
k_b = tf.math.top_k(prob, 3).indices
k_b
k_b = tf.transpose(k_b, [1, 0])
k_b
# 对真实值broadcast,与prod比较
target = tf.broadcast_to(target, [3, 2])
target
def accuracy(output, target, topk=(1, )):
maxk = max(topk)
batch_size = target.shape[0] pred = tf.math.top_k(output, maxk).indices
pred = tf.transpose(pred, perm=[1, 0])
target_ = tf.broadcast_to(target, pred.shape)
correct = tf.equal(pred, target_) res = []
for k in topk:
correct_k = tf.cast(tf.reshape(correct[:k], [-1]), dtype=tf.float32)
correct_k = tf.reduce_sum(correct_k)
acc = float(correct_k / batch_size)
res.append(acc) return res
# 10个样本6类
output = tf.random.normal([10, 6])
# 使得所有样本的概率加起来为1
output = tf.math.softmax(output, axis=1)
# 10个样本对应的标记
target = tf.random.uniform([10], maxval=6, dtype=tf.int32)
print(f'prob: {output.numpy()}')
pred = tf.argmax(output, axis=1)
print(f'pred: {pred.numpy()}')
print(f'label: {target.numpy()}') acc = accuracy(output, target, topk=(1, 2, 3, 4, 5, 6))
print(f'top-1-6 acc: {acc}')

最新文章

  1. Yii 2.x Behavior - 类图
  2. Permission denied user=hadoop access=WRITE inode=root rootsupergroup rwxr
  3. 转!java基础笔记
  4. git clone --early EOF
  5. Mac 下安装ruby,以及CocoaPods安装以及使用网摘
  6. 【转】科普Spark,Spark是什么,如何使用Spark
  7. 《iPhone高级编程—使用Mono Touch和.NET/C#》
  8. 【Demo 0004】Java基础-类封装性
  9. Nginx 负载均衡-加权轮询策略剖析
  10. 产品经理(五岁以下儿童)myVegas Slots排名上升的秘密
  11. jQuery UI 实例 - 自动完成(Autocomplete)
  12. IOS中的通知NSNotification
  13. Opencv探索之路(十九):读写xml和yml文件
  14. LeetCode - 492. Construct the Rectangle
  15. Java基础之一
  16. 谈谈MySQL死锁之二 死锁检测和处理源码分析
  17. Java东西太多,记录一些知识点
  18. jQuery EasyUI Datagrid组件的完整的基础DOM结构
  19. Phonegap 环境配置
  20. Innobackupx工具命令简单解析

热门文章

  1. Springboot配置文件内容加密
  2. CSS-lineheight
  3. 二 SVN代码冲突的解决
  4. 网络OSI七层模型及各层作用 与 TCP/IP
  5. zabbix agent的主动工作模式实战案例
  6. 动态设置html根字体大小(随着设备屏幕的大小而变化,从而实现响应式)
  7. 【转】R语言主成分分析(PCA)
  8. Java提升三:函数式接口
  9. 015、MySQL取今天是第几季度,往后几个月是第几季度
  10. POJ 3090 欧拉函数