Exemplo n.º 1
0
Arquivo: gb.py Projeto: HMMBRB/brb-1
def main():
    experiment_num, sub_num, rule_num, data_name = 10, 10, 32, "ecoli"
    data, target, att_dim, res_dim = dataset_numeric_classification(
        data_name, 1)
    Ot = 0.5 * (np.nanmax(data, 0) + np.nanmin(data, 0))
    Dt = 0.5 * (np.nanmax(data, 0) - np.nanmin(data, 0))
    data = ((data - Ot) / Dt).astype(np.float64)

    acc, cnt = tf.metrics.Accuracy(), 0
    for en in range(experiment_num):
        for train_data, train_target, test_data, test_target in kfold(
                data, target, 10, 'numeric', random_state=en):
            train_target_one_hot = tf.one_hot(train_target,
                                              res_dim,
                                              dtype=tf.float64)

            sub_models, preds = [], []
            model = training(rule_num, att_dim, res_dim, train_data,
                             train_target_one_hot, 0)
            sub_models.append(model), preds.append(model.output(train_data))
            cce = tf.keras.losses.categorical_crossentropy(
                train_target_one_hot, tf.nn.softmax(preds[0]))
            mean_cce, now_mask = tf.reduce_mean(cce).numpy(), goss(
                tf.argsort(tf.negative(cce)))
            tf.summary.scalar('cce', mean_cce, 0)
            now_train, now_target = train_data[now_mask], (
                train_target_one_hot -
                model.predict(train_data)).numpy()[now_mask]
            pre_mean = mean_cce
            for _ in range(sub_num - 1):
                model = training(rule_num, att_dim, res_dim, now_train,
                                 now_target, 1, 64, 500)
                preds.append(0.7 * model.output(train_data))
                now_pred = tf.nn.softmax(tf.reduce_sum(preds, 0))
                cce = tf.keras.losses.categorical_crossentropy(
                    train_target_one_hot, now_pred)
                mean_cce = tf.reduce_mean(cce).numpy()
                tf.summary.scalar('cce', mean_cce, _ + 1)
                if mean_cce > pre_mean:
                    break
                if mean_cce < 0.05:
                    break
                sub_models.append(model)
                pre_mean, now_mask = mean_cce, goss(
                    tf.argsort(tf.negative(cce)))
                now_train, now_target = train_data[now_mask], (
                    train_target_one_hot - now_pred).numpy()[now_mask]

            test_pred = tf.math.argmax(
                tf.nn.softmax(
                    tf.reduce_sum(
                        [(1.0 - (mi != 0) * 0.3) * model.output(test_data)
                         for mi, model in enumerate(sub_models)], 0)), -1)

            acc.update_state(test_target, test_pred)
            tf.summary.scalar('acc_%s' % data_name, acc.result().numpy(), cnt)
            cnt += 1
Exemplo n.º 2
0
Arquivo: bid.py Projeto: HMMBRB/brb-1
def main():
    experiment_num = 10
    data, target, att_dim, res_dim = dataset_numeric_classification('ecoli', 1)
    acc = tf.metrics.Accuracy()
    cnt = 0
    for en in range(experiment_num):
        for train_data, train_target, test_data, test_target in kfold(data, target, 10, 'numeric', random_state=en):
            O, D = 0.5*(np.min(train_data, 0) + np.max(train_data, 0)), 0.5 * np.ptp(train_data, 0)
            D = np.where(D == 0.0, 0.5, D)
            model = training(32, 4, 16, att_dim, 4, res_dim, (train_data - O) / D, np.eye(res_dim)[train_target])
            test_pred = tf.math.argmax(model((test_data - O) / D), -1)
            acc.update_state(test_target, test_pred)
            with sw.as_default():
                tf.summary.scalar('ecoli_16*4_32_64', acc.result().numpy(), cnt)
                cnt += 1
Exemplo n.º 3
0
def main():
    experiment_num = 20
    data, target, att_dim, res_dim = dataset_numeric_classification('ecoli', 1)
    # mass = pandas.read_csv('mass.csv').to_numpy()
    # data, target, att_dim, res_dim = mass[:, :5], mass[:, -1], 5, 2

    data = StandardScaler().fit_transform(data).astype(ntype)
    acc, cnt = tf.metrics.Accuracy(), 0
    for en in range(experiment_num):
        for train_data, train_target, test_data, test_target in kfold(
                data, target, 10, 'numeric', random_state=en):
            model = training(32, att_dim, res_dim, train_data,
                             tf.one_hot(train_target, res_dim))
            acc.update_state(test_target, model.evaluate(test_data))
            tf.summary.scalar('acc', acc.result().numpy(), cnt)
            cnt += 1
