コード例 #1
0
def test(test_file,
         test_output,
         word2num,
         model_path,
         batch_size,
         lr,
         val_acc,
         use_cuda=False):

    print('  Constructing network model...')
    statement_word2num = word2num[0]
    subject_word2num = word2num[1]
    speaker_word2num = word2num[2]
    speaker_pos_word2num = word2num[3]
    state_word2num = word2num[4]
    party_word2num = word2num[5]
    context_word2num = word2num[6]

    model = Net(len(statement_word2num), len(subject_word2num),
                len(speaker_word2num), len(speaker_pos_word2num),
                len(state_word2num), len(party_word2num),
                len(context_word2num))

    state_dict = torch.load(
        os.path.join(
            model_path,
            'model_bs_{}_lr_{}_acc_{}.pth'.format(batch_size, lr, val_acc)))
    model.load_state_dict(state_dict)
    test_samples, labels = test_data_prepare(test_file, word2num, 'test')
    dataset_to_variable(test_samples, use_cuda)
    out = open(test_output, 'w')
    acc = 0

    for (sample, label) in zip(test_samples, labels):
        statement = Variable(sample.statement).unsqueeze(0)
        subject = Variable(sample.subject).unsqueeze(0)
        speaker = Variable(sample.speaker).unsqueeze(0)
        speaker_pos = Variable(sample.speaker_pos).unsqueeze(0)
        state = Variable(sample.state).unsqueeze(0)
        party = Variable(sample.party).unsqueeze(0)
        context = Variable(sample.context).unsqueeze(0)

        prediction = model(statement, subject, speaker, speaker_pos, state,
                           party, context)
        prediction = int(np.argmax(prediction.data.numpy()))
        if prediction == label:
            acc += 1
        out.write(num_to_label[prediction] + '\n')

    out.close()
    acc /= len(test_samples)
    print('================================')
    print('Test accuracy :: {}'.format(acc))
    print('Val accuracy :: {}'.format(val_acc))
    print('================================')
コード例 #2
0
ファイル: test.py プロジェクト: zfjmike/fake-news-detection
def test(test_file, test_output, word2num, model, use_cuda = False):
    test_samples = test_data_prepare(test_file, word2num, 'test')
    dataset_to_variable(test_samples, use_cuda)
    out = open(test_output, 'w')

    for sample in test_samples:
        prediction = model(sample)
        prediction = int(np.argmax(prediction.data.numpy()))
        out.write(num_to_label[prediction]+'\n')

    out.close()
コード例 #3
0
def test(test_samples, test_output, model, classification_type, use_cuda = False):

    model.eval()

    test_samples = dataset_to_variable(test_samples, use_cuda)
    out = open(test_output, 'w', buffering=1)
    acc = 0
    
    for sample in test_samples:
        prediction = model(sample)
        prediction = int(np.argmax(prediction.cpu().data.numpy()))
        #---choose 6 way or binary classification 
        if classification_type == 2:
            out.write(num_to_label_2_way_classification[prediction]+'\n')
        else:
            out.write(num_to_label_6_way_classification[prediction]+'\n')

        if prediction == sample.label:
            acc += 1
    acc /= len(test_samples)
    print('  Test Accuracy: {:.3f}'.format(acc))
    out.close()

    return acc
コード例 #4
0
def train(train_samples, valid_samples, lr, epoch, model, num_classes,
          use_cuda, word2num, hyper, nnArchitecture, timestampLaunch):

    train_data = train_samples
    train_data = dataset_to_variable(train_data, use_cuda)
    valid_data = valid_samples
    valid_data = dataset_to_variable(valid_data, use_cuda)

    # model cuda
    device = torch.device('cuda') if use_cuda else torch.device('cpu')
    model.to(device)

    # Start training
    print('\n  Start training')

    optimizer = optim.Adam(model.parameters(), lr=lr)

    step = 0
    val_acc = 0
    display_interval = 500
    tick = time.time()

    for epoch_ in range(epoch):

        model.train()
        random.shuffle(train_data)
        total_loss = 0

        for sample in train_data:

            optimizer.zero_grad()

            # import pdb; pdb.set_trace()
            prediction = model(sample)
            label = Variable(torch.LongTensor([sample.label])).to(device)
            # loss = F.cross_entropy(prediction, label)
            # print("prediction:", prediction, " label:", label)
            loss = F.nll_loss(prediction, label)
            loss.backward()
            optimizer.step()

            step += 1
            if step % display_interval == 0:
                print('  [INFO] - Epoch ' + str(epoch_ + 1) + '/' +
                      str(epoch) + ' Step:: ' + str(step) +
                      ' Loss: {:.3f}'.format(loss.data.item()))

            total_loss += loss.cpu().data.numpy()

            # if step %100 == 0:
            #     break

        print('  [INFO] --- Epoch ' + str(epoch_ + 1) +
              ' complete. Avg. Loss: {:.3f}'.format(total_loss /
                                                    len(train_data)) +
              '  Time taken: {:.3f}'.format(time.time() - tick))
        val_acc = valid(valid_data, model)

        modelName = 'm-' + nnArchitecture + '-num_classes-' + str(
            num_classes) + '-' + str(timestampLaunch) + '-epoch-' + str(
                epoch_) + '-val_acc-{:.3f}'.format(val_acc) + '.pth.tar'
        torch.save(
            {
                'state_dict': model.state_dict(),
                'word2num': word2num,
                'hyper': hyper
            }, './models/' + modelName)
        print("Saved: ", modelName)

    return model, val_acc
