Exemple #1
0
    def test_sofm_step_reduction(self):
        X = 4 * np.ones((1, 2))
        default_params = dict(n_inputs=2,
                              n_outputs=1,
                              step=0.1,
                              weight=np.ones((2, 1)))

        sofm_1 = algorithms.SOFM(reduce_step_after=1, **default_params)
        sofm_1.train(X, epochs=4)
        dist_1 = np.linalg.norm(sofm_1.weight.T - X)

        sofm_2 = algorithms.SOFM(reduce_step_after=10, **default_params)
        sofm_2.train(X, epochs=4)
        dist_2 = np.linalg.norm(sofm_2.weight.T - X)

        # SOFM-2 suppose to be closer, becase learning rate decreases
        # slower with bigger reduction factor
        self.assertLess(dist_2, dist_1)

        sofm_3 = algorithms.SOFM(reduce_step_after=None, **default_params)
        sofm_3.train(X, epochs=4)
        dist_3 = np.linalg.norm(sofm_3.weight.T - X)

        # SOFM-3 doesn't have any step reduction, so it
        # converges even faster than SOFM-2
        self.assertLess(dist_3, dist_2)
Exemple #2
0
    def runSOFM(self, outputRows, outputCols, learningRadius, weightInit,
                gridType):

        if weightInit == "default":
            sofm = algorithms.SOFM(n_inputs=self.n_inputs,
                                   features_grid=(outputRows, outputCols),
                                   verbose=False,
                                   step=0.5,
                                   learning_radius=learningRadius,
                                   grid_type=gridType)

        else:
            sofm = algorithms.SOFM(n_inputs=self.n_inputs,
                                   features_grid=(outputRows, outputCols),
                                   verbose=False,
                                   step=0.5,
                                   learning_radius=learningRadius,
                                   grid_type=gridType,
                                   weight=weightInit)
#        try:
        trainingStart = time()
        sofm.train(self.X, epochs=300)
        trainingTime = time() - trainingStart

        predictionResults, error, errorPercent = self.dataObject.verifyPrediction(
            sofm.predict(self.X))

        if error < self.lowestError:
            self.lowestError = error
            with open(self.bestSOFMfile, 'wb') as f:
                pickle.dump(sofm, f)

        self.logRow(outputRows, outputCols, learningRadius, weightInit,
                    gridType, error, errorPercent)
Exemple #3
0
    def test_sofm_learning_radius_reduction(self):
        input_data = 4 * np.ones((1, 2))
        default_params = dict(n_inputs=2,
                              n_outputs=3,
                              weight=np.ones((2, 3)),
                              learning_radius=1)

        sofm_1 = algorithms.SOFM(reduce_radius_after=1, **default_params)
        sofm_1.train(input_data, epochs=4)
        dist_1 = np.linalg.norm(sofm_1.weight.T - input_data)

        sofm_2 = algorithms.SOFM(reduce_radius_after=3, **default_params)
        sofm_2.train(input_data, epochs=4)
        dist_2 = np.linalg.norm(sofm_2.weight.T - input_data)

        # SOFM-2 suppose to be closer, becase learning radius
        # decreases slower with bigger reduction factor
        self.assertLess(dist_2, dist_1)

        sofm_3 = algorithms.SOFM(reduce_radius_after=None, **default_params)
        sofm_3.train(input_data, epochs=4)
        dist_3 = np.linalg.norm(sofm_3.weight.T - input_data)

        # SOFM-3 doesn't have any learning radius reduction, so it
        # converges even faster than SOFM-2
        self.assertLess(dist_3, dist_2)
Exemple #4
0
    def test_sofm_hexagon_grid(self):
        data = make_circle(max_samples=100)
        sofm = algorithms.SOFM(
            n_inputs=2,
            n_outputs=9,
            learning_radius=1,
            reduce_radius_after=4,
            features_grid=(3, 3),
            verbose=False,
            grid_type='hexagon',
        )
        sofm.train(data, epochs=10)
        grid = sofm.weight.reshape((2, 3, 3))

        center = grid[:, 1, 1]
        top_left = grid[:, 0, 0]
        top_right = grid[:, 0, 2]

        distance_top_left = np.linalg.norm(center - top_left)
        distance_top_right = np.linalg.norm(center - top_right)

        self.assertLess(distance_top_right, distance_top_left)

        bottom_left = grid[:, 2, 0]
        bottom_right = grid[:, 2, 2]

        distance_bottom_left = np.linalg.norm(center - bottom_left)
        distance_bottom_right = np.linalg.norm(center - bottom_right)

        self.assertLess(distance_bottom_right, distance_bottom_left)
