3 @brief Chaotic Neural Network 4 @details Implementation based on paper @cite article::nnet::cnn::1, @cite inproceedings::nnet::cnn::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/>. 33 import matplotlib.pyplot
as plt
35 from matplotlib
import rcParams
36 from matplotlib.font_manager
import FontProperties
37 except Exception
as error_instance:
38 warnings.warn(
"Impossible to import matplotlib (please, install 'matplotlib'), pyclustering's visualization " 39 "functionality is not available (details: '%s')." % str(error_instance))
41 from enum
import IntEnum
43 from scipy.spatial
import Delaunay
45 from pyclustering.utils import euclidean_distance_square, average_neighbor_distance, heaviside, draw_dynamics
50 @brief Enumeration of connection types for Chaotic Neural Network. 60 TRIANGULATION_DELAUNAY = 1,
65 @brief Container of output dynamic of the chaotic neural network where states of each neuron during simulation are stored. 73 @brief Costructor of the chaotic neural network output dynamic. 75 @param[in] output (list): Dynamic of oscillators on each step of simulation. 76 @param[in] time (list): Simulation time. 89 @brief (uint) Returns amount of simulation steps that are stored. 97 @brief Allocates observation matrix in line with output dynamic of the network. 98 @details Matrix where state of each neuron is denoted by zero/one in line with Heaviside function on each iteration. 100 @return (list) Observation matrix of the network dynamic. 103 number_neurons = len(self.
output[0])
104 observation_matrix = []
106 for iteration
in range(len(self.
output)):
107 obervation_column = []
108 for index_neuron
in range(number_neurons):
109 obervation_column.append(heaviside(self.
output[iteration][index_neuron]))
111 observation_matrix.append(obervation_column)
113 return observation_matrix
116 def __allocate_neuron_patterns(self, start_iteration, stop_iteration):
118 @brief Allocates observation transposed matrix of neurons that is limited by specified periods of simulation. 119 @details Matrix where state of each neuron is denoted by zero/one in line with Heaviside function on each iteration. 121 @return (list) Transposed observation matrix that is limited by specified periods of simulation. 126 for index_neuron
in range(len(self.
output[0])):
128 for iteration
in range(start_iteration, stop_iteration):
129 pattern_neuron.append(heaviside(self.
output[iteration][index_neuron]))
131 pattern_matrix.append(pattern_neuron)
133 return pattern_matrix
138 @brief Allocate clusters in line with ensembles of synchronous neurons where each synchronous ensemble corresponds to only one cluster. 140 @param[in] steps (double): Amount of steps from the end that is used for analysis. During specified period chaotic neural network should have stable output 141 otherwise inccorect results are allocated. 143 @return (list) Grours (lists) of indexes of synchronous oscillators. 144 For example [ [index_osc1, index_osc3], [index_osc2], [index_osc4, index_osc5] ]. 149 if iterations >= len(self.
output):
150 iterations = len(self.
output)
154 start_iteration = len(self.
output) - iterations
155 end_iteration = len(self.
output)
159 ensembles.append( [0] )
161 for index_neuron
in range(1, len(self.
output[0])):
162 neuron_pattern = pattern_matrix[index_neuron][:]
164 neuron_assigned =
False 166 for ensemble
in ensembles:
167 ensemble_pattern = pattern_matrix[ensemble[0]][:]
169 if neuron_pattern == ensemble_pattern:
170 ensemble.append(index_neuron)
171 neuron_assigned =
True 174 if neuron_assigned
is False:
175 ensembles.append( [index_neuron] )
182 @brief Visualizer of output dynamic of chaotic neural network (CNN). 189 @brief Shows output dynamic (output of each neuron) during simulation. 191 @param[in] cnn_output_dynamic (cnn_dynamic): Output dynamic of the chaotic neural network. 193 @see show_dynamic_matrix 194 @see show_observation_matrix 198 draw_dynamics(cnn_output_dynamic.time, cnn_output_dynamic.output, x_title =
"t", y_title =
"x")
204 @brief Shows output dynamic as matrix in grey colors. 205 @details This type of visualization is convenient for observing allocated clusters. 207 @param[in] cnn_output_dynamic (cnn_dynamic): Output dynamic of the chaotic neural network. 209 @see show_output_dynamic 210 @see show_observation_matrix 214 network_dynamic = numpy.array(cnn_output_dynamic.output)
216 plt.imshow(network_dynamic.T, cmap = plt.get_cmap(
'gray'), interpolation=
'None', vmin = 0.0, vmax = 1.0)
223 @brief Shows observation matrix as black/white blocks. 224 @details This type of visualization is convenient for observing allocated clusters. 226 @param[in] cnn_output_dynamic (cnn_dynamic): Output dynamic of the chaotic neural network. 228 @see show_output_dynamic 229 @see show_dynamic_matrix 233 observation_matrix = numpy.array(cnn_output_dynamic.allocate_observation_matrix())
234 plt.imshow(observation_matrix.T, cmap = plt.get_cmap(
'gray'), interpolation=
'None', vmin = 0.0, vmax = 1.0)
240 @brief Chaotic neural network based on system of logistic map where clustering phenomenon can be observed. 244 # load stimulus from file 245 stimulus = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE1); 247 # create chaotic neural network, amount of neurons should be equal to amout of stimulus 248 network_instance = cnn_network(len(stimulus)); 250 # simulate it during 100 steps 251 output_dynamic = network_instance.simulate(steps, stimulus); 253 # display output dynamic of the network 254 cnn_visualizer.show_output_dynamic(output_dynamic); 256 # dysplay dynamic matrix and observation matrix to show clustering 258 cnn_visualizer.show_dynamic_matrix(output_dynamic); 259 cnn_visualizer.show_observation_matrix(output_dynamic); 264 def __init__(self, num_osc, conn_type = type_conn.ALL_TO_ALL, amount_neighbors = 3):
266 @brief Constructor of chaotic neural network. 268 @param[in] num_osc (uint): Amount of neurons in the chaotic neural network. 269 @param[in] conn_type (type_conn): CNN type connection for the network. 270 @param[in] amount_neighbors (uint): k-nearest neighbors for calculation scaling constant of weights. 285 self.
__output = [ random.random()
for _
in range(num_osc) ]
290 @brief Returns size of the chaotic neural network that is defined by amount of neurons. 298 @brief Simulates chaotic neural network with extrnal stimulus during specified steps. 299 @details Stimulus are considered as a coordinates of neurons and in line with that weights 302 @param[in] steps (uint): Amount of steps for simulation. 303 @param[in] stimulus (list): Stimulus that are used for simulation. 305 @return (cnn_dynamic) Output dynamic of the chaotic neural network. 313 dynamic.output.append(self.
__output)
314 dynamic.time.append(0)
316 for step
in range(1, steps, 1):
319 dynamic.output.append(self.
__output)
320 dynamic.time.append(step)
325 def __calculate_states(self):
327 @brief Calculates new state of each neuron. 328 @detail There is no any assignment. 330 @return (list) Returns new states (output). 334 output = [ 0.0
for _
in range(self.
__num_osc) ]
342 def __neuron_evolution(self, index):
344 @brief Calculates state of the neuron with specified index. 346 @param[in] index (uint): Index of neuron in the network. 348 @return (double) New output of the specified neuron. 353 for index_neighbor
in range(self.
__num_osc):
354 value += self.
__weights[index][index_neighbor] * (1.0 - 2.0 * (self.
__output[index_neighbor] ** 2))
359 def __create_weights(self, stimulus):
361 @brief Create weights between neurons in line with stimulus. 363 @param[in] stimulus (list): External stimulus for the chaotic neural network. 369 self.
__weights = [ [ 0.0
for _
in range(len(stimulus)) ]
for _
in range(len(stimulus)) ]
375 elif self.
__conn_type == type_conn.TRIANGULATION_DELAUNAY:
379 def __create_weights_all_to_all(self, stimulus):
381 @brief Create weight all-to-all structure between neurons in line with stimulus. 383 @param[in] stimulus (list): External stimulus for the chaotic neural network. 387 for i
in range(len(stimulus)):
388 for j
in range(i + 1, len(stimulus)):
398 def __create_weights_delaunay_triangulation(self, stimulus):
400 @brief Create weight Denlauny triangulation structure between neurons in line with stimulus. 402 @param[in] stimulus (list): External stimulus for the chaotic neural network. 406 points = numpy.array(stimulus)
407 triangulation = Delaunay(points)
409 for triangle
in triangulation.simplices:
410 for index_tri_point1
in range(len(triangle)):
411 for index_tri_point2
in range(index_tri_point1 + 1, len(triangle)):
412 index_point1 = triangle[index_tri_point1]
413 index_point2 = triangle[index_tri_point2]
417 self.
__weights[index_point1][index_point2] = weight
418 self.
__weights[index_point2][index_point1] = weight
424 def __calculate_weight(self, stimulus1, stimulus2):
426 @brief Calculate weight between neurons that have external stimulus1 and stimulus2. 428 @param[in] stimulus1 (list): External stimulus of the first neuron. 429 @param[in] stimulus2 (list): External stimulus of the second neuron. 431 @return (double) Weight between neurons that are under specified stimulus. 435 distance = euclidean_distance_square(stimulus1, stimulus2)
441 @brief Shows structure of the network: neurons and connections between them. 446 if (dimension != 3)
and (dimension != 2):
447 raise NameError(
'Network that is located in different from 2-d and 3-d dimensions can not be represented')
469 def __create_surface(self, dimension):
471 @brief Prepares surface for showing network structure in line with specified dimension. 473 @param[in] dimension (uint): Dimension of processed data (external stimulus). 475 @return (tuple) Description of surface for drawing network structure. 479 rcParams[
'font.sans-serif'] = [
'Arial']
480 rcParams[
'font.size'] = 12
485 axes = fig.add_subplot(111)
487 axes = fig.gca(projection=
'3d')
489 surface_font = FontProperties()
490 surface_font.set_name(
'Arial')
491 surface_font.set_size(
'12')
output
Output value of each neuron on each iteration.
def __create_weights_delaunay_triangulation(self, stimulus)
Create weight Denlauny triangulation structure between neurons in line with stimulus.
def show_dynamic_matrix(cnn_output_dynamic)
Shows output dynamic as matrix in grey colors.
time
Sequence of simulation steps of the network.
Utils that are used by modules of pyclustering.
def show_output_dynamic(cnn_output_dynamic)
Shows output dynamic (output of each neuron) during simulation.
def __create_weights(self, stimulus)
Create weights between neurons in line with stimulus.
Visualizer of output dynamic of chaotic neural network (CNN).
Enumeration of connection types for Chaotic Neural Network.
def __allocate_neuron_patterns(self, start_iteration, stop_iteration)
Allocates observation transposed matrix of neurons that is limited by specified periods of simulation...
def __calculate_weight(self, stimulus1, stimulus2)
Calculate weight between neurons that have external stimulus1 and stimulus2.
def __calculate_states(self)
Calculates new state of each neuron.
def __len__(self)
Returns size of the chaotic neural network that is defined by amount of neurons.
def show_network(self)
Shows structure of the network: neurons and connections between them.
Container of output dynamic of the chaotic neural network where states of each neuron during simulati...
def __create_weights_all_to_all(self, stimulus)
Create weight all-to-all structure between neurons in line with stimulus.
def show_observation_matrix(cnn_output_dynamic)
Shows observation matrix as black/white blocks.
def __create_surface(self, dimension)
Prepares surface for showing network structure in line with specified dimension.
def __len__(self)
(uint) Returns amount of simulation steps that are stored.
def allocate_sync_ensembles(self, steps)
Allocate clusters in line with ensembles of synchronous neurons where each synchronous ensemble corre...
def __neuron_evolution(self, index)
Calculates state of the neuron with specified index.
def simulate(self, steps, stimulus)
Simulates chaotic neural network with extrnal stimulus during specified steps.
def __init__(self, num_osc, conn_type=type_conn.ALL_TO_ALL, amount_neighbors=3)
Constructor of chaotic neural network.
Chaotic neural network based on system of logistic map where clustering phenomenon can be observed...
def allocate_observation_matrix(self)
Allocates observation matrix in line with output dynamic of the network.
def __init__(self, output=None, time=None)
Costructor of the chaotic neural network output dynamic.