def eval(fold_ix=1):
    test_data = data.Data(
        fold=fold_ix, is_test_data=True
    )  # the dataset merges self.real_U_file and self.fake_U_file
    test_data_loader = DataLoader(test_data,
                                  batch_size=50,
                                  shuffle=True,
                                  num_workers=8)
    # self.discriminator(validation_data_loader)

    X_trn, Y_trn, Y_trn_o, X_tst, Y_tst, Y_tst_o, vocabulary, vocabulary_inv = data_helpers.load_data(
        fold=fold_ix)

    X_trn = X_trn.astype(np.int32)
    X_tst = X_tst.astype(np.int32)
    Y_trn = Y_trn.astype(np.int32)
    Y_tst = Y_tst.astype(np.int32)

    args.num_classes = Y_trn.shape[1]

    embedding_weights = load_word2vec('glove',
                                      vocabulary_inv,
                                      num_features=300)

    capsule_net = CapsNet_Text(args, embedding_weights)
    path = 'save/model/fold' + str(fold_ix) + '/nlp-capsule50.pth'
    capsule_net.load_state_dict(torch.load(path))
    # discriminator.eval()

    with torch.no_grad():
        for _, (input, labels) in enumerate(test_data_loader):
            # input, labels = input, labels
            _, preds = capsule_net(
                input, labels)  # (None, vocab_size, sequence_len+1)

            # print('preds without round: ' , preds.squeeze(2))

            preds = preds.round().squeeze(2)
            # print('input : ', input)
            # print('preds squeeze 2 : ', preds)
            # print('labels: ', labels)

            # print('preds 0: ' , preds.shape[0])
            # print('preds 1: ' , preds.shape[1])
            # print('labels 0: ', labels.shape[0])
            # print('labels 1: ', labels.shape[1])

            report = metrics.classification_report(labels, preds)
            print('report on fold ' + str(fold_ix) + ': ')
            print(report)
    df = pd.DataFrame(Loss_data, columns = ['Epochs', 'Loss'])
    df.to_csv(path)



X_trn, Y_trn, X_tst, Y_tst, vocabulary, vocabulary_inv = datasets.get_cross_domain_doctor_doctor()
X_trn = X_trn.astype(np.int32)
X_tst = X_tst.astype(np.int32)
Y_trn = Y_trn.astype(np.int32)
Y_tst = Y_tst.astype(np.int32)

args.num_classes = Y_trn.shape[1]
embedding_weights = load_word2vec('glove', vocabulary_inv, num_features=300)
    
capsule_net = CapsNet_Text(args, embedding_weights)
current_lr = args.learning_rate
optimizer = Adam(capsule_net.parameters(), lr=current_lr)
# capsule_net = nn.DataParallel(capsule_net).cuda()


losses = []
for epoch in range(args.num_epochs):
    # torch.cuda.empty_cache()

    nr_trn_num = X_trn.shape[0]
    nr_batches = int(np.ceil(nr_trn_num / float(args.tr_batch_size)))

    if epoch > args.learning_rate_decay_start and args.learning_rate_decay_start >= 0:
        frac = (epoch - args.learning_rate_decay_start) // args.learning_rate_decay_every
        decay_factor = args.learning_rate_decay_rate  ** frac
Example #3
0
print(json.dumps(params, indent=2))

X_trn, Y_trn, Y_trn_o, X_tst, Y_tst, Y_tst_o, vocabulary, vocabulary_inv = data_helpers.load_data(
    args.dataset, max_length=args.sequence_length, vocab_size=args.vocab_size)
Y_trn = Y_trn.toarray()
Y_tst = Y_tst.toarray()

X_trn = X_trn.astype(np.int32)
X_tst = X_tst.astype(np.int32)
Y_trn = Y_trn.astype(np.int32)
Y_tst = Y_tst.astype(np.int32)

embedding_weights = load_word2vec('glove', vocabulary_inv, args.vec_size)
args.num_classes = Y_trn.shape[1]

capsule_net = CapsNet_Text(args, embedding_weights)
capsule_net = nn.DataParallel(capsule_net).cuda()
model_name = 'model-eur-akde-29.pth'
capsule_net.load_state_dict(
    torch.load(os.path.join(args.start_from, model_name)))
print(model_name + ' loaded')

model_name = 'model-EUR-CNN-40.pth'
baseline = CNN_KIM(args, embedding_weights)
baseline.load_state_dict(torch.load(os.path.join(args.start_from, model_name)))
baseline = nn.DataParallel(baseline).cuda()
print(model_name + ' loaded')

nr_tst_num = X_tst.shape[0]
nr_batches = int(np.ceil(nr_tst_num / float(args.ts_batch_size)))
X_trn, Y_trn, Y_trn_o, X_tst, Y_tst, Y_tst_o, vocabulary, vocabulary_inv = data_helpers.load_data(args.dataset,
                                                                           max_length=args.sequence_length,
                                                                           vocab_size=args.vocab_size)
Y_trn = Y_trn.toarray()
Y_tst = Y_tst.toarray()

X_trn = X_trn.astype(np.int32)
X_tst = X_tst.astype(np.int32)
Y_trn = Y_trn.astype(np.int32)
Y_tst = Y_tst.astype(np.int32)

embedding_weights = load_word2vec('glove', vocabulary_inv, args.vec_size)

args.num_classes = Y_trn.shape[1]

capsule_net = CapsNet_Text(args, embedding_weights)
capsule_net = nn.DataParallel(capsule_net).cuda()

model_name = 'model-EUR-CNN-40.pth'
baseline = CNN_KIM(args, embedding_weights)
baseline.load_state_dict(torch.load(os.path.join('save_new', model_name)))
baseline = nn.DataParallel(baseline).cuda()
print(model_name + ' loaded')

def transformLabels(labels, total_labels):
    label_index = list(set([l for _ in total_labels for l in _]))
    label_index.sort()

    variable_num_classes = len(label_index)
    target = []
    for _ in labels: