gpt4 book ai didi

python - Tensorflow:保留张量最大条目的 10%

转载 作者:太空宇宙 更新时间:2023-11-04 00:03:24 25 4
gpt4 key购买 nike

我想通过保留 10% 的最大条目来过滤张量。是否有 Tensorflow 函数可以做到这一点?一个可能的实现会是什么样子?我正在寻找可以处理形状为 [N,W,H,C][N,W*H*C] 的张量。

我所说的过滤器是指张量的形状保持不变,但只保留最大的 10%。因此,除了最大的 10% 之外,所有条目都变为零。

这可能吗?

最佳答案

这样做的正确方法是计算 90 个百分位数,例如 tf.contrib.distributions.percentile :

import tensorflow as tf

images = ... # [N, W, H, C]
n = tf.shape(images)[0]
images_flat = tf.reshape(images, [n, -1])
p = tf.contrib.distributions.percentile(images_flat, 90, axis=1, interpolation='higher')
images_top10 = tf.where(images >= tf.reshape(p, [n, 1, 1, 1]),
images, tf.zeros_like(images))

如果您想为 TensorFlow 2.x 做好准备,请在哪里 tf.contrib will be removed , 你可以改用 TensorFlow Probability , 这是 percentile 的位置功能将在未来永久存在。

编辑:如果你想对每个 channel 进行过滤,你可以像这样稍微修改代码:

import tensorflow as tf

images = ... # [N, W, H, C]
shape = tf.shape(images)
n, c = shape[0], shape[3]
images_flat = tf.reshape(images, [n, -1, c])
p = tf.contrib.distributions.percentile(images_flat, 90, axis=1, interpolation='higher')
images_top10 = tf.where(images >= tf.reshape(p, [n, 1, 1, c]),
images, tf.zeros_like(images))

关于python - Tensorflow:保留张量最大条目的 10%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55000477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com