- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
对于像这样的任何二维张量
[[2,5,4,7],[7,5,6,8]],
我想对每一行中的前 k 元素进行 softmax,然后通过将所有其他元素替换为 0 来构造一个新的张量。
结果应该是获取每行 [[7,5],[8,7]] 前 k(此处 k=2)个元素的 softmax,因此[[0.880797,0.11920291],[0.7310586,0.26894143]]然后根据原张量中前k个元素的索引重建一个新的张量,最终结果应该是
[[0,0.11920291,0,0.880797],[0.26894143,0,0,0.7310586]].
有没有可能在tensorflow中实现这种masked softmax?非常感谢!
最佳答案
以下是您可以如何做到这一点:
import tensorflow as tf
# Input data
a = tf.placeholder(tf.float32, [None, None])
num_top = tf.placeholder(tf.int32, [])
# Find top elements
a_top, a_top_idx = tf.nn.top_k(a, num_top, sorted=False)
# Apply softmax
a_top_sm = tf.nn.softmax(a_top)
# Reconstruct into original shape
a_shape = tf.shape(a)
a_row_idx = tf.tile(tf.range(a_shape[0])[:, tf.newaxis], (1, num_top))
scatter_idx = tf.stack([a_row_idx, a_top_idx], axis=-1)
result = tf.scatter_nd(scatter_idx, a_top_sm, a_shape)
# Test
with tf.Session() as sess:
result_val = sess.run(result, feed_dict={a: [[2, 5, 4, 7], [7, 5, 6, 8]], num_top: 2})
print(result_val)
输出:
[[0. 0.11920291 0. 0.880797 ]
[0.26894143 0. 0. 0.7310586 ]]
编辑:
实际上,有一个功能更接近您的意图,tf.sparse.softmax
.但是,它需要 SparseTensor
作为输入,我不确定它应该更快,因为它必须弄清楚哪些稀疏值在 softmax 中结合在一起。这个函数的好处是你可以在每一行中有不同数量的 softmax 元素,但在你的情况下这似乎并不重要。无论如何,如果您发现它有用,这里有一个实现。
import tensorflow as tf
a = tf.placeholder(tf.float32, [None, None])
num_top = tf.placeholder(tf.int32, [])
# Find top elements
a_top, a_top_idx = tf.nn.top_k(a, num_top, sorted=False)
# Flatten values
sparse_values = tf.reshape(a_top, [-1])
# Make sparse indices
shape = tf.cast(tf.shape(a), tf.int64)
a_row_idx = tf.tile(tf.range(shape[0])[:, tf.newaxis], (1, num_top))
sparse_idx = tf.stack([a_row_idx, tf.cast(a_top_idx, tf.int64)], axis=-1)
sparse_idx = tf.reshape(sparse_idx, [-1, 2])
# Make sparse tensor
a_top_sparse = tf.SparseTensor(sparse_idx, sparse_values, shape)
# Reorder sparse tensor
a_top_sparse = tf.sparse.reorder(a_top_sparse)
# Softmax
result_sparse = tf.sparse.softmax(a_top_sparse)
# Convert back to dense (or you can keep working with the sparse tensor)
result = tf.sparse.to_dense(result_sparse)
# Test
with tf.Session() as sess:
result_val = sess.run(result, feed_dict={a: [[2, 5, 4, 7], [7, 5, 6, 8]], num_top: 2})
print(result_val)
# Same as before
关于python - 返回二维张量每行的 top_k masked softmax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53280894/
我正在遵循this link中的教程并尝试改变模型的评估方法(底部)。我想要获得前 5 名的评估,并且我正在尝试使用以下代码: topFiver=tf.nn.in_top_k(y, y_, 5, na
我想在 pytorch 中实现 tf.nn.in_top_k。这是tf.nn.in_top_k的链接, tf.math.in_top_k( targets, predictions, k, n
对于像这样的任何二维张量 [[2,5,4,7],[7,5,6,8]], 我想对每一行中的前 k 元素进行 softmax,然后通过将所有其他元素替换为 0 来构造一个新的张量。 结果应该是获取每行 [
我有两个多维张量a和b。我想按 a 的值对它们进行排序。 我找到了tf.nn.top_k能够对张量进行排序并返回用于对输入进行排序的索引。如何使用 tf.nn.top_k(a, k=2) 返回的索引对
我在使此自定义损失函数(它检查 y_pred 数据的排序是否与 y_true 提供的实际排序索引)工作时遇到了一些问题: def custom_objective(y_true, y_pred):
我正在尝试在 tensorflow 中编写一个仅传播每个特征图的前 k 个值的操作。 例子: k=1,输入大小是[batch_size, x, y, channels] 假设它是[1,2,2,3] 输
我正在尝试预测给予患者的药物。对于每种药物,我在预测中有一列(通过 softmax)指示患者获得这种药物的概率。 但显然人们可以同时服用多种药物,因此我有另一个模型来尝试预测给予的不同药物的数量。 我
我正在尝试使用从 tf.nn.top_k 返回的索引从第二个张量中提取值。 我试过使用 numpy 类型索引,以及直接使用 tf.gather_nd,但我注意到索引是错误的。 # temp_atte
我有一个形状为 (10, 32, 32, 128) 的 4-D 张量。我想为所有前 N 个元素生成一个二进制掩码。 arr = tf.random_normal(shape=(10, 32, 32,
Pytorch 提供 torch.topk(input, k, dim=None, largest=True, sorted=True) 函数来计算给定 的 k 最大元素>input 沿给定维度 di
我是一名优秀的程序员,十分优秀!