1.4. RectiPy.rectipy.network

Module that contains rectipy.Network, the main user interface of RectiPy.

class rectipy.network.FeedbackNetwork(dt: float, device: str = 'cpu')[source]

Bases: Network

add_edge(source: str, target: str, weights: Tensor | ndarray | None = None, train: str | None = None, feedback: bool = False, dtype: dtype = torch.float64, edge_attrs: dict | None = None, **kwargs) Linear[source]

Add a feed-forward layer to the network.

Parameters:
  • source – Label of the source node.

  • target – Label of the target node.

  • weightsk x n weight matrix that realizes the linear projection of the n source outputs to the k target inputs.

  • train – Can be used to make the edge weights trainable. The following options are available: - None for a static edge - ‘gd’ for training of the edge weights via standard pytorch gradient descent - ‘rls’ for recursive least squares training of the edge weights

  • feedback – If true, this edge is treated as a feedback edge, meaning that it does not affect the feedforward path that connects the network input to its output.

  • dtype – Data type of the edge weights.

  • edge_attrs – Additional edge attributes passed to networkx.DiGraph.add_edge.

  • kwargs – Additional keyword arguments to be passed to the edge class initialization method.

Returns:

Instance of the edge class.

Return type:

Linear

compile()[source]

Automatically detects a forward pass through the network based on the nodes and edges in the network.

get_edge(source: str, target: str) Linear[source]

Returns edge instance from the network.

Parameters:
  • source – Name of the source node.

  • target – Name of the target node.

Returns:

Instance of the edge class.

Return type:

Linear

get_node(node: str) InstantNode | RateNet[source]

Returns node instance from the network.

Parameters:

node – Name of the node.

Returns:

Instance of a node class.

Return type:

Union[InstantNode, RateNet]

parameters(recurse: bool = True) Iterator[source]

Yields the trainable parameters of the network model.

Parameters:

recurse – If true, yields parameters of all submodules.

Yields:

Iterator – Trainable model parameters.

class rectipy.network.Network(dt: float, device: str = 'cpu')[source]

Bases: Module

Main user interface for initializing, training, testing, and running networks consisting of rnn, input, and output layers.

add_diffeq_node(label: str, node: str | NodeTemplate | CircuitTemplate, input_var: str, output_var: str, weights: ndarray | None = None, source_var: str | None = None, target_var: str | None = None, spike_var: str | list | None = None, reset_var: str | list | None = None, reset: bool = True, op: str | None = None, train_params: list | None = None, **kwargs) RateNet[source]

Adds a differential equation-based RNN node to the Network instance.

Parameters:
  • label – The label of the node in the network graph.

  • node – Path to the YAML template or an instance of a pyrates.NodeTemplate.

  • input_var – Name of the parameter in the node equations that input should be projected to.

  • output_var – Name of the variable in the node equations that should be used as output of the RNN node.

  • weights – Determines the number of neurons in the network as well as their connectivity. Given an N x N weights matrix, N neurons will be added to the RNN node, each of which is governed by the equations defined in the NodeTemplate (see argument node). Neurons will be labeled n0 to n<N> and every non-zero entry in the matrix will be realized by an edge between the corresponding neurons in the network.

  • source_var – Source variable that will be used for each connection in the network.

  • target_var – Target variable that will be used for each connection in the network.

  • spike_var – Name of the input variable in the node equations that recurrent input from the RNN should be projected to.

  • reset_var – Either the name of the input variable in the node equations that is used for membrane potential resetting after spiking within the node equations (if reset=False) or the name of the state variable that should be reset after a spike (if reset=True).

  • reset – If true, an additional spike resetting mechanism is added that will reset the variable defined by reset_var after a spike occurred.

  • op – Name of the operator in which all the above variables can be found. If not provided, it is assumed that the operator name is provided together with the variable names, e.g. source_var = <op>/<var>.

  • train_params – Names of all RNN parameters that should be made available for optimization.

  • kwargs – Additional keyword arguments provided to the RNNLayer (or SRNNLayer in case of spiking neurons).

Returns:

Instance of the RNN node that was added to the network.

Return type:

RateNet

add_edge(source: str, target: str, weights: Tensor | ndarray | None = None, train: str | None = None, dtype: dtype = torch.float64, edge_attrs: dict | None = None, **kwargs) Linear[source]

Add a feed-forward layer to the network.

Parameters:
  • source – Label of the source node.

  • target – Label of the target node.

  • weightsk x n weight matrix that realizes the linear projection of the n source outputs to the k target inputs.

  • train – Can be used to make the edge weights trainable. The following options are available: - None for a static edge - ‘gd’ for training of the edge weights via standard pytorch gradient descent - ‘rls’ for recursive least squares training of the edge weights

  • dtype – Data type of the edge weights.

  • edge_attrs – Additional edge attributes passed to networkx.DiGraph.add_edge.

  • kwargs – Additional keyword arguments to be passed to the edge class initialization method.

Returns:

Instance of the edge class.

Return type:

Linear

add_func_node(label: str, n: int, activation_function: str, **kwargs) InstantNode[source]

Add an activation function as a node to the network (no intrinsic dynamics, just an input-output mapping).

Parameters:
  • label – The label of the node in the network graph.

  • n – Dimensionality of the node.

  • activation_function – Activation function applied to the output of the last layer. Valid options are: - ‘tanh’ for torch.nn.Tanh() - ‘sigmoid’ for torch.nn.Sigmoid() - ‘softmax’ for torch.nn.Softmax(dim=0) - ‘softmin’ for torch.nn.Softmin(dim=0) - ‘log_softmax’ for torch.nn.LogSoftmax(dim=0) - ‘identity’ for torch.nn.Identity

Returns:

The node of the network graph.

Return type:

ActivationFunc

add_node(label: str, node: InstantNode | RateNet, node_type: str, op: str | None = None, **node_attrs) None[source]

Add node to the network, based on an instance from rectipy.nodes.

Parameters:
  • label – Name of the node in the network graph.

  • node – Instance of a class from rectipy.nodes.

  • node_type – Type of the node. Should be set to “diff_eq” for nodes that contain differential equations.

  • op – For differential equation-based nodes, an operator name can be passed that is used to identify variables on the node.

  • node_attrs – Additional keyword arguments passed to networkx.DiGraph.add_node.

Return type:

None

clear()[source]

Removes all nodes and edges from the network

compile()[source]

Automatically detects a forward pass through the network based on the nodes and edges in the network.

detach(requires_grad: bool = True, detach_params: bool = False) None[source]

Goes through all DE-based nodes and detaches their state variables from the current graph for gradient calculation.

Parameters:
  • requires_grad – If true, all tensors that will be detached will be set to require gradient calculation after detachment.

  • detach_params – If true, parameters that require gradient calculation will be detached as well.

Return type:

None

fit_bptt(inputs: ~numpy.ndarray | list, targets: [<class 'numpy.ndarray'>, <class 'list'>], optimizer: str = 'sgd', optimizer_kwargs: dict | None = None, loss: str = 'mse', loss_kwargs: dict | None = None, lr: float = 0.001, sampling_steps: int = 1, update_steps: int = 100, verbose: bool = True, **kwargs) Observer[source]

Optimize model parameters via backpropagation through time.

Parameters:
  • inputsT x m array of inputs fed to the model, where`T` is the number of training steps and m is the number of input dimensions of the network.

  • targetsT x k array of targets, where T is the number of training steps and k is the number of outputs of the network.

  • optimizer – Name of the optimization algorithm to use. Available options are: - ‘sgd’ for torch.optim.SGD - ‘adam’ for torch.optim.Adam - ‘adamw’ for torch.optim.AdamW - ‘adagrad’ for torch.optim.Adagrad - ‘adadelta’ for torch.optim.Adadelta - ‘rmsprop’ for torch.optim.RMSprop - ‘rprop’ for torch.optim.Rprop

  • optimizer_kwargs – Additional keyword arguments provided to the initialization of the optimizer.

  • loss – Name of the loss function that should be used for optimization. Available options are: - ‘mse’ for torch.nn.MSELoss - ‘l1’ for torch.nn.L1Loss - ‘nll’ for torch.nn.NLLLoss - ‘ce’ for torch.nn.CrossEntropyLoss - ‘kld’ for torch.nn.KLDivLoss - ‘hinge’ for torch.nn.HingeEmbeddingLoss

  • loss_kwargs – Additional keyword arguments provided to the initialization of the loss.

  • lr – Learning rate.

  • sampling_steps – Number of training steps at which to record observables.

  • update_steps – Number of training steps after which to perform an update of the trainable parameters based on the accumulated gradients.

  • verbose – If true, the training progress will be displayed.

  • kwargs – Additional keyword arguments used for the optimization, loss calculation and observation.

Returns:

Instance of the observer.

Return type:

Observer

fit_eprop(inputs: ndarray, targets: ndarray, feedback_weights: ndarray | None = None, epsilon: float = 0.99, delta: float = 0.9, update_steps: int = 1, sampling_steps: int = 100, verbose: bool = True, **kwargs) Observer[source]

Reinforcement learning algorithm that implements slow adjustment of the feedback weights to the RNN layer based on a running average of the residuals.

Parameters:
  • inputsT x m array of inputs fed to the model, where`T` is the number of training steps and m is the number of input dimensions of the network.

  • targetsT x k array of targets, where T is the number of training steps and k is the number of outputs of the network.

  • feedback_weightsm x k array of synaptic weights. If provided, a feedback connections is established with these weights, that projects the network output back to the RNN layer.

  • epsilon – Scalar in (0, 1] that controls how quickly the loss used for reinforcement learning can change.

  • delta – Scalar in (0, 1] that controls how quickly the feedback weights can change.

  • update_steps – Each update_steps an update of the trainable parameters will be performed.

  • sampling_steps – Number of training steps at which to record observables.

  • verbose – If true, the training progress will be displayed.

  • kwargs – Additional keyword arguments used for the optimization, loss calculation and observation.

Returns:

Instance of the observer.

Return type:

Observer

fit_ridge(inputs: ndarray, targets: ndarray, sampling_steps: int = 100, alpha: float = 0.0001, verbose: bool = True, add_readout_node: bool = True, **kwargs) Observer[source]

Train readout weights on top of the input-driven model dynamics via ridge regression.

Parameters:
  • inputsT x m array of inputs fed to the model, where`T` is the number of training steps and m is the number of input dimensions of the network.

  • targetsT x k array of targets, where T is the number of training steps and k is the number of outputs of the network.

  • sampling_steps – Number of training steps at which to record observables.

  • alpha – Ridge regression regularization constant.

  • verbose – If true, the training progress will be displayed.

  • add_readout_node – If true, a readout node is added to the network, which will be connected to the current output node of the network via the trained readout weights.

  • kwargs – Additional keyword arguments used for the observation and network simulations.

Returns:

Instance of the observer.

Return type:

Observer

fit_rls(inputs: list | ndarray, targets: list | ndarray, update_steps: int = 1, sampling_steps: int = 100, verbose: bool = True, **kwargs) Observer[source]

Finds model parameters $w$ such that $||Xw - y||_2$ is minimized, where $X$ contains the neural activity and $y$ contains the targets.

Parameters:
  • inputsT x m array of inputs fed to the model, where`T` is the number of training steps and m is the number of input dimensions of the network.

  • targetsT x k array of targets, where T is the number of training steps and k is the number of outputs of the network.

  • update_steps – Each update_steps an update of the trainable parameters will be performed.

  • sampling_steps – Number of training steps at which to record observables.

  • verbose – If true, the training progress will be displayed.

  • kwargs – Additional keyword arguments used for the optimization, loss calculation and observation.

