Exemplo n.º 1
0
    def finetune(self, model, summary):
        """Finetune the given model on a "dataset" produced from chunks of the given summary.
        Args:
            model (BertForMaskedLM): a BERT for masked language modeling torch model
            summary (str): the summary to finetune on
        """
        model.train()

        all_inputs = self.prepare_finetuning_data(summary)
        input_batches = batch_data(all_inputs, self.finetune_batch_size)

        no_decay = ["bias", "LayerNorm.weight"]
        optimizer_grouped_parameters = [
            {
                "params": [
                    p for n, p in model.named_parameters()
                    if not any(nd in n for nd in no_decay)
                ],
                "weight_decay":
                1e-2,
            },
            {
                "params": [
                    p for n, p in model.named_parameters()
                    if any(nd in n for nd in no_decay)
                ],
                "weight_decay":
                0.0,
            },
        ]
        optimizer = AdamW(optimizer_grouped_parameters,
                          lr=self.learning_rate,
                          eps=1e-8)
        scheduler = get_linear_schedule_with_warmup(
            optimizer,
            num_warmup_steps=self.warmup_steps,
            num_training_steps=len(input_batches) * self.finetune_epochs,
        )

        for epoch in range(self.finetune_epochs):
            for input_batch in input_batches:
                input_ids, attention_mask, token_type_ids, labels = get_input_tensors(
                    input_batch,
                    device=self.device,
                    tokenizer=self.model_tokenizer,
                )

                model.zero_grad()
                optimizer.zero_grad()
                loss, _ = model(
                    input_ids=input_ids,
                    attention_mask=attention_mask,
                    token_type_ids=token_type_ids,
                    masked_lm_labels=labels,
                )
                loss.backward()
                optimizer.step()
                scheduler.step()

        model.eval()
Exemplo n.º 2
0
    def mask_and_infer(self,
                       model,
                       docs,
                       doc_summaries,
                       loading_bar=True,
                       sep=None):
        """Run the given model on masked versions of the provided doc_summaries and collect model
        output
        Args:
            model (BertForMaskedLM): a BERT for masked language modeling torch model
            docs (List[str]): A list of input documents
            doc_summaries (List[List[str]]): A list of summaries for every input document
            loading_bar (bool): whether or not to use a tqdm loading bar to show progress
            sep (str): Separator between the inference help (summary) and a sentence from the doc
        Returns:
            all_outputs (List[List[List[Dict[int, str]]]]): for each doc, for each summary for the
                doc, for each input sequence for the summary, we have a dict mapping indices to
                model predictions
            all_answers (List[List[List[Dict[int, str]]]]): for each doc, for each summary for the
                doc, for each input sequence for the summary, we have a dict mapping indices to
                original tokens
        """
        # Prepare inputs
        all_inputs, all_answers = [], []
        for doc, summaries in zip(docs, doc_summaries):
            doc_inputs, doc_answers = [], []
            for summary in summaries:
                summary_inputs, summary_answers = self.get_inference_inputs(
                    doc, summary, sep)
                doc_inputs.append(summary_inputs)
                doc_answers.append(summary_answers)
            all_inputs.append(doc_inputs)
            all_answers.append(doc_answers)

        # Run inference in batches
        inputs_per_summary_per_doc = [[
            len(inputs) for inputs in summary_input
        ] for summary_input in all_inputs]
        collapsed_inputs = sum(sum(all_inputs, []), [])
        batched_inputs = batch_data(collapsed_inputs,
                                    self.inference_batch_size)

        iterator = tqdm.tqdm(batched_inputs, disable=not loading_bar)
        batched_outputs = [
            self.run_inference_batch(model, batch) for batch in iterator
        ]
        collapsed_outputs = sum(batched_outputs, [])

        # Regroup outputs
        i = 0
        all_outputs = []
        for inputs_per_summary in inputs_per_summary_per_doc:
            doc_outputs = []
            for num_inputs in inputs_per_summary:
                doc_outputs.append(collapsed_outputs[i:i + num_inputs])
                i += num_inputs
            all_outputs.append(doc_outputs)

        return all_outputs, all_answers
