spaic.Network package

Submodules

spaic.Network.Assembly module

Created on 2020/8/5 @project: SPAIC @filename: Assembly @author: Hong Chaofei @contact: hongchf@gmail.com @description:

class spaic.Network.Assembly.Assembly(name=None)[source]

Bases: BaseModule

Base class for all network units.

The Assembly represent an abstract network structure, it defines the basic network construct behavior and attributes. It can contain any neural network units like neurongroups, nodes, other assemblies and their connections. The classes like Node, NeuronGroup, and Network are special cases of Assembly.

add_assembly()[source]

Add a new assembly to this assembly as its member.

del_assembly()[source]

Delete an existed member assembly of this assembly.

add_connection()[source]

Add the connection between two member assemblies of this assembly.

del_connection()[source]

Delete an existed connection between member assemblies of this assembly.

copy_assembly()[source]

Copy an existed assembly structure into this assembly.

replace_assembly()[source]

Replace an existed member assembly with a new assembly.

merge_assembly()[source]

Add the member assemblies and connections of the target assembly, which are not already included in this assembly, to this assembly.

select_assembly()[source]

Select a list of member assemblies in this assembly, and form a new assembly that contains those selected assemblies.

Variables:
  • _class_label (str) – the label is a static variable to imply the class of this object

  • _backend (Backend) – the backend backend this assembly runs on

  • _groups (OrderedDict) – the container of member assemblies

  • _connections (OrderedDict) – the container of member connections

  • _supers (list) – the super assemblies that add this assembly as their member assemblies

  • _input_connections (list) – the connections that use this assembly as post-synaptic target

  • _output_connections (list) – the connections that use this assembly as pre-synaptic target

  • num (int) – the total number of neurons this assembly contains

  • position (Tuple(int, int)) – the top level positon of this assembly

  • _var_names – The backend variable names this assembly and its members contains

Base class for neural network units, and defines the basic attributes of a group object.

It contains other Assemblies (including neuron_groups, networks and nodes) and their connections. This class provides a abstract representation of network topology.

Parameters:

name (str) – name of the network assembly

Variables:
  • _backend (Backend) – the backend backend this assembly runs on

  • _groups (OrderedDict) – the container of member assemblies

  • _connections (OrderedDict) – the container of member connections

  • _supers (list) – the super assemblies that add this assembly as their member assemblies

  • _input_connections (list) – the connections that use this assembly as post-synaptic target

  • _output_connections (list) – the connections that use this assembly as pre-synaptic target

  • num (int) – the total number of neurons this assembly contains

  • position (Tuple(int, int)) – the top level positon of this assembly

  • _var_names – The backend variable names this assembly and its members contains

add_type(type)[source]
get_labeled_name(key: str)[source]
add_assembly(name, assembly)[source]

Add a new assembly to this assembly as its member. This is the basic method to build the network structure, neurongroups, nodes and other assemblies is added to this assembly by calling add_assembly function, explicitly or implicitly.

Parameters:
  • name (str) – the attribute name of the added assembly

  • assembly (Assembly) – the assembly to be added

Returns:

None

Examples

if you want to add a new assembly to in assembly initialization, you can explicitly use add_assembly:

>>> def __init__(self, name=None):
>>>    ...
>>>    self.add_assembly(name='layer1', assembly=Assembly())

or implicitly use add_assembly:

>>> def __init__(self, name=None):
>>>     ...
>>>     self.layer1 = Assembly()

you can also add new assemblies after the target assembly has been created:

>>> TestAsb = Assembly()
>>> TestAsb.add_assembly(name='layer1', assembly=Assembly())
del_assembly(assembly=None, name=None)[source]

Delete an existed member assembly of this assembly.

User can delete by assembly object or delete by assembly name.

Parameters:
  • assembly (Assembly) – the member assembly to be deleted

  • name (str) – the member name of the assembly to be deleted

Returns:

None

Examples

User can delete member assembly by object:

>>> TestAsb = Assembly() # assuming contains neurongroups and network structure
>>> TestAsb.del_assembly(assembly=TestAsb.layer1)

User can also delete member assembly by the name:

>>> TestAsb = Assembly() # assuming contains neurongroups and network structure
>>> TestAsb.del_assembly(name='layer1')
add_connection(name, connection)[source]

