前导k池元素的最佳方法是什么,而不仅仅是Tensorflow中的最大元素?

时间:2016-03-28 22:47:31

标签: python tensorflow

现在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

之后花费张量流操作

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

3 个答案:

答案 0 :(得分:4)

这是使用top_k获取通道的最大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 

答案 1 :(得分:1)

答案 2 :(得分:1)

usd tf.reshape(),tf.matrix_transpose(),tf.nn.top_k(sorted = False),以及tf.nn.conv2d()中的'data_format'参数,详见http://www.infocool.net/kb/OtherCloud/201703/318346.html