示例#1
0
def main():
    # start measuring execution time
    start = time.time()
    # Load training & testing datasets
    X_train, y_train, X_test, y_test = utils.load_dataset()

    # Make a SOM whose units(clusters) are from 0 to 24 and learning from X_train
    model = Som((5, 5), 42, learning_rate=0.2)
    model.fit(X_train, num_epochs=1, updates_epoch=1)

    # Choose the proper unit for each row in the testing dataset
    y_pred = model.predict(X_test)
    y_pred = utils.create_binary_prediction_som(y_pred, 175341, 0.05, 0.1)

    print("execution time: ", time.time() - start)
    utils.cal_accuracy(y_test, y_pred, 175341)

    #######Create models for testing the NSL-KDD dataset#######

    X_train = pd.read_csv('../dataset/unsw-nb15/nsl-kdd-ver/train-set.csv')

    model = Som((5, 5), 5, learning_rate=0.2)
    model.fit(X_train, num_epochs=1, updates_epoch=1)

    # Save the model
    utils.save_model(model, './model/nsl-kdd-model.sav')
示例#2
0
    def train(self, x, y, epochs, learning_rate, early_stop=False):
        history = {'epochs':[], 'loss':[], 'accuracy':[]}
        print('Start training...')
        
        for epoch in range(1, epochs + 1):
            # backpropagation
            y_hat = self.forward(x)
            self.backward(y, y_hat, learning_rate)
                
            # calculate loss and accuracy
            loss = self.MSE(y, y_hat)
            acc = cal_accuracy(y, y_hat)

            # record the values
            history['epochs'].append(epoch)
            history['loss'].append(loss)
            history['accuracy'].append(acc)

            # print information
            if (epoch % int(epochs / 20)) == 0:
                print(f'epoch {epoch:<6} loss: {loss:.5f}  accuracy: {acc:.5f}')

            # early stopping
            if early_stop and acc == 1.0:
                print(f'epoch {epoch:<6} loss: {loss:.5f}  accuracy: {acc:.5f}')
                print('Accuracy = 1.0, early stop the training.')
                break
                
        return history
示例#3
0
 def disc_train(self, sample_shape, batch_feed, fake_state_action=None):
     batch_size = sample_shape[0]
     # state_rep, action_rep = self.read_real_data(None, batch_feed)
     state_rep, action_rep = self.read_real_data_onehot_300(None, batch_feed)
     # state_action = torch.cat([self.cast_gpu(state_rep), self.cast_gpu(action_rep)], -1)
     embed_batch = self.vae.get_embed(self.cast_gpu(state_rep))
     real_disc_v = self.discriminator(embed_batch.detach(), self.cast_gpu(action_rep))
     if fake_state_action is None:
         fake_state_action = self.sample_step(sample_shape)
     else:
         fake_state_action = fake_state_action
     state_rep_f, action_rep_f = fake_state_action
     if np.random.random()<0.5:
         # print(action_rep.size())
         # state_rep_f, action_rep_f = embed_batch.detach(), self.shuffle_action(action_rep)
         state_rep_f, action_rep_f = embed_batch.detach(), action_rep[torch.randperm(action_rep.size()[0]), :] 
         # print(action_rep_f.size())
     fake_disc_v = self.discriminator(state_rep_f.detach(), action_rep_f.detach())
     rf_disc_v = torch.cat([real_disc_v, fake_disc_v], dim=0)
     labels_one = torch.cat([torch.full((batch_size,),1.0), torch.full((batch_size,),0.0)])
     labels_one = self.cast_gpu(labels_one)
     # labels_one = self.cast_gpu(torch.FloatTensor([1] * batch_size + [0] * batch_size))
     disc_loss = self.loss_BCE(rf_disc_v.view(-1), labels_one)
     # disc_loss = BCELoss_double(rf_disc_v.view(-1), labels)
     # disc_loss = - torch.mean(real_disc_v) + torch.mean(fake_disc_v)
     disc_loss = Pack(disc_loss=disc_loss)
     disc_acc = cal_accuracy(rf_disc_v.view(-1), labels_one)
     return disc_loss, disc_acc
