Пример #1
0
def train(adj, input_feats, target_labels, config, weights=None):
    num_vertices = adj.shape[0]
    label_kind = np.max(target_labels) + 1
    feat_dim = input_feats.shape[-1]
    layer_config = (feat_dim, config['hidden_dim'], label_kind)

    model, loss = GCN("GCN", adj, weights, layer_config)
    # model, loss = MLP("MLP", weights, layer_config)

    # Construct masks for training and testing
    train_size = int(num_vertices * config['train_portion'])
    train_mask = np.zeros(target_labels.shape, dtype=bool)
    train_mask[:train_size] = True
    np.random.shuffle(train_mask)
    test_mask = ~train_mask

    for epoch in range(config['max_epoch']):
        LOG_INFO('Training @ %d epoch...' % (epoch))
        train_net(model, loss, config, input_feats,
                  target_labels, train_mask, label_kind)

        if (epoch + 1) % config['test_epoch'] == 0:
            LOG_INFO('Testing @ %d epoch...' % (epoch))
            test_net(model, loss, input_feats,
                     target_labels, test_mask, label_kind)

        if (epoch + 1) % 50 == 0:
            config['learning_rate'] *= 0.5
Пример #2
0
def main():
    test_loss_list = []
    test_acc_list = []
    train_loss_list = []
    train_acc_list = []

    args = get_parser()

    model, config, loss = get_model(args)

    
    starttime = datetime.datetime.now()    
    for epoch in range(args.max_epoch):
        LOG_INFO('Training @ %d epoch...' % (epoch))
        train_loss, train_acc = train_net(model, loss, config, train_data, train_label, config['batch_size'], config['disp_freq'])
        train_loss_list.extend(train_loss)
        train_acc_list.extend(train_acc)
        if epoch > 5:
            config['learning_rate'] = config['learning_rate'] * args.learning_rate_decay
        if epoch % config['test_epoch'] == 0:
            LOG_INFO('Testing @ %d epoch...' % (epoch))
            test_loss, test_acc = test_net(model, loss, test_data, test_label, config['batch_size'])
            test_loss_list.append(test_loss)
            test_acc_list.append(test_acc)
    endtime = datetime.datetime.now()
    print ("total training time:",(endtime - starttime).seconds)

    save(args, train_loss_list, train_acc_list, test_loss_list, test_acc_list)
Пример #3
0
def train_and_save(model, loss, train_data, test_data, train_label,
                   test_label):
    best_test_loss = -1
    update_round_before = 0
    train_loss_list = []
    train_acc_list = []
    test_loss_list = []
    test_acc_list = []
    epoch_final = 0
    for epoch in range(config['max_epoch']):
        LOG_INFO('Training @ %d epoch...' % (epoch))
        train_loss_now, train_acc_now = train_net(model, loss, config,
                                                  train_data, train_label,
                                                  config['batch_size'],
                                                  config['disp_freq'])
        train_loss_list.append(train_loss_now)
        train_acc_list.append(train_acc_now)
        if epoch % config['test_epoch'] == 0:
            LOG_INFO('Testing @ %d epoch...' % (epoch))
            test_loss_now, test_acc_now = test_net(model, loss, test_data,
                                                   test_label,
                                                   config['batch_size'])
            test_loss_list.append(test_loss_now)
            test_acc_list.append(test_acc_now)
            if best_test_loss == -1:
                update_round_before = 0
                best_test_loss = test_loss_now
            elif test_loss_now <= best_test_loss:
                update_round_before = 0
                best_test_loss = test_loss_now
            else:
                update_round_before += 1
                if update_round_before >= 5:
                    epoch_final = epoch + 1
                    break
    save_dir = os.path.join('result', config['name'])
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    result_dict = {
        "train_loss": train_loss_list,
        "train_acc": train_acc_list,
        "test_loss": test_loss_list,
        "test_acc": test_acc_list
    }
    with open(os.path.join(save_dir, "result.json"), 'w') as f:
        json.dump(result_dict, f)
    x = range(epoch_final)
    plt.cla()
    plt.plot(x, train_loss_list, label="train loss")
    plt.plot(x, test_loss_list, label="test loss")
    plt.legend()
    plt.savefig(os.path.join(save_dir, "loss.png"))
    plt.cla()
    plt.plot(x, train_acc_list, label="train acc")
    plt.plot(x, test_acc_list, label="test acc")
    plt.legend()
    plt.savefig(os.path.join(save_dir, "acc.png"))
