-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodelLoader.py
More file actions
176 lines (138 loc) · 5.84 KB
/
modelLoader.py
File metadata and controls
176 lines (138 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""
Author: Fergal Stapleton
"""
import sys
import os
import json
import random
import numpy as np
import tensorflow as tf
from src import linearGenProgTree
from problem import neuroevolution
# randomGenerationNeuroLGP can be used to test method is better than random,
from algorithm import modelAnalysis
from algorithm import genoandphenoLength
#from algorithm import randomGenerationNeuroLGP as singleObjNeuroLGP
from sklearn import datasets
from datetime import datetime
from numpy import genfromtxt
#from smt.surrogate_models import KRG, KPLS, KPLSK
#from smt.utils import XSpecs
import time
import timeit
import random
import sys
from scipy.stats import norm
from copy import deepcopy
import pandas as pd
#import psutil
import csv
import heapq
#from memory_profiler import profile
import configparser
import pickle
def read_json(file_path):
with open(file_path, "r") as f:
return json.load(f)
#@profile
def main():
multi_gpu = False
if multi_gpu == True:
#os.environ['CUDA_VISIBLE_DEVICES'] = str(gpu_id)
#physical_devices = tf.config.experimental.list_physical_devices('GPU')
#tf.config.experimental.set_memory_growth(physical_devices[gpu_id], True)
try:
gpu_id = str(int(sys.argv[1]) % 2)
except:
gpu_id = str(0)
os.environ['CUDA_VISIBLE_DEVICES'] = str(gpu_id)
print("CUDA_VISIBLE_DEVICES:", os.environ.get("CUDA_VISIBLE_DEVICES"))
physical_devices = tf.config.experimental.list_physical_devices('GPU')
print(physical_devices)
tf.config.experimental.set_visible_devices(physical_devices[int(gpu_id)], 'GPU')
tf.config.experimental.set_memory_growth(physical_devices[int(gpu_id)], True)
print(physical_devices)
physical_devices2 = tf.config.experimental.list_physical_devices('GPU')
print(physical_devices2)
with tf.device('/device:GPU:'+gpu_id):
if len(sys.argv) > 1:
run_num = int(sys.argv[1])
else:
run_num = 0
#os.environ['CUDA_VISIBLE_DEVICES'] = str(gpu_id)
run_idx = 0 # dumb reassignment
gpu_id = 0
print("Welcome to GenProgMO library")
#paramsLoad = read_json('experimentNeuroLGP.json')
#paramsLoad = read_json('experimentNeuroLGPcatsdogs.json')
#paramsLoad = read_json('experimentNeuroLGPmnistevenandodd.json')
#paramsLoad = read_json('experimentNeuroLGPbreakhis40.json')
#paramsLoad = read_json('experimentNeuroLGPbreakhis100.json')
paramsLoad = read_json('experimentNeuroLGPbreakhis200.json')
#paramsLoad = read_json('experimentNeuroLGPbreakhis400.json')
#paramsLoad = read_json('experimentNeuroLGPfashionmnistfootwear.json')
print(json.dumps(paramsLoad, indent=4))
# First and foremost 'what is our problem domain?''
problem_type = paramsLoad["PROBLEM DOMAIN"]["PROBLEM TYPE"]
experiment = paramsLoad["EXPERIMENTAL SETUP"]
surrogate_model_management = paramsLoad["SURROGATE"]
try:
problemCode = paramsLoad["PROBLEM DOMAIN"]["PROBLEM CODE"]
dataSource = paramsLoad["PROBLEM DOMAIN"]["DATA SOURCE"] # Not really needed, but will leave in for consistency or if needed in future
except:
print("Error: one of the problem domain properties has not been specified in experiment.json")
sys.exit(1)
try:
opt = paramsLoad["OPTIMIZATION"]
except:
print("Error: one of the optimization properties has not been specified in experiment.json")
sys.exit(1)
# This is an index that will be associated with these runs. Going forward I will cite the index in GitHub to help with version control
exp_idx = experiment["EXP ID"]
experiment_type = experiment["EXPERIMENT TYPE"]
results_path = experiment["RESULTS PATH"]
run_start = experiment["RUN START"]
run_end = experiment["RUN END"]
dataset = problemCode # TODO repition, unecessary assignment
run_range = range(int(run_start), int(run_end)) # 0,1,2,...9
#run = range(10, 20) # 10,11,12,...19
if experiment_type == "surrogate":
method = paramsLoad["SURROGATE"]["METHOD"]
else:
method = experiment_type
print("Config settings:")
print(exp_idx)
print(experiment_type)
print(method)
print(dataset)
# Set up class files
gp = linearGenProgTree.GP_tree(paramsLoad) # genetic representation, i.e genetic program
run = linearGenProgTree.Execute(paramsLoad) # execution
op = linearGenProgTree.GeneticOP(paramsLoad) #
probDom = neuroevolution.NeuroLGP(run, gp, gpu_id) # problem domain
alg = modelAnalysis.Standard(probDom, gp, run, op, experiment, opt, problemCode, gpu_id)
#alg = genoandphenoLength.Standard(probDom, gp, run, op, experiment, opt, problemCode, gpu_id)
alg.loader(dataset)
if __name__ == "__main__":
# Note: It's not possible to make code deterministic on cuDNN, However seeding has been implemented should one choose to run on cpu's
# source: https://stackoverflow.com/questions/39938307/determinism-in-tensorflow-gradient-updates
SEED =int(sys.argv[1])
os.environ['CUDA_VISIBLE_DEVICES'] = str(int(sys.argv[1]) % 2)
#tf.keras.backend.clear_session()
#os.environ['TF_GPU_ALLOCATOR'] = 'cuda_malloc_async'
#TF_GPU_ALLOCATOR=cuda_malloc_async#
def set_seeds(seed=SEED):
os.environ['PYTHONHASHSEED'] = str(seed)
random.seed(seed)
tf.random.set_seed(seed)
np.random.seed(seed)
def set_global_determinism(seed=SEED):
set_seeds(seed=seed)
# Below two lines don't work
#os.environ['TF_DETERMINISTIC_OPS'] = '1'
#os.environ['TF_CUDNN_DETERMINISTIC'] = '1'
# Turn on if using CPU and want deterministic operations
#tf.config.threading.set_inter_op_parallelism_threads(1)
#tf.config.threading.set_intra_op_parallelism_threads(1)
set_global_determinism(seed=SEED)
main()