Networks
This module contains the classes to define and create neural network objects.
The abstract class NeuralNetwork
represents a generic computational graph with layers as nodes and connections as edges.
The children of NeuralNetwork
are SequentialNetwork
and AcyclicNetwork
, to represent standard feed-forward NNs and residual NNs, respectively.
- class pynever.networks.NeuralNetwork(identifier, input_ids)[source]
Bases:
ABC
An abstract class used for our internal representation of a generic Neural Network. It consists of a graph of
LayerNode
objects and edges connecting them. It should be noted that this data structure is not able to compute the input-output relation defined by the network. The properties of the computational graph are specialized in the concrete classes.- nodes[source]
Dictionary containing string keys and
LayerNode
values. It contains the nodes of the graph, the identifier of the node is used as a key in the nodes’ dictionary.- Type:
dict[str, LayerNode]
- edges[source]
Dictionary of
LayerNode
identifiers. It contains for each node identified by the keys, the list of nodes connected to it.- Type:
dict[str, list[str]]
- input_ids[source]
Dictionary containing the inputs of the networks as keys and the corresponding layer identifier of the node of which they are the input.
- Type:
dict[str, str | None]
- is_empty()[source]
Procedure to check whether the network is empty.
- Returns:
True if there are no nodes in the network, False otherwise.
- Return type:
bool
- is_acyclic()[source]
Procedure to check whether the network is acyclic.
- Returns:
True if the network is acyclic, False otherwise.
- Return type:
bool
- has_children(node)[source]
Procedure to check if a node has children.
- Parameters:
node (ConcreteLayerNode) – The node of which the existence of its children should be checked.
- Returns:
True if the node has children, False otherwise.
- Return type:
bool
- get_children(node)[source]
Procedure to return the children of a node as a list of ConcreteLayerNodes.
- Parameters:
node (ConcreteLayerNode) – The node whose children should be returned.
- Returns:
The children of the node passed as argument.
- Return type:
list[ConcreteLayerNode]
- has_parents(node)[source]
Procedure to check if a node has parents.
- Parameters:
node (ConcreteLayerNode) – The node of which the existence of its parents should be checked.
- Returns:
True if the node has parents, False otherwise.
- Return type:
bool
- get_parents(node)[source]
Procedure to return the parents of a node as a list of ConcreteLayerNodes.
- Parameters:
node (ConcreteLayerNode) – The node whose parents should be returned
- Returns:
The parents of the node passed as argument.
- Return type:
list[ConcreteLayerNode]
- get_input_id()[source]
Procedure to return the input_id of the network, assuming there is a single input layer.
- Returns:
The input_id of the network.
- Return type:
str
- get_roots()[source]
Procedure to return the roots of the network as a list of ConcreteLayerNodes.
- Returns:
The roots of the network as a list of
ConcreteLayerNode
objects.- Return type:
list[ConcreteLayerNode]
- get_leaves()[source]
Procedure to return the leaves of the network as a list of ConcreteLayerNodes.
- Returns:
The leaves of the network as a list of
ConcreteLayerNode
objects.- Return type:
list[ConcreteLayerNode]
- get_topological_order(reverse=False)[source]
Procedure to walk the network with a DFS and build the topological sort.
- Parameters:
reverse (bool) – Flag to reverse the order
- Returns:
The topological sort of the network nodes identifiers as a stack.
- Return type:
list[str]
- layers_iterator(offset=0)[source]
Procedure to build a generator for the layers of the network in sequential order. It allows having an iterable interface when needed
- Parameters:
offset (int) – Offset to start the generation
- Returns:
The generator object
- Return type:
Generator[ConcreteLayerNode | None, None]
- get_first_node()[source]
Procedure to get the first ConcreteLayerNode of the network.
- Returns:
The first node of the network.
- Return type:
- get_next_node(node)[source]
Procedure to get the next ConcreteLayerNode of the network given an input ConcreteLayerNode.
- Returns:
The next node of the network.
- Return type:
- get_previous_node(node)[source]
Procedure to get the previous ConcreteLayerNode of the network given an input ConcreteLayerNode.
- Returns:
The previous node in the network.
- Return type:
ConcreteLaterNode
- get_last_node()[source]
Procedure to get the last ConcreteLayerNode of the network.
- Returns:
The last node of the network.
- Return type:
- get_input_len()[source]
Procedure to count the number of inputs in in_dim
- Returns:
The number of single inputs
- Return type:
int
- get_output_len()[source]
Procedure to count the number of outputs in out_dim
- Returns:
The number of single outputs
- Return type:
int
- get_id_from_index(index)[source]
This method returns the identifier of the layer at the given index
- Parameters:
index (int) – Index of the layer to return
- Returns:
The identifier of the layer at the given index
- Return type:
str
- get_index_from_id(identifier)[source]
This method returns the index of the layer with the given identifier
- Parameters:
identifier (str) – Identifier of the layer to return
- Returns:
The index of the layer with the given identifier
- Return type:
int
- get_previous_id(identifier)[source]
Procedure to get the identifier of the previous layer given another layer identifier
- Parameters:
identifier (str) – Identifier of the layer
- Returns:
The identifier of the previous layer
- Return type:
str
- layer_precedes(layer_id1, layer_id2)[source]
Procedure to check whether a given layer precedes another or not
- Parameters:
layer_id1 (str) – Identifier of the first layer
layer_id2 (str) – Identifier of the second layer
- Returns:
True if layer1 precedes layer2, False otherwise
- Return type:
bool
- generic_add_node(node, parents=None, children=None, input_ids=None)[source]
Procedure to add a node to the network. A node cannot have both parents and inputs.
- Parameters:
node (ConcreteLayerNode) – The node to be added to the network.
parents (list[ConcreteLayerNode] | None) – The parents of the node. (Optional)
children (list[ConcreteLayerNode] | None) – The children of the node. (Optional)
input_ids (list[ConcreteLayerNode] | None) – The inputs of the node. (Optional)
- remove_node(node)[source]
Procedure to remove a node from the network.
- Parameters:
node (ConcreteLayerNode) – The node to be removed.
- class pynever.networks.SequentialNetwork(identifier, input_id)[source]
Bases:
NeuralNetwork
Concrete child of NeuralNetwork representing a sequential NeuralNetwork. It consists of a graph of ConcreteLayerNodes. It should be noted that this data structure is not able to compute the input-output relation defined by the network. The computational graph of a SequentialNetwork must correspond to a standard list.
- append_node(node)[source]
Procedure to add a new ConcreteLayerNode. In a sequential network the new node must be connected directly to the previous node forming a list.
- Parameters:
node (ConcreteLayerNode) – New node to add to the
SequentialNetwork
.
- class pynever.networks.AcyclicNetwork(identifier, input_ids)[source]
Bases:
NeuralNetwork
Concrete child of NeuralNetwork representing an acyclic NeuralNetwork. The computational graph of an AcyclicNetwork must correspond to a standard list.
- add_node(node, parents=None, children=None)[source]
Procedure to add a node to the network only if it preserves the acyclic property
- Parameters:
node (ConcreteLayerNode) – The
LayerNode
to addparents (list[ConcreteLayerNode] | None) – The new node parents, if existing
children (list[ConcreteLayerNode] | None) – The new node children, if existing