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. 241 @details Here is an example how to perform cluster analysis using chaotic neural network: 243 from pyclustering.cluster import cluster_visualizer 244 from pyclustering.samples.definitions import SIMPLE_SAMPLES 245 from pyclustering.utils import read_sample 246 from pyclustering.nnet.cnn import cnn_network, cnn_visualizer 248 # Load stimulus from file. 249 stimulus = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE3) 251 # Create chaotic neural network, amount of neurons should be equal to amount of stimulus. 252 network_instance = cnn_network(len(stimulus)) 254 # Perform simulation during 100 steps. 256 output_dynamic = network_instance.simulate(steps, stimulus) 258 # Display output dynamic of the network. 259 cnn_visualizer.show_output_dynamic(output_dynamic) 261 # Display dynamic matrix and observation matrix to show clustering phenomenon. 262 cnn_visualizer.show_dynamic_matrix(output_dynamic) 263 cnn_visualizer.show_observation_matrix(output_dynamic) 265 # Visualize clustering results. 266 clusters = output_dynamic.allocate_sync_ensembles(10) 267 visualizer = cluster_visualizer() 268 visualizer.append_clusters(clusters, stimulus) 274 def __init__(self, num_osc, conn_type = type_conn.ALL_TO_ALL, amount_neighbors = 3):
276 @brief Constructor of chaotic neural network. 278 @param[in] num_osc (uint): Amount of neurons in the chaotic neural network. 279 @param[in] conn_type (type_conn): CNN type connection for the network. 280 @param[in] amount_neighbors (uint): k-nearest neighbors for calculation scaling constant of weights. 295 self.
__output = [ random.random()
for _
in range(num_osc) ]
300 @brief Returns size of the chaotic neural network that is defined by amount of neurons. 308 @brief Simulates chaotic neural network with extrnal stimulus during specified steps. 309 @details Stimulus are considered as a coordinates of neurons and in line with that weights 312 @param[in] steps (uint): Amount of steps for simulation. 313 @param[in] stimulus (list): Stimulus that are used for simulation. 315 @return (cnn_dynamic) Output dynamic of the chaotic neural network. 323 dynamic.output.append(self.
__output)
324 dynamic.time.append(0)
326 for step
in range(1, steps, 1):
329 dynamic.output.append(self.
__output)
330 dynamic.time.append(step)
335 def __calculate_states(self):
337 @brief Calculates new state of each neuron. 338 @detail There is no any assignment. 340 @return (list) Returns new states (output). 344 output = [ 0.0
for _
in range(self.
__num_osc) ]
352 def __neuron_evolution(self, index):
354 @brief Calculates state of the neuron with specified index. 356 @param[in] index (uint): Index of neuron in the network. 358 @return (double) New output of the specified neuron. 363 for index_neighbor
in range(self.
__num_osc):
364 value += self.
__weights[index][index_neighbor] * (1.0 - 2.0 * (self.
__output[index_neighbor] ** 2))
369 def __create_weights(self, stimulus):
371 @brief Create weights between neurons in line with stimulus. 373 @param[in] stimulus (list): External stimulus for the chaotic neural network. 379 self.
__weights = [ [ 0.0
for _
in range(len(stimulus)) ]
for _
in range(len(stimulus)) ]
385 elif self.
__conn_type == type_conn.TRIANGULATION_DELAUNAY:
389 def __create_weights_all_to_all(self, stimulus):
391 @brief Create weight all-to-all structure between neurons in line with stimulus. 393 @param[in] stimulus (list): External stimulus for the chaotic neural network. 397 for i
in range(len(stimulus)):
398 for j
in range(i + 1, len(stimulus)):
408 def __create_weights_delaunay_triangulation(self, stimulus):
410 @brief Create weight Denlauny triangulation structure between neurons in line with stimulus. 412 @param[in] stimulus (list): External stimulus for the chaotic neural network. 416 points = numpy.array(stimulus)
417 triangulation = Delaunay(points)
419 for triangle
in triangulation.simplices:
420 for index_tri_point1
in range(len(triangle)):
421 for index_tri_point2
in range(index_tri_point1 + 1, len(triangle)):
422 index_point1 = triangle[index_tri_point1]
423 index_point2 = triangle[index_tri_point2]
427 self.
__weights[index_point1][index_point2] = weight
428 self.
__weights[index_point2][index_point1] = weight
434 def __calculate_weight(self, stimulus1, stimulus2):
436 @brief Calculate weight between neurons that have external stimulus1 and stimulus2. 438 @param[in] stimulus1 (list): External stimulus of the first neuron. 439 @param[in] stimulus2 (list): External stimulus of the second neuron. 441 @return (double) Weight between neurons that are under specified stimulus. 445 distance = euclidean_distance_square(stimulus1, stimulus2)
451 @brief Shows structure of the network: neurons and connections between them. 456 if (dimension != 3)
and (dimension != 2):
457 raise NameError(
'Network that is located in different from 2-d and 3-d dimensions can not be represented')
479 def __create_surface(self, dimension):
481 @brief Prepares surface for showing network structure in line with specified dimension. 483 @param[in] dimension (uint): Dimension of processed data (external stimulus). 485 @return (tuple) Description of surface for drawing network structure. 489 rcParams[
'font.sans-serif'] = [
'Arial']
490 rcParams[
'font.size'] = 12
495 axes = fig.add_subplot(111)
497 axes = fig.gca(projection=
'3d')
499 surface_font = FontProperties()
500 surface_font.set_name(
'Arial')
501 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.