Exemple #5
0
 def test_sofm_weight_init_exceptions(self):
     msg = "Cannot apply PCA"
     with self.assertRaisesRegexp(WeightInitializationError, msg):
         algorithms.SOFM(n_inputs=4,
                         n_outputs=7,
                         weight='init_pca',
                         grid_type='hexagon')
Exemple #6
0
    def test_sofm_euclide_norm_distance(self):
        weight = np.array([
            [1.41700099, 0.52680476],
            [-0.60938464, 1.56545643],
            [-0.30243644, 0.13994967],
            [-0.07456091, 0.54797268],
            [-1.12894803, 0.32702141],
            [0.92084690, 0.02683249],
        ]).T
        sn = algorithms.SOFM(n_inputs=2,
                             n_outputs=6,
                             weight=weight,
                             distance='euclid',
                             learning_radius=1,
                             features_grid=(3, 2),
                             verbose=False)

        sn.train(input_data, epochs=10)

        answers = np.array([
            [0., 0., 0., 1., 0., 0.],
            [0., 0., 0., 1., 0., 0.],
            [1., 0., 0., 0., 0., 0.],
            [1., 0., 0., 0., 0., 0.],
            [0., 0., 0., 0., 1., 0.],
            [0., 0., 0., 0., 1., 0.],
        ])

        np.testing.assert_array_almost_equal(sn.predict(input_data), answers)
Exemple #7
0
    def test_sofm_euclide_norm_distance(self):
        weight = np.array([
            [1.41700099, 0.52680476],
            [-0.60938464, 1.56545643],
            [-0.30243644, 0.13994967],
            [-0.07456091, 0.54797268],
            [-1.12894803, 0.32702141],
            [0.92084690, 0.02683249],
        ]).T
        input_layer = EuclideDistanceLayer(2, weight=weight)
        output_layer = CompetitiveOutputLayer(6)

        sn = algorithms.SOFM(input_layer > output_layer,
                             learning_radius=1,
                             features_grid=(3, 2),
                             verbose=False)

        sn.train(input_data, epochs=10)

        answers = np.array([
            [0., 0., 0., 1., 0., 0.],
            [0., 0., 0., 1., 0., 0.],
            [1., 0., 0., 0., 0., 0.],
            [1., 0., 0., 0., 0., 0.],
            [0., 0., 0., 0., 1., 0.],
            [0., 0., 0., 0., 1., 0.],
        ])

        for data, answer in zip(input_data, answers):
            network_output = sn.predict(np.reshape(data, (2, 1)).T)
            correct_result = np.reshape(answer, (6, 1)).T
            self.assertTrue(np.all(network_output == correct_result))
Exemple #8
0
    def test_linear_weight_init_in_sofm(self):
        sofm = algorithms.SOFM(
            n_inputs=4,
            features_grid=(3, 3),
            weight='init_pca',
        )

        input_data = np.random.random((100, 4))
        self.assertTrue(callable(sofm.weight))

        sofm.train(input_data, epochs=0)
        self.assertFalse(callable(sofm.weight))
        self.assertEqual(sofm.weight.shape, (4, 9))

        for row in (0, 3, 6):
            left = sofm.weight[:, row]
            center = sofm.weight[:, row + 1]
            right = sofm.weight[:, row + 2]

            self.assertLess(np.linalg.norm((left - center)**2),
                            np.linalg.norm((left - right)**2))

        for i in range(3):
            top = sofm.weight[:, i]
            center = sofm.weight[:, i + 3]
            bottom = sofm.weight[:, i + 6]

            self.assertLess(np.linalg.norm((top - center)**2),
                            np.linalg.norm((top - bottom)**2))
Exemple #9
0
    def test_invalid_attrs(self):
        with self.assertRaises(ValueError):
            algorithms.SOFM(LinearLayer(2) > OutputLayer(3),
                            learning_radius=-1,
                            verbose=False)

        with self.assertRaises(ValueError):
            algorithms.SOFM(LinearLayer(2) > OutputLayer(2),
                            learning_radius=1,
                            verbose=False)

        with self.assertRaises(ValueError):
            algorithms.SOFM(LinearLayer(2) > CompetitiveOutputLayer(4),
                            learning_radius=-1,
                            features_grid=(2, 3),
                            verbose=False)
