class srmLinear(nn.Linear): def __init__(self, in_features: int, out_features: int, bias: bool = False, v_th: float = 1.0, taum: float = 5., taus: float = 3., taug: float = 2.5, weight_norm: bool = True, eps: float = 1e-5) -> None: super().__init__(in_features, out_features, bias) nn.init.orthogonal_(self.weight) self.taum = taum self.taus = taus self.taug = taug self.v_th = v_th self.epsw = None self.epst = None self.e_taum = 1. - 1. / taum self.e_taus = 1. - 1. / taus self.e_taug = 1. - 1. / taug self.linear_func = srmLinearFunc.apply if weight_norm: self.bn_weight = nn.Parameter(torch.ones(out_features)) self.bn_bias = nn.Parameter(torch.zeros(out_features)) else: self.bn_weight = None self.bn_bias = None self.register_buffer('eps', torch.tensor([eps])) def forward(self, inputs: Tensor) -> Tensor: self.batch_reset(inputs) return self.linear_func( inputs, self.weight, self.bn_weight, self.bn_bias, self.eps, self.v_th, self.taum, self.taus, self.e_taug, self.epsw, self.epst ) def batch_reset(self, inputs: Tensor) -> None: if self.epsw is None or self.epsw.shape[0] != inputs.shape[1]: coefficient = self.taum / (self.taum - self.taus) # for i in range(inputs.shape[1]): self.epst = torch.FloatTensor([-self.e_taug ** (1 + i) for i in range(inputs.shape[1])]).to(inputs) self.epsw = torch.FloatTensor( [coefficient * (self.e_taum ** (1 + i) - self.e_taus ** (1 + i)) for i in range(inputs.shape[1])] ).to(inputs)
时间: 2023-12-27 19:02:24 浏览: 27
这是一个基于 PyTorch 实现的自适应脉冲编码神经网络(Adaptive Pulse-Coded Neural Network)中的线性层(Linear Layer)代码,其中引入了 Spike Response Model(SRM)的概念。SRM 是一种对神经元(Neuron)的响应方式进行建模的方法,即神经元只有当输入信号超过一个阈值时才会产生输出脉冲。此外,该代码还实现了权重归一化(Weight Normalization)和 Batch Normalization 等技术。