|
| 1 | +import os |
| 2 | +import sys |
| 3 | +root_path = os.path.abspath("./") |
| 4 | +if root_path not in sys.path: |
| 5 | + sys.path.append(root_path) |
| 6 | + |
| 7 | +import copy |
| 8 | +import numpy as np |
| 9 | +import tensorflow as tf |
| 10 | + |
| 11 | +from openml import datasets |
| 12 | + |
| 13 | +from _Dist.NeuralNetworks.g_DistNN.NN import DistAdvanced |
| 14 | + |
| 15 | +GPU_ID = None |
| 16 | +K_RANDOM = 9 |
| 17 | +IDS = [ |
| 18 | + 38, 46, 179, |
| 19 | + 184, 389, 554, |
| 20 | + 772, 917, 1049, |
| 21 | + 1111, 1120, 1128, |
| 22 | + 293, |
| 23 | +] |
| 24 | + |
| 25 | + |
| 26 | +def swap(arr, i1, i2): |
| 27 | + arr[..., i1], arr[..., i2] = arr[..., i2], arr[..., i1].copy() |
| 28 | + |
| 29 | + |
| 30 | +def download_data(): |
| 31 | + data_folder = "_Data" |
| 32 | + idx_folder = os.path.join(data_folder, "idx") |
| 33 | + if not os.path.isdir(data_folder): |
| 34 | + os.makedirs(data_folder) |
| 35 | + if not os.path.isdir(idx_folder): |
| 36 | + os.makedirs(idx_folder) |
| 37 | + for idx in IDS: |
| 38 | + print("Downloading {}".format(idx)) |
| 39 | + data_file = os.path.join(data_folder, "{}.txt".format(idx)) |
| 40 | + idx_file = os.path.join(idx_folder, "{}.npy".format(idx)) |
| 41 | + if os.path.isfile(data_file) and os.path.isfile(idx_file): |
| 42 | + continue |
| 43 | + dataset = datasets.get_dataset(idx) |
| 44 | + data, categorical_idx, names = dataset.get_data( |
| 45 | + return_categorical_indicator=True, |
| 46 | + return_attribute_names=True |
| 47 | + ) |
| 48 | + data = data.toarray() if not isinstance(data, np.ndarray) else data |
| 49 | + target_idx = names.index(dataset.default_target_attribute) |
| 50 | + numerical_idx = ~np.array(categorical_idx) |
| 51 | + swap(numerical_idx, target_idx, -1) |
| 52 | + swap(data, target_idx, -1) |
| 53 | + with open(data_file, "w") as file: |
| 54 | + file.write("\n".join([" ".join(map(lambda n: str(n), line)) for line in data])) |
| 55 | + np.save(idx_file, numerical_idx) |
| 56 | + |
| 57 | + |
| 58 | +def main(): |
| 59 | + base_params = { |
| 60 | + "data_info": {}, |
| 61 | + "model_param_settings": {}, |
| 62 | + # "model_param_settings": {"n_epoch": 1, "max_epoch": 1}, |
| 63 | + # "model_structure_settings": {"use_wide_network": False, "use_pruner": False} |
| 64 | + } |
| 65 | + config = tf.ConfigProto() |
| 66 | + config.gpu_options.allow_growth = True |
| 67 | + if GPU_ID is not None: |
| 68 | + os.environ["CUDA_VISIBLE_DEVICES"] = GPU_ID |
| 69 | + base_params["model_param_settings"]["sess_config"] = config |
| 70 | + for idx in IDS: |
| 71 | + numerical_idx = np.load("_Data/idx/{}.npy".format(idx)) |
| 72 | + local_params = copy.deepcopy(base_params) |
| 73 | + local_params["name"] = str(idx) |
| 74 | + local_params["data_info"]["numerical_idx"] = numerical_idx |
| 75 | + DistAdvanced(**local_params).empirical_search(cv_rate=0.1, test_rate=0.1).k_random( |
| 76 | + K_RANDOM, cv_rate=0.1, test_rate=0.1) |
| 77 | + # DistAdvanced(**local_params).k_random(K_RANDOM, cv_rate=0.1, test_rate=0.1) |
| 78 | + # DistAdvanced(**local_params).fit() |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == '__main__': |
| 82 | + download_data() |
| 83 | + main() |
0 commit comments