nn.AdaptiveAvgPool1d
时间: 2023-10-23 16:09:55 浏览: 41
nn.AdaptiveAvgPool1d is a PyTorch module that performs adaptive average pooling over a 1D input signal. Adaptive average pooling is a form of pooling operation that allows the output size to be specified, rather than a fixed size. This means that the module will dynamically adapt the pooling window size and stride to ensure that the output has the desired size.
The input to the module is expected to be a 3D tensor of shape (batch_size, num_channels, input_length), where input_length is the length of the input signal. The module takes a single argument, output_size, which specifies the desired output size as a tuple (output_length,). The output of the module is a 3D tensor of shape (batch_size, num_channels, output_length), where output_length is the specified output size.
The adaptive average pooling operation is performed by dividing the input signal into non-overlapping windows of size (input_length / output_length) and computing the average value over each window. The resulting values are then concatenated along the channel dimension to produce the output tensor.
Example usage:
```
import torch.nn as nn
# Create a 1D input tensor of shape (batch_size, num_channels, input_length)
input_tensor = torch.randn(32, 64, 128)
# Create an instance of the AdaptiveAvgPool1d module with output size of 10
adaptive_avgpool = nn.AdaptiveAvgPool1d(output_size=(10,))
# Pass the input tensor through the module to obtain the output tensor
output_tensor = adaptive_avgpool(input_tensor)
# Print the shape of the output tensor
print(output_tensor.shape) # Output: torch.Size([32, 64, 10])
```