3 @brief Neural Network: Hysteresis Oscillatory Network 4 @details Implementation based on paper @cite article::nnet::hysteresis::1. 6 @authors Andrei Novikov (pyclustering@yandex.ru) 8 @copyright GNU Public License 10 @cond GNU_PUBLIC_LICENSE 11 PyClustering is free software: you can redistribute it and/or modify 12 it under the terms of the GNU General Public License as published by 13 the Free Software Foundation, either version 3 of the License, or 14 (at your option) any later version. 16 PyClustering is distributed in the hope that it will be useful, 17 but WITHOUT ANY WARRANTY; without even the implied warranty of 18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 GNU General Public License for more details. 21 You should have received a copy of the GNU General Public License 22 along with this program. If not, see <http://www.gnu.org/licenses/>. 29 from scipy.integrate
import odeint
38 @brief Represents output dynamic of hysteresis oscillatory network. 45 @brief (list) Returns outputs of oscillator during simulation. 54 @brief (list) Returns sampling times when dynamic is measured during simulation. 63 @brief Constructor of hysteresis neural network dynamic. 65 @param[in] amplitudes (list): Dynamic (amplitudes) of oscillators on each step of simulation. 66 @param[in] time (list): Simulation time (timestamps of simulation steps) when amplitudes are stored. 70 if (len(amplitudes) != len(time)):
71 raise NameError(
"Length of list of dynamics of oscillators should be equal to length of simulation timestamps of steps.");
80 @brief (uint) Returns number of simulation steps that are stored in dynamic. 89 @brief Allocate clusters in line with ensembles of synchronous oscillators where each 90 synchronous ensemble corresponds to only one cluster. 92 @param[in] tolerance (double): Maximum error for allocation of synchronous ensemble oscillators. 93 @param[in] threshold_steps (uint): Number of steps from the end of simulation that should be analysed for ensemble allocation. 94 If amout of simulation steps has been less than threshold steps than amount of steps will be reduced to amout 97 @return (list) Grours of indexes of synchronous oscillators, for example, [ [index_osc1, index_osc3], [index_osc2], [index_osc4, index_osc5] ]." 103 number_oscillators = len(self.
_dynamic[0]);
105 for i
in range(1, number_oscillators, 1):
106 captured_neuron =
True;
107 for cluster
in clusters:
108 neuron_index = cluster[0];
110 analysis_steps = threshold_steps;
111 if (len(self.
_dynamic) < analysis_steps):
112 analysis_steps = len(self.
_dynamic);
114 analysis_start_step_index = len(self.
_dynamic) - 1;
116 for step
in range(analysis_start_step_index, analysis_start_step_index - analysis_steps, -1):
117 neuron_amplitude = self.
_dynamic[step][neuron_index];
118 candidate_amplitude = self.
_dynamic[step][i];
120 if (
not (candidate_amplitude < (neuron_amplitude + tolerance))
or not (candidate_amplitude > (neuron_amplitude - tolerance)) ):
121 captured_neuron =
False;
124 if ( captured_neuron
is True ):
128 if (captured_neuron
is False):
129 clusters.append([i]);
136 @brief Visualizer of output dynamic of hysteresis oscillatory network. 143 @brief Shows output dynamic (output of each oscillator) during simulation. 145 @param[in] hysteresis_output_dynamic (hysteresis_dynamic): Output dynamic of the hysteresis oscillatory network. 149 draw_dynamics(hysteresis_output_dynamic.time, hysteresis_output_dynamic.output, x_title =
"Time", y_title =
"x(t)");
154 @brief Hysteresis oscillatory network that uses relaxation oscillators that are represented by objective hysteresis neurons whose output in range [-1, +1]. 158 # create hysteresis oscillatory network with 5 oscillators. 159 network = hysteresis_network(5); 161 # set initial states (from range [-1, +1]). 162 network.states = [1 0 1 0 1]; 164 # set initial outputs. 165 network.outputs = [1 1 1 1 1]; 167 # perform simulation of the network during 1000 steps in 10 time units. 168 output_dynamic = network.simulate(1000, 10); 170 # show output dynamic of the network. 171 hysteresis_visualizer.show_output_dynamic(output_dynamic); 173 # obtain synchronous ensembles. 174 ensembles = output_dynamic.allocate_sync_ensembles(tolerance = 0.5, threshold_steps = 5); 183 @brief Returns current outputs of neurons. 185 @return (list) Current outputs of neurons. 194 @brief Sets outputs of neurons. 198 self.
_outputs = [val
for val
in values];
204 @brief Return current states of neurons. 206 @return (list) States of neurons. 215 @brief Set current states of neurons. 219 self.
_states = [val
for val
in values];
222 def __init__(self, num_osc, own_weight = -4, neigh_weight = -1, type_conn = conn_type.ALL_TO_ALL, type_conn_represent = conn_represent.MATRIX):
224 @brief Constructor of hysteresis oscillatory network. 226 @param[in] num_osc (uint): Number of oscillators in the network. 227 @param[in] own_weight (double): Weight of connection from oscillator to itself - own weight. 228 @param[in] neigh_weight (double): Weight of connection between oscillators. 229 @param[in] type_conn (conn_type): Type of connection between oscillators in the network. 230 @param[in] type_conn_represent (conn_represent): Internal representation of connection in the network: matrix or list. 234 super().
__init__(num_osc, type_conn, type_conn_represent);
247 for index
in range(0, self.
_num_osc, 1):
249 self.
_weight[index][index] = own_weight;
252 def _neuron_states(self, inputs, t, argv):
254 @brief Returns new value of the neuron (oscillator). 256 @param[in] inputs (list): Initial values (current) of the neuron - excitatory. 257 @param[in] t (double): Current time of simulation. 258 @param[in] argv (tuple): Extra arguments that are not used for integration - index of the neuron. 260 @return (double) New value of the neuron. 270 for i
in range(0, self.
_num_osc, 1):
282 def simulate(self, steps, time, solution = solve_type.RK4, collect_dynamic = True):
284 @brief Performs static simulation of hysteresis oscillatory network. 286 @param[in] steps (uint): Number steps of simulations during simulation. 287 @param[in] time (double): Time of simulation. 288 @param[in] solution (solve_type): Type of solution (solving). 289 @param[in] collect_dynamic (bool): If True - returns whole dynamic of oscillatory network, otherwise returns only last values of dynamics. 291 @return (hysteresis_dynamic) Dynamic of oscillatory network. If argument 'collect_dynamic' = True, than return dynamic for the whole simulation time, 292 otherwise returns only last values (last step of simulation) of dynamic. 298 def simulate_static(self, steps, time, solution = solve_type.RK4, collect_dynamic = False):
300 @brief Performs static simulation of hysteresis oscillatory network. 302 @param[in] steps (uint): Number steps of simulations during simulation. 303 @param[in] time (double): Time of simulation. 304 @param[in] solution (solve_type): Type of solution (solving). 305 @param[in] collect_dynamic (bool): If True - returns whole dynamic of oscillatory network, otherwise returns only last values of dynamics. 307 @return (hysteresis_dynamic) Dynamic of oscillatory network. If argument 'collect_dynamic' = True, than return dynamic for the whole simulation time, 308 otherwise returns only last values (last step of simulation) of dynamic. 313 if (solution == solve_type.FAST):
314 raise NameError(
"Solver FAST is not support due to low accuracy that leads to huge error.");
315 elif (solution == solve_type.RKF45):
316 raise NameError(
"Solver RKF45 is not support in python version.");
321 if (collect_dynamic ==
True):
325 dyn_state.append(self.
_states);
329 int_step = step / 10.0;
331 for t
in numpy.arange(step, time + step, step):
336 if (collect_dynamic
is True):
337 dyn_state.append(self.
_states);
340 if (collect_dynamic
is False):
341 dyn_state.append(self.
_states);
342 dyn_time.append(time);
347 def _calculate_states(self, solution, t, step, int_step):
349 @brief Calculates new states for neurons using differential calculus. Returns new states for neurons. 351 @param[in] solution (solve_type): Type solver of the differential equation. 352 @param[in] t (double): Current time of simulation. 353 @param[in] step (double): Step of solution at the end of which states of oscillators should be calculated. 354 @param[in] int_step (double): Step differentiation that is used for solving differential equation. 356 @return (list) New states for neurons. 362 for index
in range (0, self.
_num_osc, 1):
363 result = odeint(self.
_neuron_states, self.
_states[index], numpy.arange(t - step, t, int_step), (index , ));
364 next_states[index] = result[len(result) - 1][0];
def time(self)
(list) Returns sampling times when dynamic is measured during simulation.
def _calculate_states(self, solution, t, step, int_step)
Calculates new states for neurons using differential calculus.
def outputs(self)
Returns current outputs of neurons.
Utils that are used by modules of pyclustering.
Hysteresis oscillatory network that uses relaxation oscillators that are represented by objective hys...
def output(self)
(list) Returns outputs of oscillator during simulation.
Represents output dynamic of hysteresis oscillatory network.
Visualizer of output dynamic of hysteresis oscillatory network.
def allocate_sync_ensembles(self, tolerance=0.1, threshold_steps=1)
Allocate clusters in line with ensembles of synchronous oscillators where each synchronous ensemble c...
def has_connection(self, i, j)
Returns True if there is connection between i and j oscillators and False - if connection doesn't exi...
def __init__(self, amplitudes, time)
Constructor of hysteresis neural network dynamic.
def simulate(self, steps, time, solution=solve_type.RK4, collect_dynamic=True)
Performs static simulation of hysteresis oscillatory network.
def simulate_static(self, steps, time, solution=solve_type.RK4, collect_dynamic=False)
Performs static simulation of hysteresis oscillatory network.
def show_output_dynamic(hysteresis_output_dynamic)
Shows output dynamic (output of each oscillator) during simulation.
def states(self)
Return current states of neurons.
Common network description that consists of information about oscillators and connection between them...
def _neuron_states(self, inputs, t, argv)
Returns new value of the neuron (oscillator).
Neural and oscillatory network module.
def __len__(self)
(uint) Returns number of simulation steps that are stored in dynamic.
def __init__(self, num_osc, own_weight=-4, neigh_weight=-1, type_conn=conn_type.ALL_TO_ALL, type_conn_represent=conn_represent.MATRIX)
Constructor of hysteresis oscillatory network.