Esempio n. 1
1
 def test_CrossValidation_ReturnsError(self):
     model = ELM(5, 2)
     model.add_neurons(10, 'tanh')
     X = np.random.rand(100, 5)
     T = np.random.rand(100, 2)
     err = model.train(X, T, 'CV', k=3)
     self.assertIsNotNone(err)
Esempio n. 2
0
class ELM(Classifier):
    def __init__(self, neurons: Tuple[Tuple] = None) -> None:
        clf = None
        self.neurons = neurons if neurons else DEFAULT_NEURONS
        super().__init__(clf)

    def fit(self, x_train: ndarray, y_train: ndarray, *args, **kwargs)\
            -> None:
        self.classifier = ELMachine(x_train.shape[1], y_train.shape[1])
        for neuron in self.neurons:
            logger.info("Adding {} neurons with '{}' function.".format(
                neuron[0], neuron[1]))
            self.classifier.add_neurons(neuron[0], neuron[1])
        logger.debug("Training the Extreme Learning Machine Classifier...")
        start = time()
        self.classifier.train(x_train, y_train, **kwargs)
        logger.debug("Done training in {} seconds.".format(time() - start))

    def predict(self, x_test: ndarray) -> ndarray:
        logger.debug("Predicting {} samples...".format(x_test.shape[0]))
        start = time()
        predictions = np.argmax(self.classifier.predict(x_test), axis=-1)
        logger.debug("Done all predictions in {} seconds.".format(time() -
                                                                  start))
        return predictions

    def predict_proba(self, x_test: ndarray) -> float:
        logger.debug("Predicting {} samples...".format(x_test.shape[0]))
        start = time()
        predictions = self.classifier.predict(x_test)
        logger.debug("Done all predictions in {} seconds.".format(time() -
                                                                  start))
        return predictions
 def test_24_AddNeurons_InitDefault_BiasWNotZero(self):
     elm = ELM(2, 1)
     elm.add_neurons(3, "sigm")
     W = elm.neurons[0][2]
     bias = elm.neurons[0][3]
     self.assertGreater(np.sum(np.abs(W)), 0.001)
     self.assertGreater(np.sum(np.abs(bias)), 0.001)
Esempio n. 4
0
class HPELMNN(Classifier):
    
    def __init__(self):
        self.__hpelm = None
    
    @staticmethod
    def name():
        return "hpelmnn"

    def train(self, X, Y, class_number=-1):
        class_count = max(np.unique(Y).size, class_number)
        feature_count = X.shape[1]
        self.__hpelm = ELM(feature_count, class_count, 'wc')
        self.__hpelm.add_neurons(feature_count, "sigm")

        Y_arr = Y.reshape(-1, 1)
        enc = OneHotEncoder()
        enc.fit(Y_arr)
        Y_OHE = enc.transform(Y_arr).toarray()

        out_fd = sys.stdout
        sys.stdout = open(os.devnull, 'w')
        self.__hpelm.train(X, Y_OHE)
        sys.stdout = out_fd

    def predict(self, X):
        Y_predicted = self.__hpelm.predict(X)
        return Y_predicted
 def test_ELM_SaveLoad(self):
     X = np.array([1, 2, 3, 1, 2, 3])
     T = np.array([[1, 0], [1, 0], [1, 0], [0, 1], [0, 1], [0, 1]])
     elm = ELM(1, 2, precision='32', norm=0.02)
     elm.add_neurons(1, "lin")
     elm.add_neurons(2, "tanh")
     elm.train(X, T, "wc", w=(0.7, 0.3))
     B1 = elm.nnet.get_B()
     try:
         f, fname = tempfile.mkstemp()
         elm.save(fname)
         elm2 = ELM(3, 3)
         elm2.load(fname)
     finally:
         os.close(f)
     self.assertEqual(elm2.nnet.inputs, 1)
     self.assertEqual(elm2.nnet.outputs, 2)
     self.assertEqual(elm2.classification, "wc")
     self.assertIs(elm.precision, np.float32)
     self.assertIs(elm2.precision, np.float64)  # precision has changed
     np.testing.assert_allclose(np.array([0.7, 0.3]), elm2.wc)
     np.testing.assert_allclose(0.02, elm2.nnet.norm)
     np.testing.assert_allclose(B1, elm2.nnet.get_B())
     self.assertEqual(elm2.nnet.get_neurons()[0][1], "lin")
     self.assertEqual(elm2.nnet.get_neurons()[1][1], "tanh")
Esempio n. 6
0
 def test_MultiLabelClassification_Works(self):
     elm = ELM(1, 2)
     X = np.array([1, 2, 3, 4, 5, 6])
     T = np.array([[1, 1], [1, 0], [1, 0], [0, 1], [0, 1], [1, 1]])
     elm.add_neurons(1, "lin")
     elm.train(X, T, 'ml')
     elm.train(X, T, 'mc')
 def test_MultiLabelClassification_Works(self):
     elm = ELM(1, 2)
     X = np.array([1, 2, 3, 4, 5, 6])
     T = np.array([[1, 1], [1, 0], [1, 0], [0, 1], [0, 1], [1, 1]])
     elm.add_neurons(1, "lin")
     elm.train(X, T, 'ml')
     elm.train(X, T, 'mc')