Exemplo n.º 3
0
    def discriminator_evaluate(self, query, target, batch_size=16):
        """Evaluate classification accuracy of discriminator for given inputs.
        
        # Arguments
        * `query` - the input images to perform facial recognition on
        * `target` - the "known" faces that the queried images are compared to
        * `batch_size` - the number of images to put in each batch, defaults to 16
        """
        if self.device:
            # compute in batches
            accuracies = []
            minibatches = utils.batch_data(query, target, batch_size=batch_size)
            for idx, batch in enumerate(minibatches):
                # collect batch and send to CUDA
                batch_queries, batch_targets = batch
                batch_queries = batch_queries.to(self.device)
                batch_targets = batch_targets.to(self.device)

                # get embeddings
                target_embeddings = self.forward(batch_targets)
                query_embeddings = self.forward(batch_queries)

                # find distances between embeddings
                target_embeddings = torch.unsqueeze(target_embeddings, 1)
                query_embeddings = torch.unsqueeze(query_embeddings, 0)
                distances = torch.norm(target_embeddings - query_embeddings, dim=2)

                # classify queried images
                classifications = torch.argmin(distances, dim=0) + (idx * batch_size)
                id_range = torch.Tensor(
                    [(idx * batch_size) + i for i in range(batch_size)]
                ).to(self.device)
                accuracies.append(sum(classifications == id_range) / batch_size)

                # return batch to CPU
                batch_queries = batch_queries.to(torch.device("cpu"))
                batch_targets = batch_targets.to(torch.device("cpu"))
            return sum(accuracies) / len(accuracies)
        else:
            # get embeddings
            target_embeddings = self.forward(target)
            query_embeddings = self.forward(query)

            # find distances between embeddings
            target_embeddings = torch.unsqueeze(target_embeddings, 1)
            query_embeddings = torch.unsqueeze(query_embeddings, 0)
            distances = torch.norm(target_embeddings - query_embeddings, dim=2)

            # classify queried images and return accuracy
            num_queries = query.shape[0]
            classifications = torch.argmin(distances, dim=0)
            accuracy = (
                sum(classifications == torch.Tensor([i for i in range(num_queries)]))
                / num_queries
            )
            return accuracy
def extract_features(basenet, data_path):
    """
    extract features from all fold of all sets for the CFCMC classifier
    :param basenet:
    :param data_path:
    :return:
    """
    sets = ['train', 'valid', 'test']
    classes = [
        '01_TUMOR', '02_STROMA', '03_COMPLEX', '04_LYMPHO', '05_DEBRIS',
        '06_MUCOSA', '07_ADIPOSE', '08_EMPTY'
    ]

    # create folders for features
    for k in range(1, 11):
        for set_name in sets:
            for cl in classes:
                path = os.path.join(data_path, f"{basenet}_features",
                                    f"{k}_fold", set_name, cl)
                if not os.path.exists(path):
                    os.makedirs(path)
    for k in range(1, 11):
        model = get_model_for_feature_extraction(kfold=k,
                                                 basenet=basenet,
                                                 pathbase=pathbase)
        # par_model = multi_gpu_model(model, gpus=2)
        for set_name in sets:
            for cl in classes:
                print(
                    f"Extracting from {k} fold from {set_name} set from class {cl}"
                )
                save_path = os.path.join(data_path, f"{basenet}_features",
                                         f"{k}_fold", set_name, cl)
                batches = utils.batch_data(
                    os.path.join(data_path, f"{k}_fold", set_name, cl), 16)
                images = []
                for id, files in enumerate(batches):
                    print("Detecting batches... " +
                          str(round((id * 100) / len(batches), 2)) + " %",
                          end="\r")
                    images = [skimage.io.imread(x) for x in files]
                    features = model.predict(np.array(images) / 255,
                                             batch_size=16)
                    for _idx, feat in enumerate(features):
                        file_name = os.path.splitext(
                            os.path.basename(files[_idx]))[0]
                        save_file_path = os.path.join(save_path,
                                                      file_name + ".csv")
                        np.savetxt(save_file_path,
                                   features[_idx],
                                   delimiter=",")
        K.clear_session()
        del model
        gc.collect()
    def test_model():
        # source_batch, target_batch = next(batch_data(X_test, y_test, batch_size))
        print("测试")
        acc_track = []
        sum_test_conf = []
        for batch_i, (source_batch, target_batch) in enumerate(
                batch_data(X_test, y_test, batch_size)):
            dec_input = np.zeros((len(source_batch), 1)) + char2numY['<GO>']
            for i in range(y_seq_length):
                batch_logits = sess.run(logits,
                                        feed_dict={
                                            inputs: source_batch,
                                            dec_inputs: dec_input
                                        })
                prediction = batch_logits[:, -1].argmax(axis=-1)
                dec_input = np.hstack([dec_input, prediction[:, None]])

            acc_track.append(dec_input[:, 1:] == target_batch[:, 1:])
            y_true = target_batch[:, 1:].flatten()
            y_pred = dec_input[:, 1:].flatten()
            sum_test_conf.append(
                confusion_matrix(y_true,
                                 y_pred,
                                 labels=range(len(char2numY) - 1)))

        sum_test_conf = np.mean(np.array(sum_test_conf, dtype=np.float32),
                                axis=0)

        acc_avg, acc, sensitivity, specificity, PPV = evaluate_metrics(
            sum_test_conf)
        print('Average Accuracy is: {:>6.4f} on test set'.format(acc_avg))

        info = ""
        for index_ in range(n_classes):
            print(
                "\t{} rhythm -> Sensitivity: {:1.4f}, Specificity : {:1.4f}, Precision (PPV) : {:1.4f}, Accuracy : {:1.4f}"
                .format(classes[index_], sensitivity[index_],
                        specificity[index_], PPV[index_], acc[index_]))
            strings = "{:1.4f} {:1.4f} {:1.4f} {:1.4f}".format(
                sensitivity[index_], specificity[index_], PPV[index_],
                acc[index_])
            info += strings

        results.append(info)

        print(
            "\t Average -> Sensitivity: {:1.4f}, Specificity : {:1.4f}, Precision (PPV) : {:1.4f}, Accuracy : {:1.4f}"
            .format(np.mean(sensitivity), np.mean(specificity), np.mean(PPV),
                    np.mean(acc)))
        return acc_avg, acc, sensitivity, specificity, PPV
