Network Configuration Functions#

这些函数帮助用户统一设置网络中每个 子模块的配置 ,如步进模式、后端等。


These functions help users set configurations for each submodule in a network, such as step mode and backend.

spikingjelly.activation_based.functional.net_config.collect_reset_modules(net: Module) tuple[Module, ...][源代码]#
spikingjelly.activation_based.functional.net_config.detach_net(net: Module)[源代码]#

API Language: 中文 | English


  • 中文

net 中各有状态模块与之前时间步的计算图断开。

该函数会遍历 net.modules() 中的所有子模块;若某个子模块实现了 detach() 方法,则调用该方法。对于不是 MemoryModule 但实现了 detach() 的模块,此函数仍会调用 detach(),同时记录告警。

参数:

net (torch.nn.Module) -- 任何属于 nn.Module 子类的网络

返回:

None

返回类型:

None

抛出:

Exception -- 任何子模块 detach() 在执行过程中抛出的异常都会原样向上传播


  • English

Detach stateful modules in net from the computation graphs of previous time steps.

This function iterates over net.modules() and calls detach() on each submodule that implements it. If a submodule is not an instance of MemoryModule but still defines detach(), the function will still call it and emit a warning.

参数:

net (torch.nn.Module) -- Any network inherits from nn.Module

返回:

None

返回类型:

None

抛出:

Exception -- Any exception raised by a submodule detach() call is propagated unchanged

spikingjelly.activation_based.functional.net_config.reset_collected_modules(modules: tuple[Module, ...]) None[源代码]#
spikingjelly.activation_based.functional.net_config.reset_net(net: Module)[源代码]#

API Language: 中文 | English


  • 中文

重置 net 中所有可重置模块的状态。

该函数会遍历 net.modules() 中的所有子模块;若某个子模块实现了 reset() 方法,则调用该方法。对于不是 MemoryModule 但实现了 reset() 的模块,此函数仍会调用 reset(),同时记录告警。

参数:

net (torch.nn.Module) -- 任何属于 nn.Module 子类的网络

返回:

None

返回类型:

None

抛出:

Exception -- 任何子模块 reset() 在执行过程中抛出的异常都会原样向上传播


  • English

Reset the states of all resettable modules in net.

This function iterates over net.modules() and calls reset() on each submodule that implements it. If a submodule is not an instance of MemoryModule but still defines reset(), the function will still call it and emit a warning.

参数:

net (torch.nn.Module) -- Any network inherits from nn.Module

返回:

None

返回类型:

None

抛出:

Exception -- Any exception raised by a submodule reset() call is propagated unchanged

spikingjelly.activation_based.functional.net_config.set_backend(net: Module, backend: str, instance: Module | tuple[Module] | None = None)[源代码]#

API Language: 中文 | English


  • 中文

net 中所有满足 isinstance(m, instance) 且具有 backend 属性的模块后端设置为 backend

仅当目标模块的 supported_backends 包含给定 backend 时才会实际更新; 否则会记录告警并保留原有后端。若 instanceNone,则会检查所有具有 backend 属性的模块。

参数:
  • net (torch.nn.Module) -- 一个神经网络

  • backend (str) -- 使用哪个后端

  • instance (Optional[Union[nn.Module, tuple[nn.Module]]]) -- 传给 isinstance 的筛选类型。满足该筛选且具有 backend 属性的模块后端会被检查。 若为 None ,则所有具有 backend 属性的模块都会被检查

返回:

None

返回类型:

None

抛出:

Exception -- 若目标模块在访问 supported_backends 或设置 backend 时抛出异常,则该异常会原样向上传播


  • English

Set backend for all modules in net whose type matches instance and that expose a backend attribute.

The backend is updated only when backend is listed in the module's supported_backends. Otherwise, a warning is logged and the existing backend is kept unchanged. If instance is None, all modules with a backend attribute are checked.

参数:
  • net (torch.nn.Module) -- a network

  • backend (str) -- the backend to be set

  • instance (Optional[Union[nn.Module, tuple[nn.Module]]]) -- the type filter passed to isinstance. Modules that match this filter and have a backend attribute will be checked. If None, all modules with a backend attribute will be checked

返回:

None

返回类型:

None

抛出:

Exception -- Propagated if a target module raises while exposing supported_backends or assigning backend

spikingjelly.activation_based.functional.net_config.set_step_mode(net: Module, step_mode: str)[源代码]#

API Language: 中文 | English


  • 中文

net 中所有具有 step_mode 属性的模块的步进模式设置为 step_mode

备注

StepModeContainer, ElementWiseRecurrentContainer, LinearRecurrentContainer 的子模块(不包含包装器本身)的 step_mode 不会被改变。

若某个模块具有 step_mode 属性但不是 StepModule,则该函数仍会尝试赋值, 同时记录告警。

参数:
  • net (torch.nn.Module) -- 一个神经网络

  • step_mode (str) -- 's' (单步模式) 或 'm' (多步模式)

返回:

None

返回类型:

None

抛出:

ValueError -- 若某个模块的 step_mode setter 不接受给定的 step_mode,则该异常会原样向上传播


  • English

Set step_mode to step_mode for all modules in net that expose a step_mode attribute.

Note

The submodule (not including the container itself) of StepModeContainer, ElementWiseRecurrentContainer, LinearRecurrentContainer will not be changed.

If a module has a step_mode attribute but is not an instance of StepModule, the function still attempts to assign the new value and emits a warning.

参数:
  • net (nn.Module) -- a network

  • step_mode (str) -- 's' (single-step) or 'm' (multi-step)

返回:

None

返回类型:

None

抛出:

ValueError -- Propagated if a module rejects the provided step_mode in its setter