Пример #4
0
def check(adj, input_feats, target_labels, config, weights=None):
    num_vertices = adj.shape[0]
    label_kind = np.max(target_labels) + 1
    feat_dim = input_feats.shape[-1]
    layer_config = (feat_dim, config['hidden_dim'], label_kind)
    print(layer_config)

    weights = load_weights('output_0', layer_config)

    model, loss = GCN_check("GCN_check", adj, weights, layer_config)
    # model, loss = MLP("MLP", weights, layer_config)

    # Construct masks for training and testing
    train_size = int(num_vertices * config['train_portion'])
    train_mask = np.zeros(target_labels.shape, dtype=bool)
    train_mask[:train_size] = True
    np.random.shuffle(train_mask)

    for epoch in range(config['check_epoch']):
        LOG_INFO('Training @ %d epoch...' % (epoch))
        train_net(model, loss, config, input_feats, target_labels, train_mask,
                  label_kind)
Пример #5
0
def run(net_func, save_loss_path, save_acc_path, result_dir="result/"):
    model, config = net_func()
    loss_, acc_ = [], []

    for epoch in range(config['max_epoch']):
        LOG_INFO('Training @ %d epoch...' % (epoch))
        a, b = train_net(model, loss, config, train_data, train_label,
                         config['batch_size'], config['disp_freq'])
        loss_ += a
        acc_ += b
        if epoch % config['test_epoch'] == 0:
            LOG_INFO('Testing @ %d epoch...' % (epoch))
            test_net(model, loss, test_data, test_label, config['batch_size'])
    test_net(model, loss, test_data, test_label, config['batch_size'])

    if not os.path.exists(result_dir):
        os.mkdir(result_dir)
    np.save(result_dir + save_loss_path, loss_)
    np.save(result_dir + save_acc_path, acc_)
def train(model, loss, train_data, test_data, train_label, test_label):
    best_test_loss = -1
    update_round_before = 0
    for epoch in range(config['max_epoch']):
        LOG_INFO('Training @ %d epoch...' % (epoch))
        train_loss_now, train_acc_now = train_net(model, loss, config,
                                                  train_data, train_label,
                                                  config['batch_size'],
                                                  config['disp_freq'])
        if epoch % config['test_epoch'] == 0:
            LOG_INFO('Testing @ %d epoch...' % (epoch))
            test_loss_now, test_acc_now = test_net(model, loss, test_data,
                                                   test_label,
                                                   config['batch_size'])
            if best_test_loss == -1:
                update_round_before = 0
                best_test_loss = test_loss_now
            elif test_loss_now <= best_test_loss:
                update_round_before = 0
                best_test_loss = test_loss_now
            else:
                update_round_before += 1
                if update_round_before >= 5:
                    break
Пример #7
0
# You should adjust these hyperparameters
# NOTE: one iteration means model forward-backwards one batch of samples.
#       one epoch means model has gone through all the training samples.
#       'disp_freq' denotes number of iterations in one epoch to display information.

config = {
    'learning_rate': 0.01,
    'weight_decay': 0,
    'momentum': 0.7,
    'batch_size': 100,
    'max_epoch': 300,
    'disp_freq': 5,
    'test_epoch': 2
}
lo_list=[]
ac_list=[]
categories=[1,2,3,4]
for epoch in range(config['max_epoch']):
    LOG_INFO('Training @ %d epoch...' % (epoch))
    train_net(model, loss, config, train_data, train_label, config['batch_size'], config['disp_freq'])

    if epoch % config['test_epoch'] == 0:
        LOG_INFO('Testing @ %d epoch...' % (epoch))
        lo,ac=test_net(model, loss, test_data, test_label, config['batch_size'])
        lo_list.append(lo)
        ac_list.append(ac)


show4category(model, test_data, test_label, categories)
show(lo_list,ac_list,config['max_epoch'],config['test_epoch'])
Пример #8
0
    'weight_decay': 0.0005,
    'momentum': 0.9,
    'batch_size': 500,
    'max_epoch': 50,
    'disp_freq': 50,
    'test_epoch': 5
}

start_time = time.time()
train_loss_list, train_acc_list = [], []
test_loss_list, test_acc_list = [], []
for epoch in range(config['max_epoch']):
    LOG_INFO('Training @ %d epoch...' % (epoch))

    tr_l, tr_ac, te_l, te_ac = train_net(model, loss, config, train_data,
                                         train_label, config['batch_size'],
                                         config['disp_freq'], test_data,
                                         test_label)
    train_loss_list += tr_l
    train_acc_list += tr_ac
    test_loss_list += te_l
    test_acc_list += te_ac

    if epoch % config['test_epoch'] == 0:
        print('\n')
        LOG_INFO('Testing @ %d epoch...' % (epoch))
        t_l, t_ac = test_net(model, loss, test_data, test_label,
                             config['batch_size'])
        print("loss:{}, acc:{}".format(str(t_l), str(t_ac)))
        # print("loss:{}, acc:{}".format(str(test_loss_list[-1]),str(test_acc_list[-1])))
        print('\n')
