def random_state(rows, cols, connections, random_state, ccore_flag): sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE1) params = som_parameters() params.random_state = random_state network_1 = som(rows, cols, connections, ccore=ccore_flag) steps_1 = network_1.train(sample, 100, True) network_2 = som(rows, cols, connections, ccore=ccore_flag) steps_2 = network_2.train(sample, 100, True) assert steps_1 == steps_2 assert network_1.weights == network_2.weights assert network_1.capture_objects == network_2.capture_objects assert network_1.awards == network_2.awards
def templateTestAwardNeurons(file, rows, cols, time, expected_result, autostop, ccore_flag, parameters = None, **kwargs): store_load = kwargs.get('store_load', False) types = [type_conn.func_neighbor, type_conn.grid_eight, type_conn.grid_four, type_conn.honeycomb] sample = read_sample(file) if (parameters is None): parameters = som_parameters() for stucture in types: network = som(rows, cols, stucture, parameters, ccore = ccore_flag) if store_load: dump_network = pickle.dumps(network) network = pickle.loads(dump_network) network.train(sample, time, autostop) winners = network.get_winner_number() assert winners == len(expected_result) if sorted(network.awards) != expected_result: network.show_network(awards = True) assert sorted(network.awards) == expected_result total_capture_points = 0 for points in network.capture_objects: total_capture_points += len(points) assert total_capture_points == sum(expected_result) del network
def train(self): samples = []; print("Digit images preprocessing..."); for index_digit in range(0, 10, 1): list_file_digit_sample = IMAGE_DIGIT_SAMPLES.GET_LIST_IMAGE_SAMPLES(index_digit); for file_name in list_file_digit_sample: data = read_image(file_name); image_pattern = rgb2gray(data); for index_pixel in range(len(image_pattern)): if (image_pattern[index_pixel] < 128): image_pattern[index_pixel] = 1; else: image_pattern[index_pixel] = 0; samples += [ image_pattern ]; print("SOM initialization..."); self.__network = som(2, 5, type_conn.grid_four, None, True); print("SOM training..."); self.__network.train(samples, 300); print("SOM is ready...");
def template_self_organization(file, rows, cols, time, structure, init_type=None, init_radius=None, init_rate=None, umatrix=False, pmatrix=False, awards=False): parameters = som_parameters() if (init_type is not None): parameters.init_type = init_type if (init_radius is not None): parameters.init_radius = init_radius if (init_rate is not None): parameters.init_learn_rate = init_rate sample = read_sample(file) network = som(rows, cols, structure, parameters, True) network.train(sample, time) network.show_network(False, dataset=False) if (umatrix is True): network.show_distance_matrix() if (pmatrix is True): network.show_density_matrix() if (awards is True): network.show_winner_matrix()
def __init__(self, data, rows, cols, radius): """! @brief Constructor of the double layer oscillatory network SYNC-SOM. @param[in] data (list): Input data that is presented as list of points (objects), each point should be represented by list or tuple. @param[in] rows (uint): Rows of neurons (number of neurons in column) in the input layer (self-organized feature map). @param[in] cols (uint): Columns of neurons (number of neurons in row) in the input later (self-organized feature map). @param[in] radius (double): Connectivity radius between objects that defines connection between oscillators in the second layer. """ self._data = data self._radius = radius * radius self._som = som(rows, cols, conn_type=type_conn.grid_four, ccore=False) # The first (input) later - SOM layer. self._som_osc_table = list() self._sync = None # The second (output) layer - Sync layer. self._struct = None # Structure of connections between oscillators in the second layer - Sync layer. # For convenience self._analyser = None
def templateTestAwardNeurons(self, file, rows, cols, time, expected_result, autostop=False, ccore_flag=False, parameters=None): types = [ type_conn.func_neighbor, type_conn.grid_eight, type_conn.grid_four, type_conn.honeycomb ] sample = read_sample(file) if (parameters is None): parameters = som_parameters() for stucture in types: network = som(rows, cols, stucture, parameters, ccore=ccore_flag) network.train(sample, time, autostop) if (sorted(network.awards) != expected_result): network.show_network(awards=True) print(sorted(network.awards)) assert sorted(network.awards) == expected_result total_capture_points = 0 for points in network.capture_objects: total_capture_points += len(points) assert total_capture_points == sum(expected_result) del network
def testSomVisualizationByCore(self): sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE4) parameters = som_parameters() network = som(5, 5, type_conn.grid_eight, parameters, ccore = True) network.train(sample, 100, True) network.show_network() network.show_winner_matrix() network.show_distance_matrix() network.show_density_matrix()
def testSomVisualizationByCore(self): sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE4) parameters = som_parameters() network = som(5, 5, type_conn.grid_eight, parameters, ccore=True) network.train(sample, 100, True) network.show_network() network.show_winner_matrix() network.show_distance_matrix() network.show_density_matrix()
def process(self): """! @brief Performs cluster analysis by competition between neurons of SOM. @remark Results of clustering can be obtained using corresponding get methods. @see get_clusters() """ self.__network = som(1, self.__amount_clusters, type_conn.grid_four, None, self.__ccore); self.__network.train(self.__data_pointer, self.__epouch, True);
def __init__(self, data, rows, cols): """! @brief Constructor of the double layer oscillatory network SYNC-SOM. @param[in] data (list): Input data that is presented as list of points (objects), each point should be represented by list or tuple. @param[in] rows (uint): Rows of neurons (number of neurons in column) in the input layer (self-organized feature map). @param[in] cols (uint): Columns of neurons (number of neurons in row) in the input later (self-organized feature map). """ self._data = data; self._som = som(rows, cols, data, 100, conn_type = type_conn.grid_four); self._som_osc_table = list();
def __init__(self, data, rows, cols): """! @brief Constructor of the double layer oscillatory network SYNC-SOM. @param[in] data (list): Input data that is presented as list of points (objects), each point should be represented by list or tuple. @param[in] rows (uint): Rows of neurons (number of neurons in column) in the input layer (self-organized feature map). @param[in] cols (uint): Columns of neurons (number of neurons in row) in the input later (self-organized feature map). """ self._data = data self._som = som(rows, cols, data, 100, conn_type=type_conn.grid_four) self._som_osc_table = list()
def process(self): """! @brief Performs cluster analysis by competition between neurons of SOM. @return (somsc) Returns itself (SOM Simple Clustering instance). @see get_clusters() """ self.__network = som(1, self.__amount_clusters, type_conn.grid_four, None, self.__ccore) self.__network.train(self.__data_pointer, self.__epouch, True) return self
def testDoubleTrain(self): sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE1); parameters = som_parameters(); network = som(2, 2, type_conn.grid_eight, parameters, ccore = False); network.train(sample, 100, False); network.train(sample, 100, False); assert sum(network.awards) == len(sample); total_capture_points = 0; for points in network.capture_objects: total_capture_points += len(points); assert total_capture_points == len(sample);
def templateTestSimulate(connections, ccore_flag): sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE1); network = som(1, 2, connections, ccore = ccore_flag); network.train(sample, 100); expected_winners = [0, 1]; for i in range(len(sample)): index_winner = network.simulate(sample[i]); if ( (i == 0) and (index_winner == 1)): expected_winners = [1, 0]; if (i < 5): assert expected_winners[0] == index_winner; else: assert expected_winners[1] == index_winner;
def templateTestAwardNeurons(self, file, rows, cols, time, expected_result, autostop = False, ccore_flag = False): types = [type_conn.func_neighbor, type_conn.grid_eight, type_conn.grid_four, type_conn.honeycomb]; sample = read_sample(file); for stucture in types: network = som(rows, cols, sample, time, stucture, ccore = ccore_flag); network.train(autostop); assert sorted(network.awards) == expected_result; total_capture_points = 0; for points in network.capture_objects: total_capture_points += len(points); assert total_capture_points == sum(expected_result); del network;
def process(self): """! @brief Performs cluster analysis by competition between neurons in self-organized map. @return (somsc) Returns itself (SOM Simple Clustering instance). @see get_clusters() """ params = som_parameters() params.random_state = self.__random_state self.__network = som(1, self.__amount_clusters, type_conn.grid_four, params, self.__ccore) self.__network.train(self.__data_pointer, self.__epouch, True) return self
def templateTestWinners(ccore_flag): types = [type_conn.func_neighbor, type_conn.grid_eight, type_conn.grid_four, type_conn.honeycomb] sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE3) for stucture in types: network = som(5, 5, stucture, ccore = ccore_flag) network.train(sample, 100) assert sum(network.awards) == 60 points = list() for i in range(network.size): if network.awards[i] > 0: points += network.capture_objects[i] assert len(points) == len(sample) points = sorted(points) for i in range(len(points)): assert points[i] == i
def templateTestSimulate(connections, ccore_flag, **kwargs): store_load = kwargs.get('store_load', False) sample = read_sample(SIMPLE_SAMPLES.SAMPLE_SIMPLE1) network = som(1, 2, connections, ccore = ccore_flag) network.train(sample, 100) if store_load: dump_network = pickle.dumps(network) network = pickle.loads(dump_network) expected_winners = [0, 1] for i in range(len(sample)): index_winner = network.simulate(sample[i]) if (i == 0) and (index_winner == 1): expected_winners = [1, 0] if i < 5: assert expected_winners[0] == index_winner else: assert expected_winners[1] == index_winner
def __init__(self, data, rows, cols, radius): """! @brief Constructor of the double layer oscillatory network SYNC-SOM. @param[in] data (list): Input data that is presented as list of points (objects), each point should be represented by list or tuple. @param[in] rows (uint): Rows of neurons (number of neurons in column) in the input layer (self-organized feature map). @param[in] cols (uint): Columns of neurons (number of neurons in row) in the input later (self-organized feature map). @param[in] radius (double): Connectivity radius between objects that defines connection between oscillators in the second layer. """ self._data = data; self._radius = radius * radius; self._som = som(rows, cols, conn_type = type_conn.grid_four, ccore = False); # The first (input) later - SOM layer. self._som_osc_table = list(); self._sync = None; # The second (output) layer - Sync layer. self._struct = None; # Structure of connections between oscillators in the second layer - Sync layer. # For convenience self._analyser = None;
def template_self_organization(file, rows, cols, time, structure, init_type = None, init_radius = None, init_rate = None, umatrix = False, pmatrix = False, awards = False): parameters = som_parameters(); if (init_type is not None): parameters.init_type = init_type; if (init_radius is not None): parameters.init_radius = init_radius; if (init_rate is not None): parameters.init_learn_rate = init_rate; sample = read_sample(file); network = som(rows, cols, structure, parameters, True); network.train(sample, time); network.show_network(False, dataset = False); if (umatrix is True): network.show_distance_matrix(); if (pmatrix is True): network.show_density_matrix(); if (awards is True): network.show_winner_matrix();
img_org.item(y, x, 2)]) print('selected_pixel_bgr :\n', bgr_val) ## 5. SOM sample = bgr_val # create SOM parameters parameters = som_parameters() # initialization of initial neuron weights, radius, rateOfLearning, autostop # create self-organized feature map with size 7x7 rows = 6 # five rows cols = 6 # five columns structure = type_conn.honeycomb # each neuron has max. four neighbors. network = som(rows, cols, structure, parameters) # train network on sample during 100 epochs. network.train(sample, 50) # simulate trained network using randomly modified point from input dataset. index_point = random.randint(0, len(sample) - 1) point = sample[index_point] # obtain randomly point from data point[0] += random.random() * 0.2 # change randomly X-coordinate point[1] += random.random() * 0.2 # change randomly Y-coordinate index_winner = network.simulate(point) # check what are objects from input data are much close to randomly modified. index_similar_objects = network.capture_objects[index_winner]