Esempio n. 8
0
 def test_ELM_SaveLoad(self):
     X = np.array([1, 2, 3, 1, 2, 3])
     T = np.array([[1, 0], [1, 0], [1, 0], [0, 1], [0, 1], [0, 1]])
     elm = ELM(1, 2, precision='32', norm=0.02)
     elm.add_neurons(1, "lin")
     elm.add_neurons(2, "tanh")
     elm.train(X, T, "wc", w=(0.7, 0.3))
     B1 = elm.nnet.get_B()
     try:
         f, fname = tempfile.mkstemp()
         elm.save(fname)
         elm2 = ELM(3, 3)
         elm2.load(fname)
     finally:
         os.close(f)
     self.assertEqual(elm2.nnet.inputs, 1)
     self.assertEqual(elm2.nnet.outputs, 2)
     self.assertEqual(elm2.classification, "wc")
     self.assertIs(elm.precision, np.float32)
     self.assertIs(elm2.precision, np.float64)  # precision has changed
     np.testing.assert_allclose(np.array([0.7, 0.3]), elm2.wc)
     np.testing.assert_allclose(0.02, elm2.nnet.norm)
     np.testing.assert_allclose(B1, elm2.nnet.get_B())
     self.assertEqual(elm2.nnet.get_neurons()[0][1], "lin")
     self.assertEqual(elm2.nnet.get_neurons()[1][1], "tanh")
Esempio n. 9
0
 def test_24_AddNeurons_InitDefault_BiasWNotZero(self):
     elm = ELM(2, 1)
     elm.add_neurons(3, "sigm")
     W = elm.neurons[0][2]
     bias = elm.neurons[0][3]
     self.assertGreater(np.sum(np.abs(W)), 0.001)
     self.assertGreater(np.sum(np.abs(bias)), 0.001)
 def test_TrainWithBatch_OverwritesBatch(self):
     elm = ELM(1, 1, batch=123)
     X = np.array([1, 2, 3])
     T = np.array([1, 2, 3])
     elm.add_neurons(1, "lin")
     elm.train(X, T, batch=234)
     self.assertEqual(234, elm.batch)
Esempio n. 11
0
    def tune_elm(train_x, train_y, test_x_raw, test_x, act_funcs,
                 neuron_counts):
        '''
		Assumptions:
		1. NN has only 1 hidden layer
		2. act_funcs: list of distinct activation functions
		3. neuron_counts: list of distinct '# of neurons in the hidden layer'
		'''
        print("Tuning ELM...")
        features = train_x.shape[1]
        train_y = Pre_processor.one_hot_encoding(train_y)
        ind_func = 0
        while (ind_func < len(act_funcs)):
            ind_neuron = 0
            cur_act_func = act_funcs[ind_func]
            while (ind_neuron < len(neuron_counts)):
                cur_neuron_count = neuron_counts[ind_neuron]
                print(cur_act_func + " | " + str(cur_neuron_count) + "...")
                clf = ELM(features, Constants.tot_labels)
                clf.add_neurons(cur_neuron_count, cur_act_func)
                clf.train(train_x, train_y, 'CV', 'OP', 'c', k=10)
                pred_y = clf.predict(test_x)
                pred_y = Pre_processor.one_hot_decoding_full(pred_y)
                file_name = "submission_" + str(
                    cur_neuron_count) + "_" + cur_act_func + ".csv"
                Database.save_results(test_x_raw, pred_y, file_name)
                ind_neuron = ind_neuron + 1
            ind_func = ind_func + 1
Esempio n. 12
0
 def test_TrainWithBatch_OverwritesBatch(self):
     elm = ELM(1, 1, batch=123)
     X = np.array([1, 2, 3])
     T = np.array([1, 2, 3])
     elm.add_neurons(1, "lin")
     elm.train(X, T, batch=234)
     self.assertEqual(234, elm.batch)
Esempio n. 13
0
 def test_CrossValidation_ReturnsError(self):
     model = ELM(5, 2)
     model.add_neurons(10, 'tanh')
     X = np.random.rand(100, 5)
     T = np.random.rand(100, 2)
     err = model.train(X, T, 'CV', k=3)
     self.assertIsNotNone(err)
 def test_MultiLabelClassError_Works(self):
     X = np.array([1, 2, 3])
     T = np.array([[0, 1], [1, 1], [1, 0]])
     Y = np.array([[0.4, 0.6], [0.8, 0.6], [1, 1]])
     elm = ELM(1, 2, classification="ml")
     elm.add_neurons(1, "lin")
     e = elm.error(T, Y)
     np.testing.assert_allclose(e, 1.0 / 6)
