Translation keras to pytoch

berlin: <p>I’m trying to translate the below 3layer CNN architecture from keras to pytorch. The usage of the model is to predict expression value(input_shape_val) from dna sequence(input_shape_hot). The sequence is one hot encoded. The architecture orignally meant to train the model consecutively CNN (3 layers)-FC (2 layers) with batch normalization and weight dropout were applied after all layers and max-pooling after CNN layers( <a href="https://www.nature.com/articles/s41467-020-19921-4#Sec9" rel="noopener nofollow ugc">ref-paper</a>, <a href="https://github.com/zelezniak-lab/param_pipeline/blob/random_search_with_replicates/models/THIS_CNN_NN_vals_input.py" rel="noopener nofollow ugc">ref-code</a>).</p>
<pre><code class="lang-auto">from typing import List
class DNA_CNN_test2(nn.Module):
    def __init__(self,
                 seq_len: int,
                 num_filters: List[int] = [32, 64, 128],
                 kernel_size: int = 3,
                 p = 0.2):
        super().__init__()
        self.seq_len = seq_len
        # CNN module
        self.conv_net = nn.Sequential()
        num_filters = [4] + num_filters
        for idx in range(len(num_filters) - 1):
            self.conv_net.add_module(
                f"conv_{idx}",
                nn.Conv1d(num_filters[idx], num_filters[idx + 1],
                          kernel_size=kernel_size, padding='same')
            )
            self.conv_net.add_module(f"relu_{idx}", nn.ReLU(inplace=True))
            self.conv_net.add_module(f"batchNor_{idx}",nn.BatchNorm1d(num_filters[idx + 1]))
            self.conv_net.add_module(f"MaxP_{idx}",nn.MaxPool1d(kernel_size=2,stride= 4))
            self.conv_net.add_module(f"dropout_{idx}",nn.Dropout(0.2))
        self.conv_net.add_module("flatten", nn.Flatten())
        self.conv_net.add_module("linear",nn.Linear(num_filters[-1]*seq_len, 1))
        #self.conv_net.add_module("linear",nn.Linear(64, 1))
        #self.conv_net.add_module("relu", nn.ReLU(inplace=True))
        #self.conv_net.add_module("batch_normal",nn.BatchNorm1d(64))
        #self.conv_net.add_module("drop",nn.Dropout(0.2))
        #self.conv_net.add_module("linear",nn.Linear(num_filters[-1]*seq_len, 1))
        
        
        
    def forward(self, xb: torch.Tensor):
        """Forward pass."""
        xb = xb.permute(0, 2, 1) 
        out = self.conv_net(xb)
        return out
</code></pre>
<p>however I am facing challenge on using <code>MaxPool1d</code> after <code>Conv1d</code>. I have already tried many suggestions from answers to similar questions, but none of them worked. Any suggestions about what I should do?</p>
<p>Here is the error code :<br>
<code>mat1 and mat2 shapes cannot be multiplied (2048x2048 and 128000x1)</code></p>

ptrblck: <p>The shape mismatch error seems to be raised in your <code>linear</code> layer and based on the error message the <code>in_features</code> value does not match the number of features of its input activation.<br>
Assuming you are using <code>2048</code> samples each with a feature dimension of <code>2048</code>, using <code>in_features=2048</code> in the <code>linear</code> layer should work (and you should compare it to the Keras implementation to make sure the actual model architecture is the same).</p>