gpt4 book ai didi

python - 在 Tensorflow 中获取 k 个池元素而不是仅获取最大元素的最佳方法是什么?

转载 作者:太空狗 更新时间:2023-10-29 20:25:17 25 4
gpt4 key购买 nike

现在tensorflow中的最大池化函数是

tf.nn.max_pool(value, ksize, strides, padding, name=None)Returns:A Tensor with type tf.float32. The max pooled output tensor.

我想要一个 max_pool 的扩展版本,比如

tf.nn.top_k_pool(value, ksize, strides, padding, k=1, name=None)Performs the top k pooling on the input.Args:value: A 4-D Tensor with shape [batch, height, width, channels] and type tf.float32.ksize: A list of ints that has length >= 4. The size of the window for each dimension of the input tensor.strides: A list of ints that has length >= 4. The stride of the sliding window for each dimension of the input tensor.padding: A string, either 'VALID' or 'SAME'. The padding algorithm.k: 0-D int32 Tensor. Number of top elements to look in each pool.name: Optional name for the operation.Returns:A Tensor with type tf.float32. The max pooled output tensor. There will be an additional dimension saving the top k values.

我知道我可以在 https://www.tensorflow.org/versions/r0.7/how_tos/adding_an_op/index.html 之后扩展 tensorflow 操作

我想知道是否有更简单的方法来实现这一目标。

最佳答案

这是一个函数,使用 top_k 获取 channel 的最大 k 个激活。您可以修改它以适合您的目的:

def make_sparse_layer(inp_x,k, batch_size=None):

in_shape = tf.shape(inp_x)
d = inp_x.get_shape().as_list()[-1]
matrix_in = tf.reshape(inp_x, [-1,d])
values, indices = tf.nn.top_k(matrix_in, k=k, sorted=False)
out = []
vals = tf.unpack(values, axis=0, num=batch_size)
inds = tf.unpack(indices, axis=0, num=batch_size)
for i, idx in enumerate(inds):
out.append(tf.sparse_tensor_to_dense(tf.SparseTensor(tf.reshape(tf.cast(idx,tf.int64),[-1,1]),vals[i], [d]), validate_indices=False ))
shaped_out = tf.reshape(tf.pack(out), in_shape)
return shaped_out

关于python - 在 Tensorflow 中获取 k 个池元素而不是仅获取最大元素的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36272502/

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