Esempio n. 15
0
 def test_AddNeurons_WorksWithLongType(self):
     if sys.version_info[0] == 2:
         ltype = long
     else:
         ltype = int
     model = ELM(3, 2)
     L = ltype(10)
     model.add_neurons(L, 'tanh')
Esempio n. 16
0
def getElm(data, label, classification='c', w=None, nn=10, func="sigm"):

    elm = ELM(len(data[0]), len(label[0]), classification, w)
    elm.add_neurons(10, func)
    elm.add_neurons(10, "rbf_l1")
    elm.add_neurons(10, "rbf_l2")
    elm.train(data, np.array(label))
    return elm
 def test_20_SLFN_AddTwoNeuronTypes_GotThem(self):
     elm = ELM(1, 1)
     elm.add_neurons(1, "lin")
     elm.add_neurons(1, "sigm")
     self.assertEquals(2, len(elm.neurons))
     ntypes = [nr[0] for nr in elm.neurons]
     self.assertIn("lin", ntypes)
     self.assertIn("sigm", ntypes)
Esempio n. 18
0
 def test_MultiLabelClassError_Works(self):
     X = np.array([1, 2, 3])
     T = np.array([[0, 1], [1, 1], [1, 0]])
     Y = np.array([[0.4, 0.6], [0.8, 0.6], [1, 1]])
     elm = ELM(1, 2, classification="ml")
     elm.add_neurons(1, "lin")
     e = elm.error(T, Y)
     np.testing.assert_allclose(e, 1.0 / 6)
Esempio n. 19
0
 def test_AddNeurons_WorksWithLongType(self):
     if sys.version_info[0] == 2:
         ltype = long
     else:
         ltype = int
     model = ELM(3, 2)
     L = ltype(10)
     model.add_neurons(L, 'tanh')
Esempio n. 20
0
 def test_20_SLFN_AddTwoNeuronTypes_GotThem(self):
     elm = ELM(1, 1)
     elm.add_neurons(1, "lin")
     elm.add_neurons(1, "sigm")
     self.assertEquals(2, len(elm.neurons))
     ntypes = [nr[0] for nr in elm.neurons]
     self.assertIn("lin", ntypes)
     self.assertIn("sigm", ntypes)
Esempio n. 21
0
 def epoch(train_x, train_y, test_x, test_x_raw, filename):
     features = train_x.shape[1]
     train_y = Pre_processor.one_hot_encoding(train_y)
     clf = ELM(features, Constants.tot_labels)
     clf.add_neurons(550, "sigm")
     clf.train(train_x, train_y, 'CV', 'OP', 'c', k=10)
     pred_y = clf.predict(test_x)
     pred_y = Pre_processor.one_hot_decoding_full(pred_y)
     Database.save_results(test_x_raw, pred_y, filename)
 def test_Classification_WorksCorreclty(self):
     elm = ELM(1, 2)
     X = np.array([-1, -0.6, -0.3, 0.3, 0.6, 1])
     T = np.array([[1, 0], [1, 0], [1, 0], [0, 1], [0, 1], [0, 1]])
     elm.add_neurons(1, "lin")
     elm.train(X, T, 'c')
     Y = elm.predict(X)
     self.assertGreater(Y[0, 0], Y[0, 1])
     self.assertLess(Y[5, 0], Y[5, 1])
Esempio n. 23
0
 def test_WeightedClassError_Works(self):
     T = np.array([[0, 1], [0, 1], [1, 0]])
     Y = np.array([[0, 1], [0.4, 0.6], [0, 1]])
     # here class 0 is totally incorrect, and class 1 is totally correct
     w = (9, 1)
     elm = ELM(1, 2, classification="wc", w=w)
     elm.add_neurons(1, "lin")
     e = elm.error(T, Y)
     np.testing.assert_allclose(e, 0.9)
 def test_WeightedClassError_Works(self):
     T = np.array([[0, 1], [0, 1], [1, 0]])
     Y = np.array([[0, 1], [0.4, 0.6], [0, 1]])
     # here class 0 is totally incorrect, and class 1 is totally correct
     w = (9, 1)
     elm = ELM(1, 2, classification="wc", w=w)
     elm.add_neurons(1, "lin")
     e = elm.error(T, Y)
     np.testing.assert_allclose(e, 0.9)
Esempio n. 25
0
 def test_Classification_WorksCorreclty(self):
     elm = ELM(1, 2)
     X = np.array([-1, -0.6, -0.3, 0.3, 0.6, 1])
     T = np.array([[1, 0], [1, 0], [1, 0], [0, 1], [0, 1], [0, 1]])
     elm.add_neurons(1, "lin")
     elm.train(X, T, 'c')
     Y = elm.predict(X)
     self.assertGreater(Y[0, 0], Y[0, 1])
     self.assertLess(Y[5, 0], Y[5, 1])
