Skip to content

Neural networks

Neural networks: Hopfield memories, reservoir computers, and Elman's network.

The Hopfield memories include its diluted version, and Elman's network is recurrent.

BinaryWeightElement(label, active=0.0, size=None)

Bases: Element

Weight of a binary perceptron.

The state 1 stands for +1 and the state 0 for -1.

Parameters:

Name Type Description Default
label str

The label of the weight or the group.

required
active Annotated[object, FiniteEnumeratedSet([0, 1])]

Whether the weight is +1.

0.0
size int | None

The number of weights, or None for one.

None

Examples:

>>> from skcomplex.systems.computer_science.neural_networks import BinaryWeightElement
>>> BinaryWeightElement('weight', active=1.0).active
1.0
Source code in src/skcomplex/systems/computer_science/neural_networks/_perceptron.py
270
271
272
273
274
275
276
277
def __init__(
    self,
    label: str,
    active: Annotated[object, FiniteEnumeratedSet([0, 1])] = 0.0,
    size: int | None = None,
) -> None:
    super().__init__(label, size=size)
    self.active = active

CardinalitySpec(literals, bound, sense='at-least') dataclass

Bases: BaseSpec

Constraint that at least or at most a bound of its literals hold.

A literal is a member index from 1, negative for its negation.

Parameters:

Name Type Description Default
literals Sequence[int]

The literals of the constraint.

required
bound int

The number of literals that bounds those that hold.

required
sense str

'at-least' or 'at-most'.

'at-least'

Raises:

Type Description
ValidationError

A literal is 0, the bound lies outside the number of literals, or the sense is unknown, raised when its owner checks it.

Examples:

>>> from skcomplex.systems.computer_science.neural_networks import CardinalitySpec
>>> CardinalitySpec((1, -2, 3), 2).sense
'at-least'

ClauseSpec(literals) dataclass

Bases: BaseSpec

Disjunction of literals.

A literal is a member index from 1, negative for its negation.

Parameters:

Name Type Description Default
literals Sequence[int]

The literals of the clause.

required

Raises:

Type Description
ValidationError

A literal is 0 or the clause holds none, raised when its owner checks it.

Examples:

>>> from skcomplex.systems.computer_science.neural_networks import ClauseSpec
>>> ClauseSpec((1, -2)).literals
(1, -2)

DilutedHopfieldSystem(n_neurons=4000, *, connectivity=50.0, load=0.2, temperature_scale=3.0, overlap=0.3, random_state=0, label='system', solver='auto', backend='auto')

Bases: BaseSystem

The asymmetric diluted Hopfield network.

It recalls a stored pattern below its memory capacity. The network stores p = load K random patterns in the couplings J_ij = C_ij Σ_μ ξ_i ξ_j, where every C_ij is 1 with probability K / N independently of C_ji, so the couplings are sparse and asymmetric. All neurons update together at the inverse temperature β = temperature_scale / K. Started with an overlap m with the first pattern, the overlap follows m(t + 1) = f(m(t)) for large K, which keeps a nonzero fixed point below the critical load 2 / π. The system has the values neurons.spin and the observable overlap.

Parameters:

Name Type Description Default
n_neurons int

The number of neurons N.

4000
connectivity float

The mean number K of inputs of a neuron.

50.0
load float

The ratio p / K of the number of patterns to the connectivity.

0.2
temperature_scale float

The product β K of the inverse temperature and the connectivity.

3.0
overlap float

The initial overlap with the first pattern.

0.3
random_state int | Generator | None

The seed or generator of the patterns, the dilution, and the initial values.