示例#4
0
    def disc_train(self, sample_shape, batch_feed, fake_state_action=None):
        batch_size = sample_shape[0]
        # state_rep, action_rep = self.read_real_data(None, batch_feed)
        state_rep, action_rep = self.read_real_data_onehot(None, batch_feed)
        state_action = torch.cat([self.cast_gpu(state_rep), self.cast_gpu(action_rep)], -1)
        embed_batch = self.vae.encode(state_action)
        real_disc_v = self.discriminator(embed_batch.detach())
        if fake_state_action is None:
            fake_state_action = self.sample_step(sample_shape)
        else:
            fake_state_action = fake_state_action
        if self.config.round_for_disc:
            fake_disc_v = self.discriminator(fake_state_action.detach())
        else:            
            fake_disc_v = self.discriminator(fake_state_action.detach())

        rf_disc_v = torch.cat([real_disc_v, fake_disc_v], dim=0)
        labels_one = torch.cat([torch.full((batch_size,),1.0), torch.full((batch_size,),0.0)])
        labels_one = self.cast_gpu(labels_one)
        # labels_one = self.cast_gpu(torch.FloatTensor([1] * batch_size + [0] * batch_size))
        disc_loss = self.loss_BCE(rf_disc_v.view(-1), labels_one)
        # disc_loss = BCELoss_double(rf_disc_v.view(-1), labels)
        # disc_loss = - torch.mean(real_disc_v) + torch.mean(fake_disc_v)
        disc_loss = Pack(disc_loss=disc_loss)
        disc_acc = cal_accuracy(rf_disc_v.view(-1), labels_one)
        return disc_loss, disc_acc
示例#5
0
    def disc_train(self, sample_shape, batch_feed, fake_state_action=None):

        batch_size = sample_shape[0]
        real_state_rep, action_data_feed = self.read_real_data(sample_shape, batch_feed)
        real_disc_v = self.discriminator(real_state_rep, action_data_feed)
        if fake_state_action is None:
            fake_state_rep, fake_action_rep = self.sample_step(sample_shape)
        else:
            fake_state_rep, fake_action_rep = fake_state_action
        fake_disc_v = self.discriminator(fake_state_rep.detach(), fake_action_rep.detach())

        rf_disc_v = torch.cat([real_disc_v, fake_disc_v], dim=0)
        
        true_label = 1.0 
        false_label = 0.0 
        labels_one = torch.cat([torch.full((batch_size,),true_label), torch.full((batch_size,),false_label)])
        
        # labels_one = torch.cat([torch.full((batch_size,),1.0), torch.full((batch_size,),0.0)])
        labels_one = self.cast_gpu(labels_one)
        # labels_one = self.cast_gpu(torch.FloatTensor([1] * batch_size + [0] * batch_size))
        disc_loss = self.loss_BCE(rf_disc_v.view(-1), labels_one) + self.discriminator.l2_norm()
        # disc_loss = BCELoss_double(rf_disc_v.view(-1), labels)
        # disc_loss = - torch.mean(real_disc_v) + torch.mean(fake_disc_v)
        disc_loss = Pack(disc_loss=disc_loss) 
        disc_acc = cal_accuracy(rf_disc_v.view(-1), labels_one)
        return disc_loss, disc_acc
示例#6
0
    def disc_train(self, sample_shape, batch_feed, fake_state_action=None):
        batch_size = sample_shape[0]
        real_state_rep, action_data_feed = self.read_real_data(sample_shape, batch_feed)
        real_disc_v = self.discriminator(real_state_rep, action_data_feed)
        if fake_state_action is None:
            fake_state_rep, fake_action_rep = self.sample_step(sample_shape)
        else:
            fake_state_rep, fake_action_rep = fake_state_action
        # if self.config.round_for_disc:
        #     fake_disc_v = self.discriminator(fake_state_rep.detach().round(), fake_action_rep.detach().round())
        # else:            
        if np.random.random()<0.5:
            fake_state_rep, fake_action_rep = real_state_rep.detach(), action_data_feed[:,torch.randperm(action_data_feed.size()[1])] 
        fake_disc_v = self.discriminator(fake_state_rep.detach(), fake_action_rep.detach())

        rf_disc_v = torch.cat([real_disc_v, fake_disc_v], dim=0)
        labels_one = torch.cat([torch.full((batch_size,),1.0), torch.full((batch_size,),0.0)])
        labels_one = self.cast_gpu(labels_one)
        # labels_one = self.cast_gpu(torch.FloatTensor([1] * batch_size + [0] * batch_size))
        disc_loss = self.loss_BCE(rf_disc_v.view(-1), labels_one)
        # disc_loss = BCELoss_double(rf_disc_v.view(-1), labels)
        # disc_loss = - torch.mean(real_disc_v) + torch.mean(fake_disc_v)
        disc_loss = Pack(disc_loss=disc_loss)
        disc_acc = cal_accuracy(rf_disc_v.view(-1), labels_one)
        return disc_loss, disc_acc
示例#7
0
def main():
    # start measuring execution time
    start = time.time()
    # Load training & testing datasets
    X_train, y_train, X_test, y_test = utils.load_dataset()

    # Make a SOM whose units(clusters) are from 0 to 24 and learning from X_train
    model = Som((5, 5), 42, learning_rate=0.2)
    model.fit(X_train, num_epochs=1, updates_epoch=1)

    # Choose the proper unit for each row in the testing dataset
    y_pred = model.predict(X_test)
    y_pred = utils.create_binary_prediction_som(y_pred, 175341, 0.65, 0.05)

    print("execution time: ", time.time() - start)
    utils.cal_accuracy(y_test, y_pred, 175341)
示例#8
0
def test_rf_classification():
    iris = datasets.load_iris()
    X, y = iris.data, iris.target
    print (X.shape, y.shape)
    train_X, train_y, test_X, test_y = split_train_test(X, y)
    print (train_X.shape, train_y.shape, test_X.shape, test_y.shape)

    clf = RandomForestClassifier(n_estimators=100)
    clf.fit(train_X, train_y)
    preds = clf.predict(test_X)
    accuracy = cal_accuracy(test_y, preds)
    print ('accuracy: ', accuracy)
示例#9
0
def test_gradient_boosting_classification():
    iris = datasets.load_iris()
    X, y = iris.data, iris.target
    print (X.shape, y.shape)
    train_X, train_y, test_X, test_y = split_train_test(X, y)
    print (train_X.shape, train_y.shape, test_X.shape, test_y.shape)

    clf = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1)
    clf.fit(train_X, train_y)
    preds = clf.predict(test_X)
    accuracy = cal_accuracy(test_y, preds)
    print ('accuracy: ', accuracy)
示例#10
0
def main():
    # start measuring execution time
    start = time.time()
    # Load training & testing datasets
    X_train, y_train, X_test, y_test = utils.load_dataset()

    gini_classifier = train_with_index('gini', X_train, y_train)

    print("Results Using Gini Index: \n")

    # Prediction using gini
    y_pred_gini = prediction(X_test, gini_classifier)
    print("execution time of classification with gini index: ",
          time.time() - start)
    utils.cal_accuracy(y_test, y_pred_gini, 175341)

    start = time.time()
    entropy_classifier = train_with_index('entropy', X_train, y_train)

    print("Results Using Entropy: \n")
    # Prediction using entropy
    y_pred_entropy = prediction(X_test, entropy_classifier)
    print("execution time of classification with entropy index: ",
          time.time() - start)
    utils.cal_accuracy(y_test, y_pred_entropy, 175341)

    #######Create models for testing the NSL-KDD dataset#######

    X_train = pd.read_csv('../dataset/unsw-nb15/nsl-kdd-ver/train-set.csv')

    gini_classifier = train_with_index('gini', X_train, y_train)
    entropy_classifier = train_with_index('entropy', X_train, y_train)

    # Save the models
    utils.save_model(gini_classifier, './model/nsl-kdd-model-gini.sav')
    utils.save_model(entropy_classifier, './model/nsl-kdd-model-entropy.sav')
示例#11
0
def test_classification(model):

    Classifier = models[model]

    dataset = datasets.load_iris()
    X, y = dataset.data, dataset.target
    print (X.shape, y.shape)
    train_X, train_y, test_X, test_y = split_train_test(X, y)
    print (train_X.shape, train_y.shape, test_X.shape, test_y.shape)

    clf = Classifier()
    clf.fit(train_X, train_y)
    preds = clf.predict(test_X)
    accuracy = cal_accuracy(test_y, preds)
    print (accuracy)
                min_split_samples=min_split_samples,
                min_impurity=min_impurity,
                regression=False)

    def fit(self, X, y):
        y = to_categorical(y)
        super(GradientBoostingClassifier, self).fit(X, y)


if __name__ == '__main__':
    from sklearn import datasets
    from utils import split_train_test, cal_accuracy
    iris = datasets.load_iris()
    X, y = iris.data, iris.target
    print (X.shape, y.shape)
    train_X, train_y, test_X, test_y = split_train_test(X, y)
    print (train_X.shape, train_y.shape, test_X.shape, test_y.shape)

    clf = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1)
    clf.fit(train_X, train_y)
    preds = clf.predict(test_X)
    accuracy = cal_accuracy(test_y, preds)
    print ('accuracy: ', accuracy)







示例#13
0
        feature, support = convert_sparse_train_input1(adj, features)
        train_labels, weight_mask = convert_loss_input(train_labels, weight_mask)

        out = net((feature, support))
        out = out[0]
        # loss = masked_loss(out, train_labels, weight_mask)


        loss = weighted_loss(out, train_labels, weight_mask)

        # print("cross entropy loss: {:.5f} ".format(loss.item()))
        loss += args.weight_decay * net.l2_loss()

        # acc = masked_acc(out, train_labels)
        acc = cal_accuracy(out, train_labels)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        t2 = time.time()
        if (epoch+1) % 10 == 0:
            print("Epoch:" ,'%04d'% (epoch+1), "time: {:.5f}, loss: {:.5f}, acc: {:.5f}".\
                format( (t2-t1), loss.item(), acc.item()))


    # if epoch % 10 == 0:
    #     print(epoch, loss.item(), acc.item())


net.eval()
示例#14
0
rpn_accuracy_for_epoch = []

best_loss = np.Inf
while True:
    #try:
    X, Y, gta = next(gen)
    #for i in range(100):
    loss_rpn = model_rpn.train_on_batch(X, Y)
    #it will return cls, regression bbox, and base feature map
    P_rpn = model_rpn.predict_on_batch(X)
    #The input structure is [boxes, scores, maximum]
    #roi = utils.propose(P_rpn[1], P_rpn[0], 300)
    rois, scores = utils.propose_cpu(P_rpn[1], P_rpn[0], 300)
    #utils.propose_cpu(P_rpn[1], P_rpn[0], 300)
    #print(rois.shape)
    utils.cal_accuracy(gta, rois, scores)
    #print(np.asarray(roi))
    #print(roi[0])
    #print(P_rpn[0], P_rpn[1])

    print('iter {0}, epoch {1}, loss {2}'.format(iter_num, epoch_num,
                                                 loss_rpn))
    iter_num += 1
    if iter_num == epoch_length:
        name = str(iter_num) + str(epoch_num) + 'rpn.h5'
        model_rpn.save_weights(name)
        iter_num = 0
        epoch_num += 1
    if epoch_num == num_epochs:
        print('Training complete, exiting.')
        model_rpn.save_weights('rpn.h5')
