Thank you for your reply.
I followed your advice and modified the class as follows
class CNNForecast(nn.Module):
    def __init__(self, w, p_w):
        super(CNNForecast, self).__init__()
        self.first_conv1d = nn.Conv1d(in_channels=1, out_channels=32, kernel_size=2, stride=1, padding=0)
        self.maxpool1d = nn.MaxPool1d(kernel_size=2, stride=2)
        self.second_conv1d = nn.Conv1d(in_channels=1, out_channels=32, kernel_size=2, stride=1, padding=0)
        self.flatten = nn.Flatten()
        self.linear = nn.Linear(32*2, 40)
        self.dropout = nn.Dropout(p=0.5)
        self.out = nn.Linear(40, 1)

    def forward(self, x):
        x = F.relu(self.first_conv1d(x))
        x = self.maxpool1d(x)
        x = F.relu(self.second_conv1d(x))
        x = self.maxpool1d(x)
        x = self.flatten(x)
        x = self.dropout(x)
        x = self.out(x)
        return x

I still face another problem below.

RuntimeError: Given groups=1, weight of size [32, 1, 2], expected input[32, 32, 49] to have 1 channels, but got 32 channels instead

What’s the problem?