print("\n")
Пример #9
0
    'batch_size': 100,
    'max_epoch': 100,
    'disp_freq': 50,
    'test_epoch': 1
}

start_time = datetime.now()
writer = SummaryWriter(comment=f'-{args.layers}_{args.loss}_{args.activation}')

# step for tensorboard
train_step, test_step = 0, 0

for epoch in range(config['max_epoch']):
    LOG_INFO('Training @ %d epoch...' % (epoch))
    losses, accuracies = train_net(model, loss, config, train_data,
                                   train_label, config['batch_size'],
                                   config['disp_freq'])
    for loss_val, accuracy in zip(losses, accuracies):
        writer.add_scalar("Loss/train", loss_val, train_step)
        writer.add_scalar("Accuracy/train", accuracy, train_step)
        train_step += 1

    if epoch % config['test_epoch'] == 0:
        LOG_INFO('Testing @ %d epoch...' % (epoch))
        loss_val, accuracy = test_net(model, loss, test_data, test_label,
                                      config['batch_size'])
        writer.add_scalar("Loss/test", loss_val, test_step)
        writer.add_scalar("Accuracy/test", accuracy, test_step)
        test_step += 1

end_time = datetime.now()
Пример #10
0
    'learning_rate': 0.01,
    'weight_decay': 0.001,
    'momentum': 0.5,
    'batch_size': 100,
    'max_epoch': 30,
    'disp_freq': 50,
}

draw_graph = True
loss_vals_train = []
loss_vals_test = []

for epoch in range(config['max_epoch']):
    LOG_INFO('Training @ %d epoch...' % (epoch))
    loss_vals = train_net(model, loss, config, train_data, train_label,
                          config['batch_size'], config['disp_freq'],
                          draw_graph)
    loss_vals_train = list(np.concatenate((loss_vals_train, loss_vals)))

    draw_graph = False

    LOG_INFO('Testing @ %d epoch...' % (epoch))
    loss_mean, acc_mean = test_net(model, loss, test_data, test_label,
                                   config['batch_size'])
    loss_vals_test.append(loss_mean)

# Plot graphs

plt.yticks(np.arange(0, 1, 0.1))  # Must change accordingly

fig = plt.figure()
Пример #11
0
    print('acc = ' + str(acc_value))


record_inputs, record_labels = data_iterator(train_data, train_label, config['batch_size']).next()
indices = []
labels_set = set()
for i in range(len(record_labels)):
    if record_labels[i] not in labels_set and len(indices) < img_record_num:
        labels_set.add(record_labels[i])
        indices.append(i)
    elif len(indices) == img_record_num:
        break


for epoch in range(config['max_epoch']):
    current_iter_count, loss_values = train_net(model, loss, config, train_data, train_label, config['batch_size'], config['disp_freq'], current_iter_count)
    log_list = log_list + loss_values
    # debug
    # if epoch % 20 == 0:
    #     print(str(loss_values[-1]))

    if epoch == config['max_epoch'] / 100 - 1 or epoch == config['max_epoch'] / 10 - 1 or epoch == config['max_epoch'] - 1:
        # record_inputs, record_labels = data_iterator(train_data, train_label, config['batch_size']).next()
        output = record_inputs
        for i in range(5):
            output = model.layer_list[i].forward(output)
            if i == 1:
                batch_conv1_output = output
            elif i == 4:
                batch_conv2_output = output
        # debug
Пример #12
0
    'momentum': 0.9,
    'batch_size': 100,
    'max_epoch': 10,
    'disp_freq': 5,
    'test_epoch': 50
}

loss_plot_list = []
acc_plot_list = []
iter_plot_list = []
acc_test_plot_list = []

for epoch in range(config['max_epoch']):
    LOG_INFO('Training @ %d epoch...' % (epoch))
    train_net(model, loss, config, train_data, train_label,
              config['batch_size'], config['disp_freq'], loss_plot_list,
              acc_plot_list, iter_plot_list)

    if epoch % config['test_epoch'] == 0:
        LOG_INFO('Testing @ %d epoch...' % (epoch))
        test_net(model, loss, test_data, test_label, config['batch_size'],
                 acc_test_plot_list)

with open("acc_" + filename + ".txt", 'w') as json_file:
    json_file.write(json.dumps(acc_test_plot_list, indent=4))