Add the connection between two member assemblies of this assembly.

Parameters:
  • name (str) – name of this connection

  • connection (Connection) – the new connection to be added to the assembly

Returns:

None

Examples

>>> TestAsb = Assembly() # assuming contains neurongroups and network structure
>>> TestAsb.add_connection(name='con1', connection=Connection(self.layer1, self.layer2, link_type='full'))
del_connection(connection=None, name=None)[source]

Delete an existed connection between member assemblies of this assembly.

User can delete the connection by connection object or by connection name.

Parameters:
  • connection (Connection) – the connection object to be deleted

  • name (str) – the name of connection to be deleted

Returns:

None

Examples

Delete by object:

>>> TestAsb = Assembly() # assuming it contains neurongroups and network structure
>>> TestAsb.del_connection(connection=TestAsb.con1)

Delete by name:

>>> TestAsb = Assembly()
>>> TestAsb.del_connection(name='con1')
add_projection(name, projection)[source]

Add the projection between two member assemblies of this assembly.

Parameters:
  • name (str) – name of this projection

  • projection (Projection) – the new projection to be added to the assembly

Returns:

None

Examples

>>> TestAsb = Assembly() # assuming contains neurongroups and network structure
>>> TestAsb.add_projection(name='prj1', projection=Projection(self.layer1, self.layer2, link_type='full'))
copy_assembly(name, assembly)[source]

Copy an existed assembly structure into this assembly.

A new assembly is initialized which copy the structure(type and connection of the assembly members) of the original assembly, and the new assembly is added to this assembly.

Parameters:
  • name (str) – the name of the new copy assembly

  • assembly (Assembly) – the assembly object to be copied

Returns:

None

Examples

>>> Asb1 = Assembly() # assuming it contains neurongroups and network structure
>>> Asb2 = Assembly() # assuming it contains neurongroups and network structure
>>> Asb1.copy_assembly(name='layer2', assembly=Asb2)
replace_assembly(old_assembly, new_assembly)[source]

Replace an existed member assembly with a new assembly.

Delete the existed old member assembly, add the new assembly to this assembly, and redirect related connections from the old assembly to the new assembly.

Parameters:
  • old_assembly (Assembly) – the old member assembly to be replaced

  • new_assembly (Assembly) – the new assembly

Returns:

None

Examples

>>> templateAsb = Assembly() # assuming templateAsb contains a member assembly called asb1
>>> asb2 = Assembly() # assuming it contains neurongroups and network structure
>>> templateAsb.replace_assembly(templateAsb.asb1, asb2)
merge_assembly(assembly)[source]

Add the member assemblies and connections of the target assembly, which are not already included in this assembly, to this assembly.

Parameters:

assembly (Assembly) – the target assembly, from which this assembly will copy member assemblies and connections.

Returns:

None

Examples

>>> target_asb = Assembly()  # assuming it contains neurongroups and network structure
>>> test_asb = Assembly() # assuming it contains neurongroups and network structure
>>> test_asb.merge_assembly(target_asb)
select_assembly(assemblies, name=None, with_connection=True)[source]

Select a list of member assemblies in this assembly, and form a new assembly that contains those selected assemblies and their connections(if with_connection is True).

Parameters:
  • assemblies (List[Assembly]) – list of assemblies (or member assembly names) to be selected to form a new assembly

  • name – the name of the new assembly

Returns:

a new assembly that contains the selected assemblies and their connections (if with_coonection is True)

Examples

>>> testAsb = Assembly() # assuming it contains member assemblies named asb1, asb2, asb3...
>>> newAsb1 = testAsb.select_assembly(['asb1', 'asb2'], 'newAsb') # using names
>>> newAsb2 = testAsb.select_assembly([testAsb.asb2, testAsb.asb3], 'newAsb') # using assembly objects
assembly_hide()[source]

Prohibit this assembly from building and display, but keep this assembly for later use.

The set this assembly and its member assemblies with the flag hided = True.

Returns:

None

Examples

>>> TestAsb = Assembly()
>>> TestAsb.assembly_hide()
assembly_show()[source]

Make the hided assembly to normal assembly.

Returns:

None

Examples: >>> TestAsb = Assembly() # assuming hided >>> TestAsb.assembly_show()