示例#15
0
import sys
import networkx as nx
import os
import time

from utils import load_data, normalize, cal_accuracy
from model import GCN

datastr = "pubmed"

adj, features, y_train, y_val, y_test, train_mask, val_mask, test_mask = load_data(datastr)
#adj = nx.to_scipy_sparse_matrix(adj)
norm_adj = normalize(adj)
#norm_adj = adj

#print(adj.shape, adj_n.shape, features.shape, y_train.shape, train_mask.shape)
i = torch.LongTensor([norm_adj.row, norm_adj.col])
v = torch.FloatTensor(norm_adj.data)
norm_adj = torch.sparse.FloatTensor(i,v, adj.shape)
features = torch.tensor(features, dtype=torch.float, requires_grad=False)
y_train = torch.tensor(y_train, dtype=torch.long, requires_grad=False)
y_val = torch.tensor(y_val, dtype=torch.long, requires_grad=False)
y_test = torch.tensor(y_test, dtype=torch.long, requires_grad=False)

load_from = "gcn_model"
gcn = torch.load(load_from)
gcn.eval()
output = gcn(features, norm_adj)
test_acc = cal_accuracy(output, y_test, test_mask)
print("test acc:", test_acc)
                            m2_data = m2_data.reshape(batch_size, 30, 9)
                            target = target.reshape(batch_size, 20)
                            summary, c, _ = sess.run(
                                [merged_summary, cost, optimizer],
                                feed_dict={
                                    m1_X: m1_data,
                                    m2_X: m2_data,
                                    Y: target
                                })

                            m1_data = np.array([])
                            m2_data = np.array([])
                            target = np.array([])
                            count = 0

            writer.add_summary(summary, global_step=epoch)
            print("Epoch: [%2d], time: [%4.4f], loss: [%.8f]" %
                  (epoch + 1, time.time() - start_time, c))
        saver.save(sess, checkpoint_dir)
        print('Learning Finished..!')

    else:
        m2_test_x, m1_test_x, t_target = load_data(2)

        print('Test Acc : [%.8f]' %
              cal_accuracy(m1_test_x, m2_test_x, t_target, sess, accuracy,
                           correct_prediction, hypothesis, m1_X, m2_X, Y))
        print('Train Acc : [%.8f]' % cal_accuracy(
            m1_x, m2_x, label_data, sess, accuracy, correct_prediction,
            hypothesis, m1_X, m2_X, Y))  #에러분석시 주석처리
    # os.system('shutdown -s -f -t 0')
示例#17
0
	gcn = torch.load(load_from)
optimizer = torch.optim.Adam(gcn.parameters(recurse=True), lr=lr, weight_decay=weight_decay)

train_time = time.time()
val_loss_pre = 1e9
val_acc_pre = 0
dec_time = 0
for epoch in range(args.epoch):
    t = time.time()
    gcn.train()
    optimizer.zero_grad()
    output = gcn(features, norm_adj)
    pred = output[train_mask]
    ans = torch.argmax(y_train[train_mask],dim=1)
    loss = F.cross_entropy(pred, ans)
    train_acc = cal_accuracy(output, y_train, train_mask)
    loss.backward()
    optimizer.step()
    #print(torch.min(pred), torch.max(pred))

    gcn.eval()
    pred = output[val_mask]
    ans = torch.argmax(y_train[val_mask],dim=1)
    val_loss = F.cross_entropy(pred, ans)
    val_acc = cal_accuracy(output, y_val, val_mask)
    
    print("epoch:", epoch, "time:", time.time()-t)
    print("train_loss:",float(loss), "train_acc:", float(train_acc))
    print("val_loss:", float(val_loss), "val_acc:", float(val_acc))
    if val_loss > val_loss_pre and  val_acc < val_acc_pre:
        dec_time = dec_time + 1