Exemplo n.º 6
0
    def test_model():
        print('测试')
        acc_track = []
        sum_test_conf = []
        for batch_i, (source_batch, target_batch) in enumerate(batch_data(X_test, y_test, batch_size)):
            # dec_input = np.zeros((len(source_batch), 1)) + char2numY['<GO>']

            batch_logits = sess.run(infer_logits,
                                    feed_dict={
                                        inputs: source_batch,
                                        })
            y_pred = batch_logits.argmax(axis=-1)

            y_true = target_batch[:, 1:-1]
            acc_track.append(y_pred == y_true)
            # print('测试集预测值:', y_pred)
            # print('测试集真实值:', y_true)
            y_pred = y_pred.flatten()
            y_true = y_true.flatten()
            sum_test_conf.append(confusion_matrix(y_true, y_pred, labels=range(len(char2numY)-2)))

        sum_test_conf = np.mean(np.array(sum_test_conf, dtype=np.float32), axis=0)

        acc_avg, acc, sensitivity, specificity, PPV = evaluate_metrics(sum_test_conf)
        print("Average Accuracy is: {:>6.4f} on test set\n".format(acc_avg))

        info = ''
        for index_ in range(n_classes):
            print('\t {} rhythm -> Sensitivity(recall): {:1.4f}, Specificity: {:1.4f}, Precision(PPV): {:1.4f}, Accuracy: {:1.4f}'.format(
                classes[index_],
                sensitivity[index_],
                specificity[index_],
                PPV[index_],
                acc[index_]
            ))
            strings = "{:1.4f} {:1.4f} {:1.4f} {:1.4f}".format(sensitivity[index_], specificity[index_], PPV[index_], acc[index_])
            info += strings

        print('\n Average -> Sensitivity: {:1.4f}, Specificity: {:1.4f}, Precision: {:1.4f}, Accuracy: {:1.4f}'.format(
            np.mean(sensitivity),
            np.mean(specificity),
            np.mean(PPV),
            np.mean(acc)
        ))

        results.append(info)

        return acc_avg, acc, sensitivity, specificity, PPV