Exemple #10
0
    def test_predict_different_inputs(self):
        input_layer = LinearLayer(1)
        output_layer = CompetitiveOutputLayer(2)

        sofmnet = algorithms.SOFM(input_layer > output_layer, verbose=False)
        target = np.array([
            [1, 0],
            [1, 0],
            [0, 1],
            [1, 0],
            [1, 0],
            [1, 0],
            [1, 0],
            [0, 1],
            [0, 1],
            [0, 1],
            [0, 1],
            [0, 1],
        ])

        sofmnet.train(input_data.ravel())
        self.assertInvalidVectorPred(sofmnet,
                                     input_data.ravel(),
                                     target,
                                     decimal=2)
Exemple #11
0
    def test_train_different_inputs(self):
        input_layer = LinearLayer(1)
        output_layer = CompetitiveOutputLayer(1)

        self.assertInvalidVectorTrain(
            algorithms.SOFM(input_layer > output_layer, verbose=False),
            input_data.ravel())
Exemple #12
0
    def test_sofm_angle_distance(self):
        sn = algorithms.SOFM(
            n_inputs=2,
            n_outputs=3,
            transform='cos',
            learning_radius=1,
            features_grid=(3, 1),
            verbose=False
        )

        sn.train(input_data, epochs=6)

        answers = np.array([
            [1., 0., 0.],
            [1., 0., 0.],
            [0., 1., 0.],
            [0., 1., 0.],
            [0., 0., 1.],
            [0., 0., 1.],
        ])

        np.testing.assert_array_almost_equal(
            sn.predict(input_data),
            answers
        )
Exemple #13
0
    def test_sofm_custom_parameter_reduction(self):
        input_data = 4 * np.ones((1, 2))
        default_params = dict(
            n_inputs=2,
            n_outputs=3,
            weight=np.ones((2, 3)),

            std=1,
            step=0.1,
            learning_radius=1,

            reduce_radius_after=None,
            reduce_step_after=None,
            reduce_std_after=None)

        sofm_1 = algorithms.SOFM(**default_params)
        sofm_1.train(input_data, epochs=4)
        dist_1 = np.linalg.norm(sofm_1.weight.T - input_data)

        def on_epoch_end_update_radius(network):
            if network.last_epoch % 2 == 0:
                network.learning_radius = 0
            else:
                network.learning_radius = 1

        def on_epoch_end_update_step(network):
            network.step = 0.1 / network.last_epoch

        def on_epoch_end_update_std(network):
            network.std = 1. / network.last_epoch

        testcases = {
            'learning_radius': on_epoch_end_update_radius,
            'step': on_epoch_end_update_step,
            'std': on_epoch_end_update_std,
        }

        for testcase_name, on_epoch_end in testcases.items():
            sofm_2 = algorithms.SOFM(
                epoch_end_signal=on_epoch_end,
                **default_params
            )
            sofm_2.train(input_data, epochs=4)
            dist_2 = np.linalg.norm(sofm_2.weight.T - input_data)

            self.assertLess(dist_1, dist_2,
                            msg="Test case name: {}".format(testcase_name))
Exemple #14
0
 def test_sofm_1d_vector_input(self):
     sofm = algorithms.SOFM(
         n_inputs=2,
         n_outputs=3,
         weight=self.weight,
     )
     output = sofm.predict(input_data[0])
     self.assertEqual(output.shape, (1, 3))
Exemple #15
0
 def test_invalid_attrs(self):
     with self.assertRaises(ValueError):
         # Invalid feature grid shape
         algorithms.SOFM(n_inputs=2,
                         n_outputs=4,
                         learning_radius=0,
                         features_grid=(2, 3),
                         verbose=False)
Exemple #16
0
    def test_invalid_attrs(self):
        with self.assertRaisesRegexp(ValueError, "Feature grid"):
            # Invalid feature grid shape
            algorithms.SOFM(n_inputs=2, n_outputs=4, features_grid=(2, 3))

        with self.assertRaisesRegexp(ValueError, "n_outputs, features_grid"):
            algorithms.SOFM(n_inputs=2)

        with self.assertRaisesRegexp(ValueError, "more than 2 dimensions"):
            sofm = algorithms.SOFM(n_inputs=2, n_outputs=3, weight=self.weight)
            sofm.train(np.zeros((10, 2, 1)))

        with self.assertRaisesRegexp(ValueError, "more than 2 dimensions"):
            sofm = algorithms.SOFM(n_inputs=2, n_outputs=3, weight=self.weight)
            sofm.predict(np.zeros((10, 2, 1)))

        with self.assertRaisesRegexp(ValueError, "Input data expected"):
            sofm = algorithms.SOFM(n_inputs=2, n_outputs=3, weight=self.weight)
            sofm.train(np.zeros((10, 10)))

        with self.assertRaisesRegexp(ValueError, "Input data expected"):
            sofm = algorithms.SOFM(n_inputs=2, n_outputs=3, weight=self.weight)
            sofm.predict(np.zeros((10, 10)))

        with self.assertRaisesRegexp(ValueError, "one or two dimensional"):
            algorithms.SOFM(n_inputs=2,
                            features_grid=(3, 1, 1),
                            grid_type='hexagon')
