tensorflow 2.0 高阶操作 之 张量排序

Outline

  • Sort/argsort
  • Topk
  • Top-5 Acc.

Sort/argsort

a = tf.random.shuffle(tf.range(5))
# <tf.Tensor: id=25, shape=(5,), dtype=int32, numpy=array([3, 2, 0, 4, 1])>
tf.sort(a, direction='DESCENDING')
# <tf.Tensor: id=13, shape=(5,), dtype=int32, numpy=array([4, 3, 2, 1, 0])>
tf.argsort(a, direction='DESCENDING')
#  <tf.Tensor: id=50, shape=(5,), dtype=int32, numpy=array([3, 0, 1, 4, 2])>

idx = tf.argsort(a, direction='DESCENDING')
a_sorted = tf.gather(a, idx)
# <tf.Tensor: id=76, shape=(5,), dtype=int32, numpy=array([4, 3, 2, 1, 0])>

direction:参数

  1. DESCENDING 降序
  2. ASCENDING 升序
  3. 默认 ASCENDING 升序

axis 参数

  1. 默认 axis=-1 最后一个维度。

行维度上排序

a = tf.random.uniform([3,3], maxval=10, dtype=tf.int32)
# <tf.Tensor: id=148, shape=(3, 3), dtype=int32, numpy=
# array([[0, 4, 0],
#        [8, 9, 3],
#        [8, 7, 3]])>
tf.sort(a, direction='DESCENDING')
# <tf.Tensor: id=203, shape=(3, 3), dtype=int32, numpy=
# array([[4, 0, 0],
#        [9, 8, 3],
#        [8, 7, 3]])>
tf.argsort(a, direction='DESCENDING')
# <tf.Tensor: id=242, shape=(3, 3), dtype=int32, numpy=
# array([[1, 0, 2],
#        [1, 0, 2],
#        [0, 1, 2]])>

列维度上排序

a = tf.random.uniform([3,3], maxval=10, dtype=tf.int32)
# <tf.Tensor: id=1988, shape=(3, 3), dtype=int32, numpy=
# array([[7, 3, 0],
#        [8, 8, 6],
#        [2, 6, 1]])>
tf.sort(a, direction='DESCENDING', axis=0)
# <tf.Tensor: id=2185, shape=(3, 3), dtype=int32, numpy=
# array([[8, 8, 6],
#        [7, 6, 1],
#        [2, 3, 0]])>
tf.argsort(a, direction='DESCENDING', axis=0)
# <tf.Tensor: id=2295, shape=(3, 3), dtype=int32, numpy=
# array([[1, 1, 1],
#        [0, 2, 2],
#        [2, 0, 0]])>

Top_k

tf.math.top_k
return (value, indices)

  1. 参数 k 默认为 1
  2. 参数 sorted 默认为 True
  3. 默认一定是 最后一个维度上的排序
a = tf.random.uniform([3,3], maxval=10, dtype=tf.int32)
# <tf.Tensor: id=2399, shape=(3, 3), dtype=int32, numpy=
# array([[6, 3, 6],
#        [4, 7, 4],
#        [9, 6, 9]])>
res = tf.math.top_k(a, k=1, sorted=True)
res.values
# <tf.Tensor: id=3765, shape=(3, 1), dtype=int32, numpy=
# array([[6],
#        [7],
#        [9]])>
res.indices
# <tf.Tensor: id=3766, shape=(3, 1), dtype=int32, numpy=
# array([[0],
#        [1],
#        [0]])>

Top_k accuracy

  • Prob:[0.1, 0.2, 0.3, 0.4]
  • Label:[2]
  • Only consider top-1 prediction: [3]
  • Only consider top-2 prediction: [3, 2]
  • Only consider top-3 prediction: [3, 2, 1]

Top_k 精度 是指在做预测时,选取概率最大的 k 个标签,如果存在真实的标签值,则视为预测正确。 可以看出我们常用的 Top_1 是 Top_k 的特殊情况。当 k 为类别标签数时, Top_k accuracy 为 100%。

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, k=2).indices

target = tf.broadcast_to(target, [2,2])
# <tf.Tensor: id=5115, shape=(3, 2), dtype=int32, numpy=
# # array([[2, 0],
# #        [2, 0],
# #        [2, 0]])>
correct = tf.equal(target, k_b)
# <tf.Tensor: id=6768, shape=(2, 2), dtype=bool, numpy=
# array([[ True, False],
#        [False,  True]])>
bool2int = tf.cast(correct, dtype=tf.int32)
# <tf.Tensor: id=7115, shape=(2, 2), dtype=int32, numpy=
# array([[1, 0],
#        [0, 1]])>
num_of_pre_correct = tf.reduce_sum(bool2int)
# <tf.Tensor: id=7471, shape=(), dtype=int32, numpy=2>
top_k_accuracy = num_of_pre_correct/target.shape[0]
# <tf.Tensor: id=7837, shape=(), dtype=float64, numpy=1.0>
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TransientYear

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值