def auto_encoder(): if not osp.exists(osp.join(get_model_save_base(), 'feat_dim_120.pkl')): print("Cannot Find {}".format( osp.join(get_model_save_base(), 'feat_dim_120.pkl'))) x_train, x_test, y_train, y_test = train_test_split(feat_all, feat_all, test_size=0.2, random_state=42) encoding_dim = 120 input_ = Input(shape=(8051, )) encoded = Dense(encoding_dim, activation='relu')(input_) decoded = Dense(8051, activation='sigmoid')(encoded) autoencoder = Model(input=input_, output=decoded) autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy') autoencoder.fit(x_train, x_train, nb_epoch=500, batch_size=10, shuffle=True, validation_data=(x_test, x_test)) # 根据上面我们训练的自编码器,截取其中编码部分定义为编码模型 encoder = Model(input=input_, output=encoded) # 对特征进行编码降维 feat_dim_120 = encoder.predict(feat_all) with open(osp.join(get_model_save_base(), 'feat_dim_120.pkl'), 'wb') as f: pickle.dump(feat_dim_120, f) else: print("Find {}".format( osp.join(get_model_save_base(), 'feat_dim_120.pkl'))) with open(osp.join(get_model_save_base(), 'feat_dim_120.pkl'), 'rb') as f: feat_dim_120 = pickle.load(f) return feat_dim_120
concatlayer = ConcatLayer() est_configs = [ ExtraRandomForestConfig(), ExtraRandomForestConfig(), ExtraRandomForestConfig(), ExtraRandomForestConfig(), RandomForestConfig(), RandomForestConfig(), RandomForestConfig(), RandomForestConfig() ] data_save_dir = osp.join(get_data_save_base(), 'mnist') model_save_dir = osp.join(get_model_save_base(), 'mnist') auto_cascade = AutoGrowingCascadeLayer(est_configs=est_configs, early_stopping_rounds=4, stop_by_test=True, n_classes=10, data_save_dir=data_save_dir, model_save_dir=model_save_dir) model = Graph() model.add(mgs) model.add(pool) model.add(concatlayer) model.add(auto_cascade) model.fit_transform(x_train, y_train, x_test, y_test)
print('x_train shape: {}'.format(x_train.shape)) print('x_test.shape: {}'.format(x_test.shape)) est_configs = [ ExtraRandomForestConfig(), ExtraRandomForestConfig(), ExtraRandomForestConfig(), ExtraRandomForestConfig(), RandomForestConfig(), RandomForestConfig(), RandomForestConfig(), RandomForestConfig() ] data_save_dir = osp.join(get_data_save_base(), 'uci_yeast') model_save_dir = osp.join(get_model_save_base(), 'uci_yeast') auto_cascade = AutoGrowingCascadeLayer(est_configs=est_configs, early_stopping_rounds=4, n_classes=10, data_save_dir=data_save_dir, model_save_dir=model_save_dir, distribute=False, seed=0) model = Graph() model.add(auto_cascade) model.fit_transform(x_train, y_train, x_test, y_test) print("time cost: {}".format(time.time() - start_time))
concatlayer = ConcatLayer() est_configs = [ ExtraRandomForestConfig(), ExtraRandomForestConfig(), ExtraRandomForestConfig(), ExtraRandomForestConfig(), RandomForestConfig(), RandomForestConfig(), RandomForestConfig(), RandomForestConfig() ] data_save_dir = osp.join(get_data_save_base(), 'fashion_mnist') model_save_dir = osp.join(get_model_save_base(), 'fashion_mnist') auto_cascade = AutoGrowingCascadeLayer(est_configs=est_configs, early_stopping_rounds=4, stop_by_test=True, n_classes=10, data_save_dir=data_save_dir, model_save_dir=model_save_dir) model = Graph() model.add(mgs) model.add(pool) model.add(concatlayer) model.add(auto_cascade) model.fit_transform(x_train, y_train, x_test, y_test)
start_time = time.time() est_configs = [ ExtraRandomForestConfig(), ExtraRandomForestConfig(), ExtraRandomForestConfig(), ExtraRandomForestConfig(), RandomForestConfig(), RandomForestConfig(), RandomForestConfig(), RandomForestConfig() ] data_save_dir = osp.join(get_data_save_base(), 'uci_iris') model_save_dir = osp.join(get_model_save_base(), 'uci_iris') auto_cascade = AutoGrowingCascadeLayer(est_configs=est_configs, early_stopping_rounds=4, n_classes=3, stop_by_test=False, data_save_dir=data_save_dir, model_save_dir=model_save_dir, distribute=False, dis_level=0, verbose_dis=False, seed=0) model = Graph() model.add(auto_cascade) model.fit_transform(x_train, y_train, x_test, y_test)
rf1 = ExtraRandomForestConfig(n_folds=3, n_jobs=-1, min_samples_leaf=10, max_features='auto') rf2 = RandomForestConfig(n_folds=3, n_jobs=-1, min_samples_leaf=10) windows = [ Window(win_x=8, win_y=8, stride_x=2, stride_y=2, pad_x=0, pad_y=0), Window(11, 11, 2, 2), Window(16, 16, 2, 2) ] est_for_windows = [[rf1, rf2], [rf1, rf2], [rf1, rf2]] data_save_dir = osp.join(get_data_save_base(), 'cifar10') model_save_dir = osp.join(get_model_save_base(), 'cifar10') mgs = MultiGrainScanLayer(windows=windows, est_for_windows=est_for_windows, n_class=10, distribute=False, dis_level=0, keep_in_mem=False, data_save_dir=data_save_dir, cache_in_disk=False, seed=0) pools = [[MeanPooling(2, 2), MeanPooling(2, 2)], [MeanPooling(2, 2), MeanPooling(2, 2)], [MeanPooling(2, 2), MeanPooling(2, 2)]]
print('x_train.shape', x_train.shape, x_train.dtype) print('x_test.shape', x_test.shape, x_test.dtype) est_configs = [ ExtraRandomForestConfig(), ExtraRandomForestConfig(), ExtraRandomForestConfig(), ExtraRandomForestConfig(), RandomForestConfig(), RandomForestConfig(), RandomForestConfig(), RandomForestConfig() ] data_save_dir = osp.join(get_data_save_base(), 'imdb') model_save_dir = osp.join(get_model_save_base(), 'imdb') cascade = AutoGrowingCascadeLayer(est_configs=est_configs, early_stopping_rounds=4, stop_by_test=True, n_classes=2, data_save_dir=data_save_dir, model_save_dir=model_save_dir, distribute=True, dis_level=0, seed=0) cascade.fit_transform(x_train, y_train, x_test, y_test) print("Time cost: {}".format(time.time() - start_time))
print('train shape and plus shape', x_train.shape, x_train_plus.shape) print('test shape and plus shape', x_test.shape, x_test_plus.shape) rf1 = ExtraRandomForestConfig(n_folds=3, n_jobs=-1, min_samples_leaf=10, max_features='auto') rf2 = RandomForestConfig(n_folds=3, n_jobs=-1, min_samples_leaf=10) windows = [Window(win_x=24, win_y=24, stride_x=2, stride_y=2, pad_x=0, pad_y=0), Window(34, 34, 2, 2), Window(48, 48, 2, 2)] est_for_windows = [[rf1, rf2], [rf1, rf2], [rf1, rf2]] data_save_dir = osp.join(get_data_save_base(), 'small_norb') model_save_dir = osp.join(get_model_save_base(), 'small_norb') mgs = MultiGrainScanLayer(windows=windows, est_for_windows=est_for_windows, n_class=10, distribute=False, keep_in_mem=False, data_save_dir=data_save_dir, cache_in_disk=True, seed=0) model = Graph() model.add(mgs) # model.add(pool) # model.add(concatlayer) # model.add(auto_cascade)