forked from cambridge-mlg/RAT-SPN
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update, added discriminative training for RAT-SPNs
- Loading branch information
R. Peharz
committed
Aug 1, 2019
1 parent
9831956
commit a11ab87
Showing
18 changed files
with
879 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"worker_time_limit": 42900} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import numpy as np | ||
import pickle | ||
import os | ||
|
||
datasets = ['mnist', 'fashion-mnist', 'wine', 'theorem', 'higgs', 'imdb'] | ||
result_basefolder = 'results/ratspn/' | ||
|
||
|
||
def evaluate(): | ||
|
||
ls = os.listdir(result_basefolder) | ||
|
||
for dataset in datasets: | ||
print() | ||
if dataset not in ls: | ||
print('Results for {} not found.'.format(dataset)) | ||
continue | ||
|
||
ls2 = os.listdir(os.path.join(result_basefolder, dataset)) | ||
|
||
best_valid_acc = -np.inf | ||
best_test_acc = -np.inf | ||
best_model = None | ||
best_epoch = None | ||
test_accs = [] | ||
|
||
for result_folder in ls2: | ||
|
||
argdict = {} | ||
for a in result_folder.split('__'): | ||
last_ = a.rfind('_') | ||
argdict[a[:last_]] = float(a[last_ + 1:]) | ||
|
||
try: | ||
results = pickle.load(open('{}/{}/{}/results.pkl'.format( | ||
result_basefolder, | ||
dataset, | ||
result_folder), "rb")) | ||
except: | ||
print() | ||
print("can't load") | ||
print(result_folder) | ||
continue | ||
|
||
valid_acc = results['best_valid_acc'] | ||
test_acc = results['test_ACC'][results['epoch_best_valid_acc']] | ||
test_accs.append(test_acc) | ||
|
||
if valid_acc > best_valid_acc: | ||
best_valid_acc = valid_acc | ||
best_test_acc = test_acc | ||
best_model = argdict | ||
best_epoch = results['epoch_best_valid_acc'] | ||
|
||
print('Test accuracy: {}'.format(best_test_acc)) | ||
print('Achieved by configuration:') | ||
print(best_model) | ||
print('in epoch {} with validation accuracy {}'.format(best_epoch, best_valid_acc)) | ||
|
||
|
||
if __name__ == '__main__': | ||
|
||
evaluate() |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import eval_rat_spn_discriminative | ||
|
||
eval_rat_spn_discriminative.result_basefolder = 'quick_results/ratspn/' | ||
eval_rat_spn_discriminative.evaluate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import eval_rat_spn_generative | ||
|
||
eval_rat_spn_generative.result_basefolder = 'quick_results/ratspn/debd/' | ||
eval_rat_spn_generative.evaluate() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import run_rat_spn_generative | ||
|
||
run_rat_spn_generative.structure_dict = {} | ||
run_rat_spn_generative.structure_dict[2] = [ | ||
{'num_recursive_splits': 10, 'num_input_distributions': 8, 'num_sums': 8}] | ||
run_rat_spn_generative.base_result_path = "quick_results/ratspn/debd/" | ||
run_rat_spn_generative.num_epochs = 20 | ||
|
||
run_rat_spn_generative.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import run_rat_spn_mnist | ||
|
||
run_rat_spn_mnist.structure_dict = {} | ||
# depth 1 | ||
run_rat_spn_mnist.structure_dict[1] = [{'num_recursive_splits': 14, 'num_input_distributions': 15, 'num_sums': 10}] | ||
# depth 2 | ||
run_rat_spn_mnist.structure_dict[2] = [{'num_recursive_splits': 12, 'num_input_distributions': 15, 'num_sums': 15}] | ||
# depth 3 | ||
run_rat_spn_mnist.structure_dict[3] = [{'num_recursive_splits': 12, 'num_input_distributions': 14, 'num_sums': 12}] | ||
# depth 4 | ||
run_rat_spn_mnist.structure_dict[4] = [{'num_recursive_splits': 10, 'num_input_distributions': 15, 'num_sums': 10}] | ||
run_rat_spn_mnist.base_result_path = "quick_results/ratspn/mnist/" | ||
run_rat_spn_mnist.param_configs = [{'dropout_rate_input': 0.5, 'dropout_rate_sums': 0.5}] | ||
run_rat_spn_mnist.num_epochs = 100 | ||
|
||
run_rat_spn_mnist.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import os | ||
import filelock | ||
import utils | ||
import sys | ||
import subprocess | ||
import time | ||
import json | ||
|
||
print("") | ||
print("Discriminative Training of RAT-SPNs on fashion-mnist") | ||
print("") | ||
|
||
with open('configurations.json') as f: | ||
configs = json.loads(f.read()) | ||
|
||
start_time = time.time() | ||
time_limit_seconds = configs['worker_time_limit'] | ||
dont_start_if_less_than_seconds = 600.0 | ||
base_result_path = "results/ratspn/fashion-mnist/" | ||
|
||
structure_dict = {} | ||
|
||
# depth 1 | ||
structure_dict[1] = [ | ||
{'num_recursive_splits': 9, 'num_input_distributions': 10, 'num_sums': 10}, | ||
{'num_recursive_splits': 14, 'num_input_distributions': 15, 'num_sums': 10}, | ||
{'num_recursive_splits': 19, 'num_input_distributions': 20, 'num_sums': 10}, | ||
{'num_recursive_splits': 29, 'num_input_distributions': 25, 'num_sums': 10}, | ||
{'num_recursive_splits': 40, 'num_input_distributions': 33, 'num_sums': 10}] | ||
|
||
# depth 2 | ||
structure_dict[2] = [ | ||
{'num_recursive_splits': 8, 'num_input_distributions': 10, 'num_sums': 10}, | ||
{'num_recursive_splits': 12, 'num_input_distributions': 15, 'num_sums': 15}, | ||
{'num_recursive_splits': 19, 'num_input_distributions': 20, 'num_sums': 18}, | ||
{'num_recursive_splits': 30, 'num_input_distributions': 25, 'num_sums': 25}, | ||
{'num_recursive_splits': 40, 'num_input_distributions': 37, 'num_sums': 35}] | ||
|
||
# depth 3 | ||
structure_dict[3] = [ | ||
{'num_recursive_splits': 10, 'num_input_distributions': 8, 'num_sums': 8}, | ||
{'num_recursive_splits': 12, 'num_input_distributions': 14, 'num_sums': 12}, | ||
{'num_recursive_splits': 15, 'num_input_distributions': 20, 'num_sums': 18}, | ||
{'num_recursive_splits': 30, 'num_input_distributions': 25, 'num_sums': 20}, | ||
{'num_recursive_splits': 40, 'num_input_distributions': 35, 'num_sums': 30}] | ||
|
||
# depth 4 | ||
structure_dict[4] = [ | ||
{'num_recursive_splits': 5, 'num_input_distributions': 10, 'num_sums': 9}, | ||
{'num_recursive_splits': 10, 'num_input_distributions': 15, 'num_sums': 10}, | ||
{'num_recursive_splits': 14, 'num_input_distributions': 20, 'num_sums': 14}, | ||
{'num_recursive_splits': 28, 'num_input_distributions': 20, 'num_sums': 20}, | ||
{'num_recursive_splits': 40, 'num_input_distributions': 30, 'num_sums': 26}] | ||
|
||
param_configs = [ | ||
{'dropout_rate_input': 1.0, 'dropout_rate_sums': 1.0}, | ||
{'dropout_rate_input': 1.0, 'dropout_rate_sums': 0.75}, | ||
{'dropout_rate_input': 1.0, 'dropout_rate_sums': 0.5}, | ||
{'dropout_rate_input': 1.0, 'dropout_rate_sums': 0.25}, | ||
{'dropout_rate_input': 0.75, 'dropout_rate_sums': 1.0}, | ||
{'dropout_rate_input': 0.75, 'dropout_rate_sums': 0.75}, | ||
{'dropout_rate_input': 0.75, 'dropout_rate_sums': 0.5}, | ||
{'dropout_rate_input': 0.75, 'dropout_rate_sums': 0.25}, | ||
{'dropout_rate_input': 0.5, 'dropout_rate_sums': 1.0}, | ||
{'dropout_rate_input': 0.5, 'dropout_rate_sums': 0.75}, | ||
{'dropout_rate_input': 0.5, 'dropout_rate_sums': 0.5}, | ||
{'dropout_rate_input': 0.5, 'dropout_rate_sums': 0.25}, | ||
{'dropout_rate_input': 0.25, 'dropout_rate_sums': 1.0}, | ||
{'dropout_rate_input': 0.25, 'dropout_rate_sums': 0.75}, | ||
{'dropout_rate_input': 0.25, 'dropout_rate_sums': 0.5}, | ||
{'dropout_rate_input': 0.25, 'dropout_rate_sums': 0.25}] | ||
|
||
num_epochs = 200 | ||
|
||
|
||
def run(): | ||
for split_depth in structure_dict: | ||
for structure_config in structure_dict[split_depth]: | ||
for config_dict in param_configs: | ||
|
||
remaining_time = time_limit_seconds - (time.time() - start_time) | ||
if remaining_time < dont_start_if_less_than_seconds: | ||
print("Only {} seconds remaining, stop worker".format(remaining_time)) | ||
sys.exit(0) | ||
|
||
cmd = "python train_rat_spn.py --store_best_valid_loss --store_best_valid_acc --num_epochs {}".format(num_epochs) | ||
cmd += " --timeout_seconds {}".format(remaining_time) | ||
cmd += " --split_depth {}".format(split_depth) | ||
cmd += " --data_path data/fashion-mnist/" | ||
|
||
for key in sorted(structure_config.keys()): | ||
cmd += " --{} {}".format(key, structure_config[key]) | ||
for key in sorted(config_dict.keys()): | ||
cmd += " --{} {}".format(key, config_dict[key]) | ||
|
||
comb_string = "" | ||
comb_string += "split_depth_{}".format(split_depth) | ||
for key in sorted(structure_config.keys()): | ||
comb_string += "__{}_{}".format(key, structure_config[key]) | ||
for key in sorted(config_dict.keys()): | ||
comb_string += "__{}_{}".format(key, config_dict[key]) | ||
|
||
result_path = base_result_path + comb_string | ||
cmd += " --result_path " + result_path | ||
|
||
### | ||
print(cmd) | ||
|
||
utils.mkdir_p(result_path) | ||
lock_file = result_path + "/file.lock" | ||
done_file = result_path + "/file.done" | ||
lock = filelock.FileLock(lock_file) | ||
try: | ||
lock.acquire(timeout=0.1) | ||
if os.path.isfile(done_file): | ||
print(" already done -> skip") | ||
else: | ||
sys.stdout.flush() | ||
ret_val = subprocess.call(cmd, shell=True) | ||
if ret_val == 7: | ||
lock.release() | ||
print("Task timed out, stop worker") | ||
sys.exit(0) | ||
os.system("touch {}".format(done_file)) | ||
lock.release() | ||
except filelock.Timeout: | ||
print(" locked -> skip") | ||
|
||
if __name__ == '__main__': | ||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.