Beispiel #1
0
def main(opt):
    train_dataset = bAbIDataset(opt.dataroot, opt.question_id, True)
    train_dataloader = bAbIDataloader(train_dataset,
                                      batch_size=opt.batchSize,
                                      shuffle=True,
                                      num_workers=2)

    test_dataset = bAbIDataset(opt.dataroot, opt.question_id, False)
    test_dataloader = bAbIDataloader(test_dataset,
                                     batch_size=opt.batchSize,
                                     shuffle=False,
                                     num_workers=2)

    opt.annotation_dim = 1  # for bAbI
    opt.n_edge_types = train_dataset.n_edge_types
    opt.n_node = train_dataset.n_node

    net = GGNN(opt)
    print(net)

    criterion = nn.CrossEntropyLoss()

    if opt.cuda:
        net.cuda()
        criterion.cuda()

    optimizer = optim.Adam(net.parameters(), lr=opt.lr)

    for epoch in range(0, opt.niter):
        train(epoch, train_dataloader, net, criterion, optimizer, opt)
        test(test_dataloader, net, criterion, optimizer, opt)
Beispiel #2
0
def run(opt):
    start_time = time.time()
    opt.dataroot = 'babi_data/%s/train/%d_graphs.txt' % (opt.processed_path,
                                                         opt.task_id)
    print(opt)

    train_dataset = bAbIDataset(opt.dataroot, opt.question_id, True,
                                opt.train_size)
    train_dataloader = bAbIDataloader(train_dataset, batch_size=opt.batchSize, \
                                      shuffle=True, num_workers=2)

    test_dataset = bAbIDataset(opt.dataroot, opt.question_id, False,
                               opt.train_size)
    test_dataloader = bAbIDataloader(test_dataset, batch_size=opt.batchSize, \
                                     shuffle=False, num_workers=2)

    opt.annotation_dim = 1  # for bAbI
    opt.n_edge_types = train_dataset.n_edge_types
    opt.n_node = train_dataset.n_node

    if opt.net == 'GGNN':
        net = GGNN(opt)
        net.double()
    else:
        net = Graph_OurConvNet(opt)
        net.double()
    print(net)

    criterion = nn.CrossEntropyLoss()

    if opt.cuda:
        net.cuda()
        criterion.cuda()

    optimizer = optim.Adam(net.parameters(), lr=opt.lr)

    for epoch in range(0, opt.niter):
        train_loss = train(epoch, train_dataloader, net, criterion, optimizer,
                           opt)
        test_loss, numerator, denominator = test(test_dataloader, net,
                                                 criterion, optimizer, opt)

    return train_loss, test_loss, numerator, denominator, time.time(
    ) - start_time
Beispiel #3
0
    help='the number of steps after which the learning rate decay')

opt = parser.parse_args()
train_data = pickle.load(open(opt.dataset + '/train.txt', 'rb'))
test_data = pickle.load(open(opt.dataset + '/test.txt', 'rb'))

n_node = 310

train_data = Data(train_data, sub_graph=True, method=opt.method, shuffle=True)
test_data = Data(test_data, sub_graph=True, method=opt.method, shuffle=True)

model = GGNN(hidden_size=opt.hiddenSize,
             out_size=opt.hiddenSize,
             batch_size=opt.batchSize,
             n_node=n_node,
             lr=opt.lr,
             l2=opt.l2,
             step=opt.step,
             decay=opt.lr_dc_step * len(train_data.inputs) / opt.batchSize,
             lr_dc=opt.lr_dc,
             nonhybrid=opt.nonhybrid)

print(opt)

best_result = [0, 0]
best_epoch = [0, 0]

for epoch in range(opt.epoch):
    print('epoch: ', epoch, '===========================================')
    slices = train_data.generate_batch(model.batch_size)  # 得到每一个batch 的起始索引
    fetches = [model.opt, model.loss_train, model.global_step]
    print('start training: ', datetime.datetime.now())
Beispiel #4
0
train_data_loader = bAbIDataLoader(train_dataset,
                                   batch_size=batch_size,
                                   shuffle=True,
                                   num_workers=2)

test_dataset = bAbIDataset(dataset_path, question_id=0, is_train=False)
test_data_loader = bAbIDataLoader(test_dataset,
                                  batch_size=batch_size,
                                  shuffle=False,
                                  num_workers=2)

n_edge_types = train_dataset.n_edge_types
n_nodes = train_dataset.n_node

# The dataset has the form: [(adjacency matrix, annotation, target), ...]
ggnn = GGNN(state_dim, annotation_dim, n_edge_types, n_nodes, n_steps)

# The dataset is all doubles so convert the model to be double
ggnn = ggnn.double()

crit = nn.CrossEntropyLoss()

if use_cuda:
    net.use_cuda()
    crit.use_cuda()

opt = optim.Adam(ggnn.parameters(), lr=lr)