Exemplo n.º 7
0
    def evaluate(self, inputs, correct_classes, batch_size=16):
        """Evaluate classification accuracy of GAN for given inputs.
        
        # Arguments
        * `inputs` - a set of images of faces
        * `correct_classes` - the correct IDs of every face
        * `batch_size` - the number of images to put in each batch, defaults to 16
        """
        # set model to eval mode
        self.generator.eval()

        if self.device:
            # compute in batches
            accuracies = []
            minibatches = utils.batch_data(
                inputs, correct_classes, batch_size=batch_size
            )
            for batch in minibatches:
                # collect batch and send to CUDA
                batch_inputs, batch_correct_classes = batch
                batch_inputs = batch_inputs.to(self.device)
                batch_correct_classes = batch_correct_classes.to(self.device)

                # calculate classification accuracy
                batch_outputs = self.forward(batch_inputs)
                batch_output_classes = torch.argmin(batch_outputs, dim=1)
                accuracies.append(
                    sum(batch_output_classes == batch_correct_classes)
                    / len(batch_correct_classes)
                )

                # return batch to CPU
                batch_inputs = batch_inputs.to(torch.device("cpu"))
                batch_correct_classes = batch_correct_classes.to(torch.device("cpu"))
            return sum(accuracies) / len(accuracies)
        else:
            # forward propagation
            batch_outputs = self.forward(inputs)

            # calculate classification accuracy
            output_classes = torch.argmin(batch_outputs, dim=1)
            accuracy = sum(output_classes == correct_classes) / len(correct_classes)
            return accuracy
Exemplo n.º 8
0
if dataset in ['protein', 'cora', 'citeseer', 'pubmed']:
    train = sp.hstack([train, feats]).tolil()
print ae.summary()

# Specify some hyperparameters
epochs = 50
train_batch_size = 8
val_batch_size = 256

print('\nFitting autoencoder model...\n')
dummy = np.empty(shape=(adj.shape[0], 1))
y_true = dummy.copy()
mask = dummy.copy()

train_data = generate_data(adj, train, feats, y_true, mask, shuffle=True)
batch_data = batch_data(train_data, train_batch_size)
num_iters_per_train_epoch = adj.shape[0] / train_batch_size
for e in xrange(epochs):
    print('\nEpoch {:d}/{:d}'.format(e + 1, epochs))
    print('Learning rate: {:6f}'.format(K.eval(ae.optimizer.lr)))
    curr_iter = 0
    train_loss = []
    for batch_adj, batch_train, batch_f, dummy_y, dummy_m in batch_data:
        # Each iteration/loop is a batch of train_batch_size samples
        if dataset in ['conflict', 'metabolic']:
            batch_adj = StandardScaler().fit_transform(batch_adj)
            res = ae.train_on_batch([batch_adj], [batch_train, batch_f])
        else:
            res = ae.train_on_batch([batch_adj], [batch_train])
        train_loss.append(res)
        curr_iter += 1
Exemplo n.º 9
0
    optim_g = g_opt.apply_gradients(optim_gen)
    optim_dis = d_opt.compute_gradients(dis_loss, var_list=d_var_list)
    optim_d = d_opt.apply_gradients(optim_dis)
# the optim operation

sample_gen = generator(x_place, reuse=True)

with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)

    saver = tf.train.Saver(max_to_keep=1)
    for epoch in range(train_epoch):
        for idx in range(batch_idx):
            start = time.time()
            figs, labels = batch_data(idx)

            feed_dict = {x_place: labels, y_place: figs}

            sess.run(optim_d, feed_dict=feed_dict)

            for i in range(1):
                sess.run(optim_g, feed_dict=feed_dict)

            gen_value, dis_value, l1_value = sess.run(
                [gen_loss, dis_loss, gen_l1], feed_dict=feed_dict)
            # compute cost time
            end = time.time()
            single_time = (end - start) / 60
            total_time = single_time * train_epoch * batch_idx
            remain_time = total_time - epoch * batch_idx * single_time - idx * single_time
