0

I want to square the result of a maxpool layer. I tried the following:

class CNNClassifier(Classifier): # nn.Module def __init__(self, in_channels): super().__init__() self.save_hyperparameters('in_channels') self.cnn = nn.Sequential( # maxpool nn.MaxPool2d((1, 5), stride=(1, 5)), torch.square(), # layer1 nn.Conv2d(in_channels=in_channels, out_channels=32, kernel_size=5, ) 

Which to the experienced PyTorch user for sure makes no sense.

Indeed, the error is quite clear:

TypeError: square() missing 1 required positional arguments: "input"

How can I feed in to square the tensor from the preceding layer?

1 Answer 1

1

You can't put a PyTorch function in a nn.Sequential pipeline, it needs to be a nn.Module.

You could wrap it like this:

class Square(nn.Module): def forward(self, x): return torch.square(x) 

Then use it inside your sequential layer like so:

class CNNClassifier(Classifier): # nn.Module def __init__(self, in_channels): super().__init__() self.save_hyperparameters('in_channels') self.cnn = nn.Sequential( nn.MaxPool2d((1, 5), stride=(1, 5)), Square(), nn.Conv2d(in_channels=in_channels, out_channels=32, kernel_size=5)) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.