Exemplo n.º 1
0
def test(model, config, word_to_id, cat_to_id, id_to_cat):
    print("Loading test data_loaders...")
    start_time = time.time()
    x_test, y_test = process_file(config.test_dir, word_to_id, cat_to_id,
                                  config.seq_length)

    with tf.Session() as session:
        session.run(tf.global_variables_initializer())
        saver = tf.train.Saver()
        # 读取保存的模型
        saver.restore(sess=session, save_path=config.save_dir)

        print('Testing...')
        loss_test, acc_test = evaluate(session, model, x_test, y_test)
        msg = 'Test Loss: {0:>6.2}, Test Acc: {1:>7.2%}'
        print(msg.format(loss_test, acc_test))

        batch_size = 128
        data_len = len(x_test)
        num_batch = int((data_len - 1) / batch_size) + 1

        y_test_cls = np.argmax(y_test, 1)
        # 用于存储预测结果
        y_pred_cls = np.zeros(shape=len(x_test), dtype=np.int32)

        # 逐批次处理
        for i in range(num_batch):
            start_id = i * batch_size
            end_id = min((i + 1) * batch_size, data_len)
            feed_dict = {
                model.input_x: x_test[start_id:end_id],
                model.keep_prob: 1.0
            }
            y_pred_cls[start_id:end_id] = session.run(model.y_pred_cls,
                                                      feed_dict=feed_dict)

        # 评估
        print("PRF:")
        print(
            metrics.classification_report(y_test_cls,
                                          y_pred_cls,
                                          target_names=id_to_cat))

        # 混淆矩阵
        print("Confusion Matrix:")
        print("Truth \ Prediction")
        cm = metrics.confusion_matrix(y_test_cls, y_pred_cls)
        print(cm)

        time_dif = get_time_dif(start_time)
        print("Time usage:", time_dif)
Exemplo n.º 2
0
def train():
    with open(os.path.join(saves_dir, 'word2id.pkl'), 'rb') as f:
        vocab = pickle.load(f)
        f.close()
    with open(os.path.join(saves_dir, 'id2word.pkl'), 'rb') as f:
        id2word = pickle.load(f)
        f.close()
    with open(os.path.join(saves_dir, 'word_embedding.pkl'), 'rb') as f:
        word_embedding = pickle.load(f)
        f.close()
    train_loader, char_max_len = get_trainloader('./data/processed/train.pkl')
    valid_loader = get_validloader('./data/processed/valid.pkl')
    # test_loader = get_testloader('./data/processed/play.pkl')
    char2idx = json.loads(open('./data/processed/char2idx.js').read())
    char_data = {
        'char_vocab_size': len(char2idx) + 1,
        'char_emb_dim': CHAR_EMB_DIM,
        'char_hid_dim': CHAR_HID_DIM,
        'char_len': char_max_len
    }
    print('=========model architecture==========')
    model = RNNModel(args.model,
                     len(vocab),
                     args.emdim,
                     args.hidim,
                     args.nlayers,
                     args.tied,
                     args.dropout,
                     device,
                     word_embedding,
                     use_ch=args.use_ch,
                     use_he=args.use_he,
                     use_i=args.use_i,
                     use_h=args.use_h,
                     use_g=args.use_g,
                     **char_data).to(device)
    print(model)
    print('=============== end =================')
    loss_fn = nn.CrossEntropyLoss(ignore_index=0)
    parameters = filter(lambda p: p.requires_grad, model.parameters())
    optimizer = torch.optim.Adam(parameters, args.lr, weight_decay=args.wdecay)
    print('Training and evaluating...')
    start_time = time.time()
    if not os.path.exists(model_save_dir):
        os.makedirs(model_save_dir)
    best_ppl = 9999999
    last_improved = 0
    require_improvement = 5
    for epoch in range(args.epochs):
        model.training = True
        total_loss = 0.0
        loss_epoch = []
        for batch, inp in enumerate(
                tqdm(train_loader,
                     desc='Epoch: %03d' % (epoch + 1),
                     leave=False)):
            data = {
                'word':
                torch.tensor(inp['word'], dtype=torch.long).to(device),
                'seq':
                torch.tensor(torch.t(inp['seq']), dtype=torch.long).to(device),
                'chars':
                torch.tensor(inp['chars'], dtype=torch.long).to(device),
                'hnym':
                torch.tensor(inp['hnym'], dtype=torch.long).to(device),
                'hnym_weights':
                torch.tensor(inp['hnym_weights'], dtype=torch.float).to(device)
            }

            targets = torch.tensor(torch.t(inp['target']),
                                   dtype=torch.long).to(device)
            output, hidden = model(data, None)
            loss = loss_fn(output, targets.view(-1))
            optimizer.zero_grad()
            total_loss += loss.item()
            loss_epoch.append(loss.item())
            loss.backward()
            # `clip_grad_norm`
            torch.nn.utils.clip_grad_norm_(model.parameters(), args.clip)
            optimizer.step()
        train_loss = np.mean(loss_epoch)
        train_ppl = np.exp(train_loss)
        valid_loss, valid_ppl = evaluate(model, valid_loader, device)
        # generate(model, test_loader, id2word, epoch + 1, 20)

        if valid_ppl < best_ppl:
            best_ppl = valid_ppl
            last_improved = epoch
            torch.save(
                model.state_dict(), model_save_dir +
                'defseq_model_params_%s_min_ppl.pkl' % (epoch + 1))
            improved_str = '*'
        else:
            torch.save(
                model.state_dict(), model_save_dir +
                'defseq_model_params_%s_ppl.pkl' % (epoch + 1))
            improved_str = ''
        time_dif = get_time_dif(start_time)
        msg = 'Epoch: {0:>6},Train Loss: {1:>6.6}, Train Ppl: {2:>6.6},' \
              + ' Val loss: {3:>6.6}, Val Ppl: {4:>6.6},Time:{5} {6}'
        print(
            msg.format(epoch + 1, train_loss, train_ppl, valid_loss, valid_ppl,
                       time_dif, improved_str))
        if epoch - last_improved > require_improvement:
            print("No optimization for a long time, auto-stopping...")
            break
    return 1