get_groups(recursive=True)[source]

Get all member neurongroups and neurongroups in member assemblies in a list. :param recursive: flag to decide if members of the member assemblies should be returned. :type recursive: bool

Returns:

list of all member groups

get_leveled_groups()[source]

Get list of all sup groups in leveled order, such as [ [self], [subgroups], [subgroup of subgroups], …] Returns:

get_assemblies(recursive=True, include_empty=False)[source]

Get all the member assemblies and assemblies in member assemblies. :param recursive: flag to decide if members of the member assemblies should be returned. :type recursive: bool

Returns:

list of all member assemblies

get_assembly_key(assembly)[source]

Get the key of the target assembly if it is a member of this assembly :param assembly: the target assembly

Returns:

the key of target assembly

get_super_assemblies(assembly)[source]

Get all the super assembly of the target assembly if it is a member or member’s member of self assembly :param assembly: the target assembly

Returns:

list of super assemblies or []

get_connections(recursive=True)[source]

Get the Connections in this assembly

Parameters:

recursive (bool) – flag to decide if member connections of the member assemblies should be returned.

Returns:

List of Connections

update_connection(container, connections)[source]
get_var_names()[source]

Get a list of variable names the assembly member contains.

get_str(level)[source]

Get a string description of the strcuture of this assembly :param level: the deepth of this assembly relative to the top network

Returns:

String representations

build(backend=None, strategy=0)[source]

Build the front-end network structure into a back-end computation graph.

Parameters:

backend (Backend) – the backend backend to be builded into

Returns:

None

build_projections(backend)[source]
set_id()[source]

Get the ID of this assembly

register_connection(connection_obj, presynaptic)[source]

Register input or output connection of this assembly :param connection_obj: the connection :type connection_obj: Connection.Connection :param presynaptic: if this assembly is presynaptic neuron :type presynaptic: bool

Returns:

None

register_module(module_obj, pre)[source]
structure_copy(name=None)[source]

Copy the structure of this assembly with new members :param name: name of the new Assembly

Returns:

the new assembly

add_super(assembly)[source]

Tell this assemlby the target assembly is it’s super assembly :param assembly: the target super assembly

del_super(assembly)[source]

Delete certain assembly from super assemblies of this assembly :param assembly: the target super assembly

train(mode=True)[source]
eval()[source]

spaic.Network.BaseModule module

Created on 2020/9/9 @project: SPAIC @filename: BaseModule @author: Hong Chaofei @contact: hongchf@gmail.com

@description:

class spaic.Network.BaseModule.BaseModule[source]

Bases: object

Base class for all snn modules (assemblies, connection, learner, monitor, piplines).

abstract build(backend)[source]
abstract get_str(level)[source]
set_name(given_name)[source]
set_id()[source]
set_build_level(level)[source]
variable_to_backend(name, shape, value=None, is_parameter=False, is_sparse=False, init=None, init_param=None, min=None, max=None, is_constant=False, prefer_device=None)[source]
op_to_backend(outputs: list, func: callable, inputs: list)[source]
init_op_to_backend(outputs, func, inputs, prefer_device=0)[source]
get_full_name(name)[source]
get_value(name)[source]
set_value(name, value)[source]
class spaic.Network.BaseModule.VariableAgent(backend, var_name, is_parameter=False, dict_label=None)[source]

Bases: object

property var_name
new_labeled_agent(dict_label)[source]
property value
class spaic.Network.BaseModule.OperationCommand(front_module, output, function, input)[source]

Bases: object

property enabled

spaic.Network.Connection module

Base class for all kinds of connections, including full connection, sparse connection, conv connection,…. Ten connection methods are provided, as shown below (key: class):

‘full’, FullConnection ‘one_to_one_sparse’, one_to_one_sparse ‘one_to_one’, one_to_one_mask ‘conv’, conv_connect ‘sparse_connection_sparse’, sparse_connect_sparse ‘sparse_connection’, sparse_connect_mask ‘random_connection_sparse’, random_connect_sparse ‘random_connection’, random_connect_mask

param pre:

the assembly which needs to be connected.

type pre:

Assembly

param post:

the assembly which needs to connect the pre.

type post:

Assembly

param link_type:

