Esempio n. 1
0
def train(args):
    if args.verbosity == 0:
        logging.basicConfig(level=logging.WARNING)
    elif args.verbosity == 1:
        logging.basicConfig(level=logging.INFO)
    else:
        logging.basicConfig(level=logging.DEBUG)

    assert args.role in ['leader', 'follower', 'local'], \
        "role must be leader, follower, or local"
    assert args.mode in ['train', 'test', 'eval'], \
        "mode must be train, test, or eval"

    if args.data_path.endswith('.csv'):
        with open(args.data_path, 'rb') as fin:
            data = np.loadtxt(fin, delimiter=',')
        if args.mode == 'train' or args.mode == 'test':
            if args.role == 'leader' or args.role == 'local':
                X = data[:, :-1]
                y = data[:, -1]
            else:
                X = data
                y = None
        else:  # eval
            X = data
            y = None
    else:
        raise ValueError("Unsupported data type %s" % args.data_path)

    if args.role != 'local':
        bridge = Bridge(args.role, int(args.local_addr.split(':')[1]),
                        args.peer_addr, args.application_id, 0)
    else:
        bridge = None

    booster = BoostingTreeEnsamble(bridge,
                                   learning_rate=args.learning_rate,
                                   max_iters=args.max_iters,
                                   max_depth=args.max_depth,
                                   l2_regularization=args.l2_regularization,
                                   max_bins=args.max_bins,
                                   num_parallel=args.num_parallel)

    if args.load_model_path:
        booster.load_saved_model(args.load_model_path)

    if args.mode == 'train':
        booster.fit(X, y, args.checkpoint_path)
    elif args.mode == 'test':
        pred = booster.batch_predict(X)
        acc = sum((pred > 0.5) == y) / len(y)
        logging.info("Test accuracy: %f", acc)
    else:
        pred = booster.batch_predict(X)
        for i in pred:
            print(i)

    if args.export_path:
        booster.save_model(args.export_path)
Esempio n. 2
0
 def local_test_boosting_tree_helper(self, X, y):
     booster = BoostingTreeEnsamble(None,
                                    max_iters=5,
                                    max_depth=3,
                                    num_parallel=2)
     booster.fit(X, y)
     pred = booster.batch_predict(X)
     return pred
Esempio n. 3
0
 def follower_test_boosting_tree_helper(self, X, cat_X):
     bridge = fl.trainer.bridge.Bridge('follower',
                                       50052,
                                       'localhost:50051',
                                       streaming_mode=False)
     booster = BoostingTreeEnsamble(bridge, max_iters=3, max_depth=2)
     booster.fit(X, None, cat_features=cat_X)
     pred = booster.batch_predict(X, cat_features=cat_X)
     bridge.terminate()
Esempio n. 4
0
 def local_test_boosting_tree_helper(self, X, y, cat_X):
     booster = BoostingTreeEnsamble(None,
                                    max_iters=3,
                                    max_depth=2,
                                    num_parallel=2)
     train_pred = booster.fit(X, y, cat_features=cat_X)
     pred = booster.batch_predict(X, cat_features=cat_X)
     np.testing.assert_almost_equal(train_pred, pred)
     return pred
Esempio n. 5
0
 def follower_test_boosting_tree_helper(self, X, cat_X):
     bridge = fl.trainer.bridge.Bridge('follower',
                                       50052,
                                       'localhost:50051',
                                       streaming_mode=True)
     booster = BoostingTreeEnsamble(bridge, max_iters=3, max_depth=2)
     booster.fit(X, None, cat_features=cat_X)
     pred = booster.batch_predict(X, cat_features=cat_X, get_raw_score=True)
     bridge.terminate()
     np.testing.assert_almost_equal(pred, 0)
Esempio n. 6
0
 def leader_test_boosting_tree_helper(self, X, y):
     bridge = fl.trainer.bridge.Bridge('leader',
                                       50051,
                                       'localhost:50052',
                                       streaming_mode=False)
     booster = BoostingTreeEnsamble(bridge, max_iters=5, max_depth=3)
     booster.fit(X, y)
     pred = booster.batch_predict(X)
     bridge.terminate()
     return pred
Esempio n. 7
0
 def leader_test_boosting_tree_helper(self, X, y, cat_X):
     bridge = fl.trainer.bridge.Bridge('leader',
                                       50051,
                                       'localhost:50052',
                                       streaming_mode=False)
     booster = BoostingTreeEnsamble(bridge, max_iters=3, max_depth=2)
     train_pred = booster.fit(X, y, cat_features=cat_X)
     pred = booster.batch_predict(X, cat_features=cat_X)
     np.testing.assert_almost_equal(train_pred, pred)
     bridge.terminate()
     return pred
Esempio n. 8
0
 def test_boosting_tree_local(self):
     data = load_iris()
     X = data.data
     mask = np.random.choice(a=[False, True], size=X.shape, p=[0.5, 0.5])
     X[mask] = float('nan')
     y = np.minimum(data.target, 1)
     booster = BoostingTreeEnsamble(None,
                                    max_iters=5,
                                    max_depth=3,
                                    num_parallel=2)
     booster.fit(X, y)
     pred = booster.batch_predict(X)
     self.assertGreater(sum((pred > 0.5) == y) / len(y), 0.94)