0
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.computer_science.neural_networks import DilutedHopfieldSystem
>>> DilutedHopfieldSystem(200, connectivity=10).prepare().system_.elements[0].size
200
Source code in src/skcomplex/systems/computer_science/neural_networks/_diluted.py
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
def __init__(
    self,
    n_neurons: int = 4000,
    *,
    connectivity: float = 50.0,
    load: float = 0.2,
    temperature_scale: float = 3.0,
    overlap: float = 0.3,
    random_state: int | np.random.Generator | None = 0,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.n_neurons = n_neurons
    self.connectivity = connectivity
    self.load = load
    self.temperature_scale = temperature_scale
    self.overlap = overlap
    self.random_state = random_state

DilutedNeuronElement(label, *, spin=1.0, pattern=1.0, beta=0.06, size=None)

Bases: Element

Binary neuron of an asymmetric diluted Hopfield network.

It is updated in parallel with the others at every step.

The logit choice of DilutedHopfieldSystem draws the value s = ±1 of the neuron with probability 1 / (1 + exp(-2 s β h)), where h = Σ_j J_ij s_j is its local field, so its mean is tanh(β h).

Parameters:

Name Type Description Default
label str

The label of the neuron or the group.

required
spin Annotated[object, ZZ]

The initial value, -1 or 1.

1.0
pattern object

The value of the first stored pattern at the neuron, whose overlap with the network is recorded.

1.0
beta Annotated[float, closed_open(0, oo)]

The inverse temperature β.

0.06
size int | None

The number of neurons, or None for one.

None

Examples:

>>> from skcomplex.systems.computer_science.neural_networks import DilutedNeuronElement
>>> DilutedNeuronElement('neuron').beta
0.06
Source code in src/skcomplex/systems/computer_science/neural_networks/_diluted.py
58
59
60
61
62
63
64
65
66
67
68
69
70
def __init__(
    self,
    label: str,
    *,
    spin: Annotated[object, ZZ] = 1.0,
    pattern: object = 1.0,
    beta: Annotated[float, RealSet.closed_open(0, oo)] = 0.06,
    size: int | None = None,
) -> None:
    super().__init__(label, size=size)
    self.spin = spin
    self.pattern = pattern
    self.beta = beta

EchoStateNetworkSystem(n_units=200, *, spectral_radius=0.9, leak=0.3, connectivity=0.1, input_scaling=1.0, amplitude=1.0, frequency=0.2, random_state=0, label='system', solver='auto', backend='auto')

Bases: BaseSystem

An echo state network, a reservoir of leaky tanh units driven by a sine.

The recurrent weights are sparse and Gaussian, scaled to a spectral radius below 1, so the reservoir has the echo state property: its activities forget their initial values and become a function of the input history alone. A readout trained on these activities, as skcomplex.core.analysis.reservoir does, turns the reservoir into a forecaster. The system has the group units of activities r, one per unit.

Parameters:

Name Type Description Default
n_units int

The number of units.

200
spectral_radius float

The largest modulus of the eigenvalues of the recurrent weights.

0.9
leak float

The leak rate of every unit, 1 for units without memory of their own.

0.3
connectivity float

The share of the recurrent weights that are not zero.

0.1
input_scaling float

The scale of the input weights, drawn uniformly from [-input_scaling, input_scaling].

1.0
amplitude float

The amplitude of the sine input.

1.0
frequency float

The angular frequency of the sine input, per step.

0.2
random_state int | Generator | None

The seed or generator of the weights and of the initial activities.

0
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.computer_science.neural_networks import EchoStateNetworkSystem
>>> EchoStateNetworkSystem(50).prepare().system_.elements[0].size
50
Source code in src/skcomplex/systems/computer_science/neural_networks/_echo_state.py
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
def __init__(
    self,
    n_units: int = 200,
    *,
    spectral_radius: float = 0.9,
    leak: float = 0.3,
    connectivity: float = 0.1,
    input_scaling: float = 1.0,
    amplitude: float = 1.0,
    frequency: float = 0.2,
    random_state: int | np.random.Generator | None = 0,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.n_units = n_units
    self.spectral_radius = spectral_radius
    self.leak = leak
    self.connectivity = connectivity
    self.input_scaling = input_scaling
    self.amplitude = amplitude
    self.frequency = frequency
    self.random_state = random_state

ElmanNetworkSystem(n_hidden=4, *, n_bits=3000, window=12, batch_size=16, learning_rate=0.05, truncation=None, random_state=0, label='system', solver='auto', backend='auto')

Bases: BaseSystem

Elman's simple recurrent network.

It learns to predict the next bit of a temporal XOR stream. The nested discrete map net holds one input unit, n_hidden hidden units, and one output unit. The input sets the drive u = D x of the hidden units, the hidden units take s <- sigmoid(A s + u + b) (Gros 2015 Eq. 10.18), and the output predicts y <- sigmoid(W s + c), in three phases of every step, so the prediction of a step reads its input. The training interaction training trains D, A, W, b, and c with Adam on the squared error of predicting the next input from the inputs so far, over windows of window bits that start at every third bit of a stream in which every third bit is the exclusive or of the two before it. Only those bits can be predicted, so the metric xor_error falls toward 0 while random_error, the error on the other bits, stays near 0.25. One step of the system is one epoch. The system has the nested system net and the training interaction training.

Parameters:

Name Type Description Default
n_hidden int

The number of hidden units.

4
n_bits int

The length of the stream.

3000
window int

The number of bits of a sequence.

12
batch_size int

The number of sequences of a batch.

16
learning_rate float

The learning rate of Adam.

0.05
truncation int | None

The number of steps the gradient flows back through the hidden activities, 1 for Elman's original rule that holds the previous activities as constant context, or None for full backpropagation through time.

None
random_state int | Generator | None

The seed or generator of the stream and of the initial weights.

0
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.computer_science.neural_networks import ElmanNetworkSystem
>>> [item.label for item in ElmanNetworkSystem().prepare().system_.systems]
['net']
Source code in src/skcomplex/systems/computer_science/neural_networks/_elman.py
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
def __init__(
    self,
    n_hidden: int = 4,
    *,
    n_bits: int = 3000,
    window: int = 12,
    batch_size: int = 16,
    learning_rate: float = 0.05,
    truncation: int | None = None,
    random_state: int | np.random.Generator | None = 0,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.n_hidden = n_hidden
    self.n_bits = n_bits
    self.window = window
    self.batch_size = batch_size
    self.learning_rate = learning_rate
    self.truncation = truncation
    self.random_state = random_state

HiddenUnitElement(label, *, s=0.5, u=0.0, b=0.0, size=None)

Bases: Element

Hidden units of a simple recurrent network.

The activity of a unit is a sigmoid of its recurrent input and its drive. The drive u holds the weighted input of the step, and the training sets the bias b.

Parameters:

Name Type Description Default
label str

The label of the units.

required
s float

The activity in (0, 1).

0.5
u float

The weighted input of the step.

0.0
b float

The bias.

0.0
size int | None

The number of units, or None for one.

None

Examples:

>>> from skcomplex.systems.computer_science.neural_networks import HiddenUnitElement
>>> HiddenUnitElement('unit').s
0.5
Source code in src/skcomplex/systems/computer_science/neural_networks/_elman.py
57
58
59
60
61
62
63
64
65
66
67
68
69
def __init__(
    self,
    label: str,
    *,
    s: float = 0.5,
    u: float = 0.0,
    b: float = 0.0,
    size: int | None = None,
) -> None:
    super().__init__(label, size=size)
    self.s = s
    self.u = u
    self.b = b

HopfieldSystem(patterns=None, *, n_neurons=64, n_patterns=3, noise=0.2, random_state=0, label='system', solver='auto', backend='auto')

Bases: BaseSystem

A Hopfield network that stores patterns and recalls the first.

It stores the patterns with Hebb's rule and recalls the first from a corrupted copy. Neurons update one at a time in random order, so the energy -Σ w_ij s_i s_j / 2 never increases and the network settles in a stored pattern when few patterns are stored, about fewer than 0.14 n for n neurons. The system has the group neurons of states spin and weights on a complete graph.

Parameters:

Name Type Description Default
patterns ArrayLike | None

The stored patterns of -1 and 1, one row per pattern, or None for random patterns.

None
n_neurons int

The number of neurons of random patterns.

64
n_patterns int

The number of random patterns.

3
noise float

The probability that a neuron starts flipped from the first pattern.

0.2
random_state int | Generator | None

The seed or generator of the random patterns and of the corruption.

0
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Examples:

>>> from skcomplex.systems.computer_science.neural_networks import HopfieldSystem
>>> HopfieldSystem(n_neurons=16, n_patterns=2).prepare().system_.elements[0].size
16
Source code in src/skcomplex/systems/computer_science/neural_networks/_hopfield.py
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
def __init__(
    self,
    patterns: npt.ArrayLike | None = None,
    *,
    n_neurons: int = 64,
    n_patterns: int = 3,
    noise: float = 0.2,
    random_state: int | np.random.Generator | None = 0,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.patterns = patterns
    self.n_neurons = n_neurons
    self.n_patterns = n_patterns
    self.noise = noise
    self.random_state = random_state

HopfieldUpdateInteraction(label, *, target)

Bases: Interaction

Asynchronous update of a Hopfield network.

Every neuron in turn takes the sign of its weighted input. The neurons update one after the other in a new random order every step, each reading the states the neurons before it set, so the energy -sum w_ij s_i s_j / 2 never increases. A neuron whose input is zero keeps its state.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Role

The path of the group of neurons.

required

Examples:

>>> from skcomplex.systems.computer_science.neural_networks import HopfieldUpdateInteraction
>>> HopfieldUpdateInteraction('update', target='neurons').target
'neurons'
Source code in src/skcomplex/systems/computer_science/neural_networks/_hopfield.py
106
107
108
def __init__(self, label: str, *, target: Role) -> None:
    super().__init__(label)
    self.target = target

update(agent, system)

Set a neuron to the sign of its weighted input from the other neurons, or keep it for a zero input.

Parameters:

Name Type Description Default
agent NeuronView

The neuron.

required
system SystemViewProtocol

The system.

required
Source code in src/skcomplex/systems/computer_science/neural_networks/_hopfield.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def update(self, agent: NeuronView, system: SystemViewProtocol) -> None:
    """Set a neuron to the sign of its weighted input from the other neurons, or keep it for a zero input.

    Args:
        agent:
            The neuron.

        system:
            The system.
    """
    del system
    weights = np.asarray(agent.weights)
    neighbors = [other for other in agent.get_neighbors() if isinstance(other, NeuronView)]
    field = sum(weights[int(np.asarray(other.location))] * other.spin for other in neighbors)
    agent.spin = float(np.sign(field)) if field != 0 else float(agent.spin)

InputCouplingInteraction(label, *, target, source, D=0.0)

Bases: Interaction

Weighted input of a simple recurrent network, u_i <- sum_j D_ij x_j.

It sets the drive of every hidden unit at each step from the input units. The weights D have one row per hidden unit and one column per input unit. The coupling runs in the phase drive of the step, before the phases hidden of the recurrence and readout of the readout, so the hidden units read the drive of the same step.

Parameters:

Name Type Description Default
label str

The label of the interaction, unique among the components of its system.

required
target Role

The path of the group of hidden units.

required
source Role

The path of the group of input units.

required
D object

The input weights, a matrix of shape (hidden units, input units).

0.0

Examples:

>>> from skcomplex.systems.computer_science.neural_networks import InputCouplingInteraction
>>> InputCouplingInteraction('input', target='hidden', source='inputs').source
'inputs'
Source code in src/skcomplex/systems/computer_science/neural_networks/_elman.py
166
167
168
169
170
def __init__(self, label: str, *, target: Role, source: Role, D: object = 0.0) -> None:
    super().__init__(label)
    self.target = target
    self.source = source
    self.D = D

InputUnitElement(label, *, x=0.0, size=None)

Bases: Element

Input units of a simple recurrent network.

A dataset or a schedule sets the value x at every step.

Parameters:

Name Type Description Default
label str

The label of the units.

required
x float

The input.

0.0
size int | None

The number of units, or None for one.

None

Examples:

>>> from skcomplex.systems.computer_science.neural_networks import InputUnitElement
>>> InputUnitElement('bit').x
0.0
Source code in src/skcomplex/systems/computer_science/neural_networks/_elman.py
93
94
95
def __init__(self, label: str, *, x: float = 0.0, size: int | None = None) -> None:
    super().__init__(label, size=size)
    self.x = x

LorenzFlowElement(label, *, x=LORENZ_START[0], y=LORENZ_START[1], z=LORENZ_START[2])

Bases: Element

Point of the Lorenz flow that trains a reservoir.

It holds the three variables of the flow.

Parameters:

Name Type Description Default
label str

The label of the point.

required
x float

The initial value of the first variable.

LORENZ_START[0]
y float

The initial value of the second variable.

LORENZ_START[1]
z float

The initial value of the third variable.

LORENZ_START[2]

Examples:

>>> from skcomplex.systems.computer_science.neural_networks import LorenzFlowElement
>>> LorenzFlowElement('flow', z=2.0).z
2.0
Source code in src/skcomplex/systems/computer_science/neural_networks/_next_generation.py
59
60
61
62
63
64
65
66
67
68
69
70
def __init__(
    self,
    label: str,
    *,
    x: float = LORENZ_START[0],
    y: float = LORENZ_START[1],
    z: float = LORENZ_START[2],
) -> None:
    super().__init__(label)
    self.x = x
    self.y = y
    self.z = z

NeuronElement(label, *, spin=1.0, weights=0.0, values=None, size=None, space=None)

Bases: Element

Binary neuron of a Hopfield network.

The update aligns it with the sign of its weighted input. The neuron sits on its node of a complete graph and holds the weights of its incoming links, one per node.

Parameters:

Name Type Description Default
label str

The label of the agent or group.

required
spin Annotated[object, ZZ]

The initial state, -1 or 1.

1.0
weights object

The weights of the links from every node, an array with one value per node.

0.0
values Mapping[str, object] | None

Further values by name, such as the node node of every neuron.

None
size int | None

The number of neurons, or None for one.

None
space BaseSpace | None

The complete graph whose nodes hold the neurons, or None for none.

None

Examples:

>>> from skcomplex.systems.computer_science.neural_networks import NeuronElement
>>> group = NeuronElement('neurons', spin=1.0, size=4)
>>> group.label, group.size
('neurons', 4)
Source code in src/skcomplex/systems/computer_science/neural_networks/_hopfield.py
69
70
71
72
73
74
75
76
77
78
79
80
81
def __init__(
    self,
    label: str,
    *,
    spin: Annotated[object, ZZ] = 1.0,
    weights: object = 0.0,
    values: Mapping[str, object] | None = None,
    size: int | None = None,
    space: BaseSpace | None = None,
) -> None:
    super().__init__(label, values=values, size=size, space=space)
    self.spin = spin
    self.weights = weights

NextGenerationReservoirSystem(series=None, *, result=None, variables=LORENZ_VARIABLES, delay=2, order=2, ridge=2.5e-06, standardize=False, samples=403, step=0.025, label='system', solver='auto', backend='auto')

Bases: BaseSystem

A next-generation reservoir computer.

Its variables follow the ridge readout of their monomials.

Every variable keeps delay - 1 lags, and one map in the increment mode adds to it the readout of the constant, the variables with their lags, and the monomials of degree order of those. The readout comes from the result of reservoir(kind='next_generation'), and the variables start at the end of its last series, so a run is the autonomous forecast from there. One step of the system stands for one sample of the series. The defaults are those of Gauthier et al. (2021) for the Lorenz flow: 400 training samples after a warmup of 2, one sample every 0.025 from an explicit Runge-Kutta 3(2) integrator, and the ridge 2.5e-6 on features that are not standardized. A ridge this small on these features diverges within one Lyapunov time when a series comes from a much more accurate integrator, and such a series needs standardize=True. The system has the variables reservoir.x and their lags reservoir.x_lag1 for the Lorenz flow.

Parameters:

Name Type Description Default
series Array | None

The training series, one row per sample and one column per variable, or None for samples samples of the Lorenz flow every step.

None
result ReservoirResult | None

The result of a next-generation reservoir to take the readout from, which replaces the fit.

None
variables Sequence[str]

The names of the columns of the series.

LORENZ_VARIABLES
delay int

The number of consecutive samples the features read.

2
order int

The degree of the monomials.

2
ridge float

The strength of the ridge penalty.

2.5e-06
standardize bool

Whether the features read the standardized variables.

False
samples int

The number of samples of the Lorenz flow.

403
step float

The time between the samples of the Lorenz flow.

0.025
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Raises:

Type Description
ValidationError

The result is not that of a next-generation reservoir.

Examples:

>>> import numpy as np
>>> from skcomplex.systems.computer_science.neural_networks import NextGenerationReservoirSystem
>>> values = np.sin(0.1 * np.arange(200.0))[:, None]
>>> NextGenerationReservoirSystem(values, variables=['v']).prepare().states_
('reservoir.v', 'reservoir.v[-1]')
Source code in src/skcomplex/systems/computer_science/neural_networks/_next_generation.py
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
def __init__(
    self,
    series: Array | None = None,
    *,
    result: ReservoirResult | None = None,
    variables: Sequence[str] = LORENZ_VARIABLES,
    delay: int = 2,
    order: int = 2,
    ridge: float = 2.5e-6,
    standardize: bool = False,
    samples: int = 403,
    step: float = 0.025,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.series = series
    self.result = result
    self.variables = variables
    self.delay = delay
    self.order = order
    self.ridge = ridge
    self.standardize = standardize
    self.samples = samples
    self.step = step

OutputUnitElement(label, *, y=0.5, c=0.0, target=0.0, size=None)

Bases: Element

Output units of a simple recurrent network, whose activity predicts a target.

The training sets the bias c, and a dataset or a schedule sets the target.

Parameters:

Name Type Description Default
label str

The label of the units.

required
y float

The prediction in (0, 1).

0.5
c float

The bias.

0.0
target float

The value the prediction is compared with.

0.0
size int | None

The number of units, or None for one.

None

Examples:

>>> from skcomplex.systems.computer_science.neural_networks import OutputUnitElement
>>> OutputUnitElement('prediction').target
0.0
Source code in src/skcomplex/systems/computer_science/neural_networks/_elman.py
125
126
127
128
129
130
131
132
133
134
135
136
137
def __init__(
    self,
    label: str,
    *,
    y: float = 0.5,
    c: float = 0.0,
    target: float = 0.0,
    size: int | None = None,
) -> None:
    super().__init__(label, size=size)
    self.y = y
    self.c = c
    self.target = target

PerceptronSatisfiabilitySystem(n_weights=21, *, load=1.5, random_state=0, label='system', solver='auto', backend='auto')

Bases: BaseSystem

A binary perceptron that must store random patterns.

The patterns are revealed one per step, as cardinality constraints. A pattern xi of output sigma holds when sigma sum_i w_i xi_i > 0, so with an odd number N of weights w_i = +-1, when at least (N + 1) / 2 of the literals w_i = sigma xi_i hold. The patterns are satisfiable up to the critical load p / N of about 0.83 in the limit of many weights (Bertin 2021 §6.3.3, Ex. 6.4). The patterns are drawn from random_state, so a disorder average sweeps it with sweep. The system has the group weights and the reports decide.satisfiable and decide.revealed.

Parameters:

Name Type Description Default
n_weights int

The number of weights, which is odd.

21
load float

The number of patterns over the number of weights.

1.5
random_state int | Generator | None

The seed or generator of the patterns.

0
label str

The label of the system, which names it when it is nested in another one.

'system'
solver BaseSolver | str

The solver of the problem, or 'auto' for the one the system chooses.

'auto'
backend BaseBackend | str

The backend that evaluates the compiled functions, or 'auto' for the one the system chooses.

'auto'

Raises:

Type Description
ValidationError

The number of weights is even, so a pattern can tie.

Examples:

>>> from skcomplex.systems.computer_science.neural_networks import PerceptronSatisfiabilitySystem
>>> PerceptronSatisfiabilitySystem(3, load=1.0).prepare().system_.interactions[0].constraints[0].bound
2
Source code in src/skcomplex/systems/computer_science/neural_networks/_perceptron.py
324
325
326
327
328
329
330
331
332
333
334
335
336
337
def __init__(
    self,
    n_weights: int = 21,
    *,
    load: float = 1.5,
    random_state: int | np.random.Generator | None = 0,
    label: str = 'system',
    solver: BaseSolver | str = 'auto',
    backend: BaseBackend | str = 'auto',
) -> None:
    super().__init__(label, solver=solver, backend=backend)
    self.n_weights = n_weights
    self.load = load
    self.random_state = random_state

ReadoutCouplingInteraction(label, *, target, source, W=0.0)

Bases: Interaction

Readout of a simple recurrent network, y_i <- sigmoid(sum_j W_ij s_j + c_i).

The sigmoid is the logistic one. Every output unit reads the activities of the hidden units through the matrix W, adds its bias c, and takes the sigmoid of the sum as its next prediction, in the phase readout of the step.

Parameters:

Name Type Description Default
label str

The label of the interaction, unique among the components of its system.

required
target Role

The path of the group of output units.

required
source Role

The path of the group of hidden units.

required
W object

The readout weights, a matrix of shape (output units, hidden units).

0.0

Examples:

>>> from skcomplex.systems.computer_science.neural_networks import ReadoutCouplingInteraction
>>> ReadoutCouplingInteraction('readout', target='outputs', source='hidden').source
'hidden'
Source code in src/skcomplex/systems/computer_science/neural_networks/_elman.py
251
252
253
254
255
def __init__(self, label: str, *, target: Role, source: Role, W: object = 0.0) -> None:
    super().__init__(label)
    self.target = target
    self.source = source
    self.W = W

RecurrenceCouplingInteraction(label, *, target, A=0.0)

Bases: Interaction

Recurrence of a simple recurrent network, s_i <- sigmoid(sum_j A_ij s_j + u_i + b_i).

The sigmoid is the logistic one. Every hidden unit reads the activities of every hidden unit, itself included, through the matrix A, adds its drive u and its bias b, and takes the sigmoid of the sum as its next activity (Gros 2015 Eq. 10.18), in the phase hidden of the step.

Parameters:

Name Type Description Default
label str

The label of the interaction, unique among the components of its system.

required
target Role

The path of the group of hidden units.

required
A object

The recurrent weights, a square matrix with one row and one column per hidden unit.

0.0

Examples:

>>> from skcomplex.systems.computer_science.neural_networks import RecurrenceCouplingInteraction
>>> RecurrenceCouplingInteraction('recurrence', target='hidden').target
'hidden'
Source code in src/skcomplex/systems/computer_science/neural_networks/_elman.py
207
208
209
210
def __init__(self, label: str, *, target: Role, A: object = 0.0) -> None:
    super().__init__(label)
    self.target = target
    self.A = A

ReservoirUnitElement(label, *, r=0.0, w_in=1.0, leak=0.3, amplitude=1.0, frequency=0.2, size=None)

Bases: Element

Leaky tanh unit of an echo state network.

It holds its activity and the weight, leak, and sine of its input.

The map of EchoStateNetworkSystem sets r(t + 1) = (1 - a) r + a tanh(sum_j W_ij r_j + w u(t)) with the input u(t) = A sin(f t).

Parameters:

Name Type Description Default
label str

The label of the unit or the group.

required
r object

The initial activity.

0.0
w_in object

The weight w of the input.

1.0
leak Annotated[float, open(0, oo)]

The leak rate a, 1 for units without memory of their own.

0.3
amplitude float

The amplitude A of the input.

1.0
frequency float

The angular frequency f of the input, per step.

0.2
size int | None

The number of units, or None for one.

None

Examples:

>>> from skcomplex.systems.computer_science.neural_networks import ReservoirUnitElement
>>> ReservoirUnitElement('unit').leak
0.3
Source code in src/skcomplex/systems/computer_science/neural_networks/_echo_state.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def __init__(
    self,
    label: str,
    *,
    r: object = 0.0,
    w_in: object = 1.0,
    leak: Annotated[float, RealSet.open(0, oo)] = 0.3,
    amplitude: float = 1.0,
    frequency: float = 0.2,
    size: int | None = None,
) -> None:
    super().__init__(label, size=size)
    self.r = r
    self.w_in = w_in
    self.leak = leak
    self.amplitude = amplitude
    self.frequency = frequency

SatisfiabilityInteraction(label, *, target, state, constraints, reveal=1, solver='cadical153', phase=None)

Bases: Interaction

Reveal constraints on the Boolean states of a group and decide them.

The constraints come step by step, and one incremental solver decides them. Every step adds the next reveal constraints to one PySAT solver, which keeps what it learned, and sets the states of the members to a satisfying assignment when one exists. The interaction reports satisfiable, 1 or 0, and revealed, the number of active constraints, every step. Its role accepts Boolean states only, so a real state raises RoleElementError.

Parameters:

Name Type Description Default
label str

The label of the interaction.

required
target Annotated[Role, boolean]

The path of the group.

required
state str

The name of the Boolean state of the members.

required
constraints Sequence[ClauseSpec | CardinalitySpec]

The constraints, in the order they are revealed.

required
reveal int

The number of constraints revealed per step.

1
solver str

The name of the PySAT solver.

'cadical153'
phase str | None

The name of the phase of the step the rule runs in, or None for the default phase.

None

Raises:

Type Description
ValidationError

A constraint is not a spec or the number revealed per step is not positive, raised by prepare.

Examples:

>>> from skcomplex.systems.computer_science.neural_networks import SatisfiabilityInteraction
>>> from skcomplex.systems.computer_science.neural_networks import ClauseSpec
>>> constraints = [ClauseSpec((1, -2))]
>>> interaction = SatisfiabilityInteraction('sat', target='bits', state='active', constraints=constraints)
>>> interaction.reveal
1
Source code in src/skcomplex/systems/computer_science/neural_networks/_perceptron.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
def __init__(
    self,
    label: str,
    *,
    target: Annotated[Role, 'boolean'],
    state: str,
    constraints: Sequence[ClauseSpec | CardinalitySpec],
    reveal: int = 1,
    solver: str = 'cadical153',
    phase: str | None = None,
) -> None:
    super().__init__(label)
    self.target = target
    self.state = state
    self.constraints = constraints
    self.reveal = reveal
    self.solver = solver
    self.phase = phase