Help me build a Pool CNN

I am trying to build a PoolCNN in Pytorch with the following architecture:

Input

1D convolution with 32 output channels and a kernel size of 3.

Relu.

1D adaptive max pool with 1 output size.

Fully connected layer with 25 output features

Output

Can somebody help me build that?

Your description is precise enough that you could probably have built this yourself. You didn’t specify a particular input size (number of channels and size of sequence) which is required to know how many input channels the first convolution needs, and how many input features the Linear layer ought to have. Here I’m assuming 1 input channel and then using LazyLinear rather than Linear to deal with the varying sequence length.

import torch
from torch import nn

model = nn.Sequential(
    nn.Conv1d(in_channels=1, out_channels=32, kernel_size=3),
    nn.ReLU(),
    nn.AdaptiveMaxPool1d(output_size=1),
    nn.LazyLinear(out_features=25),
)

Usage:

model(torch.randn(16, 1, 100))  # batch size of 16, 1 channel, size of 100
Output:
tensor([[[ 1.5257, -1.3829,  0.6808,  ...,  1.7156,  0.8902,  0.2436],
         [ 1.0326, -0.9974,  0.3288,  ...,  0.7250,  0.6279,  0.6433],
         [ 1.0429, -1.0055,  0.3362,  ...,  0.7457,  0.6334,  0.6349],
         ...,
         [ 1.2651, -1.1792,  0.4948,  ...,  1.1921,  0.7516,  0.4548],
         [ 1.1988, -1.1273,  0.4475,  ...,  1.0589,  0.7163,  0.5086],
         [ 1.3335, -1.2326,  0.5436,  ...,  1.3294,  0.7879,  0.3994]],
...