Exemple #17
0
    def test_sofm_double_initialization_exception(self):
        sofm = algorithms.SOFM(n_inputs=2,
                               n_outputs=3,
                               verbose=False,
                               weight='sample_from_data')
        sofm.init_weights(X)

        with self.assertRaises(WeightInitializationError):
            sofm.init_weights(X)
Exemple #18
0
    def test_sofm_std_parameter(self):
        default_params = dict(n_inputs=2,
                              n_outputs=3,
                              learning_radius=1,
                              weight=self.weight)

        sofm_1 = algorithms.SOFM(std=1, **default_params)
        sofm_1.train(input_data[:1], epochs=1)
        dist_1 = np.linalg.norm(sofm_1.weight[0, :] - sofm_1.weight[1, :])

        sofm_0 = algorithms.SOFM(std=0.1, **default_params)
        sofm_0.train(input_data[:1], epochs=1)
        dist_0 = np.linalg.norm(sofm_0.weight[0, :] - sofm_0.weight[1, :])

        # Since SOFM-1 has bigger std than SOFM-0, two updated
        # neurons should be closer to each other for SOFM-1 than
        # for SOFM-0
        self.assertLess(dist_1, dist_0)
Exemple #19
0
    def test_sofm(self):
        sn = algorithms.SOFM(n_inputs=2,
                             n_outputs=3,
                             weight=self.weight,
                             learning_radius=0,
                             features_grid=(3, 1),
                             verbose=False)

        sn.train(input_data, epochs=100)
        np.testing.assert_array_almost_equal(sn.predict(input_data), answers)
Exemple #20
0
 def init_sofm(self, lr=0.5):
     self.sofm = algorithms.SOFM(n_inputs=128,
                                 n_outputs=300,
                                 step=lr,
                                 learning_radius=0,
                                 weight='init_pca',
                                 shuffle_data=True,
                                 verbose=True)
     with open('sofm_iam.pkl', 'wb') as output:
         pickle.dump(self.sofm, output, pickle.HIGHEST_PROTOCOL)
Exemple #21
0
    def test_sofm_weight_norm_before_training(self):
        sofm = algorithms.SOFM(
            n_inputs=2,
            n_outputs=3,
            verbose=False,
            distance='cos',
        )

        actual_norms = np.linalg.norm(sofm.weight, axis=0)
        expected_norms = np.array([1, 1, 1])

        np.testing.assert_array_almost_equal(expected_norms, actual_norms)
Exemple #22
0
    def test_sofm_init_during_the_training(self):
        sofm = algorithms.SOFM(
            n_inputs=4,
            n_outputs=7,
            weight='sample_from_data',
        )

        X = np.random.random((10, 4))
        self.assertTrue(callable(sofm.weight))

        sofm.train(X, epochs=1)
        self.assertFalse(callable(sofm.weight))
Exemple #23
0
def combina_colonne(lista):
    vett = np.asarray(lista)
    #print("vett:",vett)

    comb = []
    for i in range(len(vett[0])):
        temp = []
        for a in range(len(vett)):
            temp.append(vett[a][i])
        comb.append(temp)
    data = np.asarray(comb)
    #print("data:",data)
    #print("len_data:",len(data))

    #som con 20 neuroni
    GRID_HEIGHT = 1
    GRID_WIDTH = 20

    sofm = algorithms.SOFM(
        n_inputs=len(data[0]),
        features_grid=(GRID_HEIGHT, GRID_WIDTH),

        # Learning radius defines area within which we find
        # winning neuron neighbours. The higher the value
        # the more values we will be updated after each iteration.
        learning_radius=5,
        # Every 20 epochs learning radius will be reduced by 1.
        reduce_radius_after=50,
        step=0.4,
        std=1,
        #show_epoch=5
        shuffle_data=True,
        verbose=True,
    )

    sofm.train(data, epochs=500)
    clusters = sofm.predict(data).argmax(axis=1)
    #print("cluster:",clusters)
    #print("len_clusters:",len(clusters))
    '''
    colonna=[]
    for i in range(0,len(lista[0])):
        
        #sum=0
        for a in range(0,len(lista)):
            sum+=(2**a)*int(lista[a][i])
            
    
        colonna.append(str(sum))
    print("colonna:",colonna)
    exit(1)
    '''
    return clusters, sofm
