AdaptiveAvgPool2d

https://pytorch.org/docs/stable/generated/torch.nn.AdaptiveAvgPool2d.html

adaptive_avgpool2d_demo.py
import svetoviz_webgpu as sv

# 1. Prepare 2D image data
img = Image.open("sample_image.jpg").convert("RGB")
img_np = np.array(img).astype(np.float32) / 255.0
input_tensor = torch.from_numpy(img_np).permute(2, 0, 1).unsqueeze(0)

# 2. Define AdaptiveAvgPool2d
# Commonly used as 'Global Average Pooling' if output_size=(1, 1)
adap_avg_pool2d = nn.AdaptiveAvgPool2d(output_size=(7, 7))

def terminal_callback(buffer, message, images, files):
    # 3. Process the image
    output = adap_avg_pool2d(input_tensor)
    buffer.send_system_message(f"Original Resolution: {input_tensor.shape[2:]}")
    buffer.send_system_message(f"Adaptive Average Output: {list(output.shape)}")

# 4. Start the interactive session
sv.pytorch_web(module=adap_avg_pool2d, terminal_callback=terminal_callback)
Global Feature Integration
AdaptiveAvgPool2d activations
Adaptive average pooling is frequently used before fully connected layers to condense spatial information into a fixed-size representation.