Exemplo n.º 3
0
def train(model, config, word_to_id, cat_to_id):
    print("Configuring TensorBoard and Saver...")
    # 配置 Tensorboard,重新训练时,请将tensorboard文件夹删除,不然图会覆盖
    tensorboard_dir = config.tensorboard_dir
    if not os.path.exists(tensorboard_dir):
        os.makedirs(tensorboard_dir)

    tf.summary.scalar("loss", model.loss)
    tf.summary.scalar("accuracy", model.acc)
    merged_summary = tf.summary.merge_all()
    writer = tf.summary.FileWriter(tensorboard_dir)

    # 配置 Saver
    saver = tf.train.Saver()
    if not os.path.exists(config.save_dir):
        os.makedirs(config.save_dir)

    print("Loading training and validation data_loaders...")
    # 载入训练集与验证集
    start_time = time.time()
    x_train, y_train = process_file(config.train_dir, word_to_id, cat_to_id,
                                    config.seq_length)
    x_val, y_val = process_file(config.val_dir, word_to_id, cat_to_id,
                                config.seq_length)
    time_dif = get_time_dif(start_time)
    print("Time usage:", time_dif)

    # 创建session
    with tf.Session() as session:
        session.run(tf.global_variables_initializer())
        writer.add_graph(session.graph)

        print('Training and evaluating...')
        start_time = time.time()
        total_batch = 0  # 已训练的总批次
        best_acc_val = 0.0  # 最佳验证集准确率
        last_improved = 0  # 记录上一次有提升是第几个批次
        require_improvement = 1000  # 如果超过1000个批次未提升,提前结束训练

        flag = False
        for epoch in range(config.num_epochs):
            print('Epoch:', epoch + 1)
            batch_train = batch_iter(x_train, y_train, config.batch_size)
            for x_batch, y_batch in batch_train:
                feed_dict = create_feed_dict(model, x_batch, y_batch,
                                             config.dropout_keep_prob)

                session.run(model.optim, feed_dict=feed_dict)  # 运行优化
                total_batch += 1

                if total_batch % config.save_per_batch == 0:
                    # 每多少轮次将训练结果写入tensorboard scalar
                    s = session.run(merged_summary, feed_dict=feed_dict)
                    writer.add_summary(s, total_batch)

                if total_batch % config.print_per_batch == 0:
                    # 每多少轮次输出在训练集和验证集上的性能
                    feed_dict[model.keep_prob] = 1.0
                    loss_train, acc_train = session.run(
                        [model.loss, model.acc], feed_dict=feed_dict)
                    loss_val, acc_val = evaluate(session, model, x_val, y_val)

                    if acc_val > best_acc_val:
                        # 保存最好结果
                        best_acc_val = acc_val
                        last_improved = total_batch
                        saver.save(sess=session, save_path=config.save_dir)
                        improved_str = '*'
                    else:
                        improved_str = ''

                    time_dif = get_time_dif(start_time)
                    msg = 'Iter: {0:>6}, Train Loss: {1:>6.2}, Train Acc: {2:>7.2%},' \
                          + ' Val Loss: {3:>6.2}, Val Acc: {4:>7.2%}, Time: {5} {6}'
                    print(
                        msg.format(total_batch, loss_train, acc_train,
                                   loss_val, acc_val, time_dif, improved_str))

                if total_batch - last_improved > require_improvement:
                    # 验证集正确率长期不提升,提前结束训练
                    print("No optimization for a long time, auto-stopping...")
                    flag = True
                    break  # 跳出循环
            if flag:  # 同上
                break