Esempio n. 26
0
 def test_WeightedClassification_ClassWithLargerWeightWins(self):
     elm = ELM(1, 2)
     X = np.array([1, 2, 3, 1, 2, 3])
     T = np.array([[1, 0], [1, 0], [1, 0], [0, 1], [0, 1], [0, 1]])
     elm.add_neurons(1, "lin")
     elm.train(X, T, 'wc', w=(1, 0.1))
     Y = elm.predict(X)
     self.assertGreater(Y[0, 0], Y[0, 1])
     self.assertGreater(Y[1, 0], Y[1, 1])
     self.assertGreater(Y[2, 0], Y[2, 1])
Esempio n. 27
0
def build_ELM_encoder(xinput, target, num_neurons):

    elm = ELM(xinput.shape[1], target.shape[1])
    elm.add_neurons(num_neurons, "sigm")
    elm.add_neurons(num_neurons, "lin")
    #elm.add_neurons(num_neurons, "rbf_l1")
    elm.train(xinput, target, "r")
    ypred = elm.predict(xinput)
    print "mse error", elm.error(ypred, target)
    return elm, ypred
 def test_WeightedClassification_ClassWithLargerWeightWins(self):
     elm = ELM(1, 2)
     X = np.array([1, 2, 3, 1, 2, 3])
     T = np.array([[1, 0], [1, 0], [1, 0], [0, 1], [0, 1], [0, 1]])
     elm.add_neurons(1, "lin")
     elm.train(X, T, 'wc', w=(1, 0.1))
     Y = elm.predict(X)
     self.assertGreater(Y[0, 0], Y[0, 1])
     self.assertGreater(Y[1, 0], Y[1, 1])
     self.assertGreater(Y[2, 0], Y[2, 1])
 def test_25_AddNeurons_InitTwiceBiasW_CorrectlyMerged(self):
     elm = ELM(2, 1)
     W1 = np.random.rand(2, 3)
     W2 = np.random.rand(2, 4)
     bias1 = np.random.rand(3,)
     bias2 = np.random.rand(4,)
     elm.add_neurons(3, "sigm", W1, bias1)
     elm.add_neurons(4, "sigm", W2, bias2)
     np.testing.assert_array_almost_equal(np.hstack((W1, W2)), elm.neurons[0][2])
     np.testing.assert_array_almost_equal(np.hstack((bias1, bias2)), elm.neurons[0][3])
Esempio n. 30
0
 def test_LOOandOP_CanSelectMoreThanOneNeuron(self):
     X = np.random.rand(100, 5)
     T = np.random.rand(100, 2)
     for _ in range(10):
         model = ELM(5, 2)
         model.add_neurons(5, 'lin')
         model.train(X, T, 'LOO', 'OP')
         max2 = model.nnet.L
         if max2 > 1:
             break
     self.assertGreater(max2, 1)
Esempio n. 31
0
File: myEML.py Progetto: DIYer22/ELM
def getElm(data, label, classification='', w=None, nn=10, func="sigm"):

    print 'creat ELM, data,label shape=', data.shape, label.shape

    elm = ELM(data.shape[1],
              label.shape[1],
              classification=classification,
              w=w)
    elm.add_neurons(nn, func)
    elm.train(data, label, "c")
    return elm
Esempio n. 32
0
def build_ELM_encoder(xinput, target, num_neurons):


    elm = ELM(xinput.shape[1], target.shape[1])
    elm.add_neurons(num_neurons, "sigm")
    elm.add_neurons(num_neurons, "lin")
    #elm.add_neurons(num_neurons, "rbf_l1")
    elm.train(xinput, target, "r")
    ypred = elm.predict(xinput)
    print "mse error", elm.error(ypred, target)
    return elm, ypred
Esempio n. 33
0
 def test_LOOandOP_CanSelectMoreThanOneNeuron(self):
     X = np.random.rand(100, 5)
     T = np.random.rand(100, 2)
     for _ in range(10):
         model = ELM(5, 2)
         model.add_neurons(5, 'lin')
         model.train(X, T, 'LOO', 'OP')
         max2 = model.nnet.L
         if max2 > 1:
             break
     self.assertGreater(max2, 1)
Esempio n. 34
0
 def test_25_AddNeurons_InitTwiceBiasW_CorrectlyMerged(self):
     elm = ELM(2, 1)
     W1 = np.random.rand(2, 3)
     W2 = np.random.rand(2, 4)
     bias1 = np.random.rand(3, )
     bias2 = np.random.rand(4, )
     elm.add_neurons(3, "sigm", W1, bias1)
     elm.add_neurons(4, "sigm", W2, bias2)
     np.testing.assert_array_almost_equal(np.hstack((W1, W2)),
                                          elm.neurons[0][2])
     np.testing.assert_array_almost_equal(np.hstack((bias1, bias2)),
                                          elm.neurons[0][3])
