diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index df6506e28a7767cd17b4c6e4ea15ebb3c4416d85..9d829da36400d8e3f5b33efa0197f101747a34b3 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,14 +1,17 @@ -build-job: - stage: build - script: - - python3 -m venv venv - - . venv/bin/activate - - pip3 install wheel - - pip3 install -r requirements.txt --no-cache-dir +# build-job: +# stage: build +# script: +# - python3 -m venv venv +# - . venv/bin/activate +# - pip3 install wheel +# - pip3 install -r requirements.txt --no-cache-dir test-job: stage: test script: + - python3 -m venv venv + - . venv/bin/activate + - pip3 install -r requirements.txt --no-cache-dir - pytest # deploy-job: diff --git a/MetaAugment/UCB1_JC_py.py b/MetaAugment/UCB1_JC_py.py index 8ba8c93d7cfd6fb5fad91499e5a46faad7c1d91a..27322463dc6b679751aac4b2483f0e12f4352cdb 100644 --- a/MetaAugment/UCB1_JC_py.py +++ b/MetaAugment/UCB1_JC_py.py @@ -102,7 +102,7 @@ def sample_sub_policy(policies, policy, num_sub_policies): """Sample policy, open and apply above transformations""" -def run_UCB1(policies, batch_size, learning_rate, ds, toy_size, max_epochs, early_stop_num, iterations, IsLeNet, ds_name=None): +def run_UCB1(policies, batch_size, learning_rate, ds, toy_size, max_epochs, early_stop_num, early_stop_flag, average_validation, iterations, IsLeNet, ds_name=None): # get number of policies and sub-policies num_policies = len(policies) @@ -172,12 +172,13 @@ def run_UCB1(policies, batch_size, learning_rate, ds, toy_size, max_epochs, earl train_loader, test_loader = create_toy(train_dataset, test_dataset, batch_size, toy_size) # create model + device = 'cuda' if torch.cuda.is_available() else 'cpu' if IsLeNet == "LeNet": - model = LeNet(img_height, img_width, num_labels, img_channels) + model = LeNet(img_height, img_width, num_labels, img_channels).to(device) # added .to(device) elif IsLeNet == "EasyNet": - model = EasyNet(img_height, img_width, num_labels, img_channels) + model = EasyNet(img_height, img_width, num_labels, img_channels).to(device) # added .to(device) elif IsLeNet == 'SimpleNet': - model = SimpleNet(img_height, img_width, num_labels, img_channels) + model = SimpleNet(img_height, img_width, num_labels, img_channels).to(device) # added .to(device) else: model = pickle.load(open(f'datasets/childnetwork', "rb")) @@ -185,8 +186,8 @@ def run_UCB1(policies, batch_size, learning_rate, ds, toy_size, max_epochs, earl cost = nn.CrossEntropyLoss() best_acc = train_child_network(model, train_loader, test_loader, sgd, - cost, max_epochs, early_stop_num, logging=False, - print_every_epoch=False) + cost, max_epochs, early_stop_num, early_stop_flag, + average_validation, logging=False, print_every_epoch=False) # update q_values if policy < num_policies: @@ -222,6 +223,8 @@ if __name__=='__main__': toy_size = 0.02 # total propeortion of training and test set we use max_epochs = 100 # max number of epochs that is run if early stopping is not hit early_stop_num = 10 # max number of worse validation scores before early stopping is triggered + early_stop_flag = True # implement early stopping or not + average_validation = [15,25] # if not implementing early stopping, what epochs are we averaging over num_policies = 5 # fix number of policies num_sub_policies = 5 # fix number of sub-policies in a policy iterations = 100 # total iterations, should be more than the number of policies @@ -230,7 +233,7 @@ if __name__=='__main__': # generate random policies at start policies = generate_policies(num_policies, num_sub_policies) - q_values, best_q_values = run_UCB1(policies, batch_size, learning_rate, ds, toy_size, max_epochs, early_stop_num, iterations, IsLeNet) + q_values, best_q_values = run_UCB1(policies, batch_size, learning_rate, ds, toy_size, max_epochs, early_stop_num, early_stop_flag, average_validation, iterations, IsLeNet) plt.plot(best_q_values) diff --git a/MetaAugment/main.py b/MetaAugment/main.py index b9642879b5896f8f874b230267a36384acd38291..61dec50cc98134d6931d721c79b2d53fd11c750f 100644 --- a/MetaAugment/main.py +++ b/MetaAugment/main.py @@ -41,6 +41,8 @@ def train_child_network(child_network, cost, max_epochs=2000, early_stop_num=10, + early_stop_flag=True, + average_validation=[15,25], logging=False, print_every_epoch=True): if torch.cuda.is_available(): @@ -94,17 +96,26 @@ def train_child_network(child_network, # correct += torch.sum(_.numpy(), axis=-1) _sum += _.shape[0] - # update best validation accuracy if it was higher, otherwise increase early stop count + acc = correct / _sum + if average_validation[0] <= _epoch <= average_validation[1]: + total_val += acc + + # update best validation accuracy if it was higher, otherwise increase early stop count if acc > best_acc : best_acc = acc early_stop_cnt = 0 else: early_stop_cnt += 1 - # exit if validation gets worse over 10 runs - if early_stop_cnt >= early_stop_num: + # exit if validation gets worse over 10 runs and using early stopping + if early_stop_cnt >= early_stop_num and early_stop_flag: + break + + # exit if using fixed epoch length + if _epoch >= average_validation[1] and not early_stop_flag: + best_acc = total_val / (average_validation[1] - average_validation[0] + 1) break if print_every_epoch: diff --git a/requirements.txt b/requirements.txt index 6892830fe80aef2d9340766deb85b60815f117f7..a19a95d5bc34cbf5c929cdc214e7ed249757ea16 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,8 +2,9 @@ torch torchvision numpy Flask +wheel react pytest tqdm pygad -matplotlib \ No newline at end of file +matplotlib