You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请问Weldon pooling是否已在Keras中实现?作者PyTorch版本已发布

Weldon Pooling in Keras: Community Options & DIY Implementation Guide

Hey, great question! I’ve looked into this a bit because I’ve had similar needs for project work. While the official Weldon pooling implementation is indeed in PyTorch (as you noted), there are community-built Keras versions floating around, and it’s totally feasible to roll your own if you can’t find a perfect match.

Community Implementations

A quick check on code-sharing platforms turns up custom Keras layers that replicate Weldon pooling’s core logic. These are usually shared by developers who needed the layer for their own projects—they might not be as polished as the official PyTorch repo, but they handle the key top-k/bottom-k averaging behavior from the paper. Look for implementations that subclass keras.layers.Layer to encapsulate the pooling logic.

Build Your Own Version

If you want full control over the implementation, here’s a breakdown of the core logic from the 2016 CVPR paper Weldon: Weakly supervised learning of deep convolutional neural networks (Durand et al.), plus a working TensorFlow Keras example:

Weldon pooling computes the average of the top α% and bottom (1-α)% of activations across spatial dimensions, where α is a tunable hyperparameter.

Here’s a basic, functional implementation to start with:

import tensorflow as tf
from tensorflow.keras.layers import Layer

class WeldonPooling(Layer):
    def __init__(self, alpha=0.1, **kwargs):
        super().__init__(**kwargs)
        self.alpha = alpha  # Tunable hyperparameter (adjust based on your dataset)

    def call(self, inputs):
        # Input shape: (batch_size, height, width, channels)
        batch_size, h, w, c = tf.unstack(tf.shape(inputs))
        spatial_size = h * w

        # Flatten spatial dimensions to process top/bottom values
        flattened = tf.reshape(inputs, (-1, spatial_size, c))

        # Calculate number of top and bottom elements to average
        k_top = tf.cast(tf.math.ceil(self.alpha * spatial_size), tf.int32)
        k_bottom = tf.cast(tf.math.floor((1 - self.alpha) * spatial_size), tf.int32)

        # Compute average of top-k activations
        top_vals, _ = tf.math.top_k(flattened, k=k_top, sorted=False)
        top_avg = tf.reduce_mean(top_vals, axis=1)

        # Compute average of bottom-k activations (using top-k on negative values)
        bottom_vals, _ = tf.math.top_k(-flattened, k=k_bottom, sorted=False)
        bottom_avg = tf.reduce_mean(-bottom_vals, axis=1)

        # Combine top and bottom averages into the final output
        return tf.concat([top_avg, bottom_avg], axis=1)

    def compute_output_shape(self, input_shape):
        batch_size, _, _, c = input_shape
        return (batch_size, 2 * c)

Quick Adjustment Tips

  • Tune the alpha parameter (the paper uses values around 0.1 in experiments) based on your specific dataset and task.
  • If you’re implementing the weakly supervised learning workflow from the paper, you might want to add trainable weights to combine the top and bottom averages instead of concatenating them—reference the PyTorch implementation for details on this.
  • This version handles variable spatial sizes (common in Keras/TensorFlow), but you can add checks or optimizations if working with fixed-size inputs.

内容的提问来源于stack exchange,提问作者biogeek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:13:17