Пример #1
0

# Train model
t_total = time.time()
loss_values = []
bad_counter = 0
best = np.inf
best_epoch = 0
for epoch in range(args.epochs):
    loss_values.append(train(epoch))

    if loss_values[-1] < best:
        if args.cuda:
            model.cpu()
        torch.save(
            model.state_dict(), '{}_{}_{}.pkl'.format(model._get_name(),
                                                      args.dataset, epoch))
        if args.cuda:
            model.cuda()
        best = loss_values[-1]
        best_epoch = epoch
        bad_counter = 0
    else:
        bad_counter += 1

    if bad_counter == args.patience:
        print("Patience {0} exceeded. Best in last {0} epochs is {1:.4f}.".
              format(args.patience, best))
        break

    files = glob.glob('{}_{}_*.pkl'.format(model._get_name(), args.dataset))
Пример #2
0
    idx_test = idx_test.cuda()

features, adj, labels = Variable(features), Variable(adj), Variable(labels)


def compute_test():
    model.eval()
    output = model(features, adj)
    loss_test = F.nll_loss(output[idx_test], labels[idx_test])
    acc_test = accuracy(output[idx_test], labels[idx_test])
    print("Test set results:", "loss= {:.4f}".format(loss_test.data.item()),
          "accuracy= {:.4f}".format(acc_test.data.item()))


# Load model
filename = glob.glob('{}_{}_*.pkl'.format(model._get_name(), args.dataset))[0]
print("Loading {}...".format(filename))
if args.cuda:
    model_location = 'cuda'
else:
    model_location = 'cpu'
state_dict = torch.load(filename, map_location=model_location)
model.load_state_dict(state_dict)
model.eval()
print('Loaded {}.'.format(filename))

# Testing
compute_test()

state_dict['gc1.weight']