spikingjelly学习-神经元

对应网站【https://spikingjelly.readthedocs.io/zh-cn/0.0.0.0.14/activation_based/neuron.html】
【import torch

print(torch.version) # 查看torch当前版本号

print(torch.version.cuda) # 编译当前版本的torch使用的cuda版本号

print(torch.cuda.is_available()) # 查看当前cuda是否可用于当前版本的Torch,如果输出True,则表示可用

import torch
from spikingjelly.activation_based import neuron
from spikingjelly import visualizing
from matplotlib import pyplot as plt


if_layer = neuron.IFNode(v_threshold =0.5)
print(if_layer.v)
# if_layer.v=0.0


x = torch.rand(size=[2, 3])
print(f'x={x}')
if_layer(x)
print(f'x.shape={x.shape}, if_layer.v.shape={if_layer.v.shape}')
print(if_layer.v)
# x.shape=torch.Size([2, 3]), if_layer.v.shape=torch.Size([2, 3])
if_layer.reset()

x = torch.rand(size=[4, 5, 6])
if_layer(x)
print(f'x.shape={x.shape}, if_layer.v.shape={if_layer.v.shape}')
# x.shape=torch.Size([4, 5, 6]), if_layer.v.shape=torch.Size([4, 5, 6])
print(if_layer.v)
if_layer.reset()

0.0
x=tensor([[0.2073, 0.4567, 0.9076],
        [0.3870, 0.9421, 0.0846]])
x.shape=torch.Size([2, 3]), if_layer.v.shape=torch.Size([2, 3])
tensor([[0.2073, 0.4567, 0.0000],
        [0.3870, 0.0000, 0.0846]])
x.shape=torch.Size([4, 5, 6]), if_layer.v.shape=torch.Size([4, 5, 6])
tensor([[[0.0000, 0.0970, 0.0593, 0.0000, 0.1183, 0.4120],
         [0.1283, 0.3113, 0.0000, 0.0264, 0.0000, 0.0000],
         [0.0908, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
         [0.0000, 0.3399, 0.4341, 0.0000, 0.0000, 0.2555],
         [0.3466, 0.0000, 0.0000, 0.0000, 0.0000, 0.2409]],

        [[0.0000, 0.3552, 0.0000, 0.1798, 0.0000, 0.1979],
         [0.3742, 0.0000, 0.0000, 0.3114, 0.2875, 0.0620],
         [0.0000, 0.1858, 0.1320, 0.0000, 0.0756, 0.0426],
         [0.2535, 0.2203, 0.0666, 0.0000, 0.3084, 0.0960],
         [0.2010, 0.2125, 0.0000, 0.0000, 0.3538, 0.0000]],

        [[0.1304, 0.1231, 0.0000, 0.0000, 0.2787, 0.0000],
         [0.0000, 0.0000, 0.0000, 0.0000, 0.4387, 0.2478],
         [0.0000, 0.2330, 0.0000, 0.0528, 0.4681, 0.0793],
         [0.3374, 0.3828, 0.4151, 0.0000, 0.0000, 0.0000],
         [0.0000, 0.2375, 0.0000, 0.0000, 0.2498, 0.0000]],

        [[0.3589, 0.4366, 0.0000, 0.4677, 0.0000, 0.0000],
         [0.0306, 0.0000, 0.3213, 0.0000, 0.0000, 0.2210],
         [0.4829, 0.3140, 0.0000, 0.2205, 0.1642, 0.0000],
         [0.0000, 0.4774, 0.0000, 0.2892, 0.0142, 0.0000],
         [0.0000, 0.1012, 0.4472, 0.1754, 0.3887, 0.0000]]])

【surrogate_function() 在前向传播时是阶跃函数,只要输入大于或等于0,就会返回1,否则会返回0。我们将这种元素仅为0或1的 tensor 视为脉冲。】

【释放脉冲消耗了神经元之前积累的电荷,因此膜电位会有一个瞬间的降低,即膜电位的重置。在SNN中,对膜电位重置的实现,有2种方式:

Hard方式:释放脉冲后,膜电位直接被设置成重置电压:

Soft方式:释放脉冲后,膜电位减去阈值电压:】

import torch
from spikingjelly.activation_based import neuron
from spikingjelly import visualizing
from matplotlib import pyplot as plt
if_layer = neuron.IFNode()
print(if_layer.v)
# if_layer.v=0.0

if_layer.reset()
x = torch.as_tensor([0.5])
T = 150
s_list = []
v_list = []
for t in range(T):
    s_list.append(if_layer(x))
    v_list.append(if_layer.v)

dpi = 300
figsize = (12, 8)
visualizing.plot_one_neuron_v_s(torch.cat(v_list).numpy(), torch.cat(s_list).numpy(), v_threshold=if_layer.v_threshold,
                                v_reset=if_layer.v_reset,
                                figsize=figsize, dpi=dpi)
plt.show()

这个x = torch.as_tensor([0.5]),代表每一次往snn神经元里面,加入0.5的值
在这里插入图片描述
在这里插入图片描述

import torch
from spikingjelly.activation_based import neuron
from spikingjelly import visualizing
from matplotlib import pyplot as plt
if_layer = neuron.IFNode()
print(if_layer.v)
# if_layer.v=0.0

if_layer.reset()
T = 50
x = torch.rand([32]) / 8.
s_list = []
v_list = []
for t in range(T):
    s_list.append(if_layer(x).unsqueeze(0))
    v_list.append(if_layer.v.unsqueeze(0))

s_list = torch.cat(s_list)
v_list = torch.cat(v_list)

figsize = (5, 5)
dpi = 200
visualizing.plot_2d_heatmap(array=v_list.numpy(), title='membrane potentials', xlabel='simulating step',
                            ylabel='neuron index', int_x_ticks=True, x_max=T, figsize=figsize, dpi=dpi)


visualizing.plot_1d_spikes(spikes=s_list.numpy(), title='membrane sotentials', xlabel='simulating step',
                        ylabel='neuron index', figsize=figsize, dpi=dpi)

plt.show()

在这里插入图片描述

在Python中,数字后面加上一个点(.)表示这是一个浮点数而不是整数。因此,8.实际上是8.0的简写,它强制Python将数字视为浮点数。这在数学运算中很重要,特别是在除法操作中,因为它决定了运算的类型(整数除法还是浮点除法)以及结果的类型。
在这个特定的上下文中,torch.rand([32]) / 8.的作用是生成一个形状为[32]的张量,其中包含的是[0, 1)区间内的随机浮点数,然后将这些数除以8.0。使用浮点数除法(由于8.是浮点数)确保了结果也是浮点数,保留了小数部分,而不是进行整数除法后可能得到的整数结果。
这种操作生成的张量x中的每个元素都是[0, 1/8)区间内的浮点数,这对于控制输入信号的强度和范围非常有用,尤其是在需要精确控制输入到模型中数据的大小和尺度时。在脉冲神经网络(SNN)等深度学习模型中,适当的输入数据范围对于模型的训练和性能至关重要。

除以10.的结果

import torch
from spikingjelly.activation_based import neuron, functional
if_layer = neuron.IFNode(step_mode='s')
T = 8
N = 2
x_seq = torch.rand([T, N])
y_seq = functional.multi_step_forward(x_seq, if_layer)
if_layer.reset()

if_layer.step_mode = 'm'
y_seq = if_layer(x_seq)
if_layer.reset()
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_44781508

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值