Exemplo n.º 4
0
def main():
    experiment_num, data_name = 20, "ecoli"
    rule_num, epoch, batch_size = 32, 4000, 128
    data, target, att_dim, res_dim = dataset_numeric_classification(
        data_name, 1)
    data = StandardScaler().fit_transform(data).astype(ndtype)
    sum_acc, sum_cnt = 0.0, 0
    for en in range(experiment_num):
        for train_data, train_target, test_data, test_target in kfold(
                data, target, 10, 'numeric', random_state=en):
            model = training(train_data, train_target, rule_num, att_dim,
                             res_dim, epoch, batch_size, en)
            _, acc = model.evaluate(test_data, test_target)
            sum_acc += acc
            sum_cnt += 1
            tf.summary.scalar('acc_each_%s' % data_name, acc, sum_cnt)
            tf.summary.scalar('acc_%s' % data_name, sum_acc / sum_cnt, sum_cnt)
Exemplo n.º 5
0
def main():
    experiment_num = 10
    data, target, att_dim, res_dim = dataset_numeric_classification('yeast', 1)
    acc, cnt = tf.metrics.Accuracy(), 0
    for en in range(experiment_num):
        for train_data, train_target, test_data, test_target in kfold(data, target, 10, 'numeric', random_state=en):
            Ot = 0.5 * (np.nanmax(train_data, 0) + np.nanmin(train_data, 0))
            Dt = 0.5 * (np.nanmax(train_data, 0) - np.nanmin(train_data, 0))
            Dt = np.where(Dt == 0.0, 0.5, Dt)
            train_data, test_data = (train_data - Ot) / Dt, (test_data - Ot) / Dt

            sub_models, ros = [], RandomOverSampler(random_state=en)
            for rd in range(res_dim):
                x, y = ros.fit_resample(train_data, train_target == rd)
                sub_models.append(training(16, att_dim, 2, x, np.eye(2)[y.astype(int)]))
            modelz = trainingz(32, att_dim, res_dim, res_dim, sub_models, train_data, np.eye(res_dim)[train_target])

            test_pred = evaluating(modelz, sub_models, test_data)
            acc.update_state(test_pred, test_target)
            tf.summary.scalar('acc', acc.result().numpy(), cnt)
            cnt += 1
Exemplo n.º 6
0
def main():
    experiment_num, rule_num, data_name = 50, 32, 'thyroid-new'
    data, target, att_dim, res_dim = dataset_numeric_classification(
        data_name, 1)
    data = StandardScaler().fit_transform(data).astype(np.float32)
    acc, cnt = tf.metrics.Accuracy(), 0
    missing_rate = 0.5
    for en in range(experiment_num):
        for train_data, train_target, test_data, test_target in kfold(
                data, target, 5, 'numeric', en):
            model = training(rule_num,
                             att_dim,
                             res_dim,
                             train_data,
                             tf.one_hot(train_target, res_dim),
                             missing_rate=missing_rate)
            acc.update_state(
                test_target,
                model.evaluate(
                    random_replace_with_nan(test_data,
                                            missing_rate).astype(np.float32)))
            tf.summary.scalar('acc_%s' % data_name, acc.result().numpy(), cnt)
            cnt += 1
Exemplo n.º 7
0
def main():
    data_name = [
        'iris', 'wine', 'diabetes', 'ecoli', 'glass', 'seeds', 'yeast',
        'thyroid-new', 'blood-transfusion-service-center'
    ]
    res = []
    missing_rate = 0.0
    for dn in data_name:
        data, target, att_dim, res_dim = dataset_numeric_classification(dn, 1)
        experiment_num, acc = 50, 0
        for en in range(experiment_num):
            p = Pool()
            r = [
                p.apply_async(run, args=(datai, missing_rate))
                for datai in kfold(data, target, 5, 'numeric', en)
            ]
            p.close(), p.join()
            acc += np.mean([_.get() for _ in r]) / experiment_num
            print('now_round_%d' % en, end='\r')
        with open('res', 'a') as f:
            f.writelines(dn + str(acc) + '\n')
        res.append((dn, acc))
    print(res)