fangwei123456 / spikingjelly

SpikingJelly is an open-source deep learning framework for Spiking Neural Network (SNN) based on PyTorch.
https://spikingjelly.readthedocs.io
Other
1.22k stars 233 forks source link

AdaptiveAvgPool1d与BatchNorm1在多步传播模式下报错 #551

Open Zihao0 opened 4 weeks ago

Zihao0 commented 4 weeks ago

Read before creating a new issue

For faster response

You can @ the corresponding developers for your issue. Here is the division:

Features Developers
Neurons and Surrogate Functions fangwei123456
Yanqi-Chen
CUDA Acceleration fangwei123456
Yanqi-Chen
Reinforcement Learning lucifer2859
ANN to SNN Conversion DingJianhao
Lyu6PosHao
Biological Learning (e.g., STDP) AllenYolk
Others Grasshlw
lucifer2859
AllenYolk
Lyu6PosHao
DingJianhao
Yanqi-Chen
fangwei123456

We are glad to add new developers who are volunteering to help solve issues to the above table.

Issue type

SpikingJelly version

0.0.0.0.14

Description

...

Minimal code to reproduce the error/bug

import spikingjelly
# ...
import numpy as np
import torch
from spikingjelly.activation_based import neuron, layer, functional
import torch
import torch.nn as nn

class CustomModel(nn.Module):
    def __init__(self, num_cls):
        super(CustomModel, self).__init__()
        self.T = 3
        # Layer 3
        self.fc1 = nn.Sequential(
            # nn.AdaptiveAvgPool1d(num_cls),
            layer.BatchNorm1d(20)
            # nn.Flatten(),  # 添加展平层
            # nn.Linear(64 * 6 * 6, num_cls * 10),
        )

        functional.set_step_mode(self, step_mode='m')

    def forward(self, x):
        batch_size = x.size()[0]
        # encoder layer
        x = x.unsqueeze(0).repeat(self.T, 1, 1)
        print(f"Input shape: {x.shape}")
        x = self.fc1(x)
        print(f"After fc1 shape: {x.shape}")
        return x

model = CustomModel(num_cls=2)
input_tensor = torch.randn(256, 20)  # 示例输入张量
output = model(input_tensor)
print(output.shape)  # 输出

报错

ValueError: expected x with shape [T, N, C, L], but got x with shape torch.Size([3, 256, 20])!

问题

奇怪的是,在单步模式下,输入大小为[N, C],程序可以正常运行,多步模式下将输入扩展为[T, N, C]就会发生上述报错,提示期望的输入大小为 [T, N, C, L],这是为什么呢,不应该是 [T, N, C]吗?单步模式下的输入为[N, C],多步不应该为[T, N, C]吗? 上述问题在AdaptiveAvgPool1d与BatchNorm1d都存在,单步可以运行,但多步需要多一个L维度,请问这是为什么?怎样解决?谢谢!

Zihao0 commented 3 weeks ago

问题已经解决了,如果作者愿意解释一下就更好了