Esempio n. 1
0
def build_textcnn_model(vocab, config, train=True):
    model = TextCNN(vocab.vocab_size, config)
    if train:
        model.train()
    else:
        model.eval()

    if torch.cuda.is_available():
        model.cuda()
    else:
        model.cpu()
    return model
Esempio n. 2
0
def build_textcnn_model(vocab, config, train=True):
    model = TextCNN(vocab.vocab_size, config)
    if train:
        model.train()
        #在训练模型时会在前面加上train();
    else:
        model.eval()
        #在测试模型时在前面使用eval(),会将BN和DropOut固定住,不会取平均,而是用训练好的值
    if torch.cuda.is_available():
        model.cuda()
    else:
        model.cpu()
    return model
Esempio n. 3
0
def build_textcnn_model(vocab, config, train=True):
    model = TextCNN(vocab.vocab_size, config)
    if train:
        model.train()
        #在训练模型时会在前面加上train();
    else:
        model.eval()
        #在测试模型时在前面使用eval(),会将BN和DropOut固定住,不会取平均,而是用训练好的值

    #train()与eval()两个方法是针对网络train和eval时采用不同方式的情况
    #比如Batch Normalization和Dropout
    #BN的作用主要是对网络中间的每层进行归一化处理,并且使用变换重构保证所提取的特征分布不会被破坏;
    #由于训练完毕后参数都是固定的,所有BN的训练和测试时的操作不同
    #Dropopt能够克服过拟合,在每个训练batch中,通过忽略一般的特征检测器,可以明显地减少过拟合现象。
    if torch.cuda.is_available():
        model.cuda()
    else:
        model.cpu()
    return model
Esempio n. 4
0
            if it % len_print == 0 and it > 0 and args.verbose:
                if args.gpu:
                    outputs = outputs.cpu()
                    labels = labels.cpu()
                ground_true = labels.numpy()
                dist = softmax(outputs).detach().numpy()
                acc = (np.argmax(dist,axis=1) == ground_true).sum()/dist.shape[0]
                print('[%d,%5d] loss: %.3f acc: %.3f' % (ep, it, loss, acc))
        ep += 1
    if args.verbose:
        print("Testing...")
    net = best_model
    testset = TextDataset(args.dataset, args.percent, test=True, wo_unlabel=args.without_unlabel)
    testLoader = DataLoader(testset, batch_size=500,shuffle=False,num_workers=4)
    net.eval()
    net.cpu()
    f1_micro = 0.0
    f1_macro = 0.0
    cum_loss = 0.0
    ground_true = []
    pred = []
    for it, data in enumerate(testLoader):
        inputs, labels = data
        outputs = net(inputs)
        dist = softmax(outputs).detach().numpy()
        ground_true = np.concatenate([ground_true, labels.numpy()],axis=0)
        pred = np.concatenate([pred, np.argmax(dist,axis=1)],axis=0)
    f1_micro = f1_score(ground_true, pred, average='micro')
    f1_macro = f1_score(ground_true, pred, average='macro')
    print("F1 score: micro-%.5f\tmacro-%.5f" % (f1_micro, f1_macro))