吴裕雄--天生自然TensorFlow2教程:张量排序
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}')
吴裕雄--天生自然TensorFlow2教程:张量排序的更多相关文章
- 吴裕雄--天生自然TensorFlow2教程:测试(张量)- 实战
import tensorflow as tf from tensorflow import keras from tensorflow.keras import datasets import os ...
- 吴裕雄--天生自然TensorFlow2教程:张量限幅
import tensorflow as tf a = tf.range(10) a # a中小于2的元素值为2 tf.maximum(a, 2) # a中大于8的元素值为8 tf.minimum(a ...
- 吴裕雄--天生自然TensorFlow2教程:前向传播(张量)- 实战
手写数字识别流程 MNIST手写数字集7000*10张图片 60k张图片训练,10k张图片测试 每张图片是28*28,如果是彩色图片是28*28*3-255表示图片的灰度值,0表示纯白,255表示纯黑 ...
- 吴裕雄--天生自然TensorFlow2教程:手写数字问题实战
import tensorflow as tf from tensorflow import keras from keras import Sequential,datasets, layers, ...
- 吴裕雄--天生自然TensorFlow2教程:函数优化实战
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D def himme ...
- 吴裕雄--天生自然TensorFlow2教程:反向传播算法
- 吴裕雄--天生自然TensorFlow2教程:链式法则
import tensorflow as tf x = tf.constant(1.) w1 = tf.constant(2.) b1 = tf.constant(1.) w2 = tf.consta ...
- 吴裕雄--天生自然TensorFlow2教程:多输出感知机及其梯度
import tensorflow as tf x = tf.random.normal([2, 4]) w = tf.random.normal([4, 3]) b = tf.zeros([3]) ...
- 吴裕雄--天生自然TensorFlow2教程:单输出感知机及其梯度
import tensorflow as tf x = tf.random.normal([1, 3]) w = tf.ones([3, 1]) b = tf.ones([1]) y = tf.con ...
随机推荐
- Golang函数-递归函数
Golang函数-递归函数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
- Codestorm:Game with a Boomerang
题目连接:https://www.hackerrank.com/contests/codestorm/challenges/game-with-a-boomerang 上一篇博客不知怎么复制过来题目, ...
- GoJS API学习
var node = {}; node["key"] = "节点Key"; node["loc"] = "0 0";// ...
- GoJS最简单的实例
复制如下内容保存到空白的.html文件中,用浏览器打开即可查看效果 <!DOCTYPE html> <html> <head> <meta charset=& ...
- 【转】R语言主成分分析(PCA)
https://www.cnblogs.com/jin-liang/p/9064020.html 数据的导入 > data=read.csv('F:/R语言工作空间/pca/data.csv') ...
- 和为S的连续正整数序列(双指针法)
题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久,他 ...
- python多进程编程中常常能用到的几种方法
python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU资源,在python中大部分情况需要使用多进程.python提供了非常好用的多进程包Multiprocessing,只需要定义 ...
- MyBatis Generator 下划线转驼峰命名
MyBatis Generator配置文件--指定生成实体类使用实际的表列名作为实体类的属性名 table标签下的设置属性useActualColumnNames用于指定生成实体类时是否使用实际的列名 ...
- Java中用单例模式有什么好处?
Java Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在. 使用Singleton的好处还在于可以节省内存,因为它限制了实例的个数,有利于Java垃圾回收( ...
- ArrayList与LindedList区别
1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList ...