You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyTorch带步长填充后全连接层神经元数量不符问题求助

复现《(Re-)Imag(in)ing Price Trends》20日线CNN模型时全连接层尺寸不匹配问题

我在复现论文《(Re-)Imag(in)ing Price Trends》中基于OHLC图训练2DCNN的流程时,遇到了20日线(对应64x60尺寸图像)模型的全连接层神经元数量与论文不符的问题。

我严格遵循论文给出的架构规格:

  • 模块数量:32x15对应2个,64x60对应3个,96x180对应4个
  • 首个模块固定64个滤波器,后续模块滤波器数量翻倍
  • 所有图像使用5x3卷积核、2x1最大池化核
  • 第一层垂直步长分别为1、3、3,垂直膨胀率分别为1、2、3(对应三种图像)
  • 填充保证输出与输入尺寸一致

由于PyTorch在不对称步长下的padding="same"实现不符合需求,我采用了一个自定义的Conv2d替代类,但运行代码时出现如下错误:

RuntimeError: shape '[-1, 46080]' is invalid for input of size 30720

根据论文描述,20日线模型的全连接层应包含46080个神经元,但实际输出的特征尺寸仅为30720,尺寸计算明显错误,且该结果对填充计算的变化非常敏感,我无法正确设置各模块的填充值,希望得到帮助。

以下是我的代码:

import torch
from torch import nn
import math
from functools import reduce
from operator import __add__
import torch.nn.functional as F

class Conv2dSame(nn.Conv2d):
    """
    自定义实现same padding的Conv2d
    """

    def calc_same_pad(self, i: int, k: int, s: int, d: int) -> int:
        pad = max((math.ceil(i / s) - 1) * s + (k - 1) * d + 1 - i, 0)
        return pad

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        
        ih, iw = x.size()[-2:]
        kh, kw = self.weight.size()[-2:]
        pad_h = self.calc_same_pad(i=ih, k=kh, s=self.stride[0], d=self.dilation[0])
        pad_w = self.calc_same_pad(i=iw, k=kw, s=self.stride[1], d=self.dilation[1])

        if pad_h > 0 or pad_w > 0:
            x = F.pad(
                x, [pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2]
            )
        return F.conv2d(
            x,
            self.weight,
            self.bias,
            self.stride,
            self.padding,
            self.dilation,
            self.groups,
        )

class Net20(nn.Module): 
    
    def __init__(self):
        super().__init__()
        self.layer1 = nn.Sequential(
            Conv2dSame(1, 64, kernel_size=(5,3), stride=(3,1), dilation=(2,1)),
            nn.BatchNorm2d(64),
            nn.LeakyReLU(negative_slope=0.01, inplace=True),
            nn.MaxPool2d((2, 1)) 
        )
        self.layer2 = nn.Sequential(
            Conv2dSame(64, 128, kernel_size=(5,3)),
            nn.BatchNorm2d(128),
            nn.LeakyReLU(negative_slope=0.01, inplace=True),
            nn.MaxPool2d((2, 1))
        )
        self.layer3 = nn.Sequential(
            Conv2dSame(128, 256, kernel_size=(5,3)),
            nn.BatchNorm2d(256),
            nn.LeakyReLU(negative_slope=0.01, inplace=True),
            nn.MaxPool2d((2, 1))
        )
        self.fc1 = nn.Sequential(
            nn.Dropout(p=0.5),
            nn.Linear(46080, 1), 
        )

    def forward(self, x):
        x = x.reshape(-1,1,64,60)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = x.reshape(-1,46080) # FC neurons according to paper
        x = self.fc1(x)
        return x

内容的提问来源于stack exchange,提问作者fdp1996

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 06:05:29