with open("loss_" + filename + ".txt", 'w') as json_file:
    json_file.write(json.dumps(loss_plot_list, indent=4))

#
# for i in range(10):
#     for input, label in data_iterator(train_data, train_label, 10000):
Пример #13
0
def start(settings):
    start_time = time.time()
    model = settings['model']
    loss = settings['loss']
    config = settings['config']
    stop_time = config['stop_time']
    if stop_time > 0:
        valid_data = train_data[50000:]
        valid_label = train_label[50000:]
        new_train_data = train_data[:50000]
        new_train_label = train_label[:50000]
    highest_acc = 0
    times = 0
    best_model = None
    acc_list = list()
    loss_list = list()
    epoch_list = list()
    time_list = list()

    # test before training
    if stop_time <= 0:
        acc, m_loss = test_net(model, loss, test_data, test_label,
                               config['batch_size'])
        acc_list.append(acc)
        loss_list.append(m_loss)
        epoch_list.append(0)
        time_list.append(time.time() - start_time)
    for epoch in range(config['max_epoch']):
        LOG_INFO('Training @ %d epoch...' % epoch)
        if stop_time > 0:
            train_net(model, loss, config, new_train_data, new_train_label,
                      config['batch_size'], config['disp_freq'])
        else:
            train_net(model, loss, config, train_data, train_label,
                      config['batch_size'], config['disp_freq'])

        if (epoch + 1) % config['test_epoch'] == 0 or epoch < 10:
            LOG_INFO('Testing @ %d epoch...' % epoch)
            if stop_time > 0:
                acc, m_loss = test_net(model, loss, valid_data, valid_label,
                                       config['batch_size'])
            else:
                acc, m_loss = test_net(model, loss, test_data, test_label,
                                       config['batch_size'])
            acc_list.append(acc)
            loss_list.append(m_loss)
            epoch_list.append(epoch + 1)
            time_list.append(time.time() - start_time)
            if stop_time > 0:
                if highest_acc <= acc:
                    highest_acc = acc
                    times = 0
                    best_model = copy_model(model)
                else:
                    times += 1
                    if times >= config['stop_time']:
                        break

    if stop_time > 0:
        model = best_model
    final_acc, final_loss = test_net(model, loss, test_data, test_label,
                                     config['batch_size'])
    end_time = time.time()
    LOG_INFO("Final acc %.4f" % final_acc)
    LOG_INFO("Time used: %d s" % (end_time - start_time))
    x = np.array(epoch_list)
    ya = np.array(acc_list)
    yl = np.array(loss_list)
    t = np.array(time_list)
    return [final_acc, end_time - start_time, x, ya, yl, t]
Пример #14
0
    'learning_rate': 0.1,
    'weight_decay': 0.0,
    'momentum': 0.0,
    'batch_size': 100,
    'max_epoch': 100,
    'disp_freq': 100,
    'test_epoch': 5
}

reset_time()
record = []
cnt = 0
for epoch in range(config['max_epoch']):
    LOG_INFO('Training @ %d epoch...' % (epoch))
    epoch_record = train_net(model, loss, config, train_data, train_label,
                             config['batch_size'], config['disp_freq'],
                             test_data, test_label)
    min_loss = min(x[1] for x in epoch_record)
    if len(record) == 0 or min_loss < min(x[2] for x in record):
        cnt = 0
    else:
        cnt += 1
    record += [(epoch, x[0], x[1], x[2], x[3]) for x in epoch_record]
    if cnt == 3:
        break

fig = plt.figure(figsize=(15, 10))
ax = fig.add_subplot(111)
max_id = max(t[1] for t in record)
x = [t[0] * max_id + t[1] for t in record]
y = [t[2] for t in record]
Пример #15
0
loss = SoftmaxCrossEntropyLoss(name='loss')

# defining config message...
config = {
    'learning_rate': 0.0000650,
    'weight_decay': 0.8820,
    'momentum': 0.6709,
    'batch_size': 100,
    'max_epoch': 1,
    'disp_freq': 100
}

# training...
train_losses = []
train_acc = []
train_counter = []

for epoch in range(config['max_epoch']):
    LOG_INFO('Training @ %d epoch...' % (epoch))
    train_net(model, loss, config, train_data, train_label,
              config['batch_size'], config['disp_freq'], epoch, train_losses,
              train_acc, train_counter)
# ploting...
plot(0, train_losses, train_acc, train_counter)

# testing...
LOG_INFO('Testing...')
file = open('loss_info/loss.txt', 'a')
file.write('\n====================')
info = [1]
test_net(model, loss, test_data, test_label, 10000, info)