将 keras 翻译为 pytorch - CNN 层不匹配

问题描述 投票:0回答:0

我正在尝试将 keras 的代码重现到 pytorch。然而我正在努力重现结果。

from typing import List
class DNA_CNN_test2(nn.Module): # deepcre model  
    def __init__(self,
                 seq_len: int =1000,
                 num_filters: List[int] = [64, 128, 64],
                 kernel_size: int = 8,
                 p = 0.25): # drop out value 
        super().__init__()
        self.seq_len = seq_len
        # CNN module
        self.conv_net = nn.Sequential()
        num_filters = [4] + num_filters
        self.model = nn.Sequential(
            nn.Conv1d(4,64,kernel_size=kernel_size, padding='same'),
            nn.ReLU(),
            nn.Conv1d(64,64,kernel_size=kernel_size, padding='same'),
            nn.ReLU(),
            nn.MaxPool1d(kernel_size=8),
            nn.Dropout(p),
            nn.Conv1d(64,128,kernel_size=kernel_size, padding='same'),
            nn.ReLU(),
            nn.Conv1d(128,128,kernel_size=kernel_size, padding='same'),
            nn.ReLU(),
            nn.MaxPool1d(kernel_size=8),
            nn.Dropout(p),
            nn.Conv1d(128,64,kernel_size=kernel_size, padding='same'),
            nn.ReLU(),
            nn.Conv1d(64,64,kernel_size=kernel_size, padding='same'),
            nn.ReLU(),
            nn.MaxPool1d(kernel_size=8),
            nn.Dropout(p),
            nn.Flatten(),
        
            nn.Linear(64, 128),
            nn.ReLU(),
            nn.Dropout(p),
            nn.Linear(128, 64),
            nn.ReLU(),
            nn.Linear(64, 1)
        ).to(device)
         
    def forward(self, xb: torch.Tensor):
        """Forward pass."""
        xb = xb.permute(0, 2, 1) 
        out = self.conv_net(xb)
        return out 

我遵循原始代码的所有顺序,但是代码给了我一个错误,我可以找到该错误。这里我使用 4096 批量大小。我的输入是一个热编码 DNA 序列(1000bp)及其相应的转录值(数字)。我错过了什么?

The size of tensor a (4) must match the size of tensor b (4096) at non-singleton dimension 1
python keras pytorch conv-neural-network bioinformatics
© www.soinside.com 2019 - 2024. All rights reserved.