Exemplo n.º 10
0
        with open(options.conll_dev, 'r') as conllFP:
            devData = list(utils.read_conll(conllFP, parser.c2i))

        conll_sentences = []
        for sentence in devData:
            conll_sentence = [
                entry for entry in sentence
                if isinstance(entry, utils.ConllEntry)
            ]
            conll_sentences.append(conll_sentence)

        with open("Results.txt", "a") as results:
            results.write("Epoch\tUAS\tLAS\n")

        sentences, train_batches = utils.batch_data(options.conll_train, c2i,
                                                    options.batch_tokens)
        batches = len(train_batches)
        highestScore = options.highest_score
        tsId = 0
        for epoch in range(options.last_epoch, options.epochs):

            print("Starting epoch ", epoch + 1)
            random.shuffle(train_batches)

            for idx, mini_batch in enumerate(train_batches):

                t_step = (epoch * batches) + idx + 1
                if (t_step % 5000 == 0):
                    parser.Train(sentences, mini_batch, t_step, True)
                else:
                    parser.Train(sentences, mini_batch, t_step)
Exemplo n.º 11
0
    crf=utils.loadModel('ckpts/crf.pkl')
    print(crf)
    predictTags=crf.test(devWordLists)
else:
    crf=CRFModel()
    crf.train(trainWordLists,trainTagLists)
    utils.saveModel('ckpts/crf.pkl',crf)
    predictTags=crf.test(devWordLists)
accuracy=metric.accuracy(predictTags,devTagList)
print('accuracy: ',accuracy)
#BiLSTM模型训练
print('BiLSTM************************')
if os.path.exists('ckpts/bilstm.pkl'):
    model=utils.loadModel('ckpts/bilstm.pkl')
    devWordLists, devTagList = utils.create('dev.txt', make_vocab=False)
    devDatas = utils.batch_data(devWordLists, devTagList, word2id, tag2id)
    id2tag = dict((id, tag) for tag, id in tag2id.items())
    predictTags = []
    while 1:
        try:
            x, y = devDatas.__next__()
            predictScores = model(torch.LongTensor(x))
            scores = torch.argmax(predictScores, dim=2, )
            for i in range(len(scores)):
                predictTag = []
                for j in range(len(y[i])):
                    predictTag.append(id2tag[int(scores[i][j])])
                predictTags.append(predictTag)
        except:
            break
    acc = metric.accuracy(predictTags, devTagList)
Exemplo n.º 12
0
if __name__ == "__main__":
    
    d_worlds = 64
    n_worlds = 32
    batch_size = 32
    n_epochs = 30
    
    language = led_parser.propositional_language()
    parser = led_parser.Parser(language)
    n_symbols = len(language.symbols)

    cell = Sat3Cell(n_symbols, d_worlds, n_worlds, batch_size)
    encoder = TreeNets(cell, n_worlds, batch_size)
    PWN = PossibleWorldsNet(parser, encoder, n_worlds, d_worlds)
    optimizer = tf.keras.optimizers.Adam()

    for i in range(n_epochs):
        print(i)
        for A, B, y in utils.batch_data(utils.read_data('../Logical_Entailment/temp/train.txt'), batch_size):
            loss, grads, p = compute_step(PWN, A, B, y)
            gradients = zip(grads, PWN.trainable_variables)
            optimizer.apply_gradients(gradients)
            logging.info('loss: {}'.format(tf.reduce_mean(loss)))

    
        for A, B, y in utils.batch_data(utils.read_data('../Logical_Entailment/temp/validate.txt'), batch_size):

            acc = np.mean([accuracy(y, PWN(A, B))])
            logging.info('accuracy: {}'.format(acc))