コード例 #5
0
ファイル: train.py プロジェクト: zfjmike/fake-news-detection
def train(train_samples,
          valid_samples,
          word2num,
          lr=0.001,
          epoch=5,
          use_cuda=False):

    print('Training...')

    # Prepare training data
    print('  Preparing training data...')
    statement_word2num = word2num[0]
    subject_word2num = word2num[1]
    speaker_word2num = word2num[2]
    speaker_pos_word2num = word2num[3]
    state_word2num = word2num[4]
    party_word2num = word2num[5]
    context_word2num = word2num[6]

    train_data = train_samples
    dataset_to_variable(train_data, use_cuda)
    valid_data = valid_samples
    dataset_to_variable(valid_data, use_cuda)

    # Construct model instance
    print('  Constructing network model...')
    model = Net(len(statement_word2num), len(subject_word2num),
                len(speaker_word2num), len(speaker_pos_word2num),
                len(state_word2num), len(party_word2num),
                len(context_word2num))
    if use_cuda: model.cuda()

    # Start training
    print('  Start training')

    optimizer = optim.Adam(model.parameters(), lr=lr)
    model.train()

    step = 0
    display_interval = 2000

    for epoch_ in range(epoch):
        print('  ==> Epoch ' + str(epoch_) + ' started.')
        random.shuffle(train_data)
        total_loss = 0
        for sample in train_data:

            optimizer.zero_grad()

            prediction = model(sample)
            label = Variable(torch.LongTensor([sample.label]))
            loss = F.cross_entropy(prediction, label)
            loss.backward()
            optimizer.step()

            step += 1
            if step % display_interval == 0:
                print('    ==> Iter: ' + str(step) + ' Loss: ' + str(loss))

            total_loss += loss.data.numpy()

        print('  ==> Epoch ' + str(epoch_) + ' finished. Avg Loss: ' +
              str(total_loss / len(train_data)))

        valid(valid_data, word2num, model)

    return model
コード例 #6
0
def train(train_samples,
          valid_samples,
          word2num,
          lr = 0.001,
          epoch = 5,
          use_cuda = False):

    print('Training...')

    # Prepare training data
    print('  Preparing training data...')
    statement_word2num = word2num[0]
    subject_word2num = word2num[1]
    speaker_word2num = word2num[2]
    speaker_pos_word2num = word2num[3]
    state_word2num = word2num[4]
    party_word2num = word2num[5]
    context_word2num = word2num[6]

    train_data = train_samples
    dataset_to_variable(train_data, use_cuda)
    valid_data = valid_samples
    dataset_to_variable(valid_data, use_cuda)

    # Construct model instance
    print('  Constructing network model...')
    model = Net(len(statement_word2num),
                len(subject_word2num),
                len(speaker_word2num),
                len(speaker_pos_word2num),
                len(state_word2num),
                len(party_word2num),
                len(context_word2num))
    if use_cuda: model.cuda()

    # Start training
    print('  Start training')

    optimizer = optim.Adam(model.parameters(), lr = lr)
    model.train()

    step = 0
    display_interval = 2000

    for epoch_ in range(epoch):
        print('  ==> Epoch '+str(epoch_)+' started.')
        random.shuffle(train_data)
        total_loss = 0
        for sample in train_data:

            optimizer.zero_grad()

            prediction = model(sample)
            label = Variable(torch.LongTensor([sample.label]))
            loss = F.cross_entropy(prediction, label)
            loss.backward()
            optimizer.step()

            step += 1
            if step % display_interval == 0:
                print('    ==> Iter: '+str(step)+' Loss: '+str(loss))

            total_loss += loss.data.numpy()

        print('  ==> Epoch '+str(epoch_)+' finished. Avg Loss: '+str(total_loss/len(train_data)))

        valid(valid_data, word2num, model)

    return model