the type for connection: full, sparse, conv…

type link_type:

str

ivar pre_group:

the neuron group which need to be connected in the pre.

vartype pre_group:

groups

ivar post_group:

the neuron group which need to connect with pre_group neuron.

vartype post_group:

groups

ivar _var_names:

a list contain variable names.

vartype _var_names:

list

spaic.Network.Connection.__new__()

before build a new connection, do some checks.

spaic.Network.Connection.get_var_names()

get variable names.

spaic.Network.Connection.register()

register a connection class.

spaic.Network.Connection.build()

add the connection variable, variable name and opperation to the backend.

spaic.Network.Connection.get_str()
spaic.Network.Connection.condition_check()

check whether the pre_group.type is equal to the post_group.type, only if they are equal, return flag=Ture.

spaic.Network.Connection.connect()

connect the preg with postg.

spaic.Network.Connection.get_weight_name()

give a name for each connection weight.

spaic.Network.Connection.get_post_name()

give a name for each post group.

spaic.Network.Connection.get_input_name()

give a name for each input group.

Examples

when building the network: self.connection1 = spaic.Connection(self.input, self.layer1, link_type=’full’)

spaic.Network.ConnectionPolicy module

spaic.Network.Construct module

spaic.Network.DelayQueue module

Created on 2021/4/1 @project: SPAIC @filename: DelayQueue @author: Hong Chaofei @contact: hongchf@gmail.com

@description: 定义网络传递延迟的基本组件

class spaic.Network.DelayQueue.DelayQueue(var_name=None, max_len=None, backend=None)[source]

Bases: object

static register(name, deque_class)[source]

Register a DelayQueue class. Registered DelayQueue classes can be referred to # via their name.

Parameters:
  • name (str) – A short name for the backend (e.g. ‘pytorch’)

  • deque_class (ConnectionModel) – The subclass of Delaydeque object.

abstract push(input)[source]
abstract select(delay)[source]
abstract initial(var)[source]
class spaic.Network.DelayQueue.TorchDelayQueue(var_name=None, max_len=None, backend=None)[source]

Bases: DelayQueue

initial(var=None, batch_size=1)[source]
transform_delay_output(input, delay)[source]
push(input)[source]
select(delay: Tensor)[source]
spaic.Network.DelayQueue.test_queue()[source]

spaic.Network.Network module

Created on 2020/8/5 @project: SPAIC @filename: Network @author: Hong Chaofei @contact: hongchf@gmail.com

@description: 定义网络以及子网络,网络包含所有的神经网络元素、如神经集群、连接以及学习算法、仿真器等,实现最终的网络仿真与学习。 执行过程:网络定义->网络生成->网络仿真与学习

class spaic.Network.Network.Network(name=None)[source]

Bases: Assembly

set_backend(backend=None, device='cpu', partition=False)[source]
set_backend_dt(dt=0.1, partition=False)[source]
set_random_seed(seed)[source]
get_testparams()[source]
add_learner(name, learner)[source]
build(backend=None, strategy=0, full_enable_grad=None, device=None)[source]
forward_build(all_groups=None, all_connections=None)[source]
deep_forward_build(target, all_groups, all_connections, builded_groups, builded_connections)[source]
run(backend_time)[source]
run_continue(backend_time)[source]
reset()[source]
enable_full_grad(requires_grad=True)[source]
init_run()[source]
add_monitor(name, monitor)[source]
get_elements()[source]
save_state(filename=None, direct=None, save=True, hdf5=False)[source]

Save weights in memory or on hard disk.

Parameters:
  • filename – The name of saved file.

  • direct – Target direction for saving state.

  • mode – Determines whether saved in hard disk, default set false, it means will not save on disk.

Returns:

Connections’ weight of the network.

Return type:

state

state_from_dict(state=False, filename=None, direct=None, device=None)[source]

Reload states from memory or disk.

Parameters:
  • state – contains backend._parameters_dict .

  • filename – The name of saved file.

  • direct – Target direction for reloading state.

  • mode – Determines whether saved in hard disk, default set false, it means will not save on disk.

Returns:

Connections’ weight of the network.

Return type:

state

Module contents

Created on 2020/8/11 @project: SPAIC @author: Hong Chaofei @contact: hongchf@gmail.com

@description: