Пример #1
0
def main(args=None, val_file=None, tst_file=None):

    dir_list = os.listdir(args.model_dir)

    val_pred_t1 = None
    val_pred_t2 = None
    tst_pred_t1 = None
    tst_pred_t2 = None

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)

    model_name = args.model_dir + '/model.ckpt.meta'
    saver = tf.train.import_meta_graph(model_name)
    saver.restore(sess, tf.train.latest_checkpoint(args.model_dir))

    graph = tf.get_default_graph()

    val_label_t1, val_label_t2, val_tmp_t1, val_tmp_t2 = test(
        sess=sess, graph=graph, file_list=val_file)
    tst_label_t1, tst_label_t2, tst_tmp_t1, tst_tmp_t2 = test(
        sess=sess, graph=graph, file_list=tst_file)
    val_acc_t1, val_acc_t2, val_acc_bi, val_acc_tr = utils.Accuracy(
        val_tmp_t1[:, 0], val_tmp_t2[:, 0], val_label_t1, val_label_t2)
    print(
        '%s got val_acc_t1: %.4f, val_acc_t2: %.4f, val_acc_bi: %.4f, val_acc_tr: %.4f'
        % (model_name, val_acc_t1, val_acc_t2, val_acc_bi, val_acc_tr))
    tst_acc_t1, tst_acc_t2, tst_acc_bi, tst_acc_tr = utils.Accuracy(
        tst_tmp_t1[:, 0], tst_tmp_t2[:, 0], tst_label_t1, tst_label_t2)
    print(
        '%s got tst_acc_t1: %.4f, tst_acc_t2: %.4f, tst_acc_bi: %.4f, tst_acc_tr: %.4f'
        % (model_name, tst_acc_t1, tst_acc_t2, tst_acc_bi, tst_acc_tr))
    '''
    sio.savemat('DenseNet121_pred.mat',
                mdict={
                    'val_pred_t1': val_tmp_t1,
                    'val_pred_t2': val_tmp_t2,
                    'tst_pred_t1': tst_tmp_t1,
                    'tst_pred_t2': tst_tmp_t2,
                    'val_label_t1': val_label_t1,
                    'val_label_t2': val_label_t2,
                    'tst_label_t1': tst_label_t1,
                    'tst_label_t2': tst_label_t2})
    '''
    tf.reset_default_graph()
    return None
Пример #2
0
 def SetupLoss(self, predictions, labels):
     """Compute the loss and accuracy of the predictions given the labels."""
     loss_vec = tf.nn.softmax_cross_entropy_with_logits(
         logits=tf.squeeze(predictions), labels=labels)
     loss = (tf.reduce_mean(loss_vec) /
             math.log(self.learning_params.number_of_classes))
     accuracy = utils.Accuracy(predictions, labels)
     return loss, accuracy
Пример #3
0
 def create_eval_metric(metric_name: AnyStr) -> mx.metric.EvalMetric:
     """
     Creates an EvalMetric given a metric names.
     """
     # output_names refers to the list of outputs this metric should use to update itself, e.g. the softmax output
     if metric_name == C.ACCURACY:
         return utils.Accuracy(ignore_label=C.PAD_ID, output_names=[C.SOFTMAX_OUTPUT_NAME])
     elif metric_name == C.PERPLEXITY:
         return mx.metric.Perplexity(ignore_label=C.PAD_ID, output_names=[C.SOFTMAX_OUTPUT_NAME])
     else:
         raise ValueError("unknown metric name")
Пример #4
0
    def SetupTesting(self, tf_test_examples, tf_test_labels, skeleton,
                     kernel_width):
        self.test_predictions, _ = rf.RandomFeaturesGraph(
            skeleton, self.config.number_of_classes, tf_test_examples,
            kernel_width, self.tf_rf_vectors, self.tf_rf_params)

        self.test_loss = Loss(self.test_predictions, tf_test_labels,
                              self.config.number_of_classes, self.tf_rf_params,
                              self.config.l2_reg_param)

        if self.config.number_of_classes == 1:
            self.test_accuracy = utils.Accuracy_binary(self.test_predictions,
                                                       tf_test_labels)
        else:
            self.test_accuracy = utils.Accuracy(self.test_predictions,
                                                tf_test_labels)

        # make summary writers for tensorboard
        tf.summary.scalar('test_loss', self.test_loss)
        tf.summary.scalar('test_accuracy', self.test_accuracy)
Пример #5
0
    def SetupTraining(self, tf_examples, tf_labels, skeleton, kernel_width):
        # Generate prediction graph from random features set
        self.predictions, _ = rf.RandomFeaturesGraph(
            skeleton, self.config.number_of_classes, \
            tf_examples, kernel_width, self.tf_rf_vectors, self.tf_rf_params)

        decay_steps = int(
            float(self.config.number_of_examples) /
            float(self.config.batch_size) * self.config.epochs_per_decay)

        # Learning rate setting
        lr = tf.train.exponential_decay(self.config.learning_rate,
                                        self.global_step,
                                        decay_steps=decay_steps,
                                        decay_rate=self.config.decay_rate,
                                        staircase=True,
                                        name='learning_rate')

        self.loss = Loss(self.predictions, tf_labels,
                         self.config.number_of_classes, self.tf_rf_params,
                         self.config.l2_reg_param)

        if self.config.number_of_classes == 1:
            self.accuracy = utils.Accuracy_binary(self.predictions, tf_labels)
        else:
            self.accuracy = utils.Accuracy(self.predictions, tf_labels)

        grads = tf.gradients(self.loss, tf.trainable_variables())

        optimizer = utils.GetOptimizer(self.config, lr)

        self.update = optimizer.apply_gradients(zip(grads,
                                                    tf.trainable_variables()),
                                                global_step=self.global_step)

        # make summary writers for tensorboard
        tf.summary.scalar('loss', self.loss)
        tf.summary.scalar('accuracy', self.accuracy)
        tf.summary.scalar('learning_rate', lr)
Пример #6
0
        type_loss = classify_loss_fn(pre_type, ent_type)
        loss = rank_loss * param["loss_w"] + type_loss * (1 - param["loss_w"])
        # optimization
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        train_res = utils.record(train_res, id_list,
                                 torch.softmax(pre_label, dim=-1), pre_type,
                                 label)
        if i % 1000 == 0:
            print('Epoch [{}/{}], Step [{}/{}]'.format(epoch, param["epoch"],
                                                       i, total_step))
            print("Loss: ", loss.item(), "rank_loss: ", rank_loss.item(),
                  "type_loss: ", type_loss.item())

    accuracy = utils.Accuracy(train_res)
    train_accuracy.append(accuracy)
    print("train accuracy: ", accuracy)
    dev_res = {}
    # evalue
    with torch.no_grad():
        for i, data in enumerate(dev_loader):
            id_list, query, offset, cand_desc, seq_len = data
            # move the data to the device
            query = query.to(device)
            cand_desc = cand_desc.to(device)
            # forward
            pre_label, pre_type = model.predict(query, offset, cand_desc,
                                                seq_len)
            # loss
            rank_loss = rank_loss_fn(pre_label, label)