def model_elm(XX, TT, XX_test, TT_test, model_type):
    '''
	Builda elm model using hpelm package

	Arguments:
	XX -- randomized and normalized training X-values, numpy array of shape (# compositions, # molar%)
	TT -- randomized and normalized training Y-values, numpy array of shape (Fragility value, 1)
	XX_test -- randomized and normalized testing X-values, numpy array of shape (# compositions, # molar%)
	TT_test -- randomized and normalized testing Y-values, numpy array of shape (Fragility value, 1)

	Returns:
	model -- save model in ELM format
	'''

    # Hyperparameters
    k = 5  # Use this if model_type == CV
    np.random.seed(10)

    # Build hpelm model
    # ELM(inputs, outputs, classification='', w=None, batch=1000, accelerator=None, precision='double', norm=None, tprint=5)
    model = ELM(20, 1, tprint=5)

    # Add neurons
    model.add_neurons(7, 'tanh')  # Number of neurons with tanh activation
    model.add_neurons(7, 'lin')  # Number of neurons with linear activation

    # if then condition for types of training
    if (model_type == 'CV'):
        print('-' * 10 + 'Training with Cross-Validation' + '-' * 10)
        model.train(XX, TT, 'CV', k=k)  # Train the model with cross-validation
    elif (model_type == 'LOO'):
        print('-' * 10 + 'Training with Leave-One-Out' + '-' * 10)
        model.train(XX, TT, 'LOO')  # Train the model with Leave-One-Out
    else:
        print('-' * 10 + 'Training with regression' + '-' * 10)
        model.train(XX, TT, 'r')  # Train the model with regression

    # Train ELM models
    TTH = model.predict(XX)  # Calculate training error
    YY_test = model.predict(XX_test)  # Calculate testing error
    print('Model Training Error: ', model.error(TT,
                                                TTH))  # Print training error
    print('Model Test Error: ', model.error(YY_test,
                                            TT_test))  # Print testing error
    print(str(model))  # Print model information
    print('-' * 50)

    # Call plot function
    my_plot(TT_test, YY_test)

    return model
Esempio n. 36
0
def model_training(model_path, data_path, neurons=300):
    images, labels = read_mnist.load_mnist(data_path, kind='train')

    images = map(read_mnist.up_to_2D, images)
    images = map(get_hog, images)
    images = np.mat(np.array(images))

    labels = np.mat(map(read_mnist.handle_label, labels))

    elm = ELM(images.shape[1], labels.shape[1])

    elm.add_neurons(neurons, 'sigm')
    elm.add_neurons(neurons, 'sigm')
    # elm.add_neurons(int(images.shape[1]*0.8), 'sigm')
    # elm.add_neurons(int(images.shape[1]*0.6), 'tanh')
    elm.train(images, labels)
    elm.save(model_path)
Esempio n. 37
0
def run(X, Y):
    X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=42)
    #ELM model
    X_train=X_train.values
    X_test=X_test.values
    y_train=y_train.values
    y_test=y_test.values
    print 'ELM tanh'
    for x in range(50, 500, 50):
        elm = ELM(X_train.shape[1], 1, classification='c')
        elm.add_neurons(x, 'tanh')
        elm.train(X_train, y_train)
        pred = elm.predict(X_test)
        temp = []
#        print 'Error(TANH, ', x, '): ', elm.error(y_test, pred)
        for p in pred:
            if p >=0.5:
                temp.append(1)
            else:
                temp.append(0)
        pred = np.asarray(temp)
#        print 'Error(TANH, ', x, '): ', elm.error(y_test, pred)
        evaluate(y_test, pred)
    
    print 'ELM rbf_linf tanh'
    for x in range(10, 100, 10):
        elm = ELM(X_train.shape[1], 1)
        elm.add_neurons(x, 'rbf_linf')
        elm.add_neurons(x*2, 'tanh')
        elm.train(X_train, y_train)
        pred = elm.predict(X_test)
        temp = []
#        print 'Error(TANH, ', x, '): ', elm.error(y_test, pred)
        for p in pred:
            if p >=0.5:
                temp.append(1)
            else:
                temp.append(0)
        pred = np.asarray(temp)
#        print 'Error(RBF+TANH, ', x, ',', 2*x, '): ', elm.error(y_test, pred)
        evaluate(y_test, pred)
Esempio n. 38
0
from hpelm import ELM
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

X = np.loadtxt("tweet_vecs.txt", delimiter=',')
Y = np.loadtxt("tweet_label.txt", delimiter=',')
Y_onehot = np.loadtxt("tweet_label_onehot.txt", delimiter=',')

X_train, X_test, Y_train, Y_test, Y_onehot_train, Y_onehot_test = train_test_split(X, Y, Y_onehot, test_size=0.20, shuffle=False)