Exemplo n.º 13
0
def train(args):
    max_time = args.max_time # defaule 9
    epochs = args.epochs # 1000
    batch_size = args.batch_size # 20
    num_units = args.num_units # 128
    bidirectional = args.bidirectional
    n_oversampling = args.n_oversample
    checkpoint_dir = args.checkpoint_dir
    result_dir = args.result_dir
    ckpt_name = args.ckpt_name
    test_steps = args.test_steps
    classes = args.classes  # ['N', 'S','V']
    filename = args.data_dir
    use_SE = args.use_SE

    X_train, y_train, X_test, y_test, n_classes, char2numY, input_depth, y_seq_length\
    = data_process(max_time, n_oversampling, classes, filename)

    my_model = model()
    inputs, targets, dec_inputs = my_model.model_input(max_time=max_time,
                                                    input_depth=input_depth)

    logits, infer_logits = my_model.Attention_seq2seq(num_units=num_units,
                                                   batch_size=batch_size,
                                                   char2numY=char2numY,
                                                   inputs=inputs,
                                                   dec_inputs=dec_inputs,
                                                   n_channels=10,
                                                   input_depth=input_depth,
                                                   max_time=max_time,
                                                   bidirectional=bidirectional,
                                                   use_SE=use_SE)
    with tf.variable_scope("optimization", reuse=tf.AUTO_REUSE):
        vars = tf.trainable_variables()
        beta = 0.001
        lossL2 = tf.add_n([tf.nn.l2_loss(v) for v in vars if 'bias' not in v.name]) * beta

        loss = tf.contrib.seq2seq.sequence_loss(logits, targets, tf.ones([batch_size, y_seq_length]))

        # optimizer
        loss = tf.reduce_mean(loss + lossL2)
        optimizer = tf.train.RMSPropOptimizer(1e-3).minimize(loss)


    results = []
    def test_model():
        print('测试')
        acc_track = []
        sum_test_conf = []
        for batch_i, (source_batch, target_batch) in enumerate(batch_data(X_test, y_test, batch_size)):
            # dec_input = np.zeros((len(source_batch), 1)) + char2numY['<GO>']

            batch_logits = sess.run(infer_logits,
                                    feed_dict={
                                        inputs: source_batch,
                                        })
            y_pred = batch_logits.argmax(axis=-1)

            y_true = target_batch[:, 1:-1]
            acc_track.append(y_pred == y_true)
            # print('测试集预测值:', y_pred)
            # print('测试集真实值:', y_true)
            y_pred = y_pred.flatten()
            y_true = y_true.flatten()
            sum_test_conf.append(confusion_matrix(y_true, y_pred, labels=range(len(char2numY)-2)))

        sum_test_conf = np.mean(np.array(sum_test_conf, dtype=np.float32), axis=0)

        acc_avg, acc, sensitivity, specificity, PPV = evaluate_metrics(sum_test_conf)
        print("Average Accuracy is: {:>6.4f} on test set\n".format(acc_avg))

        info = ''
        for index_ in range(n_classes):
            print('\t {} rhythm -> Sensitivity(recall): {:1.4f}, Specificity: {:1.4f}, Precision(PPV): {:1.4f}, Accuracy: {:1.4f}'.format(
                classes[index_],
                sensitivity[index_],
                specificity[index_],
                PPV[index_],
                acc[index_]
            ))
            strings = "{:1.4f} {:1.4f} {:1.4f} {:1.4f}".format(sensitivity[index_], specificity[index_], PPV[index_], acc[index_])
            info += strings

        print('\n Average -> Sensitivity: {:1.4f}, Specificity: {:1.4f}, Precision: {:1.4f}, Accuracy: {:1.4f}'.format(
            np.mean(sensitivity),
            np.mean(specificity),
            np.mean(PPV),
            np.mean(acc)
        ))

        results.append(info)

        return acc_avg, acc, sensitivity, specificity, PPV



    def count_pramaters():
        print('# of params:', np.sum([np.prod(v.get_shape().as_list()) for v in tf.trainable_variables()]))

    count_pramaters()

    mkdir(result_dir)
    mkdir(checkpoint_dir)


    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        saver = tf.train.Saver()
        print(str(datetime.now()))
        pre_acc_avg = 0.0
        ckpt = tf.train.get_checkpoint_state(checkpoint_dir)
        if ckpt and ckpt.model_checkpoint_path:
            ckpt_name = os.path.basename(ckpt.model_checkpoint_path)
            saver.restore(sess, tf.train.latest_checkpoint(checkpoint_dir))
            test_model()
        else:
            losses = []
            loss_track = []
            for epoch_i in range(epochs):
                start_time = time.time()
                train_acc = []
                for batch_i, (source_batch, target_batch) in enumerate(batch_data(X_train, y_train, batch_size)):
                    _, batch_loss, batch_logits = sess.run([optimizer, loss, logits],
                    feed_dict = {
                        inputs: source_batch,
                        dec_inputs: target_batch[:, :-2],
                        targets: target_batch[:, 1:-1]
                    })
                    loss_track.append(batch_loss)
                    train_acc.append(batch_logits.argmax(axis=-1) == target_batch[:, 1:-1])
                    # print('训练预测', batch_logits.argmax(axis=-1))
                    # print('训练真实值', target_batch[:, 1:-1])

                accuracy = np.mean(train_acc)
                print('Epoch {:3} Loss:{:>6.3f} Accuracy:{:>6.4f} Epoch duration:{:>6.3f}'.format(
                    epoch_i,
                    sum(loss_track)/len(loss_track),
                    accuracy,
                    time.time() - start_time
                ))
                losses.append(sum(loss_track) / len(loss_track))
                if (epoch_i + 1) % test_steps == 0:
                    acc_avg, acc, sensitivity, specificity, PPV = test_model() # 输出测试结果
                    print("loss:{:.4f} after {} epochs (batch_size={})".format(loss_track[-1], epoch_i + 1, batch_size))

                    save_path = os.path.join(checkpoint_dir, ckpt_name)
                    saver.save(sess, save_path)
                    print("Model saved in path:%s" % save_path)

            with open(os.path.join(result_dir, "loss.txt"), mode="w") as f: # 保存loss
                for l in losses:
                    f.write(str(l) + "\n")

            with open(os.path.join(result_dir, "infos.txt"), mode='w') as f: # 保存每一次测结果
                for info in results:
                    f.write(info + "\n")
        print(str(datetime.now()))