Пример #7
0
def run_train(config):
    with open(config["LTL_vocab"], "r") as f:
        index_to_LTL = [x.strip() for x in f]
    with open(config["trace_vocab"], "r") as f:
        index_to_trace = [x.strip() for x in f]

    LTL_to_index = {x: i for i, x in enumerate(index_to_LTL)}
    trace_to_index = {x: i for i, x in enumerate(index_to_trace)}

    device = config["device"]
    model_path = config["model_path"]
    log_file = open(os.path.join(model_path, "model.log"), "w")

    train_data = utils.LTL_Dataset(config["data_file"], LTL_to_index,
                                   trace_to_index)
    if config["val_file"] is not None:
        val_data = utils.LTL_Dataset(config["val_file"], LTL_to_index,
                                     trace_to_index)
    '''
	train_loader=DataLoader(train_data, batch_size=config["batch_size"], shuffle=False, collate_fn=utils.input_collate_fn_train)

	for data in train_loader:
		pass

	exit(0)
	'''

    model = Model_with_Proof(n_src_vocab=len(LTL_to_index),
                             n_tgt_vocab=len(trace_to_index),
                             d_model=config["d_model"],
                             nhead=config["nhead"],
                             nlayers=config["nlayers"],
                             nhid=config["nhid"],
                             dropout=config["dropout"],
                             d_block=config["d_block"],
                             d_block_hid=config["d_block_hid"],
                             P_node_hid=config["P_node_hid"],
                             P_edge_hid=config["P_edge_hid"],
                             loss_weight=torch.FloatTensor(
                                 config["loss_weight"]).to(device))

    if config["model_file"] is not None:
        model_dict = model.state_dict()
        tmp_dict = torch.load(config["model_file"])

        pretrained_dict = {}
        for key, value in tmp_dict.items():
            if "transformer." + key in model_dict:
                pretrained_dict["transformer." + key] = value
            elif key in model_dict:
                pretrained_dict[key] = value

        model_dict.update(pretrained_dict)
        model.load_state_dict(model_dict)

        if config["model_freeze"] == 1:
            for x in model.transformer.parameters():
                x.requires_grad = False

    model.to(device)

    optimizer = Adam(filter(lambda p: p.requires_grad, model.parameters()),
                     lr=config["lr"])
    lr_decay = lr_scheduler.ExponentialLR(optimizer, gamma=config["lr_decay"])

    epochs = config["epochs"]
    batch_size = config["batch_size"]

    for epoch in range(epochs):
        print("epoch: ", epoch)
        print("epoch: ", epoch, file=log_file)

        train_loader = DataLoader(train_data,
                                  batch_size=batch_size,
                                  shuffle=True,
                                  collate_fn=utils.input_collate_fn_train)

        loss_list = []
        acc_count = 0
        count = 0

        print("train")
        model.train()

        for data in tqdm(train_loader):
            cuda_data = utils.convert_to_cuda(data, device)
            output, loss, loss_total = model(
                cuda_data["source"], cuda_data["source_len"],
                cuda_data["right_pos_truth"], cuda_data["target"],
                cuda_data["state_len"], cuda_data["target_offset"],
                cuda_data["node_label"], cuda_data["edge_index"],
                cuda_data["edge_label"])

            loss_list = loss_total.cpu().detach().numpy().tolist()

            x, y = utils.Accuracy(output, cuda_data["target"][:, 1:],
                                  trace_to_index["[PAD]"])
            acc_count += x
            count += y

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

        lr_decay.step()

        torch.save(model.state_dict(),
                   os.path.join(model_path, "model" + str(epoch) + ".pkl"))

        if config["val_file"] is not None:
            val_loader = DataLoader(val_data,
                                    batch_size=batch_size,
                                    shuffle=False,
                                    collate_fn=utils.input_collate_fn_train)
            acc_count = 0
            count = 0

            print("val")
            model.eval()

            for data in tqdm(val_loader):
                cuda_data = utils.convert_to_cuda(data, device)
                output, loss, loss_total = model(
                    cuda_data["source"], cuda_data["source_len"],
                    cuda_data["right_pos_truth"], cuda_data["target"],
                    cuda_data["state_len"], cuda_data["target_offset"],
                    cuda_data["node_label"], cuda_data["edge_index"],
                    cuda_data["edge_label"])

                x, y = utils.Accuracy(output, cuda_data["target"][:, 1:],
                                      trace_to_index["[PAD]"])
                acc_count += x
                count += y

        print("accuracy: ", acc_count / count)
        print("accuracy: ", acc_count / count, file=log_file)
        print("loss: ", loss_list)
        print("loss: ", loss_list, file=log_file)
        log_file.flush()
Пример #8
0
print("In-Built MNB")
clf = MultinomialNB()
clf.fit(x_train,y_train)
print(clf.score(x_test,y_test))

train_data = utils.SeparateByClass(x_train, y_train)

test_data = utils.SeparateByClass(x_test, y_test)



Priors = utils.GetPriors(Y)

print("MNB")
prob_vect_by_classes = utils.getProbabilityVectorForEachClass(train_data)

ResultAccuracy, Final_Posterors = utils.Accuracy(test_data, prob_vect_by_classes, Priors)
print(ResultAccuracy)

#Dirichlet
print("Dirichlet")
AlphaParameters = utils.estimate_dirichlet_par(train_data)
ResultAccuracy = utils.dirichlet_accuracy(test_data,AlphaParameters,Priors)
print(ResultAccuracy)
#Dirichlet
print("Dirichlet1")
AlphaParameters1 = utils.FindAlphaParameter(train_data)
ResultAccuracy = utils.dirichlet_accuracy(test_data,AlphaParameters1,Priors)
print(ResultAccuracy)