print("Starting training...")
elm = ELM(X_train.shape[1], Y_onehot_train.shape[1])
elm.add_neurons(200, "sigm")
elm.add_neurons(100, "tanh")
elm.add_neurons(100, "sigm")
elm.add_neurons(100, "sigm")
elm.add_neurons(100, "tanh")
elm.train(X_train, Y_onehot_train, "CV", "OP", "c", k=5)
print("Finished training...")

Y_predicted_elm = elm.predict(X_test)
Y_predicted = np.zeros((Y_predicted_elm.shape[0]))
for i, row in enumerate(Y_predicted_elm):
    idx_of_max = np.argmax(row)
    Y_predicted[i] = idx_of_max+1

with open("Y_predicted.txt", 'w+') as predfile, open("Y_true.txt", 'w+') as trufile:
    for i in Y_predicted:
        predfile.write(str(i))
        predfile.write("\n")
result6 = open("data/result/lin.txt", "w")
result6.write("accuracy" + "\t" + "precision" + "\t" + "recall" + "\t" +
              "f1-measure" + "\t" + " mse" + "\t" + " mae" + "\t" + "auc" +
              "\t" + "tpr" + "\t" + "fpr")
result6.write("\n")

result6a = open("data/result/linmse.txt", "w")
result6a.write("accuracy" + "\t" + "precision" + "\t" + "recall" + "\t" +
               "f1-measure" + "\t" + " mse" + "\t" + " mae" + "\t" + "auc" +
               "\t" + "pr" + "\t" + "fpr")
result6a.write("\n")

print "sigmoid with multi class error"
elm = ELM(41, 41)
elm.add_neurons(40, "sigm")
elm.train(X, Y, "c")
r1 = elm.predict(X1)
result.write(str(elm))
result.write("\n")

r1 = r1.argmax(1)
accuracy = accuracy_score(Y1, r1)
recall = recall_score(Y1, r1, average="weighted")
precision = precision_score(Y1, r1, average="weighted")
f1 = f1_score(Y1, r1, average="weighted")
mse = mean_squared_error(Y1, r1)
mae = mean_absolute_error(Y1, r1)
fpr, tpr, thresholds = metrics.roc_curve(Y1, r1, pos_label=9)
auc = metrics.auc(fpr, tpr)
 def test_22_AddNeurons_InitBias_BiasInModel(self):
     elm = ELM(1, 1)
     bias = np.array([1, 2, 3])
     elm.add_neurons(3, "sigm", None, bias)
     np.testing.assert_array_almost_equal(bias, elm.neurons[0][3])
Esempio n. 41
0
X_test_d2 = Csp2.transform(x_test_d2)
print(X_train_d3.shape)
print(X_train_d2.shape)
print(X_test_d3.shape)
print(X_test_d2.shape)
X_train = ss.fit_transform(np.concatenate((X_train_d3, X_train_d2), axis=1))
X_test = ss.transform(np.concatenate((X_test_d3, X_test_d2), axis=1))
print(X_train.shape)
print(X_test.shape)

#use ELM as classifier

from hpelm import ELM
acc = []
elm = ELM(4, 1)
elm.add_neurons(50, 'sigm')
elm.train(X_train, y_train, "LOO")
y_pred = elm.predict(X_test)
print(len(y_pred))
for i in range(len(y_pred)):
    if y_pred[i] >= 0.5:
        y_pred[i] = 1
    else:
        y_pred[i] = 0
print(y_test)
acc.append(accuracy_score(y_test, y_pred))
avg_acc = np.mean(acc)
print(avg_acc)

# use LDA as classifier
 def test_8_OneDimensionTargets_RunsCorrectly(self):
     X = np.array([[1, 2], [3, 4], [5, 6]])
     T = np.array([[0], [0], [0]])
     elm = ELM(2, 1)
     elm.add_neurons(1, "lin")
     elm.train(X, T)
