spikingjelly.activation_based.monitor module#
- class spikingjelly.activation_based.monitor.BaseMonitor[源代码]#
基类:
object
中文
中文
监视器基类。维护钩子句柄、被监视层列表、记录缓存以及启停状态。
- 返回:
None- 返回类型:
None
English
English
Base monitor class. It maintains hook handles, monitored layer list, recorded data buffers, and enable/disable state.
- 返回:
None- 返回类型:
None
- class spikingjelly.activation_based.monitor.OutputMonitor(net: ~torch.nn.modules.module.Module, instance: type | tuple[type, ...] | None = None, function_on_output: ~typing.Callable = <function OutputMonitor.<lambda>>)[源代码]#
基类:
BaseMonitor
中文
对
net中所有类型为instance的模块的输出使用function_on_output作用后, 记录到类型为 list` 的self.records中。可以通过self.enable()和self.disable()来启用或停用这个监视器。可以通过self.clear_recorded_data()来清除已经记录的数据。阅读 监视器教程 以获得更多信息。
- 参数:
net (torch.nn.Module) -- 一个神经网络
instance (Optional[Union[type, tuple[type, ...]]]) -- 被监视的模块的数据类型。若为
None则表示类型为type(net)function_on_output (Callable) -- 作用于被监控的模块输出的自定义的函数
English
Applies
function_on_outputon outputs of all modules whose instances areinstanceinnet, and records the data intoself.records, which is alist. Callself.enable()orself.disable()to enable or disable the monitor. Callself.clear_recorded_data()to clear the recorded data.Refer to the Monitor Tutorial for more details.
- 参数:
示例代码 | Example
class Net(nn.Module): def __init__(self): super().__init__() self.fc1 = layer.Linear(8, 4) self.sn1 = neuron.IFNode() self.fc2 = layer.Linear(4, 2) self.sn2 = neuron.IFNode() functional.set_step_mode(self, "m") def forward(self, x_seq: torch.Tensor): x_seq = self.fc1(x_seq) x_seq = self.sn1(x_seq) x_seq = self.fc2(x_seq) x_seq = self.sn2(x_seq) return x_seq net = Net() for param in net.parameters(): param.data.abs_() mtor = monitor.OutputMonitor(net, instance=neuron.IFNode) with torch.no_grad(): y = net(torch.rand([1, 8])) print(f"mtor.records={mtor.records}") # mtor.records=[tensor([[0., 0., 0., 1.]]), tensor([[0., 0.]])] print(f"mtor[0]={mtor[0]}") # mtor[0]=tensor([[0., 0., 0., 1.]]) print(f"mtor.monitored_layers={mtor.monitored_layers}") # mtor.monitored_layers=['sn1', 'sn2'] print(f"mtor['sn1']={mtor['sn1']}") # mtor['sn1']=[tensor([[0., 0., 0., 1.]])]
- class spikingjelly.activation_based.monitor.InputMonitor(net: ~torch.nn.modules.module.Module, instance: type | tuple[type, ...] | None = None, function_on_input: ~typing.Callable = <function InputMonitor.<lambda>>)[源代码]#
基类:
BaseMonitor
中文
对
net中所有类型为instance的模块的输入使用function_on_input作用后, 记录到类型为 list` 的self.records中。可以通过self.enable()和self.disable()来启用或停用这个监视器。可以通过self.clear_recorded_data()来清除已经记录的数据。阅读 监视器教程 以获得更多信息。
- 参数:
English
Applies
function_on_inputon inputs of all modules whose instances areinstanceinnet, and records the data intoself.records, which is alist. Callself.enable()orself.disable()to enable or disable the monitor. Callself.clear_recorded_data()to clear the recorded data.Refer to the Monitor Tutorial for more details.
- 参数:
示例代码 | Example
import torch import torch.nn as nn from spikingjelly.activation_based import monitor, neuron, functional, layer class Net(nn.Module): def __init__(self): super().__init__() self.fc1 = layer.Linear(8, 4) self.sn1 = neuron.IFNode() self.fc2 = layer.Linear(4, 2) self.sn2 = neuron.IFNode() functional.set_step_mode(self, "m") def forward(self, x_seq: torch.Tensor): x_seq = self.fc1(x_seq) x_seq = self.sn1(x_seq) x_seq = self.fc2(x_seq) x_seq = self.sn2(x_seq) return x_seq net = Net() for param in net.parameters(): param.data.abs_() mtor = monitor.InputMonitor(net, instance=neuron.IFNode) with torch.no_grad(): y = net(torch.rand([1, 8])) print(f"mtor.records={mtor.records}") # mtor.records=[tensor([[1.0165, 1.1934, 0.9347, 0.9539]]), tensor([[0.9115, 0.9508]])] print(f"mtor[0]={mtor[0]}") # mtor[0]=tensor([[1.0165, 1.1934, 0.9347, 0.9539]]) print(f"mtor.monitored_layers={mtor.monitored_layers}") # mtor.monitored_layers=['sn1', 'sn2'] print(f"mtor['sn1']={mtor['sn1']}") # mtor['sn1']=[tensor([[1.0165, 1.1934, 0.9347, 0.9539]])]
- class spikingjelly.activation_based.monitor.AttributeMonitor(attribute_name: str, pre_forward: bool, net: ~torch.nn.modules.module.Module, instance: type | tuple[type, ...] | None = None, function_on_attribute: ~typing.Callable = <function AttributeMonitor.<lambda>>)[源代码]#
基类:
BaseMonitor
中文
对
net中所有类型为instance的模块m的成员m.attribute_name使用function_on_attribute作用后,记录到类型为 list` 的self.records。 可以通过self.enable()和self.disable()来启用或停用这个监视器。 可以通过self.clear_recorded_data()来清除已经记录的数据。阅读 监视器教程 以获得更多信息。
- 参数:
English
Applies
function_on_attributeonm.attribute_nameof each monitored modulemwhose instance isinstanceinnet, and records the data intoself.records, which is alist. Callself.enable()orself.disable()to enable or disable the monitor. Callself.clear_recorded_data()to clear the recorded data.Refer to the Monitor Tutorial for more details.
- 参数:
attribute_name (str) -- the monitored attribute's name
pre_forward (bool) -- If
True, recording the attribute before forward, otherwise recording the attribute after forwardnet (nn.Module) -- a network
instance (Optional[Union[type, tuple[type, ...]]]) -- the instance of modules to be monitored. If
None, it will be regarded astype(net)function_on_attribute (Callable) -- the function that applies on each monitored module's attribute
示例代码 | Example
import torch import torch.nn as nn from spikingjelly.activation_based import monitor, neuron, functional, layer class Net(nn.Module): def __init__(self): super().__init__() self.fc1 = layer.Linear(8, 4) self.sn1 = neuron.IFNode() self.fc2 = layer.Linear(4, 2) self.sn2 = neuron.IFNode() functional.set_step_mode(self, "m") def forward(self, x_seq: torch.Tensor): x_seq = self.fc1(x_seq) x_seq = self.sn1(x_seq) x_seq = self.fc2(x_seq) x_seq = self.sn2(x_seq) return x_seq net = Net() for param in net.parameters(): param.data.abs_() mtor = monitor.AttributeMonitor("v", False, net, instance=neuron.IFNode) with torch.no_grad(): y = net(torch.rand([1, 8])) print(f"mtor.records={mtor.records}") # mtor.records=[tensor([0.0000, 0.6854, 0.0000, 0.7968]), tensor([0.4472, 0.0000])] print(f"mtor[0]={mtor[0]}") # mtor[0]=tensor([0.0000, 0.6854, 0.0000, 0.7968]) print(f"mtor.monitored_layers={mtor.monitored_layers}") # mtor.monitored_layers=['sn1', 'sn2'] print(f"mtor['sn1']={mtor['sn1']}") # mtor['sn1']=[tensor([0.0000, 0.6854, 0.0000, 0.7968])]
- class spikingjelly.activation_based.monitor.GradInputMonitor(net: ~torch.nn.modules.module.Module, instance: type | tuple[type, ...] | None = None, function_on_grad_input: ~typing.Callable = <function GradInputMonitor.<lambda>>)[源代码]#
基类:
BaseMonitor
中文
对
net中所有类型为instance的模块的输入的梯度使用function_on_grad_input作用后,记录到类型为 list` 的self.records中。 可以通过self.enable()和self.disable()来启用或停用这个监视器。 可以通过self.clear_recorded_data()来清除已经记录的数据。阅读 监视器教程 以获得更多信息。
备注
对于一个模块,输入为 \(X\),输出为 \(Y\),损失为 \(L\),则
GradOutputMonitor记录的是对输入的梯度 \(\frac{\partial L}{\partial X}\)。- 参数:
English
Applies
function_on_grad_inputon grad of inputs of all modules whose instances areinstanceinnet, and records the data intoself.records, which is alist. Callself.enable()orself.disable()to enable or disable the monitor. Callself.clear_recorded_data()to clear the recorded data.Refer to the Monitor Tutorial for more details.
备注
Denote the input and output of the monitored module as \(X\) and \(Y\), and the loss is \(L\), then
GradInputMonitorwill record the gradient of input, which is \(\frac{\partial L}{\partial X}\).- 参数:
示例代码 | Example
class Net(nn.Module): def __init__(self): super().__init__() self.fc1 = layer.Linear(8, 4) self.sn1 = neuron.IFNode() self.fc2 = layer.Linear(4, 2) self.sn2 = neuron.IFNode() functional.set_step_mode(self, "m") def forward(self, x_seq: torch.Tensor): x_seq = self.fc1(x_seq) x_seq = self.sn1(x_seq) x_seq = self.fc2(x_seq) x_seq = self.sn2(x_seq) return x_seq net = Net() for param in net.parameters(): param.data.abs_() mtor = monitor.GradInputMonitor(net, instance=neuron.IFNode) with torch.no_grad(): y = net(torch.rand([1, 8])) print(f"mtor.records={mtor.records}") # mtor.records=[tensor([0.0000, 0.6854, 0.0000, 0.7968]), tensor([0.4472, 0.0000])] print(f"mtor[0]={mtor[0]}") # mtor[0]=tensor([0.0000, 0.6854, 0.0000, 0.7968]) print(f"mtor.monitored_layers={mtor.monitored_layers}") # mtor.monitored_layers=['sn1', 'sn2'] print(f"mtor['sn1']={mtor['sn1']}") # mtor['sn1']=[tensor([0.0000, 0.6854, 0.0000, 0.7968])]
- class spikingjelly.activation_based.monitor.GradOutputMonitor(net: ~torch.nn.modules.module.Module, instance: type | tuple[type, ...] | None = None, function_on_grad_output: ~typing.Callable = <function GradOutputMonitor.<lambda>>)[源代码]#
基类:
BaseMonitor
中文
对
net中所有类型为instance的模块的输出的梯度使用function_on_grad_output作用后,记录到类型为 list` 的self.records中。 可以通过self.enable()和self.disable()来启用或停用这个监视器。 可以通过self.clear_recorded_data()来清除已经记录的数据。阅读 监视器教程 以获得更多信息。
备注
对于一个模块,输入为 \(X\),输出为 \(Y\),损失为 \(L\),则
GradOutputMonitor记录的是对输出的梯度 \(\frac{\partial L}{\partial Y}\)。- 参数:
English
Applies
function_on_grad_outputon grad of outputs of all modules whose instances areinstanceinnet, and records the data intoself.records, which is alist. Callself.enable()orself.disable()to enable or disable the monitor. Callself.clear_recorded_data()to clear the recorded data.Refer to the Monitor Tutorial for more details.
备注
Denote the input and output of the monitored module as \(X\) and \(Y\), and the loss is \(L\), then
GradOutputMonitorwill record the gradient of output, which is \(\frac{\partial L}{\partial Y}\).- 参数:
示例代码 | Example
import torch import torch.nn as nn from spikingjelly.activation_based import monitor, neuron, functional, layer class Net(nn.Module): def __init__(self): super().__init__() self.fc1 = layer.Linear(8, 4) self.sn1 = neuron.IFNode() self.fc2 = layer.Linear(4, 2) self.sn2 = neuron.IFNode() functional.set_step_mode(self, "m") def forward(self, x_seq: torch.Tensor): x_seq = self.fc1(x_seq) x_seq = self.sn1(x_seq) x_seq = self.fc2(x_seq) x_seq = self.sn2(x_seq) return x_seq net = Net() for param in net.parameters(): param.data.abs_() mtor = monitor.GradOutputMonitor(net, instance=neuron.IFNode) net(torch.rand([1, 8])).sum().backward() print(f"mtor.records={mtor.records}") # mtor.records=[tensor([[1., 1.]]), tensor([[0.1372, 0.1081, 0.0880, 0.1089]])] print(f"mtor[0]={mtor[0]}") # mtor[0]=tensor([[1., 1.]]) print(f"mtor.monitored_layers={mtor.monitored_layers}") # mtor.monitored_layers=['sn1', 'sn2'] print(f"mtor['sn1']={mtor['sn1']}") # mtor['sn1']=[tensor([[0.1372, 0.1081, 0.0880, 0.1089]])]
- class spikingjelly.activation_based.monitor.GPUMonitor(log_dir: str | None = None, gpu_ids: tuple = (0,), interval: float = 600.0, start_now=True)[源代码]#
基类:
Thread
中文
GPU监视器,可以开启一个新的线程来记录
gpu_ids的使用率和显存使用情况, 每interval秒记录一次数据。警告
在主线程的工作完成后一定要调用GPU监视器的
stop()函数,否则主线程不会退出。- 参数:
English
The GPU monitor, which starts a new thread to record the utilization and memory used of
gpu_idseveryintervalseconds.警告
Do not forget to call this module's
stop()after the main thread finishes its job, otherwise the main thread will never stop!- 参数:
log_dir (Optional[str]) -- the directory for saving logs with tensorboard. If it is None, this module will print logs
gpu_ids (tuple) -- the id of GPUs to be monitored, e.g.,
(0, 1, 2, 3). The default value is(0, )interval (float) -- the recording interval (in seconds)
start_now (bool) -- if true, the monitor will start to record now. Otherwise, it will start after the user call
start()manually
示例代码 | Example
import time gm = GPUMonitor(interval=1) time.sleep(2) # make the main thread sleep gm.stop() # The outputs are: # 2022-04-28 10:52:25 # utilization.gpu [%], memory.used [MiB] # 0 %, 376 MiB