def run_program(args):
    max_time = args.max_time  # defaule 9
    epochs = args.epochs  # 1000
    batch_size = args.batch_size  # 20
    num_units = args.num_units  # 128
    bidirectional = args.bidirectional
    use_Embedding = args.use_Embedding  # 是否对输出进行Embedding,否的话进行标准化输入
    use_SE = args.use_SE
    n_oversampling = args.n_oversample
    checkpoint_dir = args.checkpoint_dir
    ckpt_name = args.ckpt_name
    test_steps = args.test_steps
    classes = args.classes  # ['N', 'S','V']
    filename = args.data_dir
    result_dir = args.result_dir  # 用于保存每次结果

    print(use_Embedding,
          "==============++++++++++++++++++++++=+++++++++++++++++")
    X_train, y_train, X_test, y_test, n_classes, char2numY, input_depth, y_seq_length \
        = data_process(max_time, n_oversampling, classes, filename, use_Embedding)

    # Placeholders
    inputs = tf.placeholder(tf.float32, [None, max_time, input_depth],
                            name='inputs')
    targets = tf.placeholder(tf.int32, (None, None), 'targets')
    dec_inputs = tf.placeholder(tf.int32, (None, None), 'output')

    logits = build_network(inputs,
                           dec_inputs,
                           char2numY,
                           n_channels=10,
                           input_depth=input_depth,
                           num_units=num_units,
                           max_time=max_time,
                           bidirectional=bidirectional,
                           use_Embedding=use_Embedding,
                           use_SE=use_SE)

    with tf.variable_scope("optimization", reuse=tf.AUTO_REUSE):
        # with tf.name_scope("optimization"):
        # Loss function
        vars = tf.trainable_variables()
        beta = 0.001
        lossL2 = tf.add_n(
            [tf.nn.l2_loss(v) for v in vars if 'bias' not in v.name]) * beta
        loss = tf.contrib.seq2seq.sequence_loss(
            logits, targets, tf.ones([batch_size, y_seq_length]))
        # Optimizer
        loss = tf.reduce_mean(loss + lossL2)
        optimizer = tf.train.RMSPropOptimizer(1e-3).minimize(loss)

    results = []

    def test_model():
        # source_batch, target_batch = next(batch_data(X_test, y_test, batch_size))
        print("测试")
        acc_track = []
        sum_test_conf = []
        for batch_i, (source_batch, target_batch) in enumerate(
                batch_data(X_test, y_test, batch_size)):
            dec_input = np.zeros((len(source_batch), 1)) + char2numY['<GO>']
            for i in range(y_seq_length):
                batch_logits = sess.run(logits,
                                        feed_dict={
                                            inputs: source_batch,
                                            dec_inputs: dec_input
                                        })
                prediction = batch_logits[:, -1].argmax(axis=-1)
                dec_input = np.hstack([dec_input, prediction[:, None]])

            acc_track.append(dec_input[:, 1:] == target_batch[:, 1:])
            y_true = target_batch[:, 1:].flatten()
            y_pred = dec_input[:, 1:].flatten()
            sum_test_conf.append(
                confusion_matrix(y_true,
                                 y_pred,
                                 labels=range(len(char2numY) - 1)))

        sum_test_conf = np.mean(np.array(sum_test_conf, dtype=np.float32),
                                axis=0)

        acc_avg, acc, sensitivity, specificity, PPV = evaluate_metrics(
            sum_test_conf)
        print('Average Accuracy is: {:>6.4f} on test set'.format(acc_avg))

        info = ""
        for index_ in range(n_classes):
            print(
                "\t{} rhythm -> Sensitivity: {:1.4f}, Specificity : {:1.4f}, Precision (PPV) : {:1.4f}, Accuracy : {:1.4f}"
                .format(classes[index_], sensitivity[index_],
                        specificity[index_], PPV[index_], acc[index_]))
            strings = "{:1.4f} {:1.4f} {:1.4f} {:1.4f}".format(
                sensitivity[index_], specificity[index_], PPV[index_],
                acc[index_])
            info += strings

        results.append(info)

        print(
            "\t Average -> Sensitivity: {:1.4f}, Specificity : {:1.4f}, Precision (PPV) : {:1.4f}, Accuracy : {:1.4f}"
            .format(np.mean(sensitivity), np.mean(specificity), np.mean(PPV),
                    np.mean(acc)))
        return acc_avg, acc, sensitivity, specificity, PPV

    def count_prameters():
        print(
            '# of Params: ',
            np.sum([
                np.prod(v.get_shape().as_list())
                for v in tf.trainable_variables()
            ]))

    count_prameters()

    mkdir(result_dir)
    mkdir(checkpoint_dir)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        saver = tf.train.Saver()
        print(str(datetime.now()))
        pre_acc_avg = 0.0
        ckpt = tf.train.get_checkpoint_state(checkpoint_dir)
        if ckpt and ckpt.model_checkpoint_path:
            # # Restore
            ckpt_name = os.path.basename(ckpt.model_checkpoint_path)
            # saver.restore(session, os.path.join(checkpoint_dir, ckpt_name))
            saver.restore(sess, tf.train.latest_checkpoint(checkpoint_dir))
            # or 'load meta graph' and restore weights
            # saver = tf.train.import_meta_graph(ckpt_name+".meta")
            # saver.restore(session,tf.train.latest_checkpoint(checkpoint_dir))
            test_model()
        else:
            losses = []
            loss_track = []
            for epoch_i in range(epochs):
                start_time = time.time()
                train_acc = []
                for batch_i, (source_batch, target_batch) in enumerate(
                        batch_data(X_train, y_train, batch_size)):
                    _, batch_loss, batch_logits = sess.run(
                        [optimizer, loss, logits],
                        feed_dict={
                            inputs: source_batch,
                            dec_inputs: target_batch[:, :-1],
                            targets: target_batch[:, 1:]
                        })
                    loss_track.append(batch_loss)
                    train_acc.append(
                        batch_logits.argmax(axis=-1) == target_batch[:, 1:])

                accuracy = np.mean(train_acc)
                print(
                    'Epoch {:3} Loss: {:>6.3f} Accuracy: {:>6.4f} Epoch duration: {:>6.3f}s'
                    .format(epoch_i,
                            sum(loss_track) / len(loss_track), accuracy,
                            time.time() - start_time))
                losses.append(sum(loss_track) / len(loss_track))

                if epoch_i % test_steps == 0:
                    acc_avg, acc, sensitivity, specificity, PPV = test_model()

                    print('loss {:.4f} after {} epochs (batch_size={})'.format(
                        loss_track[-1], epoch_i + 1, batch_size))
                    save_path = os.path.join(checkpoint_dir, ckpt_name)
                    saver.save(sess, save_path)
                    print("Model saved in path: %s" % save_path)

            with open(os.path.join(result_dir, "loss.txt"),
                      mode="w") as f:  # 保存loss
                for l in losses:
                    f.write(str(l) + "\n")

            with open(os.path.join(result_dir, "infos.txt"),
                      mode='w') as f:  # 保存每一次测结果
                for info in results:
                    f.write(info + "\n")
        print(str(datetime.now()))