def model_inference(ggnn, adj_matrix, annotation, target):
    padding = torch.zeros(len(annotation), n_nodes,
Beispiel #5
0
def main(args):
    # strategy 0 is similar with DGL version, but strategy 1 is much faster
    strategy = 1
    data = load_data(args.dataset)
    # data = load_data(args)

    features = torch.FloatTensor(data.features)
    labels = torch.LongTensor(data.labels)
    train_mask = torch.ByteTensor(data.train_mask)
    val_mask = torch.ByteTensor(data.val_mask)
    test_mask = torch.ByteTensor(data.test_mask)

    g = data.graph
    num_classes = data.num_labels
    num_edges = g.num_edges
    feature_dim = features.shape[1]
    num_edge_type = 2

    if args.gpu < 0:
        cuda = False
    else:
        cuda = True
        torch.cuda.set_device(args.gpu)
        g.edge_types = g.edge_types.cuda()
        g.edge_list = g.edge_list.cuda()
        g.src_list = g.src_list.cuda()
        g.dst_list = g.dst_list.cuda()
        g.edge_types = g.edge_types.cuda()

        features = features.cuda()
        labels = labels.cuda()
        train_mask = train_mask.cuda()
        val_mask = val_mask.cuda()
        test_mask = test_mask.cuda()

    model = GGNN(g,
                 num_classes,
                 num_edge_type,
                 feature_dim,
                 feature_dim,
                 strategy=strategy)

    if cuda:
        model = model.cuda()
        if strategy == 0:
            model.edge_matrix = model.edge_matrix.cuda()
        elif strategy == 1:
            for i in range(num_edge_type):
                model.edge_matrix[i] = model.edge_matrix[i].cuda()

    loss_criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=args.lr,
                                 weight_decay=args.weight_decay)

    dur = []
    for epoch in range(args.n_epochs):
        model.train()
        if epoch >= 3:
            t0 = time.time()

        stt = time.time()
        logits = model(features)
        loss = loss_criterion(logits[train_mask], labels[train_mask])
        end2 = time.time()

        optimizer.zero_grad()
        loss.backward()
        end3 = time.time()
        optimizer.step()
        print("bp", end2 - stt, end3 - end2)

        if epoch >= 3:
            dur.append(time.time() - t0)

        acc = evaluate(model, features, labels, val_mask)
        print(
            "Epoch {:05d} | Time(s) {:.4f} | Loss {:.4f} | Accuracy {:.4f} | "
            "ETputs(KTEPS) {:.2f}".format(epoch, np.mean(dur), loss.item(),
                                          acc,
                                          num_edges / np.mean(dur) / 1000))

    print()
    acc = evaluate(model, features, labels, test_mask)
    print("Test Accuracy {:.4f}".format(acc))
Beispiel #6
0
def main(args):
    data = load_data(args.dataset)
    # data = load_data(args)

    features = torch.FloatTensor(data.features)
    labels = torch.LongTensor(data.labels)
    train_mask = torch.ByteTensor(data.train_mask)
    val_mask = torch.ByteTensor(data.val_mask)
    test_mask = torch.ByteTensor(data.test_mask)

    num_classes = data.num_labels
    num_edges = data.graph.number_of_edges()
    feature_dim = features.shape[1]
    num_edge_type = 2

    edge_types = Variable(
        (torch.rand([num_edges]).squeeze() >
         0.5).long())  # random assign edge type for all edges

    if args.gpu < 0:
        cuda = False
    else:
        cuda = True
        torch.cuda.set_device(args.gpu)
        features = features.cuda()
        edge_types = edge_types.cuda()
        labels = labels.cuda()
        train_mask = train_mask.cuda()
        val_mask = val_mask.cuda()
        test_mask = test_mask.cuda()

    g = DGLGraph(data.graph)
    g.edata['e'] = edge_types

    model = GGNN(g, num_classes, num_edge_type, feature_dim, feature_dim)

    if cuda:
        model = model.cuda()

    loss_criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(model.parameters(),
                                 lr=args.lr,
                                 weight_decay=args.weight_decay)

    dur = []
    for epoch in range(args.n_epochs):
        model.train()
        if epoch >= 3:
            t0 = time.time()

        logits = model(features)
        loss = loss_criterion(logits[train_mask], labels[train_mask])

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        if epoch >= 3:
            dur.append(time.time() - t0)

        acc = evaluate(model, features, labels, val_mask)
        print(
            "Epoch {:05d} | Time(s) {:.4f} | Loss {:.4f} | Accuracy {:.4f} | "
            "ETputs(KTEPS) {:.2f}".format(epoch, np.mean(dur), loss.item(),
                                          acc,
                                          num_edges / np.mean(dur) / 1000))

    print()
    acc = evaluate(model, features, labels, test_mask)
    print("Test Accuracy {:.4f}".format(acc))
Beispiel #7
0
def main(opt):
    train_dataset = bAbIDataset(opt.dataroot, opt.question_id, True)
    train_dataloader = bAbIDataloader(train_dataset,
                                      batch_size=opt.batch_size,
                                      shuffle=True,
                                      num_workers=2)

    test_dataset = bAbIDataset(opt.dataroot, opt.question_id, False)
    test_dataloader = bAbIDataloader(test_dataset,
                                     batch_size=opt.batch_size,
                                     shuffle=False,
                                     num_workers=2)

    opt.edge_type_num = train_dataset.edge_type_num
    opt.node_num = train_dataset.node_num

    net = GGNN(opt)
    net.double()

    criterion = nn.CrossEntropyLoss()

    if opt.cuda:
        net.cuda()
        criterion.cuda()

    optimizer = optim.Adam(net.parameters(), lr=opt.lr)

    for epoch in range(0, opt.niter):
        net._train(epoch, train_dataloader, net, criterion, optimizer, opt)
        net._test(test_dataloader, net, criterion, optimizer, opt)