相关源码:bindsnet/bindsnet/encoding/encoders.py;
相关源码:bindsnet/bindsnet/encoding/encodings.py;
def bernoulli( datum: torch.Tensor, time: Optional[int] = None, dt: float = 1.0, device="cpu", **kwargs ) -> torch.Tensor: # language=rst """ Generates Bernoulli-distributed spike trains based on input intensity. Inputs must be non-negative. Spikes correspond to successful Bernoulli trials, with success probability equal to (normalized in [0, 1]) input value. :param datum: Tensor of shape ``[n_1, ..., n_k]``. :param time: Length of Bernoulli spike train per input variable. :param dt: Simulation time step. :return: Tensor of shape ``[time, n_1, ..., n_k]`` of Bernoulli-distributed spikes. Keyword arguments: :param float max_prob: Maximum probability of spike per Bernoulli trial. """ # Setting kwargs. max_prob = kwargs.get("max_prob", 1.0) assert 0 <= max_prob <= 1, "Maximum firing probability must be in range [0, 1]" assert (datum >= 0).all(), "Inputs must be non-negative" shape, size = datum.shape, datum.numel() datum = datum.flatten() if time is not None: # pass time = int(time / dt) # Normalize inputs and rescale (spike probability proportional to input intensity). if datum.max() > 1.0: datum /= datum.max() # Make spike data from Bernoulli sampling. if time is None: spikes = torch.bernoulli(max_prob * datum).to(device) spikes = spikes.view(*shape) else: # pass spikes = torch.bernoulli(max_prob * datum.repeat([time, 1])) spikes = spikes.view(time, *shape) return spikes.byte()
Bernoulli编码:基于输入强度生成Bernoulli分布脉冲序列。输入必须为非负。脉冲对应于成功的Bernoulli试验,成功概率等于(标准化为[0, 1])输入值。