#id_input = np.random.permutation(n_example)
#id_input =  [i for i in range(n_example) if y[i] ==1]
#id_output = id_input[::-1]
#xinput = XXtrain[id_input,]
#xoutput = XXtrain[id_input,]
#print xinput.shape
## INPUT LAYER
singleLayer = True
if singleLayer:
    t = 5
    mn_error = np.zeros([t, 1])
    for i in range(0, t):
        elmSingle = ELM(input_shape, YY.shape[1])
        #elmSingle.add_neurons(ninputsig/2, "sigm")
        elmSingle.add_neurons(ninputsig, "sigm")
        #elmSingle.add_neurons(100, "rbf_l1")
        #elmSingle.add_neurons(ninputsig/10, "lin")
        elmSingle.train(XXtrainIn, YYtrain, "c", norm=1e-5)
        print "\n Trained input elm", elmSingle
        youtput = elmSingle.predict(XXtest)

        p = ytest.squeeze()
        yout = np.argmax(youtput, axis=1)
        nhit = sum(yout == p)
        ntpos = sum((yout == 1) & (p == 1))
        ntneg = sum((yout == 0) & (p == 0))
        npos = sum((p == 1))
        nneg = sum((p == 0))
        print "\n Testing results"
        print "Tpos:", ntpos, " / ", npos, "TD:", ntpos / float(npos)
 def test_6_WrongDimensionalityTargets_RaiseError(self):
     X = np.array([[1, 2], [3, 4], [5, 6]])
     T = np.array([[1], [2], [3]])
     elm = ELM(1, 2)
     elm.add_neurons(1, "lin")
     self.assertRaises(AssertionError, elm.train, X, T)
 def test_4_OneDimensionTargets_RunsCorrectly(self):
     X = np.array([1, 2, 3])
     T = np.array([1, 2, 3])
     elm = ELM(1, 1)
     elm.add_neurons(1, "lin")
     elm.train(X, T)
 def test_14_SLFN_AddSigmoidalNeurons_GotThem(self):
     elm = ELM(1, 1)
     elm.add_neurons(1, "sigm")
     self.assertEquals("sigm", elm.neurons[0][0])
Esempio n. 47
0
                        test.append(items[_id] + users[i])
                        temp = [0] * 5
                        temp[rating_mat['test_1'].T[mat_iid][i - 1] - 1] = 1
                        test_rat.append(temp)
                        #print mat_iid
                mat_iid += 1

        X = np.asarray(X, dtype=np.uint8)
        T = np.asarray(T, dtype=np.uint8)
        test = np.asarray(test, dtype=np.uint8)
        test_rat = np.asarray(test_rat, dtype=np.uint8)

        ##print X.shape,test.shape

        elm = ELM(X.shape[1], T.shape[1])
        elm.add_neurons(neuron, node)
        elm.train(X, T, "LOO")
        Y = elm.predict(test)

        pred = np.argmax(Y, axis=1)
        true = np.argmax(test_rat, axis=1)

        print 'Split 1 RMSE: ', mse(true, pred)**0.5
        print 'Split 1 NMAE: ', mae(true, pred) / 4

        i1_rmse = mse(true, pred)**0.5
        i1_nmae = mae(true, pred) / 4
        #################################SPLIT 2#####################################
        train_ids = map(int, train_ids_read.readline().strip().split(','))
        test_ids = map(int, test_ids_read.readline().strip().split(','))
 def test_13_SLFN_AddLinearNeurons_GotThem(self):
     elm = ELM(1, 1)
     elm.add_neurons(1, "lin")
     self.assertEquals("lin", elm.neurons[0][0])
 def test_12_LinearNeurons_DefaultMatrix_Identity(self):
     elm = ELM(4, 1)
     elm.add_neurons(3, "lin")
     np.testing.assert_array_almost_equal(np.eye(4, 3), elm.neurons[0][2])
 def test_11_LinearNeurons_MoreThanInputs_Truncated(self):
     elm = ELM(2, 1)
     elm.add_neurons(3, "lin")
     self.assertEqual(2, elm.neurons[0][1])
result5 = open("data/result1/tanh.txt", "w")

result5.write("accuracy" +  "\t" +  "precision" + "\t" + "recall" + "\t" + "f1-measure" +"\t" + " mse" + "\t" + " mae" + "\t" + "auc")
result5.write("\n")


result6 = open("data/result1/lin.txt", "w")

result6.write("accuracy" +  "\t" +  "precision" + "\t" + "recall" + "\t" + "f1-measure" +"\t" + " mse" + "\t" + " mae" + "\t" + "auc")
result6.write("\n")


print "sigmoid with multi class error"
elm = ELM(42,2)
elm.add_neurons(34, "sigm")
elm.train(X, Y, "c")
r1 = elm.predict(X)
print(str(elm))
print("performance measures")
result.write(str(elm))
result.write("\n")

r1=r1.argmax(1)
accuracy = accuracy_score(Y1, r1)
print(accuracy)
recall = recall_score(Y1, r1, average="binary")
precision = precision_score(Y1, r1 , average="binary")
f1 = f1_score(Y1, r1 , average="binary")
mse = mean_squared_error(Y1, r1)
mae = mean_absolute_error(Y1, r1)
 def test_23_AddNeurons_InitW_WInModel(self):
     elm = ELM(2, 1)
     W = np.array([[1, 2, 3], [4, 5, 6]])
     elm.add_neurons(3, "sigm", W, None)
     np.testing.assert_array_almost_equal(W, elm.neurons[0][2])
result5 = open("data/result/tanh.txt", "w")

result5.write("accuracy" +  "\t" +  "precision" + "\t" + "recall" + "\t" + "f1-measure" +"\t" + " mse" + "\t" + " mae" + "\t" + "auc")
result5.write("\n")


result6 = open("data/result/lin.txt", "w")

