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:
BaseModuleBase 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.
- 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_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:
- 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
- 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
- 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
- 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
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:
objectBase class for all snn modules (assemblies, connection, learner, monitor, piplines).
- class spaic.Network.BaseModule.VariableAgent(backend, var_name, is_parameter=False, dict_label=None)[source]
Bases:
object- property var_name
- property value
- class spaic.Network.BaseModule.OperationCommand(front_module, output, function, input)[source]
Bases:
object- property enabled
- class spaic.Network.BaseModule.Op(output: ~typing.Optional[~typing.List] = <factory>, func_name: ~typing.Optional[str] = None, input: ~typing.Optional[~typing.List] = <factory>, place: ~typing.Optional[~typing.Any] = None, owner: ~typing.Optional[~spaic.Network.BaseModule.BaseModule] = None, requires_grad: ~typing.Optional[bool] = False, operation_type: ~typing.Optional[str] = None, func: ~typing.Optional[~typing.Any] = None)[source]
Bases:
objectOperation data class.
- output: Optional[List]
- func_name: Optional[str] = None
- input: Optional[List]
- place: Optional[Any] = None
- owner: Optional[BaseModule] = None
- requires_grad: Optional[bool] = False
- operation_type: Optional[str] = None
- func: Optional[Any] = None