Exemple #24
0
    def test_sample_data_weight_init_in_sofm(self):
        sofm = algorithms.SOFM(
            n_inputs=4,
            n_outputs=7,
            weight='sample_from_data',
        )

        input_data = np.random.random((10, 4))
        self.assertTrue(callable(sofm.weight))

        sofm.train(input_data, epochs=0)
        self.assertFalse(callable(sofm.weight))
        self.assertEqual(sofm.weight.shape, (4, 7))
Exemple #25
0
    def test_sofm(self):
        input_layer = LinearLayer(2, weight=self.weight)
        output_layer = CompetitiveOutputLayer(3)

        sn = algorithms.SOFM(input_layer > output_layer,
                             learning_radius=0,
                             features_grid=(3, 1),
                             verbose=False)

        sn.train(input_data, epochs=100)

        for data, answer in zip(input_data, answers):
            network_output = sn.predict(np.reshape(data, (2, 1)).T)
            correct_result = np.reshape(answer, (3, 1)).T
            self.assertTrue(np.all(network_output == correct_result))
Exemple #26
0
    def test_sofm_storage(self):
        input_data = np.random.random((100, 10))
        sofm = algorithms.SOFM(n_inputs=10,
                               features_grid=(10, 10),
                               std=1,
                               step=0.5,
                               learning_radius=2,
                               weight=np.random.random((10, 100)))

        sofm.train(input_data, epochs=10)
        self.assertPickledNetwork(sofm, input_data)

        parameters = sofm.get_params()
        self.assertIn('weight', parameters)
        self.assertIsInstance(parameters['weight'], np.ndarray)
Exemple #27
0
    def test_sofm_training_with_4d_grid(self):
        sofm = algorithms.SOFM(
            n_inputs=4,
            n_outputs=8,
            features_grid=(2, 2, 2),
            verbose=False,
        )

        data = np.concatenate([input_data, input_data], axis=1)

        sofm.train(data, epochs=1)
        error_after_first_epoch = sofm.errors.last()

        sofm.train(data, epochs=9)
        self.assertLess(sofm.errors.last(), error_after_first_epoch)
def main():

    preprocess()
    #normalise()
    
    alldata=[]


#    with open (path+'normalised', 'rb') as fp:
    with open (path+'outfile.1', 'rb') as fp:

        alldata = pickle.load(fp)
    
    for data in alldata:
        plt.scatter(data[0], data[1], color='silver', alpha=0.1)
    
    sofmnet = algorithms.SOFM(
        n_inputs=2,
        n_outputs=5,
    
        step=0.01,
        std=0.1,
        reduce_step_after=200,
        reduce_radius_after=2,
        reduce_std_after=20,
        show_epoch=10,
        shuffle_data=True,
        verbose=True,

        learning_radius=2,
        distance='euclid',
        features_grid=(5, 1),
    )
    
    sofmnet.train(alldata, epochs=10)

    plt.scatter(sofmnet.weight[0:1,:], sofmnet.weight[1:2,:], color='red')
    
    with open (path+'test_output.1', 'rb') as fp:

        testdata = pickle.load(fp)
                
    for data in testdata:
        plt.scatter(data[0], data[1], color='green')
        
    test(sofmnet)
    plt.savefig('graph_with_test.png')
    plt.show()
Exemple #29
0
    def test_sofm(self):
        sn = algorithms.SOFM(
            n_inputs=2,
            n_outputs=3,
            weight=input_data[(2, 0, 4), :].T,
            learning_radius=0,
            features_grid=(3, ),
            shuffle_data=True,
            verbose=False,
            reduce_radius_after=None,
            reduce_step_after=None,
            reduce_std_after=None,
        )
        sn.train(input_data, epochs=100)

        np.testing.assert_array_almost_equal(sn.predict(input_data), answers)
Exemple #30
0
def codebook_generation(num_batches,sofm,epoch):

    if(sofm is None):
        sofm = algorithms.SOFM(n_inputs = 128,
                                     n_outputs = 300 ,
                                     step = 0.5,
                                     learning_radius = 0,
                                     signals=on_epoch_end
                                     )


    with h5py.File('Datasets/SDpoints0.h5', 'r') as hf:
        data = hf['keypoints-batch'][:]
    for x in range(1,int(num_batches)+1):
        with h5py.File('Datasets/SDpoints0.h5', 'r') as hf:
            data = np.append(data,hf['keypoints-batch'][:],axis=0)
    sofm.train(data,epochs=int(epoch))