result6.write("accuracy" +  "\t" +  "precision" + "\t" + "recall" + "\t" + "f1-measure" +"\t" + " mse" + "\t" + " mae" + "\t" + "auc")
result6.write("\n")


print "sigmoid with multi class error"
elm = ELM(41,23)
elm.add_neurons(15, "sigm")
elm.train(X, Y, "c")
r1 = elm.predict(X1)
print(str(elm))
print("performance measures")
result.write(str(elm))
result.write("\n")

r1=r1.argmax(1)
accuracy = accuracy_score(Y1, r1)
print(accuracy)
recall = recall_score(Y1, r1, average="weighted")
precision = precision_score(Y1, r1 , average="weighted")
f1 = f1_score(Y1, r1 , average="weighted")
mse = mean_squared_error(Y1, r1)
mae = mean_absolute_error(Y1, r1)
 def test_7_ZeroInputs_RunsCorrectly(self):
     X = np.array([[0, 0], [0, 0], [0, 0]])
     T = np.array([1, 2, 3])
     elm = ELM(2, 1)
     elm.add_neurons(1, "lin")
     elm.train(X, T)
Esempio n. 55
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 19 16:29:09 2014

@author: akusok
"""

import numpy as np
import os
from hpelm import ELM

curdir = os.path.dirname(__file__)
pX = os.path.join(curdir, "../datasets/Unittest-Iris/iris_data.txt")
pY = os.path.join(curdir, "../datasets/Unittest-Iris/iris_classes.txt")

X = np.loadtxt(pX)
Y = np.loadtxt(pY)

elm = ELM(4, 3)
elm.add_neurons(15, "sigm")
elm.train(X, Y, "c")
Yh = elm.predict(X)
acc = float(np.sum(Y.argmax(1) == Yh.argmax(1))) / Y.shape[0]
print "Iris dataset training error: %.1f%%" % (100 - acc * 100)
 def test_3_OneDimensionInputs_RunsCorrectly(self):
     X = np.array([1, 2, 3])
     T = np.array([[1], [2], [3]])
     elm = ELM(1, 1)
     elm.add_neurons(1, "lin")
     elm.train(X, T)
#!/usr/bin/env python

import numpy as np
import time
import random
import sys
from hpelm import ELM

inp = np.loadtxt("input.txt")
outp = np.loadtxt("output.txt")

for neuron in range(10, 1000, 10):
    elm = ELM(92, 40)
    elm.add_neurons(neuron, "sigm")

    t0 = time.clock()
    elm.train(inp, outp, "c")
    t1 = time.clock()
    t = t1-t0

    pred = elm.predict(inp)
    acc = float(np.sum(outp.argmax(1) == pred.argmax(1))) / outp.shape[0]
    print "neuron=%d error=%.1f%% time=%dns" % (neuron, 100-acc*100, t*1000000)
    
    if int(acc) == 1:
        break
Esempio n. 58
0
def calc_W_B_para(C=0.7,input_node_num=input_node_num,hide_node_num=hide_node_num,):
    beta=C*math.pow(hide_node_num,(1/float(input_node_num)))
    W_old=np.random.uniform(-0.5,0.5,size=(input_node_num,hide_node_num))

    if input_node_num == 1:
        W_old = W_old / np.abs(W_old)
    else:
        W_old = np.sqrt(1. / np.square(W_old).sum(axis=1).reshape(input_node_num, 1)) * W_old
    W_new=beta*W_old
    Bias=np.random.uniform(-beta,beta,size=(hide_node_num,))
    return [W_new,Bias]
W,B=calc_W_B_para()

elm = ELM(input_node_num,output_node_num,ak=ak,bk=bk)
elm.add_neurons(20, "avg_arcsinh_morlet",W=W,B=B)
elm.train(X_learn, Y_learn, "r")

def plot_prognostic(train_out):

    inputs_regressors_num=list(X_learn[len(X_learn)-1,:])

    len_just_prog=len_prognostics-1100
    FC1_prognostics=[]
    for i in range(len_just_prog):
        if i <regressors_num:
            if i ==0:
                inputs=list(inputs_regressors_num)
                inputs=np.array(inputs)
                inputs.resize(1,4)
                FC1_prognostics.append(elm.predict(inputs))
 def test_2_NonNumpyTargets_RaiseError(self):
     X = np.array([[1, 2], [3, 4], [5, 6]])
     T = np.array([['a'], ['b'], ['c']])
     elm = ELM(2, 1)
     elm.add_neurons(1, "lin")
     self.assertRaises(AssertionError, elm.train, X, T)
 def test_1_NonNumpyInputs_RaiseError(self):
     X = np.array([['1', '2'], ['3', '4'], ['5', '6']])
     T = np.array([[1], [2], [3]])
     elm = ELM(2, 1)
     elm.add_neurons(1, "lin")
     self.assertRaises(AssertionError, elm.train, X, T)