Returns:

Instance of the observer.

Return type:

Observer

forward(x: Tensor | ndarray) Tensor[source]

Forward method as implemented for any torch.Module.

Parameters:

x – Input tensor.

Returns:

Output tensor.

Return type:

torch.Tensor

get_edge(source: str, target: str) Linear[source]

Returns edge instance from the network.

Parameters:
  • source – Name of the source node.

  • target – Name of the target node.

Returns:

Instance of the edge class.

Return type:

Linear

get_node(node: str) InstantNode | RateNet[source]

Returns node instance from the network.

Parameters:

node – Name of the node.

Returns:

Instance of a node class.

Return type:

Union[InstantNode, RateNet]

get_var(node: str, var: str) Tensor | float[source]

Returns variable from network node.

Parameters:
  • node – Name of the network node.

  • var – Name of the node variable.

Return type:

Union[torch.Tensor, float]

property n_in: int

Current input dimensionality of the network.

property n_out: int

Current output dimensionality.

property nodes: NodeView

Network nodes

parameters(recurse: bool = True) Iterator[source]

Yields the trainable parameters of the network model.

Parameters:

recurse – If true, yields parameters of all submodules.

Yields:

Iterator – Trainable model parameters.

pop_edge(source: str, target: str) Linear[source]

Removes (and returns) an edge from the network.

Parameters:
  • source – Name of the source node.

  • target – Name of the target node.

Returns:

Removed edge.

Return type:

Linear

pop_node(node: str) InstantNode | RateNet[source]

Removes (and returns) a node from the network.

Parameters:

node – Name of the node to remove.

Returns:

Removed node.

Return type:

Union[InstantNode, RateNet]

reset(state: dict | None = None)[source]

Reset the network state.

Parameters:

state – Optional dictionary, that contains state-vectors (values) for nodes of the network (keys).

Return type:

None

run(inputs: ndarray | Tensor, sampling_steps: int = 1, cutoff: int = 0, verbose: bool = True, enable_grad: bool = True, **kwargs) Observer[source]

Perform numerical integration of the input-driven network equations.

Parameters:
  • inputs

    T x m array of inputs fed to the model, where`T` is the number of integration steps and m is the number

    of input dimensions of the network.

  • sampling_steps – Number of integration steps at which to record observables.

  • cutoff – Initial number of simulation steps to disregard.

  • verbose – If true, the progress of the integration will be displayed.

  • enable_grad – If true, the simulation will be performed with gradient calculation.

  • kwargs – Additional keyword arguments used for the observation.

Returns:

Instance of the Observer.

Return type:

Observer

set_var(node: str, var: str, val: Tensor | float)[source]

Set the value of a network node variable.

Parameters:
  • node – Name of the network node.

  • var – Name of the node variable.

  • val – New variable value.

Return type:

None

property state: dict

Dictionary containing the state vectors of each differential equation node in the network.

test(inputs: ndarray, targets: ndarray, loss: str = 'mse', loss_kwargs: dict | None = None, sampling_steps: int = 100, verbose: bool = True, **kwargs) tuple[source]

Test the model performance on a set of inputs and target outputs, with frozen model parameters.

Parameters:
  • inputsT x m array of inputs fed to the model, where`T` is the number of testing steps and m is the number of input dimensions of the network.

  • targetsT x k array of targets, where T is the number of testing steps and k is the number of outputs of the network.

  • loss – Name of the loss function that should be used to calculate the loss on the test data. See Network.train for available options.

  • loss_kwargs – Additional keyword arguments provided to the initialization of the loss.

  • sampling_steps – Number of testing steps at which to record observables.

  • verbose – If true, the progress of the test run will be displayed.

  • kwargs – Additional keyword arguments used for the loss calculation and observation.

Returns:

The Observer instance and the total loss on the test data.

Return type:

Tuple[Observer,float]