├── .idea ├── .gitignore ├── vcs.xml ├── inspectionProfiles │ └── profiles_settings.xml ├── modules.xml ├── misc.xml └── MM_Bench.iml ├── demo ├── flow_chart.png ├── One_to_many.png ├── material_schematics.png ├── Main_perf_and_example.png └── environment.yml ├── GA ├── models │ ├── Chen_best_model │ │ ├── flags.obj │ │ └── parameters.txt │ ├── Peurifoy_best_model │ │ ├── flags.obj │ │ └── parameters.txt │ └── Yang_sim_best_model │ │ ├── flags.obj │ │ └── parameters.txt ├── parameters.py ├── train.py ├── model_maker.py └── flag_reader.py ├── MDN ├── models │ ├── Chen_best_model │ │ ├── flags.obj │ │ └── parameters.txt │ ├── Yang_best_model │ │ ├── flags.obj │ │ └── parameters.txt │ └── Peurifoy_best_model │ │ ├── flags.obj │ │ └── parameters.txt ├── parameters.py ├── model_maker.py ├── flag_reader.py ├── mdn_tony_duan.py ├── train.py ├── evaluate.py └── mdn_manu_joseph.py ├── NA ├── models │ ├── Chen_best_model │ │ ├── flags.obj │ │ └── parameters.txt │ ├── Yang_best_model │ │ ├── flags.obj │ │ └── parameters.txt │ └── Peurifoy_best_model │ │ ├── flags.obj │ │ └── parameters.txt ├── get_MSE_plots.py ├── train.py ├── parameters.py ├── model_maker.py └── flag_reader.py ├── VAE ├── models │ ├── Chen_best_model │ │ ├── flags.obj │ │ └── parameters.txt │ ├── Yang_best_model │ │ ├── flags.obj │ │ └── parameters.txt │ └── Peurifoy_best_model │ │ ├── flags.obj │ │ └── parameters.txt ├── parameters.py ├── train.py ├── flag_reader.py └── evaluate.py ├── cINN ├── models │ ├── Chen_best_model │ │ ├── flags.obj │ │ └── parameters.txt │ ├── Yang_best_model │ │ ├── flags.obj │ │ └── parameters.txt │ └── Peurifoy_best_model │ │ ├── flags.obj │ │ └── parameters.txt ├── parameters.py ├── model_maker.py ├── flag_reader.py └── train.py ├── Tandem ├── models │ ├── Chen_best_model │ │ ├── flags.obj │ │ └── parameters.txt │ ├── Yang_best_model │ │ ├── flags.obj │ │ └── parameters.txt │ └── Peurifoy_best_model │ │ ├── flags.obj │ │ └── parameters.txt ├── parameters.py ├── train.py ├── flag_reader.py ├── model_maker.py └── evaluate.py ├── INN_FrEIA ├── models │ ├── Chen_best_model │ │ ├── flags.obj │ │ └── parameters.txt │ ├── Yang_best_model │ │ ├── flags.obj │ │ └── parameters.txt │ └── Peurifoy_best_model │ │ ├── flags.obj │ │ └── parameters.txt ├── model_maker.py ├── parameters.py ├── train.py ├── flag_reader.py └── evaluate.py ├── inverse ├── models │ ├── Chen_best_model │ │ ├── flags.obj │ │ └── parameters.txt │ ├── Yang_best_model │ │ ├── flags.obj │ │ └── parameters.txt │ └── Peurifoy_best_model │ │ ├── flags.obj │ │ └── parameters.txt ├── get_MSE_plots.py ├── parameters.py ├── model_maker.py ├── train.py ├── flag_reader.py └── modulized_eval.py ├── .gitignore ├── utils ├── README.md ├── concate_dataset_into_large_file.py ├── time_recorder.py ├── chen_batch_predict.py ├── step_function.py ├── peurifoy_batch_predict.py ├── delete.py ├── evaluation_helper.py └── get_deterministic_multi.ipynb ├── LICENSE └── Data └── Yang_sim └── generate_mm_x.py /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /demo/flow_chart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/demo/flow_chart.png -------------------------------------------------------------------------------- /demo/One_to_many.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/demo/One_to_many.png -------------------------------------------------------------------------------- /demo/material_schematics.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/demo/material_schematics.png -------------------------------------------------------------------------------- /demo/Main_perf_and_example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/demo/Main_perf_and_example.png -------------------------------------------------------------------------------- /GA/models/Chen_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/GA/models/Chen_best_model/flags.obj -------------------------------------------------------------------------------- /MDN/models/Chen_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/MDN/models/Chen_best_model/flags.obj -------------------------------------------------------------------------------- /MDN/models/Yang_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/MDN/models/Yang_best_model/flags.obj -------------------------------------------------------------------------------- /NA/models/Chen_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/NA/models/Chen_best_model/flags.obj -------------------------------------------------------------------------------- /NA/models/Yang_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/NA/models/Yang_best_model/flags.obj -------------------------------------------------------------------------------- /VAE/models/Chen_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/VAE/models/Chen_best_model/flags.obj -------------------------------------------------------------------------------- /VAE/models/Yang_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/VAE/models/Yang_best_model/flags.obj -------------------------------------------------------------------------------- /cINN/models/Chen_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/cINN/models/Chen_best_model/flags.obj -------------------------------------------------------------------------------- /cINN/models/Yang_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/cINN/models/Yang_best_model/flags.obj -------------------------------------------------------------------------------- /GA/models/Peurifoy_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/GA/models/Peurifoy_best_model/flags.obj -------------------------------------------------------------------------------- /GA/models/Yang_sim_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/GA/models/Yang_sim_best_model/flags.obj -------------------------------------------------------------------------------- /NA/models/Peurifoy_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/NA/models/Peurifoy_best_model/flags.obj -------------------------------------------------------------------------------- /Tandem/models/Chen_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/Tandem/models/Chen_best_model/flags.obj -------------------------------------------------------------------------------- /Tandem/models/Yang_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/Tandem/models/Yang_best_model/flags.obj -------------------------------------------------------------------------------- /INN_FrEIA/models/Chen_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/INN_FrEIA/models/Chen_best_model/flags.obj -------------------------------------------------------------------------------- /INN_FrEIA/models/Yang_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/INN_FrEIA/models/Yang_best_model/flags.obj -------------------------------------------------------------------------------- /MDN/models/Peurifoy_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/MDN/models/Peurifoy_best_model/flags.obj -------------------------------------------------------------------------------- /VAE/models/Peurifoy_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/VAE/models/Peurifoy_best_model/flags.obj -------------------------------------------------------------------------------- /cINN/models/Peurifoy_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/cINN/models/Peurifoy_best_model/flags.obj -------------------------------------------------------------------------------- /inverse/models/Chen_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/inverse/models/Chen_best_model/flags.obj -------------------------------------------------------------------------------- /inverse/models/Yang_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/inverse/models/Yang_best_model/flags.obj -------------------------------------------------------------------------------- /Tandem/models/Peurifoy_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/Tandem/models/Peurifoy_best_model/flags.obj -------------------------------------------------------------------------------- /inverse/models/Peurifoy_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/inverse/models/Peurifoy_best_model/flags.obj -------------------------------------------------------------------------------- /INN_FrEIA/models/Peurifoy_best_model/flags.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BensonRen/AEM_DIM_Bench/HEAD/INN_FrEIA/models/Peurifoy_best_model/flags.obj -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | **.csv 2 | Data/ 3 | **.nohup 4 | **.zip 5 | */models/ 6 | *multi_eval* 7 | *temp.ipynb 8 | **__ 9 | **.sh 10 | */*.out 11 | **.jpg 12 | **ensemble_plot* 13 | Data/Yang_sim/state_dicts/*.png 14 | **.png 15 | **.out 16 | **.err 17 | */data/ 18 | *results* 19 | **.pt 20 | **event 21 | **pidfile.txt 22 | exp_data/ 23 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | -------------------------------------------------------------------------------- /utils/README.md: -------------------------------------------------------------------------------- 1 | #utils module 2 | ## Function: The package that stores all the shared functions for all the modules such as supporting data_readers 3 | ### Current List 4 | 5 | #### data_reader.py 6 | Description: Data input interface. Read the data out and prepare them to data loaders. 7 | #### get_pred_truth_file.py 8 | Description: Small helper function that returns the prediciton and truth file 9 | #### plotsAnalysis.py 10 | Description: All the plotting analysis functions are inside 11 | #### time_recorder.py 12 | Description: The time recorder class which helps time the training and inference performance -------------------------------------------------------------------------------- /MDN/models/Chen_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Chen', 'test_ratio': 0.2, 'num_gaussian': 8, 'linear': [256, 100, 100, 100, 5], 'optim': 'Adam', 'reg_scale': 0.001, 'batch_size': 512, 'eval_batch_size': 4096, 'eval_step': 5, 'train_step': 500, 'lr': 0.005, 'lr_decay_rate': 0.8, 'stop_threshold': -inf, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 'y_range': [16, 2016], 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'model_name': 'Chen_gaussian_8_layer_num_5_unit_100_lr_0.005_reg_scale_0.001', 'data_dir': '/home/sr365/MM_Bench/Data/', 'normalize_input': True, 'eval_model': None, 'use_cpu_only': False, 'input_dim': 256, 'sigma_bias_flag': True, 'mu_bias_init': None, 'best_validation_loss': -9.075838756561279} 2 | -------------------------------------------------------------------------------- /MDN/models/Peurifoy_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Peurifoy', 'test_ratio': 0.2, 'num_gaussian': 32, 'linear': [201, 100, 100, 100, 100, 100, 8], 'optim': 'Adam', 'reg_scale': 0.001, 'batch_size': 512, 'eval_batch_size': 4096, 'eval_step': 5, 'train_step': 500, 'lr': 0.005, 'lr_decay_rate': 0.8, 'stop_threshold': -inf, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 'y_range': [16, 2016], 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'model_name': 'Peurifoy_gaussian_32_layer_num_7_unit_100_lr_0.005_reg_scale_0.001', 'data_dir': '/home/sr365/MM_Bench/Data/', 'normalize_input': True, 'eval_model': None, 'use_cpu_only': False, 'input_dim': 201, 'sigma_bias_flag': True, 'mu_bias_init': None, 'best_validation_loss': -1.0044535249471664} 2 | -------------------------------------------------------------------------------- /MDN/models/Yang_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Yang_sim', 'test_ratio': 0.2, 'num_gaussian': 16, 'linear': [2000, 100, 100, 100, 100, 100, 100, 100, 14], 'optim': 'Adam', 'reg_scale': 0.001, 'batch_size': 512, 'eval_batch_size': 4096, 'eval_step': 5, 'train_step': 500, 'lr': 0.005, 'lr_decay_rate': 0.8, 'stop_threshold': -inf, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 'y_range': [16, 2016], 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'model_name': 'Yang_sim_gaussian_16_layer_num_9_unit_100_lr_0.005_reg_scale_0.001', 'data_dir': '/home/sr365/MM_Bench/Data/', 'normalize_input': True, 'eval_model': None, 'use_cpu_only': False, 'input_dim': 2000, 'sigma_bias_flag': True, 'mu_bias_init': None, 'best_validation_loss': 8.671456336975098} 2 | -------------------------------------------------------------------------------- /GA/models/Chen_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Chen', 'test_ratio': 0.2, 'use_lorentz': False, 'linear': [5, 700, 700, 700, 700, 700, 700, 700, 700, 700, 256], 'conv_out_channel': [], 'conv_kernel_size': [], 'conv_stride': [], 'optim': 'Adam', 'reg_scale': 0, 'batch_size': 1024, 'eval_batch_size': 2048, 'eval_step': 20, 'train_step': 300, 'backprop_step': 300, 'lr': 0.001, 'lr_decay_rate': 0.8, 'stop_threshold': 1e-09, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 'y_range': [16, 2016], 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'model_name': 'Chenno_conv_700_num_11_lr_0.001reg_scale_0trail_1', 'data_dir': '/home/sr365/MM_Bench/Data/', 'normalize_input': True, 'eval_model': 'mm', 'use_cpu_only': False, 'best_validation_loss': 2.3181964934337884e-06} 2 | -------------------------------------------------------------------------------- /NA/models/Chen_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Chen', 'test_ratio': 0.2, 'use_lorentz': False, 'linear': [5, 700, 700, 700, 700, 700, 700, 700, 700, 700, 256], 'conv_out_channel': [], 'conv_kernel_size': [], 'conv_stride': [], 'optim': 'Adam', 'reg_scale': 0, 'batch_size': 1024, 'eval_batch_size': 2048, 'eval_step': 20, 'train_step': 300, 'backprop_step': 300, 'lr': 0.001, 'lr_decay_rate': 0.8, 'stop_threshold': 1e-09, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 'y_range': [16, 2016], 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'model_name': 'Chenno_conv_700_num_11_lr_0.001reg_scale_0trail_1', 'data_dir': '/home/sr365/MM_Bench/Data/', 'normalize_input': True, 'eval_model': 'mm', 'use_cpu_only': False, 'best_validation_loss': 2.3181964934337884e-06} 2 | -------------------------------------------------------------------------------- /.idea/MM_Bench.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 15 | 16 | 18 | -------------------------------------------------------------------------------- /GA/models/Peurifoy_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Peurifoy', 'test_ratio': 0.2, 'use_lorentz': False, 'linear': [8, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 201], 'conv_out_channel': [], 'conv_kernel_size': [], 'conv_stride': [], 'optim': 'Adam', 'reg_scale': 0.0001, 'batch_size': 1024, 'eval_batch_size': 2048, 'eval_step': 20, 'train_step': 300, 'backprop_step': 300, 'lr': 0.0001, 'lr_decay_rate': 0.6, 'stop_threshold': 1e-09, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 'y_range': [16, 2016], 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'model_name': 'Peurifoy\\17_1700_0.0001_0.0001_0.6_0', 'data_dir': '../Data', 'normalize_input': True, 'eval_model': 'mm', 'use_cpu_only': False, 'best_validation_loss': 0.005607408657670021} 2 | -------------------------------------------------------------------------------- /NA/models/Peurifoy_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Peurifoy', 'test_ratio': 0.2, 'use_lorentz': False, 'linear': [8, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 201], 'conv_out_channel': [], 'conv_kernel_size': [], 'conv_stride': [], 'optim': 'Adam', 'reg_scale': 0.0001, 'batch_size': 1024, 'eval_batch_size': 2048, 'eval_step': 20, 'train_step': 300, 'backprop_step': 300, 'lr': 0.0001, 'lr_decay_rate': 0.6, 'stop_threshold': 1e-09, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 'y_range': [16, 2016], 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'model_name': 'Peurifoy\\17_1700_0.0001_0.0001_0.6_0', 'data_dir': '../Data', 'normalize_input': True, 'eval_model': 'mm', 'use_cpu_only': False, 'best_validation_loss': 0.005607408657670021} 2 | -------------------------------------------------------------------------------- /NA/models/Yang_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Yang_sim', 'test_ratio': 0.2, 'use_lorentz': False, 'linear': [14, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1000], 'conv_out_channel': [4, 4, 4], 'conv_kernel_size': [4, 3, 3], 'conv_stride': [2, 1, 1], 'optim': 'Adam', 'reg_scale': 0, 'batch_size': 1024, 'eval_batch_size': 2048, 'eval_step': 20, 'train_step': 300, 'backprop_step': 300, 'lr': 0.001, 'lr_decay_rate': 0.8, 'stop_threshold': 1e-09, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 'y_range': [16, 2016], 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'model_name': 'Yang_simconv_444_433_211_linear_1500_num_10_lr_0.001reg_scale_0trail_1', 'data_dir': '/home/sr365/MM_Bench/Data/', 'normalize_input': True, 'eval_model': 'mm', 'use_cpu_only': False, 'best_validation_loss': 0.0012949189404025674} 2 | -------------------------------------------------------------------------------- /GA/models/Yang_sim_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Yang_sim', 'test_ratio': 0.2, 'use_lorentz': False, 'linear': [14, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1000], 'conv_out_channel': [4, 4, 4], 'conv_kernel_size': [4, 3, 3], 'conv_stride': [2, 1, 1], 'optim': 'Adam', 'reg_scale': 0, 'batch_size': 1024, 'eval_batch_size': 2048, 'eval_step': 20, 'train_step': 300, 'backprop_step': 300, 'lr': 0.001, 'lr_decay_rate': 0.8, 'stop_threshold': 1e-09, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 'y_range': [16, 2016], 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'model_name': 'Yang_simconv_444_433_211_linear_1500_num_10_lr_0.001reg_scale_0trail_1', 'data_dir': '/home/sr365/MM_Bench/Data/', 'normalize_input': True, 'eval_model': 'mm', 'use_cpu_only': False, 'best_validation_loss': 0.0012949189404025674} 2 | -------------------------------------------------------------------------------- /cINN/models/Peurifoy_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Peurifoy', 'test_ratio': 0.2, 'dim_z': 8, 'dim_x': 8, 'dim_y': 201, 'dim_spec': None, 'couple_layer_num': 14, 'linear_se': [], 'conv_out_channel_se': [], 'conv_kernel_size_se': [], 'conv_stride_se': [], 'optim': 'Adam', 'reg_scale': 0.0001, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 'y_range': [16, 2016], 'grad_clamp': 15, 'batch_size': 2048, 'eval_batch_size': 2048, 'eval_step': 50, 'train_step': 500, 'verb_step': 50, 'lr': 0.001, 'lr_decay_rate': 0.8, 'stop_threshold': -inf, 'model_name': 'Peurifoy_mid_layer_1024_couple_layer_num14_lr_0.001_reg_scale_0.0001_trail_2', 'data_dir': '/home/sr365/MM_Bench/Data/', 'normalize_input': True, 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'eval_model': 'fake_2k', 'use_cpu_only': False, 'best_validation_loss': -8.930118560791016} 2 | -------------------------------------------------------------------------------- /cINN/models/Yang_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Yang_sim', 'test_ratio': 0.2, 'dim_z': 14, 'dim_x': 14, 'dim_y': 2000, 'dim_spec': None, 'couple_layer_num': 9, 'linear_se': [], 'conv_out_channel_se': [], 'conv_kernel_size_se': [], 'conv_stride_se': [], 'optim': 'Adam', 'reg_scale': 0.0001, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 'y_range': [16, 2016], 'grad_clamp': 15, 'batch_size': 2048, 'eval_batch_size': 2048, 'eval_step': 50, 'train_step': 500, 'verb_step': 50, 'lr': 0.001, 'lr_decay_rate': 0.8, 'stop_threshold': -inf, 'model_name': 'Yang_sim_mid_layer_1024_couple_layer_num9_lr_0.001_reg_scale_0.0001_trail_1', 'data_dir': '/home/sr365/MM_Bench/Data/', 'normalize_input': True, 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'eval_model': 'fake_2k', 'use_cpu_only': False, 'best_validation_loss': -5.751742362976074} 2 | -------------------------------------------------------------------------------- /inverse/models/Chen_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Chen', 'test_ratio': 0.2, 'md_radius': 0.2, 'md_start': -inf, 'md_end': inf, 'md_coeff': 5e-05, 'use_lorentz': False, 'linear': [256, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 5], 'conv_out_channel': [], 'conv_kernel_size': [], 'conv_stride': [], 'optim': 'Adam', 'reg_scale': 0.002, 'batch_size': 1024, 'eval_batch_size': 2048, 'eval_step': 20, 'train_step': 1000, 'backprop_step': 300, 'lr': 0.0001, 'lr_decay_rate': 0.8, 'stop_threshold': 1e-09, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 'y_range': [16, 2016], 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'model_name': 'Chenno_conv_1500_size_1500_num_10_lr_0.0001reg_scale_0.002trail_1', 'data_dir': '../Data', 'normalize_input': True, 'eval_model': 'mm', 'use_cpu_only': False, 'best_validation_loss': 0.0011525508016347885} 2 | -------------------------------------------------------------------------------- /inverse/models/Peurifoy_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Peurifoy', 'test_ratio': 0.2, 'md_radius': 0.2, 'md_start': -inf, 'md_end': inf, 'md_coeff': 5e-05, 'use_lorentz': False, 'linear': [201, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 8], 'conv_out_channel': [], 'conv_kernel_size': [], 'conv_stride': [], 'optim': 'Adam', 'reg_scale': 0.002, 'batch_size': 1024, 'eval_batch_size': 2048, 'eval_step': 20, 'train_step': 1000, 'backprop_step': 300, 'lr': 0.0001, 'lr_decay_rate': 0.8, 'stop_threshold': 1e-09, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 'y_range': [16, 2016], 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'model_name': 'Peurifoyno_conv_1500_size_1500_num_14_lr_0.0001reg_scale_0.002trail_1', 'data_dir': '../Data', 'normalize_input': True, 'eval_model': 'mm', 'use_cpu_only': False, 'best_validation_loss': 0.010933078080415725} 2 | -------------------------------------------------------------------------------- /inverse/models/Yang_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Yang_sim', 'test_ratio': 0.2, 'md_radius': 0.2, 'md_start': -inf, 'md_end': inf, 'md_coeff': 5e-05, 'use_lorentz': False, 'linear': [2000, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 14], 'conv_out_channel': [], 'conv_kernel_size': [], 'conv_stride': [], 'optim': 'Adam', 'reg_scale': 1e-05, 'batch_size': 1024, 'eval_batch_size': 2048, 'eval_step': 20, 'train_step': 300, 'backprop_step': 300, 'lr': 0.001, 'lr_decay_rate': 0.8, 'stop_threshold': 1e-09, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 'y_range': [16, 2016], 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'model_name': 'Yang_simno_conv_1500_size_1500_num_18_lr_0.001reg_scale_1e-05trail_1', 'data_dir': '../Data', 'normalize_input': True, 'eval_model': 'mm', 'use_cpu_only': False, 'best_validation_loss': 0.2787666916847229} 2 | -------------------------------------------------------------------------------- /VAE/models/Chen_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Chen', 'test_ratio': 0.2, 'dim_z': 5, 'dim_x': 5, 'dim_y': 256, 'dim_spec': None, 'linear_d': [261, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 5], 'linear_e': [261, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 10], 'linear_se': [], 'conv_out_channel_se': [], 'conv_kernel_size_se': [], 'conv_stride_se': [], 'kl_coeff': 0.005, 'optim': 'Adam', 'reg_scale': 0.005, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 'y_range': [16, 2016], 'batch_size': 1024, 'eval_batch_size': 4096, 'eval_step': 20, 'train_step': 300, 'verb_step': 30, 'lr': 0.0001, 'lr_decay_rate': 0.8, 'stop_threshold': -inf, 'model_name': 'Chen_kl_coeff_0.005_layer_num_9_unit_1000_dim_z_5_reg_scale_0.005', 'data_dir': '/home/sr365/MM_Bench/Data/', 'normalize_input': True, 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'eval_model': None, 'use_cpu_only': False, 'best_validation_loss': 8.803717041015625} 2 | -------------------------------------------------------------------------------- /VAE/models/Yang_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Yang_sim', 'test_ratio': 0.2, 'dim_z': 20, 'dim_x': 14, 'dim_y': 2000, 'dim_spec': None, 'linear_d': [2020, 1000, 1000, 1000, 1000, 1000, 1000, 14], 'linear_e': [2014, 1000, 1000, 1000, 1000, 1000, 1000, 40], 'linear_se': [], 'conv_out_channel_se': [], 'conv_kernel_size_se': [], 'conv_stride_se': [], 'kl_coeff': 0.05, 'optim': 'Adam', 'reg_scale': 0.0001, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 'y_range': [16, 2016], 'batch_size': 1024, 'eval_batch_size': 4096, 'eval_step': 20, 'train_step': 300, 'verb_step': 30, 'lr': 0.0001, 'lr_decay_rate': 0.8, 'stop_threshold': -inf, 'model_name': 'Yang_sim_kl_coeff_0.05_layer_num_8_unit_1000_dim_z_20_reg_scale_0.0001', 'data_dir': '/home/sr365/MM_Bench/Data/', 'normalize_input': True, 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'eval_model': None, 'use_cpu_only': False, 'best_validation_loss': 1272.60498046875} 2 | -------------------------------------------------------------------------------- /cINN/models/Chen_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Chen', 'test_ratio': 0.2, 'dim_z': 5, 'dim_x': 5, 'dim_y': 256, 'dim_spec': None, 'couple_layer_num': 14, 'linear_se': [], 'conv_out_channel_se': [], 'conv_kernel_size_se': [], 'conv_stride_se': [], 'lambda_mse': 1, 'lambda_z': 300.0, 'lambda_rev': 400.0, 'zeros_noise_scale': 0.05, 'y_noise_scale': 0.1, 'optim': 'Adam', 'reg_scale': 0.0005, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 'y_range': [16, 2016], 'grad_clamp': 15, 'batch_size': 2048, 'eval_batch_size': 2048, 'eval_step': 20, 'train_step': 300, 'verb_step': 20, 'lr': 0.001, 'lr_decay_rate': 0.8, 'stop_threshold': -inf, 'model_name': 'Chencouple_layer_num14labmda_mse1_lr_0.001_reg_scale_0.0005', 'data_dir': '/home/sr365/MM_Bench/Data/', 'normalize_input': True, 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'eval_model': 'fake_2k', 'use_cpu_only': False, 'best_validation_loss': -13.997468566894531} 2 | -------------------------------------------------------------------------------- /Tandem/models/Chen_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Chen', 'test_ratio': 0.2, 'load_forward_ckpt_dir': None, 'linear_f': [5, 500, 500, 500, 500, 500, 256], 'conv_out_channel_f': [], 'conv_kernel_size_f': [], 'conv_stride_f': [], 'linear_b': [256, 500, 500, 500, 500, 500, 500, 5], 'conv_out_channel_b': [], 'conv_kernel_size_b': [], 'conv_stride_b': [], 'optim': 'Adam', 'reg_scale': 0.0001, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 'y_range': [16, 2016], 'batch_size': 1024, 'eval_batch_size': 1024, 'eval_step': 20, 'train_step': 300, 'verb_step': 20, 'lr': 0.0001, 'lr_decay_rate': 0.8, 'stop_threshold': -1, 'model_name': 'Chen_layer_num_8_unit_500_reg_scale_0.0001_trail_0', 'data_dir': '/home/sr365/MM_Bench/Data/', 'normalize_input': True, 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'eval_model': None, 'use_cpu_only': False, 'best_validation_loss': 4.376457218313589e-06, 'best_forward_validation_loss': 5.754659650847316e-06} 2 | -------------------------------------------------------------------------------- /VAE/models/Peurifoy_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Peurifoy', 'test_ratio': 0.2, 'dim_z': 30, 'dim_x': 8, 'dim_y': 201, 'dim_spec': None, 'linear_d': [231, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 8], 'linear_e': [209, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 60], 'linear_se': [], 'conv_out_channel_se': [], 'conv_kernel_size_se': [], 'conv_stride_se': [], 'kl_coeff': 0.01, 'optim': 'Adam', 'reg_scale': 0.0001, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 'y_range': [16, 2016], 'batch_size': 1024, 'eval_batch_size': 4096, 'eval_step': 20, 'train_step': 300, 'verb_step': 30, 'lr': 0.0001, 'lr_decay_rate': 0.8, 'stop_threshold': -inf, 'model_name': 'Peurifoy_kl_coeff_0.01_layer_num_12_unit_1000_dim_z_30_reg_scale_0.0001', 'data_dir': '/home/sr365/MM_Bench/Data/', 'normalize_input': True, 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'eval_model': None, 'use_cpu_only': False, 'best_validation_loss': 78.95667724609375} 2 | -------------------------------------------------------------------------------- /INN_FrEIA/models/Yang_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Yang_sim', 'test_ratio': 0.2, 'dim_z': 9, 'dim_x': 14, 'dim_y': 2000, 'dim_tot': 2024, 'dim_spec': None, 'couple_layer_num': 7, 'subnet_linear': [], 'linear_se': [], 'conv_out_channel_se': [], 'conv_kernel_size_se': [], 'conv_stride_se': [], 'lambda_mse': 0.0001, 'lambda_z': 300.0, 'lambda_rev': 400.0, 'zeros_noise_scale': 0.05, 'y_noise_scale': 0.01, 'optim': 'Adam', 'reg_scale': 0.005, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9], 'y_range': [10, 309], 'grad_clamp': 15, 'batch_size': 1024, 'eval_batch_size': 4096, 'eval_step': 20, 'train_step': 500, 'verb_step': 50, 'lr': 0.001, 'lr_decay_rate': 0.9, 'stop_threshold': -inf, 'model_name': 'Yang_simcouple_layer_num7labmda_mse0.0001_lr_0.001_dim_pad_15_dim_z_9', 'data_dir': '/home/sr365/MM_Bench/Data/', 'ckpt_dir': 'models/', 'normalize_input': True, 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'eval_model': None, 'use_cpu_only': False, 'best_validation_loss': -3477.89404296875} 2 | -------------------------------------------------------------------------------- /INN_FrEIA/models/Peurifoy_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Peurifoy', 'test_ratio': 0.2, 'dim_z': 5, 'dim_x': 8, 'dim_y': 201, 'dim_tot': 216, 'dim_spec': None, 'couple_layer_num': 12, 'subnet_linear': [], 'linear_se': [], 'conv_out_channel_se': [], 'conv_kernel_size_se': [], 'conv_stride_se': [], 'lambda_mse': 0.0001, 'lambda_z': 300.0, 'lambda_rev': 400.0, 'zeros_noise_scale': 0.05, 'y_noise_scale': 0.01, 'optim': 'Adam', 'reg_scale': 0.005, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9], 'y_range': [10, 309], 'grad_clamp': 15, 'batch_size': 1024, 'eval_batch_size': 4096, 'eval_step': 20, 'train_step': 500, 'verb_step': 50, 'lr': 0.001, 'lr_decay_rate': 0.9, 'stop_threshold': -inf, 'model_name': 'Peurifoy_mid_layer_1024_couple_layer_num12labmda_mse0.0001_lr_0.001_dim_pad_10_dim_z_5', 'data_dir': '/home/sr365/MM_Bench/Data/', 'ckpt_dir': 'models/', 'normalize_input': True, 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'eval_model': None, 'use_cpu_only': False, 'best_validation_loss': -132.84808349609375} 2 | -------------------------------------------------------------------------------- /INN_FrEIA/models/Chen_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Chen', 'test_ratio': 0.2, 'dim_z': 1, 'dim_x': 5, 'dim_y': 256, 'dim_tot': 267, 'dim_spec': None, 'couple_layer_num': 9, 'subnet_linear': [], 'linear_se': [], 'conv_out_channel_se': [], 'conv_kernel_size_se': [], 'conv_stride_se': [], 'lambda_mse': 1, 'lambda_z': 300.0, 'lambda_rev': 400.0, 'zeros_noise_scale': 0.05, 'y_noise_scale': 0.01, 'optim': 'Adam', 'reg_scale': 0.005, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9], 'y_range': [10, 309], 'grad_clamp': 15, 'batch_size': 1024, 'eval_batch_size': 4096, 'eval_step': 20, 'train_step': 500, 'verb_step': 50, 'lr': 0.001, 'lr_decay_rate': 0.9, 'stop_threshold': -inf, 'model_name': 'Chencouple_layer_num9labmda_mse1_lr_0.001_dim_pad_10_dim_z_1', 'data_dir': '/home/sr365/MM_Bench/Data/', 'ckpt_dir': 'models/', 'normalize_input': True, 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'eval_model': 'retrain_time_evalsine_wavecouple_layer_num4dim_total4lambda_mse0.01trail1', 'use_cpu_only': False, 'best_validation_loss': -1409.21640625} 2 | -------------------------------------------------------------------------------- /utils/concate_dataset_into_large_file.py: -------------------------------------------------------------------------------- 1 | # This function concatenate a bunch of files into a large, single file for dataset reading 2 | 3 | import os 4 | import numpy as np 5 | import pandas as pd 6 | 7 | mother_folder = '/home/sr365/MM_Bench/Data/Omar_bowtie' 8 | geometry, spectra = None, None 9 | 10 | for file_prefix in ['inputs_Srico_MMPA_bowtie_x1000_','Abs_Srico_MMPA_bowtie_x1000_']: 11 | value_all = None # Init the value 12 | for i in range(1, 8): 13 | # Combine the values 14 | file = os.path.join(mother_folder, file_prefix + '{}.csv'.format(i)) 15 | value = pd.read_csv(file, sep=',', header=None).values 16 | #print(np.shape(value)) 17 | if value_all is None: 18 | value_all = value 19 | else: 20 | value_all = np.concatenate([value_all, value]) # Concate the value 21 | print(np.shape(value_all)) 22 | 23 | # Save the values 24 | np.savetxt(os.path.join(mother_folder, file_prefix + 'all.csv'), value_all) 25 | -------------------------------------------------------------------------------- /Tandem/models/Yang_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Yang_sim', 'test_ratio': 0.2, 'load_forward_ckpt_dir': None, 'linear_f': [14, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1000], 'conv_out_channel_f': [4, 4, 4], 'conv_kernel_size_f': [4, 3, 3], 'conv_stride_f': [2, 1, 1], 'linear_b': [2000, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 14], 'conv_out_channel_b': [], 'conv_kernel_size_b': [], 'conv_stride_b': [], 'optim': 'Adam', 'reg_scale': 5e-05, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 'y_range': [16, 2016], 'batch_size': 1024, 'eval_batch_size': 1024, 'eval_step': 50, 'train_step': 300, 'verb_step': 20, 'lr': 0.0001, 'lr_decay_rate': 0.8, 'stop_threshold': -1, 'model_name': 'Yang_sim_Backward_no_conv_layer_num_15_unit_1500_reg_scale_5e-05_trail_2', 'data_dir': '/home/sr365/MM_Bench/Data/', 'normalize_input': True, 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'eval_model': None, 'use_cpu_only': False, 'best_validation_loss': 0.0016646708827465773, 'best_forward_validation_loss': 0.0024146009236574173} 2 | -------------------------------------------------------------------------------- /Tandem/models/Peurifoy_best_model/parameters.txt: -------------------------------------------------------------------------------- 1 | {'data_set': 'Peurifoy', 'test_ratio': 0.2, 'load_forward_ckpt_dir': None, 'linear_f': [8, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 201], 'conv_out_channel_f': [], 'conv_kernel_size_f': [], 'conv_stride_f': [], 'linear_b': [201, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 8], 'conv_out_channel_b': [], 'conv_kernel_size_b': [], 'conv_stride_b': [], 'optim': 'Adam', 'reg_scale': 0, 'x_range': [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 'y_range': [16, 2016], 'batch_size': 1024, 'eval_batch_size': 1024, 'eval_step': 50, 'train_step': 300, 'verb_step': 20, 'lr': 0.0001, 'lr_decay_rate': 0.8, 'stop_threshold': -1, 'model_name': 'Peurifoy_Backward_no_conv_layer_num_16_unit_2000_reg_scale_0_trail_1', 'data_dir': '/home/sr365/MM_Bench/Data/', 'normalize_input': True, 'geoboundary': [0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786], 'eval_model': None, 'use_cpu_only': False, 'best_validation_loss': 0.007134734839200974, 'best_forward_validation_loss': 0.007013515383005142} 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Simiao Ren 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /utils/time_recorder.py: -------------------------------------------------------------------------------- 1 | import time 2 | import numpy as np 3 | """ 4 | This is a class for keeping time and possible recording it to a file 5 | """ 6 | 7 | 8 | class time_keeper(object): 9 | def __init__(self, time_keeping_file="time_keeper.txt", max_running_time=9999): 10 | self.start = time.time() 11 | self.max_running_time = max_running_time * 60 * 60 12 | self.time_keeping_file = time_keeping_file 13 | self.end = -1 14 | self.duration = -1 15 | 16 | def record(self, write_number): 17 | """ 18 | Record the time to the time_keeping_file, the time marked is the interval between current time and the start time 19 | :param write_number: 20 | :return: 21 | """ 22 | with open(self.time_keeping_file, "a") as f: 23 | self.end = time.time() 24 | self.duration = self.end - self.start 25 | f.write('{},{}\n'.format(write_number, self.duration)) 26 | if (self.duration > self.max_running_time): 27 | raise ValueError('Your program has run over the maximum time limit set by Ben in time_keeper function') 28 | -------------------------------------------------------------------------------- /Data/Yang_sim/generate_mm_x.py: -------------------------------------------------------------------------------- 1 | # This function generates a random geometry x for meta-material dataset. 2 | # This is only the data_x.csv generator, after generating this, please go to NA/predict.py and run the create_mm_dataset() function to get the data_y.csv which is the spectra of the meta-material dataset. Pls be reminded that the neural simulator only has the accuracy of 6e-5 only at the given range above. 3 | # Running this file again would help to create a new set of meta-material dataset, which would help make sure that the models you chose from the 10 trained ones are not biased towards the validataion set instead of the real test performance. 4 | import numpy as np 5 | import os 6 | 7 | def generate_meta_material(data_num): 8 | x_dim = 14 9 | # Generate random number 10 | data_x = np.random.uniform(size=(data_num,x_dim), low=-1, high=1) 11 | print('data_x now has shape:', np.shape(data_x)) 12 | return data_x 13 | 14 | if __name__ == '__main__': 15 | ndata = 10000 # Training and validation set 16 | # ndata = 1000 # Test set (half would be taken) 17 | data_x = generate_meta_material(ndata) 18 | os.makedirs('dataIn') 19 | np.savetxt('dataIn/data_x.csv', data_x) 20 | 21 | -------------------------------------------------------------------------------- /MDN/parameters.py: -------------------------------------------------------------------------------- 1 | """ 2 | Params for Back propagation model 3 | """ 4 | # Define which data set you are using 5 | DATA_SET = 'Yang_sim' 6 | #DATA_SET = 'Chen' 7 | #DATA_SET = 'Peurifoy' 8 | TEST_RATIO = 0.2 9 | 10 | # Model Architectural Params for meta_material data Set 11 | NUM_GAUSSIAN = 10 12 | LINEAR = [2, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 4] 13 | 14 | # Optimizer Params 15 | OPTIM = "Adam" 16 | REG_SCALE = 5e-3 17 | BATCH_SIZE = 512 18 | EVAL_BATCH_SIZE = 4096 19 | EVAL_STEP = 5 20 | TRAIN_STEP = 500 21 | LEARN_RATE = 5e-3 22 | # DECAY_STEP = 25000 # This is for step decay, however we are using dynamic decaying 23 | LR_DECAY_RATE = 0.8 24 | STOP_THRESHOLD = -float('inf') 25 | 26 | # Data specific Params 27 | X_RANGE = [i for i in range(2, 16 )] 28 | Y_RANGE = [i for i in range(16 , 2017 )] # Artificial Meta-material dataset 29 | FORCE_RUN = True 30 | MODEL_NAME = None 31 | DATA_DIR = '/home/sr365/MM_Bench/Data/' # All simulated simple dataset 32 | #DATA_DIR = '../Data/Yang_data/' # All simulated simple dataset 33 | GEOBOUNDARY =[0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786] 34 | NORMALIZE_INPUT = True 35 | 36 | # Running specific 37 | USE_CPU_ONLY = False 38 | EVAL_MODEL = None 39 | -------------------------------------------------------------------------------- /NA/get_MSE_plots.py: -------------------------------------------------------------------------------- 1 | # Get the MSE plot for the evaluated results from /data* folders 2 | 3 | import os 4 | from utils.evaluation_helper import plotMSELossDistrib 5 | 6 | 7 | big_folder = '/home/sr365/MM_Bench/NA/' 8 | #big_folder = '/home/sr365/MM_Bench/NA/' 9 | 10 | for folder in os.listdir(big_folder): 11 | # ignore if not a folder or no data in folder name 12 | if 'data' not in folder or not os.path.isdir(os.path.join(big_folder, folder)) or 'Chen' not in folder: 13 | continue 14 | 15 | print('going into folder ', folder) 16 | # Loop over the data folder 17 | for file in os.listdir(os.path.join(big_folder, folder)): 18 | if 'test_Ytruth' not in file: 19 | continue 20 | print('going in file', file) 21 | truth_file = os.path.join(big_folder, folder, file) 22 | pred_file = os.path.join(big_folder, folder, file.replace('Ytruth', 'Ypred')) 23 | # Make sure Ypred is also present 24 | if not os.path.isfile(pred_file): 25 | print('no Ypred file, abort!') 26 | continue 27 | 28 | print('doing MSE plot for file ', file, 'in folder ', os.path.join(big_folder, folder)) 29 | plotMSELossDistrib(pred_file=pred_file, truth_file=truth_file, 30 | save_dir=os.path.join(big_folder, folder)) 31 | -------------------------------------------------------------------------------- /inverse/get_MSE_plots.py: -------------------------------------------------------------------------------- 1 | # Get the MSE plot for the evaluated results from /data* folders 2 | 3 | import os 4 | from utils.evaluation_helper import plotMSELossDistrib 5 | 6 | 7 | big_folder = '/home/sr365/MM_Bench/NA/' 8 | #big_folder = '/home/sr365/MM_Bench/NA/' 9 | 10 | for folder in os.listdir(big_folder): 11 | # ignore if not a folder or no data in folder name 12 | if 'data' not in folder or not os.path.isdir(os.path.join(big_folder, folder)) or 'Chen' not in folder: 13 | continue 14 | 15 | print('going into folder ', folder) 16 | # Loop over the data folder 17 | for file in os.listdir(os.path.join(big_folder, folder)): 18 | if 'test_Ytruth' not in file: 19 | continue 20 | print('going in file', file) 21 | truth_file = os.path.join(big_folder, folder, file) 22 | pred_file = os.path.join(big_folder, folder, file.replace('Ytruth', 'Ypred')) 23 | # Make sure Ypred is also present 24 | if not os.path.isfile(pred_file): 25 | print('no Ypred file, abort!') 26 | continue 27 | 28 | print('doing MSE plot for file ', file, 'in folder ', os.path.join(big_folder, folder)) 29 | plotMSELossDistrib(pred_file=pred_file, truth_file=truth_file, 30 | save_dir=os.path.join(big_folder, folder)) 31 | -------------------------------------------------------------------------------- /GA/parameters.py: -------------------------------------------------------------------------------- 1 | """ 2 | Params for Genetic Algorithm 3 | """ 4 | 5 | # Define which data set you are using 6 | DATA_SET = 'Peurifoy' #'Chen', 'Yang_sim' 7 | TEST_RATIO = 0.2 8 | 9 | # GA SPECIFIC PARAMETERS 10 | # Essential Params 11 | POP_SIZE = 2048 12 | ELITISM = 500 13 | MUTATION = 0.05 14 | CROSSOVER = 0.8 15 | K = 500 16 | 17 | # Categorical Params 18 | SELECT_OPS = 'roulette' # 'decimation' 'tournament' 19 | CROSS_OPS = 'single-point' # 20 | GA_EVAL = False # Geometry -> Spectra calculation done by simulator function rather than a neural network 21 | 22 | # Optimization Params 23 | EVAL_STEP = 20 24 | GENERATIONS = 300 25 | STOP_THRESHOLD = 1e-9 26 | 27 | # Data specific Params 28 | X_RANGE = range(2,16)#[i for i in range(2, 16 )] 29 | #Y_RANGE = [i for i in range(10 , 2011 )] # Real Meta-material dataset range 30 | Y_RANGE = range(16, 2017)#[i for i in range(16 , 2017 )] # Artificial Meta-material dataset 31 | FORCE_RUN = True 32 | MODEL_NAME = None 33 | #DATA_DIR = '/home/sr365/MM_Bench/Data/' # All simulated simple dataset 34 | #DATA_DIR = '/work/sr365/' # real Meta-material dataset 35 | #DATA_DIR = '/work/sr365/NN_based_MM_data/' # Artificial Meta-material dataset 36 | #DATA_DIR = '/home/omar/PycharmProjects/github/idlm_Pytorch-master/forward/' 37 | DATA_DIR = '../Data' 38 | NORMALIZE_INPUT = True 39 | 40 | # Running specific 41 | USE_CPU_ONLY = False 42 | EVAL_MODEL = "ga" 43 | 44 | # NA Specific Parameters 45 | USE_LORENTZ = False 46 | LINEAR = [8, 400, 400, 400, 201] 47 | CONV_OUT_CHANNEL = [4, 4, 4] 48 | CONV_KERNEL_SIZE = [3, 3, 5] 49 | CONV_STRIDE = [1, 1, 1] 50 | 51 | # Optimizer Params 52 | OPTIM = "Adam" 53 | REG_SCALE = 5e-3 54 | BATCH_SIZE = 1024 55 | TRAIN_STEP = 300 56 | LEARN_RATE = 1e-3 57 | LR_DECAY_RATE = 0.8 58 | -------------------------------------------------------------------------------- /cINN/parameters.py: -------------------------------------------------------------------------------- 1 | """ 2 | The parameter file storing the parameters for INN Model 3 | """ 4 | 5 | # Define which data set you are using 6 | #DATA_SET = 'Yang_sim' 7 | #DATA_SET = 'Chen' 8 | DATA_SET = 'Peurifoy' 9 | 10 | TEST_RATIO = 0.2 11 | 12 | # Architectural Params 13 | """ 14 | # Chen 15 | DIM_Z = 5 16 | DIM_X = 5 17 | DIM_Y = 256 18 | """ 19 | # Peurifoy 20 | DIM_Z = 8 21 | DIM_X = 8 22 | DIM_Y = 201 23 | """ 24 | # Yang 25 | DIM_Z = 14 26 | DIM_X = 14 27 | DIM_Y = 2000 28 | """ 29 | 30 | COUPLE_LAYER_NUM = 13 31 | DIM_SPEC = None 32 | # The below definitions are useless now since we are using the package 33 | LINEAR_SE = [] # Linear units for spectra encoder 34 | CONV_OUT_CHANNEL_SE = [] 35 | CONV_KERNEL_SIZE_SE = [] 36 | CONV_STRIDE_SE = [] 37 | 38 | # This set of parameters are used for dataset meta-material 39 | #LINEAR_SE = [150, 500, 500, 500, 500, DIM_Y] # Linear units for spectra encoder 40 | #CONV_OUT_CHANNEL_SE = [4, 4, 4] 41 | #CONV_KERNEL_SIZE_SE = [5, 5, 8] 42 | #CONV_STRIDE_SE = [1, 1, 2] 43 | 44 | # Optimization params 45 | OPTIM = "Adam" 46 | REG_SCALE = 5e-3 47 | BATCH_SIZE = 2048 48 | EVAL_BATCH_SIZE = 2048 49 | EVAL_STEP = 50 50 | GRAD_CLAMP = 15 51 | TRAIN_STEP = 500 52 | VERB_STEP = 50 53 | LEARN_RATE = 1e-3 54 | # DECAY_STEP = 25000 # This is for step decay, however we are using dynamic decaying 55 | LR_DECAY_RATE = 0.8 56 | STOP_THRESHOLD = -float('inf') 57 | 58 | X_RANGE = [i for i in range(2, 16 )] 59 | Y_RANGE = [i for i in range(16 , 2017 )] # Artificial Meta-material dataset 60 | FORCE_RUN = True 61 | MODEL_NAME = None 62 | DATA_DIR = '/home/sr365/MM_Bench/Data/' # All simulated simple dataset 63 | #DATA_DIR = '../Data/Yang_data/' # All simulated simple dataset 64 | GEOBOUNDARY =[0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786] 65 | NORMALIZE_INPUT = True 66 | 67 | # Running specific params 68 | USE_CPU_ONLY = False 69 | EVAL_MODEL = 'fake_2k' 70 | -------------------------------------------------------------------------------- /GA/train.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file serves as a training interface for training the network 3 | """ 4 | # Built in 5 | import glob 6 | import os 7 | import shutil 8 | import sys 9 | sys.path.append('../utils/') 10 | 11 | # Torch 12 | 13 | # Own 14 | import flag_reader 15 | from utils import data_reader 16 | from class_wrapper import Network 17 | from model_maker import Net 18 | from utils.helper_functions import put_param_into_folder, write_flags_and_BVE 19 | 20 | def training_from_flag(flags): 21 | """ 22 | Training interface. 1. Read data 2. initialize network 3. train network 4. record flags 23 | :param flag: The training flags read from command line or parameter.py 24 | :return: None 25 | """ 26 | # Get the data 27 | train_loader, test_loader = data_reader.read_data(flags) 28 | print("Making network now") 29 | 30 | # Make Network 31 | ntwk = Network(Net, flags, train_loader, test_loader) 32 | 33 | # Training process 34 | print("Start training now...") 35 | ntwk.train() 36 | 37 | # Do the house keeping, write the parameters and put into folder, also use pickle to save the flags obejct 38 | write_flags_and_BVE(flags, ntwk.best_validation_loss, ntwk.ckpt_dir) 39 | 40 | 41 | def retrain_different_dataset(index): 42 | """ 43 | This function is to evaluate all different datasets in the model with one function call 44 | """ 45 | from utils.helper_functions import load_flags 46 | data_set_list = ['Peurifoy','Yang_sim','Chen'] 47 | for eval_model in data_set_list: 48 | flags = load_flags(os.path.join("models", eval_model+'_best_model')) 49 | flags.model_name = "retrain" + str(index) + eval_model 50 | flags.train_step = 500 51 | flags.test_ratio = 0.2 52 | training_from_flag(flags) 53 | 54 | 55 | if __name__ == '__main__': 56 | # Read the parameters to be set 57 | #training_from_flag(flags) 58 | # Do the retraining for all the data set to get the training 59 | for i in range(5): 60 | retrain_different_dataset(i) 61 | 62 | -------------------------------------------------------------------------------- /VAE/parameters.py: -------------------------------------------------------------------------------- 1 | """ 2 | The parameter file storing the parameters for VAE Model 3 | """ 4 | 5 | # Define which data set you are using 6 | DATA_SET = 'Yang_sim' 7 | #DATA_SET = 'Chen' 8 | #DATA_SET = 'Peurifoy' 9 | 10 | TEST_RATIO = 0.2 11 | 12 | # Architectural Params 13 | """ 14 | # Chen 15 | DIM_Z = 5 16 | DIM_X = 5 17 | DIM_Y = 256 18 | # Peurifoy 19 | DIM_Z = 8 20 | DIM_X = 8 21 | DIM_Y = 201 22 | """ 23 | # Yang 24 | DIM_Z = 14 25 | DIM_X = 14 26 | DIM_Y = 2000 27 | DIM_SPEC = None 28 | LINEAR_D = [DIM_Y + DIM_Z, 500, 500, 500, 500, 500, 500, 500, DIM_X] # Linear units for Decoder 29 | LINEAR_E = [DIM_Y + DIM_X, 500, 500, 500, 500, 500, 500, 500, 2*DIM_Z] # Linear units for Encoder 30 | LINEAR_SE = [] # Linear units for spectra encoder 31 | CONV_OUT_CHANNEL_SE = [] 32 | CONV_KERNEL_SIZE_SE = [] 33 | CONV_STRIDE_SE = [] 34 | #LINEAR_SE = [150, 500, 500, 500, 500, DIM_Y] # Linear units for spectra encoder 35 | #CONV_OUT_CHANNEL_SE = [4, 4, 4] 36 | #CONV_KERNEL_SIZE_SE = [5, 5, 8] 37 | #CONV_STRIDE_SE = [1, 1, 2] 38 | 39 | # Optimization params 40 | KL_COEFF = 0.005 41 | OPTIM = "Adam" 42 | REG_SCALE = 5e-3 43 | BATCH_SIZE = 1024 44 | EVAL_BATCH_SIZE = 4096 45 | EVAL_STEP = 20 46 | TRAIN_STEP = 300 47 | VERB_STEP = 30 48 | LEARN_RATE = 1e-4 49 | # DECAY_STEP = 25000 # This is for step decay, however we are using dynamic decaying 50 | LR_DECAY_RATE = 0.8 51 | STOP_THRESHOLD = -float('inf') 52 | 53 | X_RANGE = [i for i in range(2, 16 )] 54 | Y_RANGE = [i for i in range(16 , 2017 )] # Artificial Meta-material dataset 55 | FORCE_RUN = True 56 | MODEL_NAME = None 57 | DATA_DIR = '/home/sr365/MM_Bench/Data/' # All simulated simple dataset 58 | #DATA_DIR = '../Data/Yang_data/' # All simulated simple dataset 59 | GEOBOUNDARY =[0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786] 60 | NORMALIZE_INPUT = True 61 | 62 | # Running specific params 63 | USE_CPU_ONLY = False 64 | EVAL_MODEL = None 65 | -------------------------------------------------------------------------------- /Tandem/parameters.py: -------------------------------------------------------------------------------- 1 | """ 2 | Hyper-parameters of the Tandem model 3 | """ 4 | # Define which data set you are using 5 | #DATA_SET = 'Yang_sim' 6 | #DATA_SET = 'Chen' 7 | DATA_SET = 'Peurifoy' 8 | 9 | TEST_RATIO = 0.2 10 | 11 | # Model Architecture parameters 12 | LOAD_FORWARD_CKPT_DIR = None 13 | """ 14 | #LINEAR_F = [4, 500, 500, 500, 1] 15 | LINEAR_F = [8, 1000, 1000, 1000, 1000, 150] 16 | CONV_OUT_CHANNEL_F = [4, 4, 4] 17 | CONV_KERNEL_SIZE_F = [8, 5, 5] 18 | CONV_STRIDE_F = [2, 1, 1] 19 | 20 | LINEAR_B = [150, 1000, 1000, 1000, 1000, 1000, 8] 21 | CONV_OUT_CHANNEL_B = [4, 4, 4] 22 | CONV_KERNEL_SIZE_B = [5, 5, 8] 23 | CONV_STRIDE_B = [1, 1, 2] 24 | 25 | """ 26 | # Model Architectural Params for gaussian mixture dataset 27 | LINEAR_F = [8, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 1700, 201] 28 | #LINEAR_F = [3, 500, 500, 500, 500, 500, 256] 29 | CONV_OUT_CHANNEL_F = [] 30 | CONV_KERNEL_SIZE_F = [] 31 | CONV_STRIDE_F = [] 32 | #CONV_OUT_CHANNEL_F = [4,4,4] 33 | #CONV_KERNEL_SIZE_F = [4,3,3] 34 | #CONV_STRIDE_F = [2,1,1] 35 | 36 | #LINEAR_B = [2, 500, 500, 500, 500, 500, 3] 37 | LINEAR_B = [201, 500, 500, 500, 500, 500, 3] 38 | CONV_OUT_CHANNEL_B = [] 39 | CONV_KERNEL_SIZE_B = [] 40 | CONV_STRIDE_B = [] 41 | 42 | 43 | # Optimizer parameters 44 | OPTIM = "Adam" 45 | REG_SCALE = 5e-4 46 | BATCH_SIZE = 1024 47 | EVAL_BATCH_SIZE = 1024 48 | EVAL_STEP = 50 49 | TRAIN_STEP = 300 50 | VERB_STEP = 20 51 | LEARN_RATE = 1e-4 52 | LR_DECAY_RATE = 0.8 53 | STOP_THRESHOLD = -1 # -1 means dont stop 54 | 55 | # Running specific parameter 56 | USE_CPU_ONLY = False 57 | DETAIL_TRAIN_LOSS_FORWARD = True 58 | 59 | # Data-specific parameters# Data specific Params 60 | X_RANGE = [i for i in range(2, 16 )] 61 | Y_RANGE = [i for i in range(16 , 2017 )] # Artificial Meta-material dataset 62 | FORCE_RUN = True 63 | MODEL_NAME = None 64 | DATA_DIR = '/home/sr365/MM_Bench/Data/' # All simulated simple dataset 65 | GEOBOUNDARY =[0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786] 66 | NORMALIZE_INPUT = True 67 | 68 | EVAL_MODEL = None 69 | -------------------------------------------------------------------------------- /MDN/model_maker.py: -------------------------------------------------------------------------------- 1 | """ 2 | This is the module where the model is defined. It uses the nn.Module as backbone to create the network structure 3 | """ 4 | # Own modules 5 | 6 | # Built in 7 | import math 8 | # Libs 9 | import numpy as np 10 | 11 | # Pytorch module 12 | import torch.nn as nn 13 | import torch.nn.functional as F 14 | import torch 15 | from torch import pow, add, mul, div, sqrt 16 | import mdn 17 | 18 | 19 | class MDN(nn.Module): 20 | def __init__(self, flags): 21 | super(MDN, self).__init__() 22 | # Linear Layer and Batch_norm Layer definitions here 23 | # self.linears = nn.ModuleList([]) 24 | # self.bn_linears = nn.ModuleList([]) 25 | # for ind, fc_num in enumerate(flags.linear[0:-2]): # Excluding the last one as we need intervals 26 | # self.linears.append(nn.Linear(fc_num, flags.linear[ind + 1])) 27 | # self.bn_linears.append(nn.BatchNorm1d(flags.linear[ind + 1])) 28 | 29 | # The mixture density network uses 3 hyper-parameters for initialization (#in_features, #out_features, #Guassians) 30 | self.mdn = mdn.MDN(flags) 31 | 32 | def forward(self, G): 33 | """ 34 | The forward function which defines how the network is connected 35 | :param G: The input geometry (Since this is a forward network) 36 | :return: S: The 300 dimension spectra 37 | """ 38 | out = G # initialize the out 39 | # For the linear part 40 | for ind, (fc, bn) in enumerate(zip(self.linears, self.bn_linears)): 41 | if ind != len(self.linears) - 1: 42 | out = F.relu(bn(fc(out))) # ReLU + BN + Linear 43 | #out = F.relu(fc(out)) # ReLU + BN + Linear 44 | else: 45 | out = F.relu(fc(out)) 46 | 47 | # The mixture density network outputs 3 values (Pi is a multinomial distribution of the Gaussians. Sigma 48 | # is the standard deviation of each Gaussian. Mu is the mean of each 49 | # Gaussian.) 50 | pi, sigma, mu = self.mdn(out) 51 | return pi, sigma, mu 52 | -------------------------------------------------------------------------------- /utils/chen_batch_predict.py: -------------------------------------------------------------------------------- 1 | from utils.helper_functions import simulator 2 | from multiprocessing import Pool 3 | from utils.evaluation_helper import plotMSELossDistrib 4 | import numpy as np 5 | import os 6 | import pandas as pd 7 | 8 | # This is the script for doing batch evaluation 9 | num_cpu = 40 10 | def eval_chen_for_file(filename): 11 | # Read the Xpred file 12 | Xpred = pd.read_csv(filename, sep=' ', header=None).values 13 | # Run the simulator 14 | Ypred = simulator('Chen', Xpred) 15 | # Save the Ypred into the same folder with name change 16 | with open(filename.replace('Xpred','Ypred'), 'a') as fyp: 17 | np.savetxt(fyp, Ypred) 18 | 19 | def eval_whole_folder(folder): 20 | """ 21 | Run the eval chen for file in a loop 22 | """ 23 | try: 24 | pool = Pool(num_cpu) 25 | args_list = [] 26 | for filename in os.listdir(folder): 27 | if not ('Chen' in filename and 'Xpred' in filename): 28 | continue 29 | args_list.append((os.path.join(folder, filename),)) 30 | print((args_list)) 31 | print(len(args_list)) 32 | pool.starmap(eval_chen_for_file, args_list) 33 | finally: 34 | pool.close() 35 | pool.join() 36 | 37 | def plot_MSE(folder): 38 | """ 39 | Plot the MSE plots for the simulated pairs of MSE 40 | """ 41 | for file in os.listdir(folder): 42 | if not ('Chen' in file and 'Xpred' in file): 43 | continue 44 | Xpred_file = os.path.join(folder, file) 45 | Ypred_file = Xpred_file.replace('Xpred','Ypred') 46 | Ytruth_file = Ypred_file.replace('Ypred','Ytruth') 47 | save_dir = '/'.join(Xpred_file.split('/')[:-1]) 48 | print(save_dir) 49 | plotMSELossDistrib(Ypred_file, Ytruth_file, save_dir=save_dir) 50 | 51 | if __name__ == '__main__': 52 | # The folder to work 53 | # folder_list = ['cINN'] 54 | #folder_list = ['MDN','INN','VAE','NN'] 55 | folder_list = ['VAE','Tandem','NN','NA','GA','INN','MDN','cINN'] 56 | for folders in folder_list: 57 | folder = '../mm_bench_multi_eval/{}/Chen'.format(folders) 58 | # Run simulator for the whole folder 59 | eval_whole_folder(folder) 60 | 61 | #plot_MSE(folder) 62 | -------------------------------------------------------------------------------- /NA/train.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file serves as a training interface for training the network 3 | """ 4 | # Built in 5 | import glob 6 | import os 7 | import shutil 8 | import random 9 | import sys 10 | sys.path.append('../utils/') 11 | 12 | # Torch 13 | 14 | # Own 15 | import flag_reader 16 | from utils import data_reader 17 | from class_wrapper import Network 18 | from model_maker import NA 19 | from utils.helper_functions import put_param_into_folder, write_flags_and_BVE 20 | 21 | def training_from_flag(flags): 22 | """ 23 | Training interface. 1. Read data 2. initialize network 3. train network 4. record flags 24 | :param flag: The training flags read from command line or parameter.py 25 | :return: None 26 | """ 27 | # Get the data 28 | train_loader, test_loader = data_reader.read_data(flags) 29 | print("Making network now") 30 | 31 | # Make Network 32 | ntwk = Network(NA, flags, train_loader, test_loader) 33 | 34 | # Training process 35 | print("Start training now...") 36 | ntwk.train() 37 | 38 | # Do the house keeping, write the parameters and put into folder, also use pickle to save the flags obejct 39 | write_flags_and_BVE(flags, ntwk.best_validation_loss, ntwk.ckpt_dir) 40 | 41 | 42 | def retrain_different_dataset(index): 43 | """ 44 | This function is to evaluate all different datasets in the model with one function call 45 | """ 46 | from utils.helper_functions import load_flags 47 | data_set_list = ["Peurifoy"] 48 | # data_set_list = ["Chen"] 49 | # data_set_list = ["Yang"] 50 | #data_set_list = ["Peurifoy","Chen","Yang_sim"] 51 | for eval_model in data_set_list: 52 | flags = load_flags(os.path.join("models", eval_model+"_best_model")) 53 | flags.model_name = "retrain" + str(index) + eval_model 54 | flags.train_step = 500 55 | flags.test_ratio = 0.2 56 | flags.batch_size = 1000 57 | training_from_flag(flags) 58 | 59 | if __name__ == '__main__': 60 | # Read the parameters to be set 61 | #hyperswipe() 62 | flags = flag_reader.read_flag() #setting the base case 63 | 64 | # training_from_flag(flags) 65 | # Do the retraining for all the data set to get the model 66 | #for i in range(6, 10): 67 | # retrain_different_dataset(i) 68 | retrain_different_dataset(7) 69 | -------------------------------------------------------------------------------- /INN_FrEIA/model_maker.py: -------------------------------------------------------------------------------- 1 | # This is the model maker for the Invertible Neural Network 2 | 3 | # From Built-in 4 | from time import time 5 | 6 | # From libs 7 | import torch 8 | import torch.nn as nn 9 | import torch.optim 10 | import numpy as np 11 | import matplotlib.pyplot as plt 12 | from tqdm import tqdm 13 | 14 | # From FrEIA 15 | from FrEIA.framework import InputNode, OutputNode, Node, ReversibleGraphNet 16 | from FrEIA.modules import GLOWCouplingBlock, PermuteRandom 17 | 18 | 19 | def INN(flags): 20 | """ 21 | The constructor for INN network 22 | :param flags: input flags from configuration 23 | :return: The INN network 24 | """ 25 | 26 | # mid_layer = 512 if 'Chen' in flags.data_set else 1024 27 | # Start from input layer 28 | nodes = [InputNode(flags.dim_tot, name='input')] 29 | # Recursively add the coupling layers and random permutation layer 30 | for i in range(flags.couple_layer_num): 31 | nodes.append(Node(nodes[-1], GLOWCouplingBlock, 32 | {'subnet_constructor': subnet_fc, 33 | 'clamp': 2.0}, 34 | name='coupling_{}'.format(i))) 35 | nodes.append(Node(nodes[-1], PermuteRandom, {'seed': i}, name='permute_{}'.format(i))) 36 | # Attach the output Node 37 | nodes.append(OutputNode(nodes[-1], name='output')) 38 | print("The nodes are:", nodes) 39 | # Return the 40 | return ReversibleGraphNet(nodes, verbose=True) 41 | 42 | ########## 43 | # Subnet # 44 | ########## 45 | 46 | def subnet_fc(c_in, c_out): 47 | if c_in > 120 and c_in < 150: 48 | mid_layer = 512 49 | print('INN Chen') 50 | print('cin = ', c_in) 51 | else: 52 | mid_layer = 1024 53 | print('INN Peurifoy or Yang') 54 | print('cin = ', c_in) 55 | return nn.Sequential(nn.Linear(c_in, mid_layer), nn.ReLU(), 56 | nn.Linear(mid_layer,mid_layer),nn.ReLU(), 57 | nn.Linear(mid_layer, c_out)) 58 | #def subnet_fc(c_in, c_out): 59 | # #Original version of internal layer 60 | # return nn.Sequential(nn.Linear(c_in, 160), nn.ReLU(), 61 | # nn.Linear(160, 160), nn.ReLU(), 62 | # nn.Linear(160, 160), nn.ReLU(), 63 | # nn.Linear(160, c_out)) 64 | -------------------------------------------------------------------------------- /utils/step_function.py: -------------------------------------------------------------------------------- 1 | # This function is to generate step function for the testing of H1 condition 2 | 3 | import numpy as np 4 | import os 5 | import matplotlib.pyplot as plt 6 | 7 | dataset_list = ['Yang','Peurifoy','Chen'] 8 | x_dim_dict = {'Yang': 14, 'Peurifoy': 3, 'Chen': 5 } 9 | y_dim_dict = {'Yang': 2000, 'Peurifoy': 201, 'Chen': 256 } 10 | 11 | 12 | def get_step_function(dataset, orientation='s', amplitude=1, step_pos=0.5): 13 | """ 14 | The main function for outputting a step function given arguments 15 | :param dataset: The dataset to do the step function, which controls the length of y vector 16 | :param orientation: The orientation of the step function, either s or z 17 | :param amplitude: The high point of the step function 18 | :param step_pos: The position of the transition, this is a number between 0 to 1 19 | """ 20 | y = np.zeros([y_dim_dict[dataset],]) 21 | if orientation == 's': 22 | y[int(len(y)*step_pos):] = amplitude 23 | elif orientation == 'z': 24 | y[:int(len(y)*step_pos)] = amplitude 25 | else: 26 | print('orientation can only be z or s, aborting!') 27 | quit() 28 | return y 29 | 30 | 31 | def generate_step_functions(): 32 | """ 33 | The funciton for calling the step function in loops and store them in a file 34 | """ 35 | for dataset in dataset_list: 36 | y_tot = [] 37 | for orientation in ['s','z']: 38 | for amplitude in [0.1*j for j in range(1, 11)]: 39 | for step_pos in [0.1*k for k in range(1, 10)]: 40 | y = get_step_function(dataset=dataset, orientation=orientation, amplitude=amplitude, step_pos=step_pos) 41 | y_tot.append(y) 42 | # print('len of y_tot = ', len(y_tot)) 43 | y_tot = np.array(y_tot) 44 | print('shape of np version = ', np.shape(y_tot)) 45 | save_dir = '/home/sr365/MM_Bench/Data/step_func/' 46 | if not os.path.isdir(save_dir): 47 | os.makedirs(save_dir) 48 | np.savetxt(os.path.join(save_dir ,dataset+'step_function.txt'), y_tot) 49 | 50 | 51 | if __name__ == '__main__': 52 | generate_step_functions() 53 | """ 54 | f = plt.figure() 55 | y = get_step_function('Yang', 's',0.7, 0.2) 56 | plt.plot(y) 57 | plt.xlabel('frequency') 58 | plt.ylabel('R') 59 | plt.title('step function') 60 | plt.savefig('step_funciton.png') 61 | """ -------------------------------------------------------------------------------- /NA/parameters.py: -------------------------------------------------------------------------------- 1 | """ 2 | Params for Back propagation model 3 | """ 4 | # Define which data set you are using 5 | #DATA_SET = 'Yang_sim' 6 | from math import inf 7 | 8 | 9 | DATA_SET = 'Chen' 10 | #DATA_SET = 'Peurifoy' 11 | #DATA_SET = 'Omar' 12 | TEST_RATIO = 0.2 13 | 14 | # Model Architectural Params for meta_material data Set 15 | USE_LORENTZ = False 16 | LINEAR = [5, 500, 500, 256] 17 | #LINEAR = [10, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 1001] 18 | CONV_OUT_CHANNEL = [] 19 | CONV_KERNEL_SIZE = [] 20 | CONV_STRIDE = [] 21 | 22 | 23 | # MD loss related params 24 | MD_COEFF = 5e-5 25 | MD_RADIUS = 0.2 26 | MD_START = -inf 27 | MD_END = inf 28 | 29 | # Model Architectural Params for Yang dataset 30 | #LINEAR = [4, 500, 500, 500, 500, 1] # Dimension of data set cross check with data generator 31 | #CONV_OUT_CHANNEL = [4, 4, 4] 32 | #CONV_KERNEL_SIZE = [3, 3, 5] 33 | #CONV_STRIDE = [2, 1, 1] 34 | 35 | 36 | # Optimizer Params 37 | OPTIM = "Adam" 38 | REG_SCALE = 5e-3 39 | BATCH_SIZE = 1024 40 | EVAL_BATCH_SIZE = 2048 41 | EVAL_STEP = 20 42 | TRAIN_STEP = 300 43 | BACKPROP_STEP = 300 44 | LEARN_RATE = 1e-3 45 | # DECAY_STEP = 25000 # This is for step decay, however we are using dynamic decaying 46 | LR_DECAY_RATE = 0.8 47 | STOP_THRESHOLD = 1e-9 48 | 49 | # Data specific Params 50 | X_RANGE = [i for i in range(2, 16 )] 51 | #Y_RANGE = [i for i in range(10 , 2011 )] # Real Meta-material dataset range 52 | Y_RANGE = [i for i in range(16 , 2017 )] # Artificial Meta-material dataset 53 | FORCE_RUN = True 54 | MODEL_NAME = None 55 | DATA_DIR = '../Data' # All simulated simple dataset 56 | #DATA_DIR = '/work/sr365/' # real Meta-material dataset 57 | #DATA_DIR = '/work/sr365/NN_based_MM_data/' # Artificial Meta-material dataset 58 | #DATA_DIR = '/home/omar/PycharmProjects/github/idlm_Pytorch-master/forward/' 59 | GEOBOUNDARY =[0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786] 60 | NORMALIZE_INPUT = True 61 | 62 | # Running specific 63 | USE_CPU_ONLY = False 64 | #EVAL_MODEL = "sine_wavereg2e-05trail_0_forward_swipe9" 65 | EVAL_MODEL = "mm" 66 | #EVAL_MODEL = "robotic_armreg0.0005trail_0_backward_complexity_swipe_layer500_num6" 67 | #EVAL_MODEL = "ballisticsreg0.0005trail_0_complexity_swipe_layer500_num5" 68 | #EVAL_MODEL = "meta_materialreg2e-05trail_0_forward_swipe6" 69 | #EVAL_MODEL = "20200506_104444" 70 | -------------------------------------------------------------------------------- /inverse/parameters.py: -------------------------------------------------------------------------------- 1 | """ 2 | Params for Back propagation model 3 | """ 4 | # Define which data set you are using 5 | #DATA_SET = 'Yang_sim' 6 | from math import inf 7 | 8 | 9 | DATA_SET = 'Chen' 10 | #DATA_SET = 'Peurifoy' 11 | #DATA_SET = 'Yang_sim' 12 | TEST_RATIO = 0.2 13 | 14 | # Model Architectural Params for meta_material data Set 15 | USE_LORENTZ = False 16 | LINEAR = [5, 500, 500, 256] 17 | #LINEAR = [10, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 1001] 18 | CONV_OUT_CHANNEL = [] 19 | CONV_KERNEL_SIZE = [] 20 | CONV_STRIDE = [] 21 | 22 | 23 | # MD loss related params 24 | MD_COEFF = 5e-5 25 | MD_RADIUS = 0.2 26 | MD_START = -inf 27 | MD_END = inf 28 | 29 | # Model Architectural Params for Yang dataset 30 | #LINEAR = [4, 500, 500, 500, 500, 1] # Dimension of data set cross check with data generator 31 | #CONV_OUT_CHANNEL = [4, 4, 4] 32 | #CONV_KERNEL_SIZE = [3, 3, 5] 33 | #CONV_STRIDE = [2, 1, 1] 34 | 35 | 36 | # Optimizer Params 37 | OPTIM = "Adam" 38 | REG_SCALE = 5e-3 39 | BATCH_SIZE = 1024 40 | EVAL_BATCH_SIZE = 2048 41 | EVAL_STEP = 20 42 | TRAIN_STEP = 1000 43 | BACKPROP_STEP = 300 44 | LEARN_RATE = 1e-3 45 | # DECAY_STEP = 25000 # This is for step decay, however we are using dynamic decaying 46 | LR_DECAY_RATE = 0.8 47 | STOP_THRESHOLD = 1e-9 48 | 49 | # Data specific Params 50 | X_RANGE = [i for i in range(2, 16 )] 51 | #Y_RANGE = [i for i in range(10 , 2011 )] # Real Meta-material dataset range 52 | Y_RANGE = [i for i in range(16 , 2017 )] # Artificial Meta-material dataset 53 | FORCE_RUN = True 54 | MODEL_NAME = None 55 | DATA_DIR = '../Data' # All simulated simple dataset 56 | #DATA_DIR = '/work/sr365/' # real Meta-material dataset 57 | #DATA_DIR = '/work/sr365/NN_based_MM_data/' # Artificial Meta-material dataset 58 | #DATA_DIR = '/home/omar/PycharmProjects/github/idlm_Pytorch-master/forward/' 59 | GEOBOUNDARY =[0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786] 60 | NORMALIZE_INPUT = True 61 | 62 | # Running specific 63 | USE_CPU_ONLY = False 64 | #EVAL_MODEL = "sine_wavereg2e-05trail_0_forward_swipe9" 65 | EVAL_MODEL = "mm" 66 | #EVAL_MODEL = "robotic_armreg0.0005trail_0_backward_complexity_swipe_layer500_num6" 67 | #EVAL_MODEL = "ballisticsreg0.0005trail_0_complexity_swipe_layer500_num5" 68 | #EVAL_MODEL = "meta_materialreg2e-05trail_0_forward_swipe6" 69 | #EVAL_MODEL = "20200506_104444" 70 | -------------------------------------------------------------------------------- /utils/peurifoy_batch_predict.py: -------------------------------------------------------------------------------- 1 | from utils.helper_functions import simulator 2 | from multiprocessing import Pool 3 | from utils.evaluation_helper import plotMSELossDistrib 4 | import numpy as np 5 | import os 6 | import pandas as pd 7 | 8 | # This is the script for doing batch evaluation 9 | num_cpu = 10 10 | def eval_peurifoy_for_file(filename): 11 | # Read the Xpred file 12 | Xpred = pd.read_csv(filename, sep=' ', header=None).values 13 | # Run the simulator 14 | Ypred = simulator('Peurifoy', Xpred) 15 | # Save the Ypred into the same folder with name change 16 | with open(filename.replace('Xpred','Ypred'), 'a') as fyp: 17 | np.savetxt(fyp, Ypred) 18 | 19 | def eval_whole_folder(folder): 20 | """ 21 | Run the eval peurifoy for file in a loop 22 | """ 23 | try: 24 | pool = Pool(num_cpu) 25 | args_list = [] 26 | for filename in os.listdir(folder): 27 | if not ('Peurifoy' in filename and 'Xpred' in filename): 28 | continue 29 | args_list.append((os.path.join(folder, filename),)) 30 | print((args_list)) 31 | print(len(args_list)) 32 | pool.starmap(eval_peurifoy_for_file, args_list) 33 | finally: 34 | pool.close() 35 | pool.join() 36 | 37 | def plot_MSE(folder): 38 | """ 39 | Plot the MSE plots for the simulated pairs of MSE 40 | """ 41 | for file in os.listdir(folder): 42 | if not ('Peurifoy' in file and 'Xpred' in file): 43 | continue 44 | Xpred_file = os.path.join(folder, file) 45 | Ypred_file = Xpred_file.replace('Xpred','Ypred') 46 | Ytruth_file = Ypred_file.replace('Ypred','Ytruth') 47 | save_dir = '/'.join(Xpred_file.split('/')[:-1]) 48 | print(save_dir) 49 | plotMSELossDistrib(Ypred_file, Ytruth_file, save_dir=save_dir) 50 | 51 | if __name__ == '__main__': 52 | # Tandem and inverse only need to do the evaluation once 53 | #eval_whole_folder('../inverse/data') 54 | #plot_MSE('../inverse/data') 55 | #eval_whole_folder('../Tandem/data') 56 | #plot_MSE('../Tandem/data') 57 | #quit() 58 | # For the multi_T evaluation 59 | folder_list = ['VAE','Tandem','NN','NA','GA','INN','MDN','cINN'] 60 | #folder_list = ['GA'] 61 | # folder_list = ['MDN','INN','VAE','cINN'] 62 | for folders in folder_list: 63 | folder = '../mm_bench_multi_eval/{}/Peurifoy'.format(folders) 64 | # Run simulator for the whole folder 65 | eval_whole_folder(folder) 66 | 67 | #plot_MSE(folder) 68 | -------------------------------------------------------------------------------- /INN_FrEIA/parameters.py: -------------------------------------------------------------------------------- 1 | """ 2 | The parameter file storing the parameters for INN Model 3 | """ 4 | 5 | # Define which data set you are using 6 | #DATA_SET = 'Yang_sim' 7 | #DATA_SET = 'Chen' 8 | DATA_SET = 'Peurifoy' 9 | 10 | TEST_RATIO = 0.2 11 | 12 | # Architectural Params 13 | """ 14 | # Chen 15 | DIM_X = 5 16 | DIM_Y = 256 17 | DIM_Z = 1 18 | DIM_TOT = 300 19 | """ 20 | # Peurifoy 21 | DIM_X = 8 22 | DIM_Y = 201 23 | DIM_Z = 1 24 | DIM_TOT = 9 25 | """ 26 | # Yang 27 | DIM_X = 14 28 | DIM_Y = 2000 29 | DIM_Z = 1 30 | DIM_TOT = 4 31 | """ 32 | 33 | # Architectural Params 34 | COUPLE_LAYER_NUM = 6 35 | DIM_SPEC = None 36 | SUBNET_LINEAR = [] # Linear units for Subnet FC layer 37 | #LINEAR_SE = [150, 150, 150, 150, DIM_Y] # Linear units for spectra encoder 38 | LINEAR_SE = [] # Linear units for spectra encoder 39 | CONV_OUT_CHANNEL_SE = [] 40 | CONV_KERNEL_SIZE_SE = [] 41 | CONV_STRIDE_SE = [] 42 | #CONV_OUT_CHANNEL_SE = [4, 4, 4] 43 | #CONV_KERNEL_SIZE_SE = [5, 5, 8] 44 | #CONV_STRIDE_SE = [1, 1, 2] 45 | 46 | # Loss ratio 47 | LAMBDA_MSE = 0.1 # The Loss factor of the MSE loss (reconstruction loss) 48 | LAMBDA_Z = 300. # The Loss factor of the latent dimension (converging to normal distribution) 49 | LAMBDA_REV = 400. # The Loss factor of the reverse transformation (let x converge to input distribution) 50 | ZEROS_NOISE_SCALE = 5e-2 # The noise scale to add to 51 | Y_NOISE_SCALE = 1e-2 52 | 53 | 54 | # Optimization params 55 | OPTIM = "Adam" 56 | REG_SCALE = 5e-3 57 | BATCH_SIZE = 1024 58 | EVAL_BATCH_SIZE = 4096 59 | EVAL_STEP = 20 60 | GRAD_CLAMP = 15 61 | TRAIN_STEP = 500 62 | VERB_STEP = 50 63 | LEARN_RATE = 1e-3 64 | # DECAY_STEP = 25000 # This is for step decay, however we are using dynamic decaying 65 | LR_DECAY_RATE = 0.9 66 | STOP_THRESHOLD = -float('inf') 67 | CKPT_DIR = 'models/' 68 | 69 | # Data specific params 70 | X_RANGE = [i for i in range(2, 10 )] 71 | #Y_RANGE = [i for i in range(10 , 2011 )] # Real Meta-material dataset range 72 | Y_RANGE = [i for i in range(10 , 310 )] # Artificial Meta-material dataset 73 | FORCE_RUN = True 74 | MODEL_NAME = None 75 | DATA_DIR = '/home/sr365/MM_Bench/Data/' # All simulated simple dataset 76 | GEOBOUNDARY =[0.3, 0.6, 1, 1.5, 0.1, 0.2, -0.786, 0.786] 77 | NORMALIZE_INPUT = True 78 | 79 | # Running specific params 80 | USE_CPU_ONLY = False 81 | EVAL_MODEL = None 82 | -------------------------------------------------------------------------------- /cINN/model_maker.py: -------------------------------------------------------------------------------- 1 | # This is the model maker for the Invertible Neural Network 2 | 3 | # From Built-in 4 | from time import time 5 | 6 | # From libs 7 | import torch 8 | import torch.nn as nn 9 | import torch.optim 10 | import numpy as np 11 | import matplotlib.pyplot as plt 12 | from tqdm import tqdm 13 | 14 | # From FrEIA 15 | from FrEIA.framework import InputNode, OutputNode, Node, ReversibleGraphNet, ConditionNode 16 | from FrEIA.modules import GLOWCouplingBlock, PermuteRandom 17 | 18 | 19 | def cINN(flags): 20 | """ 21 | The constructor for INN network 22 | :param flags: input flags from configuration 23 | :return: The INN network 24 | """ 25 | # Set up the conditional node (y) 26 | cond_node = ConditionNode(flags.dim_y) 27 | # Start from input layer 28 | nodes = [InputNode(flags.dim_x, name='input')] 29 | # Recursively add the coupling layers and random permutation layer 30 | for i in range(flags.couple_layer_num): 31 | nodes.append(Node(nodes[-1], GLOWCouplingBlock, 32 | {'subnet_constructor': subnet_fc, 33 | 'clamp': 2.0}, 34 | conditions=cond_node, 35 | name='coupling_{}'.format(i))) 36 | nodes.append(Node(nodes[-1], PermuteRandom, {'seed': i}, name='permute_{}'.format(i))) 37 | # Attach the output Node 38 | nodes.append(OutputNode(nodes[-1], name='output')) 39 | nodes.append(cond_node) 40 | print("The nodes are:", nodes) 41 | # Return the 42 | return ReversibleGraphNet(nodes, verbose=True) 43 | 44 | ########## 45 | # Subnet # 46 | ########## 47 | def subnet_fc(c_in, c_out): 48 | if c_in > 240 and c_in < 280: 49 | print('INN Chen') 50 | print('cin = ', c_in) 51 | return nn.Sequential(nn.Linear(c_in, 160), nn.ReLU(), 52 | nn.Linear(160, 160), nn.ReLU(), 53 | nn.Linear(160, 160), nn.ReLU(), 54 | nn.Linear(160, c_out)) 55 | else: 56 | mid_layer = 1024 57 | print('INN Peurifoy or Yang') 58 | print('cin = ', c_in) 59 | return nn.Sequential(nn.Linear(c_in, mid_layer), nn.ReLU(), 60 | nn.Linear(mid_layer,mid_layer),nn.ReLU(), 61 | nn.Linear(mid_layer, c_out)) 62 | 63 | # def subnet_fc(c_in, c_out): 64 | # #Original version of internal layer 65 | # return nn.Sequential(nn.Linear(c_in, 160), nn.ReLU(), 66 | # nn.Linear(160, 160), nn.ReLU(), 67 | # nn.Linear(160, 160), nn.ReLU(), 68 | # nn.Linear(160, c_out)) 69 | -------------------------------------------------------------------------------- /utils/delete.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy 3 | 4 | # This is the program to delete all the duplicate Xtruth Ytruth files generated 5 | #input_dir = '/work/sr365/multi_eval/' # NIPS code version 6 | #input_dir = '/work/sr365/multi_eval/' # NIPS code version 7 | #input_dir = '/work/sr365/NA_compare/' # ICML code version 8 | #input_dir = '/data/users/ben/robotic_stuck/retrain5/' 9 | #input_dir = '/data/users/ben/multi_eval/' 10 | ############### 11 | # QUAD MACHINE# 12 | ############### 13 | #input_dir = '/home/sr365/MM_Bench/GA/temp-dat' 14 | #input_dir = '/home/sr365/mm_bench_multi_eval_Chen_sweep/' 15 | input_dir = '../mm_bench_multi_eval' # quad 16 | #input_dir = '/home/sr365/MM_bench_multi_eval' # quad 17 | #input_dir = '/home/sr365/ICML_exp_cINN_ball/' # For quad 18 | #input_dir = '/home/sr365/ICML_exp/' # quad 19 | #input_dir = '/home/sr365/multi_eval/' # quad 20 | #input_dir = '/data/users/ben/ICML_exp/' # I am Groot! 21 | #input_dir = '/data/users/ben/ICML_exp_mm/' # I am Groot! 22 | #input_dir = '/work/sr365/ICML_mm/' # ICML code version --- MM special 23 | delete_mse_file_mode = False # Deleting the mse file for the forward filtering 24 | 25 | 26 | # For all the architectures 27 | for folders in os.listdir(input_dir): 28 | #print(folders) 29 | if not os.path.isdir(os.path.join(input_dir,folders)): 30 | continue 31 | # For all the datasets inside it 32 | for dataset in os.listdir(os.path.join(input_dir, folders)): 33 | #print(dataset) 34 | if not os.path.isdir(os.path.join(input_dir, folders, dataset)):# or ('test_200' in folders and 'Peur' in dataset): 35 | continue 36 | current_folder = os.path.join(input_dir, folders, dataset) 37 | print("current folder is:", current_folder) 38 | for file in os.listdir(current_folder): 39 | current_file = os.path.join(current_folder, file) 40 | if os.path.getsize(current_file) == 0: 41 | print('deleting file {} due to empty'.format(current_file)) 42 | os.remove(current_file) 43 | elif '_Ytruth_' in file: 44 | if 'ce0.csv' in file or 'NA' in current_folder or 'GA' in current_folder: 45 | os.rename(current_file, os.path.join(current_folder, 'Ytruth.csv')) 46 | else: 47 | os.remove(current_file) 48 | elif '_Xtruth_' in file: 49 | if 'ce0.csv' in file or 'NA' in current_folder or 'GA' in current_folder: 50 | os.rename(current_file, os.path.join(current_folder, 'Xtruth.csv')) 51 | else: 52 | os.remove(current_file) 53 | elif '_Ypred_' in file and (file.endswith(dataset + '.csv') or file.endswith('model.csv')): 54 | os.rename(current_file, os.path.join(current_folder, 'Ypred.csv')) 55 | elif '_Xpred_' in file and (file.endswith(dataset + '.csv') or file.endswith('model.csv')): 56 | os.rename(current_file, os.path.join(current_folder, 'Xpred.csv')) 57 | if delete_mse_file_mode and 'mse_' in file: 58 | os.remove(current_file) 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /MDN/flag_reader.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file serves to hold helper functions that is related to the "Flag" object which contains 3 | all the parameters during training and inference 4 | """ 5 | # Built-in 6 | import argparse 7 | # Libs 8 | 9 | # Own module 10 | from parameters import * 11 | 12 | # Torch 13 | 14 | def read_flag(): 15 | """ 16 | This function is to write the read the flags from a parameter file and put them in formats 17 | :return: flags: a struct where all the input params are stored 18 | """ 19 | parser = argparse.ArgumentParser() 20 | # Data_Set parameter 21 | parser.add_argument('--data-set', default=DATA_SET, type=str, help='which data set you are chosing') 22 | parser.add_argument('--test-ratio', default=TEST_RATIO, type=float, help='the ratio of the test set') 23 | 24 | # Model Architectural Params 25 | parser.add_argument('--num-gaussian', type=int, default=NUM_GAUSSIAN, help='The number of gaussians') 26 | parser.add_argument('--linear', type=list, default=LINEAR, help='The fc layers units') 27 | 28 | # Optimizer Params 29 | parser.add_argument('--optim', default=OPTIM, type=str, help='the type of optimizer that you want to use') 30 | parser.add_argument('--reg-scale', type=float, default=REG_SCALE, help='#scale for regularization of dense layers') 31 | parser.add_argument('--batch-size', default=BATCH_SIZE, type=int, help='batch size (100)') 32 | parser.add_argument('--eval-batch-size', default=EVAL_BATCH_SIZE, type=int, 33 | help='The Batch size for back propagation') 34 | parser.add_argument('--eval-step', default=EVAL_STEP, type=int, help='# steps between evaluations') 35 | parser.add_argument('--train-step', default=TRAIN_STEP, type=int, help='# steps to train on the dataSet') 36 | parser.add_argument('--lr', default=LEARN_RATE, type=float, help='learning rate') 37 | parser.add_argument('--lr-decay-rate', default=LR_DECAY_RATE, type=float, 38 | help='decay learn rate by multiplying this factor') 39 | parser.add_argument('--stop_threshold', default=STOP_THRESHOLD, type=float, 40 | help='The threshold below which training should stop') 41 | # parser.add_argument('--decay-step', default=DECAY_STEP, type=int, 42 | # help='decay learning rate at this number of steps') 43 | 44 | # Data Specific Params 45 | parser.add_argument('--x-range', type=list, default=X_RANGE, help='columns of input parameters') 46 | parser.add_argument('--y-range', type=list, default=Y_RANGE, help='columns of output parameters') 47 | parser.add_argument('--geoboundary', default=GEOBOUNDARY, type=tuple, help='the boundary of the geometric data') 48 | parser.add_argument('--model-name', default=MODEL_NAME, type=str, help='name of the model') 49 | parser.add_argument('--data-dir', default=DATA_DIR, type=str, help='data directory') 50 | parser.add_argument('--normalize-input', default=NORMALIZE_INPUT, type=bool, 51 | help='whether we should normalize the input or not') 52 | 53 | # Running specific 54 | parser.add_argument('--eval-model', default=EVAL_MODEL, type=str, help='the folder name of the model that you want to evaluate') 55 | parser.add_argument('--use-cpu-only', type=bool, default=USE_CPU_ONLY, help='The boolean flag that indicate use CPU only') 56 | flags = parser.parse_args() # This is for command line version of the code 57 | # flags = parser.parse_args(args = [])#This is for jupyter notebook version of the code 58 | # flagsVar = vars(flags) 59 | return flags 60 | -------------------------------------------------------------------------------- /GA/model_maker.py: -------------------------------------------------------------------------------- 1 | """ 2 | This is the module where the model is defined. It uses the nn.Module as backbone to create the network structure 3 | """ 4 | # Own modules 5 | 6 | # Built in 7 | import math 8 | # Libs 9 | import numpy as np 10 | 11 | # Pytorch module 12 | import torch.nn as nn 13 | import torch.nn.functional as F 14 | import torch 15 | from torch import pow, add, mul, div, sqrt 16 | 17 | 18 | class Net(nn.Module): 19 | def __init__(self, flags): 20 | super(Net, self).__init__() 21 | self.bp = False # The flag that the model is backpropagating 22 | # Initialize the geometry_eval field 23 | # self.geometry_eval = torch.randn([flags.eval_batch_size, flags.linear[0]], requires_grad=True) 24 | # Linear Layer and Batch_norm Layer definitions here 25 | self.linears = nn.ModuleList([]) 26 | self.bn_linears = nn.ModuleList([]) 27 | for ind, fc_num in enumerate(flags.linear[0:-1]): # Excluding the last one as we need intervals 28 | self.linears.append(nn.Linear(fc_num, flags.linear[ind + 1])) 29 | self.bn_linears.append(nn.BatchNorm1d(flags.linear[ind + 1])) 30 | 31 | # Conv Layer definitions here 32 | self.convs = nn.ModuleList([]) 33 | in_channel = 1 # Initialize the in_channel number 34 | for ind, (out_channel, kernel_size, stride) in enumerate(zip(flags.conv_out_channel, 35 | flags.conv_kernel_size, 36 | flags.conv_stride)): 37 | if stride == 2: # We want to double the number 38 | pad = int(kernel_size/2 - 1) 39 | elif stride == 1: # We want to keep the number unchanged 40 | pad = int((kernel_size - 1)/2) 41 | else: 42 | Exception("Now only support stride = 1 or 2, contact Ben") 43 | 44 | self.convs.append(nn.ConvTranspose1d(in_channel, out_channel, kernel_size, 45 | stride=stride, padding=pad)) # To make sure L_out double each time 46 | in_channel = out_channel # Update the out_channel 47 | if len(self.convs): # If there are upconvolutions, do the convolution back to single channel 48 | self.convs.append(nn.Conv1d(in_channel, out_channels=1, kernel_size=1, stride=1, padding=0)) 49 | 50 | def forward(self, G): 51 | """ 52 | The forward function which defines how the network is connected 53 | :param G: The input geometry (Since this is a forward network) 54 | :return: S: The 300 dimension spectra 55 | """ 56 | out = G # initialize the out 57 | # For the linear part 58 | for ind, (fc, bn) in enumerate(zip(self.linears, self.bn_linears)): 59 | if ind != len(self.linears) - 1: 60 | out = F.relu(bn(fc(out))) # ReLU + BN + Linear 61 | else: 62 | out = fc(out) # For last layer, no activation function 63 | 64 | out = out.unsqueeze(1) # Add 1 dimension to get N,L_in, H 65 | # For the conv part 66 | for ind, conv in enumerate(self.convs): 67 | #print(out.size()) 68 | out = conv(out) 69 | S = out.squeeze(1) 70 | return S 71 | 72 | -------------------------------------------------------------------------------- /NA/model_maker.py: -------------------------------------------------------------------------------- 1 | """ 2 | This is the module where the model is defined. It uses the nn.Module as backbone to create the network structure 3 | """ 4 | # Own modules 5 | 6 | # Built in 7 | import math 8 | # Libs 9 | import numpy as np 10 | 11 | # Pytorch module 12 | import torch.nn as nn 13 | import torch.nn.functional as F 14 | import torch 15 | from torch import pow, add, mul, div, sqrt 16 | 17 | 18 | class NA(nn.Module): 19 | def __init__(self, flags): 20 | super(NA, self).__init__() 21 | self.bp = False # The flag that the model is backpropagating 22 | # Initialize the geometry_eval field 23 | # self.geometry_eval = torch.randn([flags.eval_batch_size, flags.linear[0]], requires_grad=True) 24 | # Linear Layer and Batch_norm Layer definitions here 25 | self.linears = nn.ModuleList([]) 26 | self.bn_linears = nn.ModuleList([]) 27 | for ind, fc_num in enumerate(flags.linear[0:-1]): # Excluding the last one as we need intervals 28 | self.linears.append(nn.Linear(fc_num, flags.linear[ind + 1])) 29 | self.bn_linears.append(nn.BatchNorm1d(flags.linear[ind + 1])) 30 | 31 | # Conv Layer definitions here 32 | self.convs = nn.ModuleList([]) 33 | in_channel = 1 # Initialize the in_channel number 34 | for ind, (out_channel, kernel_size, stride) in enumerate(zip(flags.conv_out_channel, 35 | flags.conv_kernel_size, 36 | flags.conv_stride)): 37 | if stride == 2: # We want to double the number 38 | pad = int(kernel_size/2 - 1) 39 | elif stride == 1: # We want to keep the number unchanged 40 | pad = int((kernel_size - 1)/2) 41 | else: 42 | Exception("Now only support stride = 1 or 2, contact Ben") 43 | 44 | self.convs.append(nn.ConvTranspose1d(in_channel, out_channel, kernel_size, 45 | stride=stride, padding=pad)) # To make sure L_out double each time 46 | in_channel = out_channel # Update the out_channel 47 | if len(self.convs): # If there are upconvolutions, do the convolution back to single channel 48 | self.convs.append(nn.Conv1d(in_channel, out_channels=1, kernel_size=1, stride=1, padding=0)) 49 | 50 | def forward(self, G): 51 | """ 52 | The forward function which defines how the network is connected 53 | :param G: The input geometry (Since this is a forward network) 54 | :return: S: The 300 dimension spectra 55 | """ 56 | out = G # initialize the out 57 | # For the linear part 58 | for ind, (fc, bn) in enumerate(zip(self.linears, self.bn_linears)): 59 | if ind != len(self.linears) - 1: 60 | out = F.relu(bn(fc(out))) # ReLU + BN + Linear 61 | else: 62 | out = fc(out) # For last layer, no activation function 63 | 64 | out = out.unsqueeze(1) # Add 1 dimension to get N,L_in, H 65 | # For the conv part 66 | for ind, conv in enumerate(self.convs): 67 | #print(out.size()) 68 | out = conv(out) 69 | S = out.squeeze(1) 70 | return S 71 | 72 | -------------------------------------------------------------------------------- /inverse/model_maker.py: -------------------------------------------------------------------------------- 1 | """ 2 | This is the module where the model is defined. It uses the nn.Module as backbone to create the network structure 3 | """ 4 | # Own modules 5 | 6 | # Built in 7 | import math 8 | # Libs 9 | import numpy as np 10 | 11 | # Pytorch module 12 | import torch.nn as nn 13 | import torch.nn.functional as F 14 | import torch 15 | from torch import pow, add, mul, div, sqrt 16 | 17 | 18 | class NA(nn.Module): 19 | def __init__(self, flags): 20 | super(NA, self).__init__() 21 | self.bp = False # The flag that the model is backpropagating 22 | # Initialize the geometry_eval field 23 | # self.geometry_eval = torch.randn([flags.eval_batch_size, flags.linear[0]], requires_grad=True) 24 | # Linear Layer and Batch_norm Layer definitions here 25 | self.linears = nn.ModuleList([]) 26 | self.bn_linears = nn.ModuleList([]) 27 | for ind, fc_num in enumerate(flags.linear[0:-1]): # Excluding the last one as we need intervals 28 | self.linears.append(nn.Linear(fc_num, flags.linear[ind + 1])) 29 | self.bn_linears.append(nn.BatchNorm1d(flags.linear[ind + 1])) 30 | 31 | # Conv Layer definitions here 32 | self.convs = nn.ModuleList([]) 33 | in_channel = 1 # Initialize the in_channel number 34 | for ind, (out_channel, kernel_size, stride) in enumerate(zip(flags.conv_out_channel, 35 | flags.conv_kernel_size, 36 | flags.conv_stride)): 37 | if stride == 2: # We want to double the number 38 | pad = int(kernel_size/2 - 1) 39 | elif stride == 1: # We want to keep the number unchanged 40 | pad = int((kernel_size - 1)/2) 41 | else: 42 | Exception("Now only support stride = 1 or 2, contact Ben") 43 | 44 | self.convs.append(nn.ConvTranspose1d(in_channel, out_channel, kernel_size, 45 | stride=stride, padding=pad)) # To make sure L_out double each time 46 | in_channel = out_channel # Update the out_channel 47 | if len(self.convs): # If there are upconvolutions, do the convolution back to single channel 48 | self.convs.append(nn.Conv1d(in_channel, out_channels=1, kernel_size=1, stride=1, padding=0)) 49 | 50 | def forward(self, G): 51 | """ 52 | The forward function which defines how the network is connected 53 | :param G: The input geometry (Since this is a forward network) 54 | :return: S: The 300 dimension spectra 55 | """ 56 | out = G # initialize the out 57 | # For the linear part 58 | for ind, (fc, bn) in enumerate(zip(self.linears, self.bn_linears)): 59 | if ind != len(self.linears) - 1: 60 | out = F.relu(bn(fc(out))) # ReLU + BN + Linear 61 | else: 62 | out = fc(out) # For last layer, no activation function 63 | 64 | out = out.unsqueeze(1) # Add 1 dimension to get N,L_in, H 65 | # For the conv part 66 | for ind, conv in enumerate(self.convs): 67 | #print(out.size()) 68 | out = conv(out) 69 | S = out.squeeze(1) 70 | return S 71 | 72 | -------------------------------------------------------------------------------- /MDN/mdn_tony_duan.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | from torch.distributions import Normal, OneHotCategorical 4 | 5 | # Implementation adapted from: 6 | # https://github.com/tonyduan/mdn/blob/master/mdn/models.py 7 | 8 | class MDN(nn.Module): 9 | """ 10 | Mixture density network. 11 | [ Bishop, 1994 ] 12 | Parameters 13 | ---------- 14 | dim_in: int; dimensionality of the covariates 15 | dim_out: int; dimensionality of the response variable 16 | n_components: int; number of components in the mixture model 17 | """ 18 | def __init__(self, flags): 19 | super().__init__() 20 | self.pi_network = CategoricalNetwork(flags) 21 | self.normal_network = MixtureDiagNormalNetwork(flags) 22 | 23 | def forward(self, x): 24 | return self.pi_network(x), self.normal_network(x) 25 | 26 | def loss(self, x, y): 27 | pi, normal = self.forward(x) 28 | loglik = normal.log_prob(y.unsqueeze(1).expand_as(normal.loc)) 29 | loglik = torch.sum(loglik, dim=2) 30 | loss = -torch.logsumexp(torch.log(pi.probs) + loglik, dim=1) 31 | return loss 32 | 33 | def sample(self, x): 34 | pi, normal = self.forward(x) 35 | samples = torch.sum(pi.sample().unsqueeze(2) * normal.sample(), dim=1) 36 | return samples 37 | 38 | 39 | class MixtureDiagNormalNetwork(nn.Module): 40 | 41 | def __init__(self, flags): 42 | super().__init__() 43 | in_features, out_features, num_gaussians = flags.linear[0], flags.linear[-1], flags.num_gaussian 44 | self.n_components = num_gaussians 45 | self.mu = nn.ModuleList([]) 46 | for ind, fc_num in enumerate(flags.linear[:-2]): 47 | self.mu.append(nn.Linear(fc_num, flags.linear[ind + 1])) 48 | # self.mu.append(nn.BatchNorm1d(flags.linear[ind + 1])) 49 | self.mu.append(nn.ELU()) 50 | self.mu.append(nn.Linear(flags.linear[-2], 2*out_features*num_gaussians)) 51 | self.network = nn.Sequential(*self.mu) 52 | # Original implementation 53 | # if hidden_dim is None: 54 | # hidden_dim = in_dim 55 | # self.network = nn.Sequential( 56 | # nn.Linear(in_dim, hidden_dim), 57 | # nn.ELU(), 58 | # nn.Linear(hidden_dim, 2 * out_dim * n_components), 59 | # ) 60 | 61 | def forward(self, x): 62 | params = self.network(x) 63 | mean, sd = torch.split(params, params.shape[1] // 2, dim=1) 64 | mean = torch.stack(mean.split(mean.shape[1] // self.n_components, 1)) 65 | sd = torch.stack(sd.split(sd.shape[1] // self.n_components, 1)) 66 | return Normal(mean.transpose(0, 1), torch.exp(sd).transpose(0, 1)) 67 | 68 | class CategoricalNetwork(nn.Module): 69 | 70 | def __init__(self, flags): 71 | super().__init__() 72 | in_features, out_features, num_gaussians = flags.linear[0], flags.linear[-1], flags.num_gaussian 73 | self.pi = nn.ModuleList([]) 74 | for ind, fc_num in enumerate(flags.linear[:-2]): 75 | self.pi.append(nn.Linear(fc_num, flags.linear[ind + 1])) 76 | # self.pi.append(nn.BatchNorm1d(flags.linear[ind + 1])) 77 | self.pi.append(nn.ELU()) 78 | self.pi.append(nn.Linear(flags.linear[-2], num_gaussians)) 79 | self.network = nn.Sequential(*self.pi) 80 | 81 | # Original implementation 82 | # self.network = nn.Sequential( 83 | # nn.Linear(in_dim, hidden_dim), 84 | # nn.ELU(), 85 | # nn.Linear(hidden_dim, out_dim) 86 | # ) 87 | 88 | def forward(self, x): 89 | params = self.network(x) 90 | return OneHotCategorical(logits=params) -------------------------------------------------------------------------------- /INN_FrEIA/train.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file serves as a training interface for training the network 3 | """ 4 | # Built in 5 | import glob 6 | import os 7 | import shutil 8 | 9 | # Torch 10 | import torch 11 | # Own 12 | import flag_reader 13 | from utils import data_reader 14 | from class_wrapper import Network 15 | from model_maker import INN 16 | from utils.helper_functions import put_param_into_folder, write_flags_and_BVE 17 | 18 | def training_from_flag(flags): 19 | """ 20 | Training interface. 1. Read data 2. initialize network 3. train network 4. record flags 21 | :param flag: The training flags read from command line or parameter.py 22 | :return: None 23 | """ 24 | # Get the data 25 | train_loader, test_loader = data_reader.read_data(flags) 26 | print("Making network now") 27 | 28 | # Make Network 29 | ntwk = Network(INN, flags, train_loader, test_loader, ckpt_dir=flags.ckpt_dir) 30 | 31 | print("number of trainable parameters is :") 32 | pytorch_total_params = sum(p.numel() for p in ntwk.model.parameters() if p.requires_grad) 33 | print(pytorch_total_params) 34 | 35 | # Training process 36 | print("Start training now...") 37 | ntwk.train() 38 | 39 | # Do the house keeping, write the parameters and put into folder, also use pickle to save the flags obejct 40 | write_flags_and_BVE(flags, ntwk.best_validation_loss, ntwk.ckpt_dir) 41 | # put_param_into_folder(ntwk.ckpt_dir) 42 | 43 | 44 | def retrain_different_dataset(index): 45 | """ 46 | This function is to evaluate all different datasets in the model with one function call 47 | """ 48 | from utils.helper_functions import load_flags 49 | data_set_list = ["Peurifoy"] 50 | # data_set_list = ["Chen"] 51 | # data_set_list = ["Yang"] 52 | #data_set_list = ["Peurifoy","Chen","Yang_sim"] 53 | for eval_model in data_set_list: 54 | flags = load_flags(os.path.join("models", eval_model+"_best_model")) 55 | flags.model_name = "retrain" + str(index) + eval_model 56 | flags.train_step = 500 57 | flags.test_ratio = 0.2 58 | training_from_flag(flags) 59 | 60 | def hyperswipe(): 61 | """ 62 | This is for doing hyperswiping for the model parameters 63 | """ 64 | dim_pad_list = [10] 65 | lambda_mse_list = [0.0001]#, 0.0001] 66 | dim_z_list = [3,5] 67 | for dim_z in dim_z_list: 68 | for dim_pad in dim_pad_list: 69 | for couple_layer_num in [12, 14, 16]:#range(10, 20): 70 | for lambda_mse in lambda_mse_list: 71 | flags = flag_reader.read_flag() #setting the base case 72 | flags.couple_layer_num = couple_layer_num 73 | flags.lambda_mse = lambda_mse 74 | flags.dim_z = dim_z 75 | flags.dim_tot = flags.dim_y + flags.dim_z + dim_pad 76 | #print("currently running flag", flags) 77 | print(flags.data_set) 78 | flags.model_name = flags.data_set + '_mid_layer_1024_couple_layer_num' + str(couple_layer_num) + 'labmda_mse' + str(lambda_mse) + '_lr_' + str(flags.lr) + '_dim_pad_' + str(dim_pad) + '_dim_z_' + str(flags.dim_z) 79 | training_from_flag(flags) 80 | 81 | if __name__ == '__main__': 82 | # Read the parameters to be set 83 | #flags = flag_reader.read_flag() 84 | 85 | # Call the train from flag function 86 | #training_from_flag(flags) 87 | # hyperswipe() 88 | # Do the retraining for all the data set to get the training for reproducibility 89 | #for i in range(4, 5): 90 | #for i in range(5, 10): 91 | # retrain_different_dataset(i) 92 | retrain_different_dataset(8) 93 | -------------------------------------------------------------------------------- /Tandem/train.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file serves as a training interface for training the network 3 | """ 4 | # Built in 5 | import glob 6 | import os 7 | import shutil 8 | import sys 9 | sys.path.append('../utils/') 10 | 11 | # Torch 12 | 13 | # Own 14 | import flag_reader 15 | from utils import data_reader 16 | from class_wrapper import Network 17 | from model_maker import Forward, Backward 18 | from utils.helper_functions import put_param_into_folder, write_flags_and_BVE 19 | 20 | def training_from_flag(flags): 21 | """ 22 | Training interface. 1. Read data 2. initialize network 3. train network 4. record flags 23 | :param flag: The training flags read from command line or parameter.py 24 | :return: None 25 | """ 26 | # Get the data 27 | train_loader, test_loader = data_reader.read_data(flags) 28 | 29 | print("Making network now") 30 | 31 | # Make Network 32 | ntwk = Network(Forward, Backward, flags, train_loader, test_loader) 33 | 34 | # Training process 35 | print("Start training now...") 36 | ntwk.train() 37 | 38 | # Do the house keeping, write the parameters and put into folder, also use pickle to save the flags obejct 39 | write_flags_and_BVE(flags, ntwk.best_validation_loss, ntwk.ckpt_dir, forward_best_loss=ntwk.best_forward_validation_loss) 40 | # put_param_into_folder(ntwk.ckpt_dir) 41 | 42 | 43 | def retrain_different_dataset(index): 44 | """ 45 | This function is to evaluate all different datasets in the model with one function call 46 | """ 47 | from utils.helper_functions import load_flags 48 | data_set_list = ["Peurifoy"] 49 | # data_set_list = ["Chen"] 50 | # data_set_list = ["Yang"] 51 | for eval_model in data_set_list: 52 | flags = load_flags(os.path.join("models", eval_model+"_best_model")) 53 | flags.model_name = "retrain" + str(index) + eval_model 54 | flags.geoboundary = [-1,1,-1,1] 55 | flags.batch_size = 1024 56 | flags.train_step = 500 57 | flags.test_ratio = 0.2 58 | training_from_flag(flags) 59 | 60 | def hyperswipe(): 61 | """ 62 | This is for doing hyperswiping for the model parameters 63 | """ 64 | reg_scale_list = [1e-4] 65 | #reg_scale_list = [0, 1e-5, 5e-5, 1e-4, 5e-4, 1e-3] 66 | layer_size_list = [1500] 67 | for reg_scale in reg_scale_list: 68 | for i in range(3): 69 | for layer_num in range(12, 17): 70 | for layer_size in layer_size_list: 71 | flags = flag_reader.read_flag() #setting the base case 72 | # Decoder arch 73 | linear_b = [layer_size for j in range(layer_num)] 74 | linear_b[0] = 201 75 | linear_b[-1] = 8 76 | #flags.conv_out_channel_b = [4, 4, 4] 77 | #flags.conv_kernel_size_b = [3,3,4] 78 | #flags.conv_stride_b = [1,1,2] 79 | flags.linear_b = linear_b 80 | flags.reg_scale = reg_scale 81 | flags.model_name = flags.data_set + '_Backward_no_conv_layer_num_' + str(layer_num) + '_unit_' + str(layer_size) + '_reg_scale_' + str(flags.reg_scale) + '_trail_' + str(i) 82 | #flags.model_name = flags.data_set + '_Backward_conv_444_334_112_layer_num_' + str(layer_num) + '_unit_' + str(layer_size) + '_reg_scale_' + str(flags.reg_scale) + '_trail_' + str(i) 83 | training_from_flag(flags) 84 | 85 | 86 | 87 | if __name__ == '__main__': 88 | # Read the parameters to be set 89 | flags = flag_reader.read_flag() 90 | 91 | # hyperswipe() 92 | # Call the train from flag function 93 | #training_from_flag(flags) 94 | 95 | # Do the retraining for all the data set to get the training 96 | #for i in range(5): 97 | for i in range(5, 10): 98 | retrain_different_dataset(i) 99 | -------------------------------------------------------------------------------- /VAE/train.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file serves as a training interface for training the network 3 | """ 4 | # Built in 5 | import glob 6 | import os 7 | import shutil 8 | 9 | # Torch 10 | import torch 11 | # Own 12 | import flag_reader 13 | from utils import data_reader 14 | from class_wrapper import Network 15 | from model_maker import VAE 16 | from utils.helper_functions import put_param_into_folder,write_flags_and_BVE 17 | 18 | def training_from_flag(flags): 19 | """ 20 | Training interface. 1. Read data 2. initialize network 3. train network 4. record flags 21 | :param flag: The training flags read from command line or parameter.py 22 | :return: None 23 | """ 24 | # Get the data 25 | train_loader, test_loader = data_reader.read_data(flags) 26 | print("Making network now") 27 | 28 | # Make Network 29 | ntwk = Network(VAE, flags, train_loader, test_loader) 30 | 31 | # Training process 32 | print("Start training now...") 33 | ntwk.train() 34 | 35 | # Do the house keeping, write the parameters and put into folder, also use pickle to save the flags obejct 36 | write_flags_and_BVE(flags, ntwk.best_validation_loss, ntwk.ckpt_dir) 37 | # put_param_into_folder(ntwk.ckpt_dir) 38 | 39 | 40 | def retrain_different_dataset(index): 41 | """ 42 | This function is to evaluate all different datasets in the model with one function call 43 | """ 44 | from utils.helper_functions import load_flags 45 | data_set_list = ["Peurifoy"] 46 | # data_set_list = ["Chen"] 47 | # data_set_list = ["Yang"] 48 | #data_set_list = ["Peurifoy","Chen","Yang_sim"] 49 | for eval_model in data_set_list: 50 | flags = load_flags(os.path.join("models", eval_model+"_best_model")) 51 | flags.model_name = "retrain" + str(index) + eval_model 52 | flags.train_step = 500 53 | flags.test_ratio = 0.2 54 | training_from_flag(flags) 55 | 56 | def hyperswipe(): 57 | """ 58 | This is for doing hyperswiping for the model parameters 59 | """ 60 | reg_scale_list = [1e-4] 61 | #reg_scale_list = [1e-3, 1e-4, 5e-3] 62 | kl_coeff_list = [0.1]# 63 | #kl_coeff_list = [5e-2, 0.1, 1, 5] 64 | #kl_coeff_list = [5e-2, 0.1, 1, 5] 65 | #kl_coeff_list = [5e-2, 0.1, 1, 5] 66 | layer_size_list = [1000] 67 | dim_z_list = [10, 20] 68 | for kl_coeff in kl_coeff_list: 69 | for layer_num in range(8, 15, 2): 70 | for layer_size in layer_size_list: 71 | for dim_z in dim_z_list: 72 | for reg_scale in reg_scale_list: 73 | flags = flag_reader.read_flag() #setting the base case 74 | flags.reg_scale = reg_scale 75 | flags.dim_z = dim_z 76 | flags.kl_coeff = kl_coeff 77 | # Decoder arch 78 | linear_d = [layer_size for j in range(layer_num)] 79 | linear_d[0] = flags.dim_y + flags.dim_z 80 | linear_d[-1] = flags.dim_x 81 | # Encoder arch 82 | linear_e = [layer_size for j in range(layer_num)] 83 | linear_e[0] = flags.dim_y + flags.dim_x 84 | linear_e[-1] = 2 * flags.dim_z 85 | flags.linear_d = linear_d 86 | flags.linear_e = linear_e 87 | flags.model_name = flags.data_set + '_kl_coeff_'+str(kl_coeff) + '_layer_num_' + str(layer_num) + '_unit_' + str(layer_size) + '_dim_z_' + str(dim_z) + '_reg_scale_' + str(flags.reg_scale) 88 | training_from_flag(flags) 89 | 90 | if __name__ == '__main__': 91 | # torch.manual_seed(1) 92 | # torch.cuda.manual_seed(1) 93 | # Read the parameters to be set 94 | flags = flag_reader.read_flag() 95 | 96 | # hyperswipe() 97 | # Call the train from flag function 98 | #training_from_flag(flags) 99 | 100 | # Do the retraining for all the data set to get the training 101 | for i in range(5, 6): 102 | retrain_different_dataset(i) 103 | 104 | 105 | 106 | 107 | -------------------------------------------------------------------------------- /inverse/train.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file serves as a training interface for training the network 3 | """ 4 | # Built in 5 | import glob 6 | import os 7 | import shutil 8 | import random 9 | import sys 10 | sys.path.append('../utils/') 11 | 12 | # Torch 13 | 14 | # Own 15 | import flag_reader 16 | from utils import data_reader 17 | from class_wrapper import Network 18 | from model_maker import NA 19 | from utils.helper_functions import put_param_into_folder, write_flags_and_BVE 20 | 21 | def training_from_flag(flags): 22 | """ 23 | Training interface. 1. Read data 2. initialize network 3. train network 4. record flags 24 | :param flag: The training flags read from command line or parameter.py 25 | :return: None 26 | """ 27 | # Get the data 28 | train_loader, test_loader = data_reader.read_data(flags) 29 | print("Making network now") 30 | 31 | # Make Network 32 | ntwk = Network(NA, flags, train_loader, test_loader) 33 | 34 | # Training process 35 | print("Start training now...") 36 | ntwk.train() 37 | 38 | # Do the house keeping, write the parameters and put into folder, also use pickle to save the flags obejct 39 | write_flags_and_BVE(flags, ntwk.best_validation_loss, ntwk.ckpt_dir) 40 | 41 | 42 | def retrain_different_dataset(index): 43 | """ 44 | This function is to evaluate all different datasets in the model with one function call 45 | """ 46 | from utils.helper_functions import load_flags 47 | #data_set_list = ["Peurifoy"] 48 | #data_set_list = ["Chen"] 49 | data_set_list = ["Yang"] 50 | #data_set_list = ["Peurifoy","Chen","Yang_sim"] 51 | for eval_model in data_set_list: 52 | flags = load_flags(os.path.join("models", eval_model+"_best_model")) 53 | flags.model_name = "retrain" + str(index) + eval_model 54 | flags.train_step = 500 55 | flags.test_ratio = 0.2 56 | training_from_flag(flags) 57 | 58 | def hyperswipe(): 59 | """ 60 | This is for doing hyperswiping for the model parameters 61 | """ 62 | layer_size_list = [1000, 1500] 63 | #layer_size_list = [1750] 64 | #layer_size_list = [100, 250, 500] 65 | #reg_scale_list = [2e-3, 5e-3, 1e-2] 66 | reg_scale_list = [0] 67 | #layer_num = 7 68 | dataset = 'Chen'# 'Peurifoy' # ,, 'Yang_sim' # 69 | linear_start = {'Chen': 256, 'Peurifoy':201, 'Yang_sim':2000} 70 | linear_end = {'Chen':5, 'Peurifoy':8, 'Yang_sim':14} 71 | 72 | for reg_scale in reg_scale_list: 73 | for i in range(2): 74 | for layer_num in range(6, 15, 2): 75 | for layer_size in layer_size_list: 76 | flags = flag_reader.read_flag() #setting the base case 77 | flags.data_set = dataset 78 | linear = [layer_size for j in range(layer_num)] #Set the linear units 79 | linear[0] = linear_start[dataset] # The start of linear 80 | linear[-1] = linear_end[dataset] # The end of linear 81 | flags.lr = 1e-4 82 | flags.linear = linear 83 | flags.reg_scale = reg_scale 84 | #flags.conv_kernel_size = [3, 3, 5] 85 | #flags.conv_channel_out = [4, 4, 4] 86 | #flags.conv_stride = [1, 1, 1] 87 | flags.model_name = flags.data_set + 'no_conv_' + str(layer_size) + '_size_' + str(layer_size) + '_num_' + str(layer_num) + '_lr_' + str(flags.lr) + 'reg_scale_' + str(reg_scale) + 'trail_' + str(i) 88 | #flags.model_name = flags.data_set + 'conv_444_335_111_linear_' + str(layer_size) + '_num_' + str(layer_num) + '_lr_' + str(flags.lr) + 'reg_scale_' + str(reg_scale) + 'trail_' + str(i) 89 | training_from_flag(flags) 90 | #except: 91 | # print("Probably a bad configuration") 92 | 93 | #dirs = os.listdir(spec_dir) 94 | 95 | 96 | if __name__ == '__main__': 97 | # Read the parameters to be set 98 | # hyperswipe() 99 | # flags = flag_reader.read_flag() #setting the base case 100 | 101 | # training_from_flag(flags) 102 | # Do the retraining for all the data set to get the training 103 | for i in range(10): 104 | retrain_different_dataset(i) 105 | 106 | -------------------------------------------------------------------------------- /utils/evaluation_helper.py: -------------------------------------------------------------------------------- 1 | """ 2 | This is the helper functions for evaluation purposes 3 | 4 | """ 5 | import numpy as np 6 | from sklearn.metrics import confusion_matrix 7 | import seaborn as sns 8 | import matplotlib.pyplot as plt 9 | import os 10 | import pandas as pd 11 | from utils.helper_functions import simulator 12 | 13 | 14 | def get_test_ratio_helper(flags): 15 | """ 16 | The unified place for getting the test_ratio the same for all methods for the dataset, 17 | This is for easier changing for multi_eval 18 | """ 19 | if flags.data_set == 'Chen': 20 | return 0.5 # 500 in total out of 5k 21 | elif flags.data_set == 'Peurifoy': 22 | return 0.5 23 | #return 0.0625 # 500 in total 24 | elif 'Yang' in flags.data_set: 25 | return 0.5 # 500 in total 26 | else: 27 | print("Your dataset is none of the artificial datasets") 28 | return None 29 | 30 | def compare_truth_pred(pred_file, truth_file, cut_off_outlier_thres=None, quiet_mode=False): 31 | """ 32 | Read truth and pred from csv files, compute their mean-absolute-error and the mean-squared-error 33 | :param pred_file: full path to pred file 34 | :param truth_file: full path to truth file 35 | :return: mae and mse 36 | """ 37 | if isinstance(pred_file, str): # If input is a file name (original set up) 38 | pred = pd.read_csv(pred_file, header=None, sep=' ').values 39 | print(np.shape(pred)) 40 | if np.shape(pred)[1] == 1: 41 | pred = pd.read_csv(pred_file, header=None, sep=',').values 42 | truth = pd.read_csv(truth_file, header=None, sep=' ').values 43 | print(np.shape(truth)) 44 | if np.shape(truth)[1] == 1: 45 | truth = pd.read_csv(truth_file, header=None, sep=',').values 46 | elif isinstance(pred_file, np.ndarray): 47 | pred = pred_file 48 | truth = truth_file 49 | else: 50 | print('In the compare_truth_pred function, your input pred and truth is neither a file nor a numpy array') 51 | if not quiet_mode: 52 | print("in compare truth pred function in eval_help package, your shape of pred file is", np.shape(pred)) 53 | 54 | mae = np.mean(np.abs(pred-truth), axis=1) 55 | mse = np.mean(np.square(pred-truth), axis=1) 56 | 57 | if cut_off_outlier_thres is not None: 58 | mse = mse[mse < cut_off_outlier_thres] 59 | mae = mae[mae < cut_off_outlier_thres] 60 | 61 | 62 | return mae, mse 63 | 64 | 65 | def plotMSELossDistrib(pred_file, truth_file, flags=None, save_dir='data/'): 66 | """ 67 | Function to plot the MSE distribution histogram 68 | :param: pred_file: The Y prediction file 69 | :param: truth_file: The Y truth file 70 | :param: flags: The flags of the model/evaluation 71 | :param: save_dir: The directory to save the plot 72 | """ 73 | mae, mse = compare_truth_pred(pred_file, truth_file) 74 | plt.figure(figsize=(12, 6)) 75 | plt.hist(mse, bins=100) 76 | plt.xlabel('Mean Squared Error') 77 | plt.ylabel('cnt') 78 | plt.suptitle('(Avg MSE={:.4e}, 25%={:.3e}, 75%={:.3e})'.format(np.mean(mse), np.percentile(mse, 25), np.percentile(mse, 75))) 79 | if flags is not None: 80 | eval_model_str = flags.eval_model.replace('/','_') 81 | else: 82 | if isinstance(pred_file, str): 83 | eval_model_str = pred_file.split('Ypred')[-1].split('.')[:-1] 84 | else: 85 | eval_model_str = 'MSE_unknon_name' 86 | plt.savefig(os.path.join(save_dir, 87 | '{}.png'.format(eval_model_str))) 88 | print('(Avg MSE={:.4e})'.format(np.mean(mse))) 89 | return np.mean(mse) 90 | 91 | 92 | def eval_from_simulator(Xpred_file, flags): 93 | """ 94 | Evaluate using simulators from pred_file and return a new file with simulator results 95 | :param Xpred_file: The prediction file with the Xpred in its name 96 | :param data_set: The name of the dataset 97 | """ 98 | Xpred = np.loadtxt(Xpred_file, delimiter=' ') 99 | Ypred = simulator(flags.data_set, Xpred) 100 | Ypred_file = Xpred_file.replace('Xpred', 'Ypred_Simulated') 101 | np.savetxt(Ypred_file, Ypred) 102 | Ytruth_file = Xpred_file.replace('Xpred','Ytruth') 103 | plotMSELossDistrib(Ypred_file, Ytruth_file, flags) 104 | -------------------------------------------------------------------------------- /NA/flag_reader.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file serves to hold helper functions that is related to the "Flag" object which contains 3 | all the parameters during training and inference 4 | """ 5 | # Built-in 6 | import argparse 7 | # Libs 8 | 9 | # Own module 10 | from parameters import * 11 | 12 | # Torch 13 | 14 | def read_flag(): 15 | """ 16 | This function is to write the read the flags from a parameter file and put them in formats 17 | :return: flags: a struct where all the input params are stored 18 | """ 19 | parser = argparse.ArgumentParser() 20 | # Data_Set parameter 21 | parser.add_argument('--data-set', default=DATA_SET, type=str, help='which data set you are chosing') 22 | parser.add_argument('--test-ratio', default=TEST_RATIO, type=float, help='the ratio of the test set') 23 | 24 | # MD loss related params 25 | parser.add_argument('--md-radius', default=MD_RADIUS, type=float, help='radius for the MD ') 26 | parser.add_argument('--md-start', default=MD_START, type=int, help='coefficient of the MD loss') 27 | parser.add_argument('--md-end', default=MD_END, type=int, help='coefficient of the MD loss') 28 | parser.add_argument('--md-coeff', default=MD_COEFF, type=float, help='coefficient of the MD loss') 29 | 30 | # Model Architectural Params 31 | parser.add_argument('--use-lorentz', type=bool, default=USE_LORENTZ, help='The boolean flag that indicate whether we use lorentz oscillator') 32 | parser.add_argument('--linear', type=list, default=LINEAR, help='The fc layers units') 33 | parser.add_argument('--conv-out-channel', type=list, default=CONV_OUT_CHANNEL, help='The output channel of your 1d conv') 34 | parser.add_argument('--conv-kernel-size', type=list, default=CONV_KERNEL_SIZE, help='The kernel size of your 1d conv') 35 | parser.add_argument('--conv-stride', type=list, default=CONV_STRIDE, help='The strides of your 1d conv') 36 | 37 | # Optimizer Params 38 | parser.add_argument('--optim', default=OPTIM, type=str, help='the type of optimizer that you want to use') 39 | parser.add_argument('--reg-scale', type=float, default=REG_SCALE, help='#scale for regularization of dense layers') 40 | parser.add_argument('--batch-size', default=BATCH_SIZE, type=int, help='batch size (100)') 41 | parser.add_argument('--eval-batch-size', default=EVAL_BATCH_SIZE, type=int, 42 | help='The Batch size for back propagation') 43 | parser.add_argument('--eval-step', default=EVAL_STEP, type=int, help='# steps between evaluations') 44 | parser.add_argument('--train-step', default=TRAIN_STEP, type=int, help='# steps to train on the dataSet') 45 | parser.add_argument('--backprop-step', default=BACKPROP_STEP, type=int, help='# steps for back propagation') 46 | parser.add_argument('--lr', default=LEARN_RATE, type=float, help='learning rate') 47 | parser.add_argument('--lr-decay-rate', default=LR_DECAY_RATE, type=float, 48 | help='decay learn rate by multiplying this factor') 49 | parser.add_argument('--stop_threshold', default=STOP_THRESHOLD, type=float, 50 | help='The threshold below which training should stop') 51 | 52 | # parser.add_argument('--decay-step', default=DECAY_STEP, type=int, 53 | # help='decay learning rate at this number of steps') 54 | 55 | # Data Specific Params 56 | parser.add_argument('--x-range', type=list, default=X_RANGE, help='columns of input parameters') 57 | parser.add_argument('--y-range', type=list, default=Y_RANGE, help='columns of output parameters') 58 | parser.add_argument('--geoboundary', default=GEOBOUNDARY, type=tuple, help='the boundary of the geometric data') 59 | parser.add_argument('--model-name', default=MODEL_NAME, type=str, help='name of the model') 60 | parser.add_argument('--data-dir', default=DATA_DIR, type=str, help='data directory') 61 | parser.add_argument('--normalize-input', default=NORMALIZE_INPUT, type=bool, 62 | help='whether we should normalize the input or not') 63 | 64 | # Running specific 65 | parser.add_argument('--eval-model', default=EVAL_MODEL, type=str, help='the folder name of the model that you want to evaluate') 66 | parser.add_argument('--use-cpu-only', type=bool, default=USE_CPU_ONLY, help='The boolean flag that indicate use CPU only') 67 | flags = parser.parse_args() # This is for command line version of the code 68 | # flags = parser.parse_args(args = [])#This is for jupyter notebook version of the code 69 | # flagsVar = vars(flags) 70 | return flags 71 | -------------------------------------------------------------------------------- /inverse/flag_reader.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file serves to hold helper functions that is related to the "Flag" object which contains 3 | all the parameters during training and inference 4 | """ 5 | # Built-in 6 | import argparse 7 | # Libs 8 | 9 | # Own module 10 | from parameters import * 11 | 12 | # Torch 13 | 14 | def read_flag(): 15 | """ 16 | This function is to write the read the flags from a parameter file and put them in formats 17 | :return: flags: a struct where all the input params are stored 18 | """ 19 | parser = argparse.ArgumentParser() 20 | # Data_Set parameter 21 | parser.add_argument('--data-set', default=DATA_SET, type=str, help='which data set you are chosing') 22 | parser.add_argument('--test-ratio', default=TEST_RATIO, type=float, help='the ratio of the test set') 23 | 24 | # MD loss related params 25 | parser.add_argument('--md-radius', default=MD_RADIUS, type=float, help='radius for the MD ') 26 | parser.add_argument('--md-start', default=MD_START, type=int, help='coefficient of the MD loss') 27 | parser.add_argument('--md-end', default=MD_END, type=int, help='coefficient of the MD loss') 28 | parser.add_argument('--md-coeff', default=MD_COEFF, type=float, help='coefficient of the MD loss') 29 | 30 | # Model Architectural Params 31 | parser.add_argument('--use-lorentz', type=bool, default=USE_LORENTZ, help='The boolean flag that indicate whether we use lorentz oscillator') 32 | parser.add_argument('--linear', type=list, default=LINEAR, help='The fc layers units') 33 | parser.add_argument('--conv-out-channel', type=list, default=CONV_OUT_CHANNEL, help='The output channel of your 1d conv') 34 | parser.add_argument('--conv-kernel-size', type=list, default=CONV_KERNEL_SIZE, help='The kernel size of your 1d conv') 35 | parser.add_argument('--conv-stride', type=list, default=CONV_STRIDE, help='The strides of your 1d conv') 36 | 37 | # Optimizer Params 38 | parser.add_argument('--optim', default=OPTIM, type=str, help='the type of optimizer that you want to use') 39 | parser.add_argument('--reg-scale', type=float, default=REG_SCALE, help='#scale for regularization of dense layers') 40 | parser.add_argument('--batch-size', default=BATCH_SIZE, type=int, help='batch size (100)') 41 | parser.add_argument('--eval-batch-size', default=EVAL_BATCH_SIZE, type=int, 42 | help='The Batch size for back propagation') 43 | parser.add_argument('--eval-step', default=EVAL_STEP, type=int, help='# steps between evaluations') 44 | parser.add_argument('--train-step', default=TRAIN_STEP, type=int, help='# steps to train on the dataSet') 45 | parser.add_argument('--backprop-step', default=BACKPROP_STEP, type=int, help='# steps for back propagation') 46 | parser.add_argument('--lr', default=LEARN_RATE, type=float, help='learning rate') 47 | parser.add_argument('--lr-decay-rate', default=LR_DECAY_RATE, type=float, 48 | help='decay learn rate by multiplying this factor') 49 | parser.add_argument('--stop_threshold', default=STOP_THRESHOLD, type=float, 50 | help='The threshold below which training should stop') 51 | 52 | # parser.add_argument('--decay-step', default=DECAY_STEP, type=int, 53 | # help='decay learning rate at this number of steps') 54 | 55 | # Data Specific Params 56 | parser.add_argument('--x-range', type=list, default=X_RANGE, help='columns of input parameters') 57 | parser.add_argument('--y-range', type=list, default=Y_RANGE, help='columns of output parameters') 58 | parser.add_argument('--geoboundary', default=GEOBOUNDARY, type=tuple, help='the boundary of the geometric data') 59 | parser.add_argument('--model-name', default=MODEL_NAME, type=str, help='name of the model') 60 | parser.add_argument('--data-dir', default=DATA_DIR, type=str, help='data directory') 61 | parser.add_argument('--normalize-input', default=NORMALIZE_INPUT, type=bool, 62 | help='whether we should normalize the input or not') 63 | 64 | # Running specific 65 | parser.add_argument('--eval-model', default=EVAL_MODEL, type=str, help='the folder name of the model that you want to evaluate') 66 | parser.add_argument('--use-cpu-only', type=bool, default=USE_CPU_ONLY, help='The boolean flag that indicate use CPU only') 67 | flags = parser.parse_args() # This is for command line version of the code 68 | # flags = parser.parse_args(args = [])#This is for jupyter notebook version of the code 69 | # flagsVar = vars(flags) 70 | return flags 71 | -------------------------------------------------------------------------------- /cINN/flag_reader.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file serves to hold helper functions that is related to the "Flag" object which contains 3 | all the parameters during training and inference 4 | """ 5 | # Built-in 6 | import argparse 7 | import pickle 8 | import os 9 | # Libs 10 | 11 | # Own module 12 | from parameters import * 13 | 14 | # Torch 15 | 16 | def read_flag(): 17 | """ 18 | This function is to write the read the flags from a parameter file and put them in formats 19 | :return: flags: a struct where all the input params are stored 20 | """ 21 | parser = argparse.ArgumentParser() 22 | # Data_Set parameter 23 | parser.add_argument('--data-set', default=DATA_SET, type=str, help='which data set you are chosing') 24 | parser.add_argument('--test-ratio', default=TEST_RATIO, type=float, help='the ratio of the test set') 25 | # VAE model architecture hyper parameters 26 | parser.add_argument('--dim-z', default=DIM_Z, type=int, help='dimension of the latent variable z') 27 | parser.add_argument('--dim-x', default=DIM_X, type=int, help='dimension of the latent variable x') 28 | parser.add_argument('--dim-y', default=DIM_Y, type=int, help='dimension of the latent variable y') 29 | parser.add_argument('--dim-spec', default=DIM_SPEC, type=int, help='dimension of the spectra encoded conponent') 30 | parser.add_argument('--couple-layer-num', default=COUPLE_LAYER_NUM, type=int, help='The number of coupling blocks to use') 31 | parser.add_argument('--linear-se', type=list, default=LINEAR_SE, help='The fc layers units for spectra encoder model') 32 | parser.add_argument('--conv-out-channel-se', type=list, default=CONV_OUT_CHANNEL_SE, help='The output channel of your 1d conv for spectra encoder model') 33 | parser.add_argument('--conv-kernel-size-se', type=list, default=CONV_KERNEL_SIZE_SE, help='The kernel size of your 1d conv for spectra encoder model') 34 | parser.add_argument('--conv-stride-se', type=list, default=CONV_STRIDE_SE, help='The strides of your 1d conv fro spectra encoder model') 35 | # Optimization Params 36 | parser.add_argument('--optim', default=OPTIM, type=str, help='the type of optimizer that you want to use') 37 | parser.add_argument('--reg-scale', type=float, default=REG_SCALE, help='#scale for regularization of dense layers') 38 | parser.add_argument('--x-range', type=list, default=X_RANGE, help='columns of input parameters') 39 | parser.add_argument('--y-range', type=list, default=Y_RANGE, help='columns of output parameters') 40 | parser.add_argument('--grad-clamp', default=GRAD_CLAMP, type=float, help='gradient is clamped within [-15, 15]') 41 | parser.add_argument('--batch-size', default=BATCH_SIZE, type=int, help='batch size (100)') 42 | parser.add_argument('--eval-batch-size', default=EVAL_BATCH_SIZE, type=int, help='The Batch size for back propagation') 43 | parser.add_argument('--eval-step', default=EVAL_STEP, type=int, help='# steps between evaluations') 44 | parser.add_argument('--train-step', default=TRAIN_STEP, type=int, help='# steps to train on the dataSet') 45 | parser.add_argument('--verb-step', default=VERB_STEP, type=int, help='# steps to print and check best performance') 46 | parser.add_argument('--lr', default=LEARN_RATE, type=float, help='learning rate') 47 | parser.add_argument('--lr-decay-rate', default=LR_DECAY_RATE, type=float, 48 | help='decay learn rate by multiplying this factor') 49 | parser.add_argument('--stop_threshold', default=STOP_THRESHOLD, type=float, 50 | help='The threshold below which training should stop') 51 | # Data Specific params 52 | parser.add_argument('--model-name', default=MODEL_NAME, type=str, help='name of the model') 53 | parser.add_argument('--data-dir', default=DATA_DIR, type=str, help='data directory') 54 | parser.add_argument('--normalize-input', default=NORMALIZE_INPUT, type=bool, 55 | help='whether we should normalize the input or not') 56 | parser.add_argument('--geoboundary', default=GEOBOUNDARY, type=tuple, help='the boundary of the geometric data') 57 | # Running specific params 58 | parser.add_argument('--eval-model', default=EVAL_MODEL, type=str, help='the folder name of the model that you want to evaluate') 59 | parser.add_argument('--use-cpu-only', type=bool, default=USE_CPU_ONLY, help='The boolean flag that indicate use CPU only') 60 | flags = parser.parse_args() # This is for command line version of the code 61 | # flags = parser.parse_args(args = [])#This is for jupyter notebook version of the code 62 | # flagsVar = vars(flags) 63 | return flags 64 | 65 | -------------------------------------------------------------------------------- /Tandem/flag_reader.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file serves to hold helper functions that is related to the "Flag" object which contains 3 | all the parameters during training and inference 4 | """ 5 | # Built-in 6 | import argparse 7 | import pickle 8 | import os 9 | # Libs 10 | 11 | # Own module 12 | from parameters import * 13 | 14 | # Torch 15 | 16 | def read_flag(): 17 | """ 18 | This function is to write the read the flags from a parameter file and put them in formats 19 | :return: flags: a struct where all the input params are stored 20 | """ 21 | parser = argparse.ArgumentParser() 22 | # Data_Set parameter 23 | parser.add_argument('--data-set', default=DATA_SET, type=str, help='which data set you are chosing') 24 | parser.add_argument('--test-ratio', default=TEST_RATIO, type=float, help='the ratio of the test set') 25 | # Forward model architecture hyper parameters 26 | parser.add_argument('--load-forward-ckpt-dir', type=str, default=LOAD_FORWARD_CKPT_DIR, help='To load the forward training result and provide the directory') 27 | parser.add_argument('--linear-f', type=list, default=LINEAR_F, help='The fc layers units for forward model') 28 | parser.add_argument('--conv-out-channel-f', type=list, default=CONV_OUT_CHANNEL_F, help='The output channel of your 1d conv for forward model') 29 | parser.add_argument('--conv-kernel-size-f', type=list, default=CONV_KERNEL_SIZE_F, help='The kernel size of your 1d conv for forward model') 30 | parser.add_argument('--conv-stride-f', type=list, default=CONV_STRIDE_F, help='The strides of your 1d conv fro forward model') 31 | # Backward model architecture hyper parameters 32 | parser.add_argument('--linear-b', type=list, default=LINEAR_B, help='The fc layers units for forward model') 33 | parser.add_argument('--conv-out-channel-b', type=list, default=CONV_OUT_CHANNEL_B, help='The output channel of your 1d conv for forward model') 34 | parser.add_argument('--conv-kernel-size-b', type=list, default=CONV_KERNEL_SIZE_B, help='The kernel size of your 1d conv for forward model') 35 | parser.add_argument('--conv-stride-b', type=list, default=CONV_STRIDE_B, help='The strides of your 1d conv fro forward model') 36 | # Optimization Params 37 | parser.add_argument('--optim', default=OPTIM, type=str, help='the type of optimizer that you want to use') 38 | parser.add_argument('--reg-scale', type=float, default=REG_SCALE, help='#scale for regularization of dense layers') 39 | parser.add_argument('--x-range', type=list, default=X_RANGE, help='columns of input parameters') 40 | parser.add_argument('--y-range', type=list, default=Y_RANGE, help='columns of output parameters') 41 | parser.add_argument('--batch-size', default=BATCH_SIZE, type=int, help='batch size (100)') 42 | parser.add_argument('--eval-batch-size', default=EVAL_BATCH_SIZE, type=int, help='The Batch size for back propagation') 43 | parser.add_argument('--eval-step', default=EVAL_STEP, type=int, help='# steps between evaluations') 44 | parser.add_argument('--train-step', default=TRAIN_STEP, type=int, help='# steps to train on the dataSet') 45 | parser.add_argument('--verb-step', default=VERB_STEP, type=int, help='# steps to print and check best performance') 46 | parser.add_argument('--lr', default=LEARN_RATE, type=float, help='learning rate') 47 | parser.add_argument('--lr-decay-rate', default=LR_DECAY_RATE, type=float, 48 | help='decay learn rate by multiplying this factor') 49 | parser.add_argument('--stop_threshold', default=STOP_THRESHOLD, type=float, 50 | help='The threshold below which training should stop') 51 | # Data Specific params 52 | parser.add_argument('--model-name', default=MODEL_NAME, type=str, help='name of the model') 53 | parser.add_argument('--data-dir', default=DATA_DIR, type=str, help='data directory') 54 | parser.add_argument('--normalize-input', default=NORMALIZE_INPUT, type=bool, 55 | help='whether we should normalize the input or not') 56 | parser.add_argument('--geoboundary', default=GEOBOUNDARY, type=tuple, help='the boundary of the geometric data') 57 | # Running specific params 58 | parser.add_argument('--eval-model', default=EVAL_MODEL, type=str, help='the folder name of the model that you want to evaluate') 59 | parser.add_argument('--use-cpu-only', type=bool, default=USE_CPU_ONLY, help='The boolean flag that indicate use CPU only') 60 | flags = parser.parse_args() # This is for command line version of the code 61 | # flags = parser.parse_args(args = [])#This is for jupyter notebook version of the code 62 | # flagsVar = vars(flags) 63 | return flags 64 | 65 | -------------------------------------------------------------------------------- /VAE/flag_reader.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file serves to hold helper functions that is related to the "Flag" object which contains 3 | all the parameters during training and inference 4 | """ 5 | # Built-in 6 | import argparse 7 | import pickle 8 | import os 9 | # Libs 10 | 11 | # Own module 12 | from parameters import * 13 | 14 | # Torch 15 | 16 | def read_flag(): 17 | """ 18 | This function is to write the read the flags from a parameter file and put them in formats 19 | :return: flags: a struct where all the input params are stored 20 | """ 21 | parser = argparse.ArgumentParser() 22 | # Data_Set parameter 23 | parser.add_argument('--data-set', default=DATA_SET, type=str, help='which data set you are chosing') 24 | parser.add_argument('--test-ratio', default=TEST_RATIO, type=float, help='the ratio of the test set') 25 | # VAE model architecture hyper parameters 26 | parser.add_argument('--dim-z', default=DIM_Z, type=int, help='dimension of the latent variable z') 27 | parser.add_argument('--dim-x', default=DIM_X, type=int, help='dimension of the latent variable x') 28 | parser.add_argument('--dim-y', default=DIM_Y, type=int, help='dimension of the latent variable y') 29 | parser.add_argument('--dim-spec', default=DIM_SPEC, type=int, help='dimension of the spectra encoded conponent') 30 | parser.add_argument('--linear-d', type=list, default=LINEAR_D, help='The fc layers units for decoder') 31 | parser.add_argument('--linear-e', type=list, default=LINEAR_E, help='The fc layers units for encoder model') 32 | parser.add_argument('--linear-se', type=list, default=LINEAR_SE, help='The fc layers units for spectra encoder model') 33 | parser.add_argument('--conv-out-channel-se', type=list, default=CONV_OUT_CHANNEL_SE, help='The output channel of your 1d conv for spectra encoder model') 34 | parser.add_argument('--conv-kernel-size-se', type=list, default=CONV_KERNEL_SIZE_SE, help='The kernel size of your 1d conv for spectra encoder model') 35 | parser.add_argument('--conv-stride-se', type=list, default=CONV_STRIDE_SE, help='The strides of your 1d conv fro spectra encoder model') 36 | # Optimization Params 37 | parser.add_argument('--kl-coeff', default=KL_COEFF, type=float, help='The Coefficient of KL loss') 38 | parser.add_argument('--optim', default=OPTIM, type=str, help='the type of optimizer that you want to use') 39 | parser.add_argument('--reg-scale', type=float, default=REG_SCALE, help='#scale for regularization of dense layers') 40 | parser.add_argument('--x-range', type=list, default=X_RANGE, help='columns of input parameters') 41 | parser.add_argument('--y-range', type=list, default=Y_RANGE, help='columns of output parameters') 42 | parser.add_argument('--batch-size', default=BATCH_SIZE, type=int, help='batch size (100)') 43 | parser.add_argument('--eval-batch-size', default=EVAL_BATCH_SIZE, type=int, help='The Batch size for back propagation') 44 | parser.add_argument('--eval-step', default=EVAL_STEP, type=int, help='# steps between evaluations') 45 | parser.add_argument('--train-step', default=TRAIN_STEP, type=int, help='# steps to train on the dataSet') 46 | parser.add_argument('--verb-step', default=VERB_STEP, type=int, help='# steps to print and check best performance') 47 | parser.add_argument('--lr', default=LEARN_RATE, type=float, help='learning rate') 48 | parser.add_argument('--lr-decay-rate', default=LR_DECAY_RATE, type=float, 49 | help='decay learn rate by multiplying this factor') 50 | parser.add_argument('--stop_threshold', default=STOP_THRESHOLD, type=float, 51 | help='The threshold below which training should stop') 52 | # Data Specific params 53 | parser.add_argument('--model-name', default=MODEL_NAME, type=str, help='name of the model') 54 | parser.add_argument('--data-dir', default=DATA_DIR, type=str, help='data directory') 55 | parser.add_argument('--normalize-input', default=NORMALIZE_INPUT, type=bool, 56 | help='whether we should normalize the input or not') 57 | parser.add_argument('--geoboundary', default=GEOBOUNDARY, type=tuple, help='the boundary of the geometric data') 58 | # Running specific params 59 | parser.add_argument('--eval-model', default=EVAL_MODEL, type=str, help='the folder name of the model that you want to evaluate') 60 | parser.add_argument('--use-cpu-only', type=bool, default=USE_CPU_ONLY, help='The boolean flag that indicate use CPU only') 61 | flags = parser.parse_args() # This is for command line version of the code 62 | # flags = parser.parse_args(args = [])#This is for jupyter notebook version of the code 63 | # flagsVar = vars(flags) 64 | return flags 65 | 66 | -------------------------------------------------------------------------------- /GA/flag_reader.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file serves to hold helper functions that is related to the "Flag" object which contains 3 | all the parameters during training and inference 4 | """ 5 | # Built-in 6 | import argparse 7 | # Libs 8 | 9 | # Own module 10 | from parameters import * 11 | 12 | # Torch 13 | 14 | def read_flag(): 15 | """ 16 | This function is to write the read the flags from a parameter file and put them in formats 17 | :return: flags: a struct where all the input params are stored 18 | """ 19 | parser = argparse.ArgumentParser() 20 | # Data_Set parameter 21 | parser.add_argument('--data-set', default=DATA_SET, type=str, help='which data set you are chosing') 22 | parser.add_argument('--test-ratio', default=TEST_RATIO, type=float, help='the ratio of the test set') 23 | 24 | # GA SPECIFIC ARGUMENTS 25 | # Eseential Algorithmic Params 26 | parser.add_argument('--population', type=int, default=POP_SIZE, help='The size of populations in each generation') 27 | parser.add_argument('--elitism', type=int, default=ELITISM, help='# of highest fitness models passed to next generation') 28 | parser.add_argument('--mutation', type=float, default=MUTATION, 29 | help='Probability of a point mutation occuring in an individual') 30 | parser.add_argument('--crossover', type=float, default=CROSSOVER, 31 | help='Probability of crossover occuring b/w parents to make children') 32 | parser.add_argument('--k', type=int, default=K, help='If selection operator is decimation - identifies fitness threshold ' 33 | 'for individuals to become parents. If selection operator is ' 34 | 'tournament - identifies number of individuals in each tournament ') 35 | 36 | # Categorical Algorithmic Params 37 | parser.add_argument('--selection-operator', type=str, default=SELECT_OPS, 38 | help="Selection operator: 'roulette', 'decimation', 'tournament'") 39 | parser.add_argument('--cross-operator', type=str, default=CROSS_OPS, help="Crossover Operator: 'single-point', 'uniform'") 40 | parser.add_argument('--ga-eval', type=bool, default=GA_EVAL, help='Evaluation of GA') 41 | 42 | # Optimization Params 43 | parser.add_argument('--eval-step', default=EVAL_STEP, type=int, help='# steps between evaluations') 44 | parser.add_argument('--generations', default=GENERATIONS, type=int, help='# steps for back propagation') 45 | 46 | # Data Specific Params 47 | parser.add_argument('--x-range', type=list, default=X_RANGE, help='columns of input parameters') 48 | parser.add_argument('--y-range', type=list, default=Y_RANGE, help='columns of output parameters') 49 | parser.add_argument('--geoboundary', default=GEOBOUNDARY, type=tuple, help='the boundary of the geometric data') 50 | parser.add_argument('--model-name', default=MODEL_NAME, type=str, help='name of the model') 51 | parser.add_argument('--data-dir', default=DATA_DIR, type=str, help='data directory') 52 | parser.add_argument('--normalize-input', default=NORMALIZE_INPUT, type=bool, 53 | help='whether we should normalize the input or not') 54 | 55 | # Network Specific Parameters 56 | # Running specific 57 | parser.add_argument('--eval-model', default=EVAL_MODEL, type=str, help='the folder name of the model that you want to evaluate') 58 | parser.add_argument('--use-cpu-only', type=bool, default=USE_CPU_ONLY, help='The boolean flag that indicate use CPU only') 59 | 60 | # Model Architectural Params 61 | parser.add_argument('--use-lorentz', type=bool, default=USE_LORENTZ, help='The boolean flag that indicate whether we use lorentz oscillator') 62 | parser.add_argument('--linear', type=list, default=LINEAR, help='The fc layers units') 63 | parser.add_argument('--conv-out-channel', type=list, default=CONV_OUT_CHANNEL, help='The output channel of your 1d conv') 64 | parser.add_argument('--conv-kernel-size', type=list, default=CONV_KERNEL_SIZE, help='The kernel size of your 1d conv') 65 | parser.add_argument('--conv-stride', type=list, default=CONV_STRIDE, help='The strides of your 1d conv') 66 | 67 | # Optimizer Params 68 | parser.add_argument('--optim', default=OPTIM, type=str, help='the type of optimizer that you want to use') 69 | parser.add_argument('--reg-scale', type=float, default=REG_SCALE, help='#scale for regularization of dense layers') 70 | parser.add_argument('--batch-size', default=BATCH_SIZE, type=int, help='batch size (100)') 71 | parser.add_argument('--train-step', default=TRAIN_STEP, type=int, help='# steps to train on the dataSet') 72 | parser.add_argument('--lr',type=float, default=LEARN_RATE, help='learning rate') 73 | parser.add_argument('--lr-decay-rate', default=LR_DECAY_RATE, type=float, 74 | help='decay learn rate by multiplying this factor') 75 | parser.add_argument('--stop_threshold', default=STOP_THRESHOLD, type=float, 76 | help='The threshold below which training should stop') 77 | 78 | flags = parser.parse_args() 79 | return flags 80 | 81 | -------------------------------------------------------------------------------- /cINN/train.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file serves as a training interface for training the network 3 | """ 4 | # Built in 5 | import glob 6 | import os 7 | import shutil 8 | 9 | # Torch 10 | import torch 11 | # Own 12 | import flag_reader 13 | from utils import data_reader 14 | from class_wrapper import Network 15 | from model_maker import cINN 16 | from utils.helper_functions import put_param_into_folder,write_flags_and_BVE 17 | from evaluate import evaluate_from_model 18 | import numpy as np 19 | 20 | def training_from_flag(flags): 21 | """ 22 | Training interface. 1. Read data 2. initialize network 3. train network 4. record flags 23 | :param flag: The training flags read from command line or parameter.py 24 | :return: None 25 | """ 26 | # Get the data 27 | train_loader, test_loader = data_reader.read_data(flags) 28 | print("Making network now") 29 | 30 | # Make Network 31 | ntwk = Network(cINN, flags, train_loader, test_loader) 32 | 33 | print("number of trainable parameters is :") 34 | pytorch_total_params = sum(p.numel() for p in ntwk.model.parameters() if p.requires_grad) 35 | print(pytorch_total_params) 36 | 37 | # Training process 38 | print("Start training now...") 39 | ntwk.train() 40 | 41 | # Do the house keeping, write the parameters and put into folder, also use pickle to save the flags obejct 42 | write_flags_and_BVE(flags, ntwk.best_validation_loss, ntwk.ckpt_dir) 43 | 44 | 45 | def retrain_different_dataset(index): 46 | """ 47 | This function is to evaluate all different datasets in the model with one function call 48 | """ 49 | from utils.helper_functions import load_flags 50 | data_set_list = ["Peurifoy"] 51 | # data_set_list = ["Chen"] 52 | # data_set_list = ["Yang"] 53 | #data_set_list = ["Peurifoy","Chen","Yang_sim"] 54 | for eval_model in data_set_list: 55 | flags = load_flags(os.path.join("models", eval_model+"_best_model")) 56 | flags.model_name = "retrain" + str(index) + eval_model 57 | flags.train_step = 500 58 | flags.test_ratio = 0.2 59 | training_from_flag(flags) 60 | 61 | def hyperswipe(): 62 | """ 63 | This is for doing hyperswiping for the model parameters 64 | """ 65 | reg_scale_list = [1e-4] 66 | lr_list = [1e-3, 1e-2] 67 | # lr_list = [1e-1, 1e-2, 1e-3, 1e-4] 68 | #reg_scale_list = [1e-2, 1e-3, 1e-1] 69 | for reg_scale in reg_scale_list: 70 | for couple_layer_num in [14, 16, 18]:#range(12,): 71 | for lr in lr_list: 72 | for i in range(1, 3): 73 | flags = flag_reader.read_flag() #setting the base case 74 | flags.couple_layer_num = couple_layer_num 75 | flags.lr = lr 76 | flags.reg_scale = reg_scale 77 | flags.model_name = flags.data_set + '_mid_layer_1024_couple_layer_num' + str(couple_layer_num) + '_lr_' + str(flags.lr) + '_reg_scale_' + str(reg_scale) + '_trail_' + str(i) 78 | training_from_flag(flags) 79 | 80 | 81 | def random_swipe(): 82 | """ 83 | This is the random version of hyperswiping for the model parameters 84 | """ 85 | # The list of params that signified by a lower and upper limit and use np.random.uniform to select 86 | lambda_mse_range = [1, 1000] 87 | lambda_z_range = [1, 1000] 88 | lambda_rev_range = [1, 1000] 89 | # The list of params that signified by a permutation of the values in the list 90 | zeros_noise_scale_list = [1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1] 91 | y_noise_scale_list = [1e-5, 1e-4, 1e-3, 1e-2, 1e-1] 92 | # Number of samples to draw 93 | num_samples = 60 94 | for i in range(num_samples): 95 | flags = flag_reader.read_flag() #setting the base case 96 | flags.lambda_mse = np.random.uniform(low=lambda_mse_range[0], high=lambda_mse_range[1]) 97 | flags.lambda_z = np.random.uniform(low=lambda_z_range[0], high=lambda_z_range[1]) 98 | flags.lambda_rev = np.random.uniform(low=lambda_rev_range[0], high=lambda_rev_range[1]) 99 | flags.zeros_noise_scale = zeros_noise_scale_list[np.random.permutation(len(zeros_noise_scale_list))[0]] 100 | flags.y_noise_scale = y_noise_scale_list[np.random.permutation(len(y_noise_scale_list))[0]] 101 | flags.model_name = flags.data_set + 'lambda__mse_{:.2g}_z_{:.2g}_rev_{:.2g}_noise__zeros_{:.3g}_y_{:.3g}'.format(flags.lambda_mse, 102 | flags.lambda_z, flags.lambda_rev, flags.zeros_noise_scale, flags.y_noise_scale) 103 | training_from_flag(flags) 104 | 105 | 106 | if __name__ == '__main__': 107 | # Read the parameters to be set 108 | flags = flag_reader.read_flag() 109 | 110 | #random_swipe() 111 | # hyperswipe() 112 | 113 | # Call the train from flag function 114 | #training_from_flag(flags) 115 | 116 | #retrain_different_dataset(0) 117 | 118 | # Do the retraining for all the data set to get the training for reproducibility 119 | for i in range(5): 120 | retrain_different_dataset(i) 121 | -------------------------------------------------------------------------------- /utils/get_deterministic_multi.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import numpy as np\n", 10 | "import os\n", 11 | "import matplotlib.pyplot as plt\n", 12 | "import pandas as pd" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 11, 18 | "metadata": {}, 19 | "outputs": [ 20 | { 21 | "name": "stdout", 22 | "output_type": "stream", 23 | "text": [ 24 | "working on dataset Peurifoy\n", 25 | "working on method Tandem\n", 26 | "../Tandem/data\n", 27 | "test_Ypred_new_best_Peurifoy.csv\n", 28 | "(2, 200)\n", 29 | "working on method NN\n", 30 | "../inverse/data\n", 31 | "test_Ypred_new_best_Peurifoy.csv\n", 32 | "(2, 200)\n" 33 | ] 34 | } 35 | ], 36 | "source": [ 37 | "# Generate the mse_min_list.txt and mse_quan2575_list.txt for Tandem and naive inverse \n", 38 | "dataset = ['Peurifoy']\n", 39 | "# dataset = ['Yang','Chen','Peurifoy']\n", 40 | "min_list_name = 'mse_min_list.txt'\n", 41 | "quan2575_list_name = 'mse_quan2575_list.txt'\n", 42 | "method_list = ['Tandem','NN']\n", 43 | "directory_pos = {'Tandem': '../Tandem/data', \n", 44 | " 'NN': '../inverse/data'}\n", 45 | "target_dir = '../mm_bench_multi_eval/'\n", 46 | "for data in dataset:\n", 47 | " print('working on dataset', data)\n", 48 | " for method in method_list:\n", 49 | " print('working on method', method)\n", 50 | " # Get the directory\n", 51 | " directory = directory_pos[method]\n", 52 | " # Loop over the whole thing to find Ypred and Ytruth\n", 53 | " for file in os.listdir(directory):\n", 54 | " if data in file and 'Ypred' in file:\n", 55 | " print(directory)\n", 56 | " print(file)\n", 57 | " Yp = pd.read_csv(os.path.join(directory, file), sep=' ', header=None).values\n", 58 | " Yt = pd.read_csv(os.path.join(directory, file.replace('Ypred','Ytruth')), sep=' ', header=None).values\n", 59 | " mse = np.mean(np.square(Yt - Yp), axis=1)\n", 60 | " # Calculate the mse stats from mse list\n", 61 | " mse_25_percent = np.percentile(mse, 25)*np.ones([1, 200])\n", 62 | " mse_75_percent = np.percentile(mse, 75)*np.ones([1, 200])\n", 63 | " mean_mse = np.mean(mse)*np.ones([200,1])\n", 64 | " mse_quan_2575 = np.concatenate([mse_25_percent, mse_75_percent], axis=0)\n", 65 | " print(np.shape(mse_quan_2575))\n", 66 | " # Change the name to keep the same with other multi-eval\n", 67 | " if data == 'Yang':\n", 68 | " data = 'Yang_sim'\n", 69 | " # Create dir\n", 70 | " if not os.path.isdir(os.path.join(target_dir, method, data)):\n", 71 | " os.makedirs(os.path.join(target_dir, method, data))\n", 72 | " # Write the list down\n", 73 | " np.savetxt(os.path.join(target_dir, method, data, min_list_name), mean_mse)\n", 74 | " np.savetxt(os.path.join(target_dir, method, data, quan2575_list_name), mse_quan_2575)\n", 75 | " break" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 4, 81 | "metadata": {}, 82 | "outputs": [], 83 | "source": [ 84 | "# Finding problem with cINN peurifoy\n", 85 | "mse_mat = pd.read_csv('/home/sr365/MM_Bench/mm_bench_multi_eval/finished/cINN/Peurifoy/mse_mat.csv',sep=' ', header=None)" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 9, 91 | "metadata": {}, 92 | "outputs": [ 93 | { 94 | "name": "stdout", 95 | "output_type": "stream", 96 | "text": [ 97 | "6\n" 98 | ] 99 | } 100 | ], 101 | "source": [ 102 | "# mse_mat\n", 103 | "# print(np.isnan(mse_mat))\n", 104 | "print(np.sum(np.sum(np.isnan(mse_mat))))" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [] 113 | }, 114 | { 115 | "cell_type": "code", 116 | "execution_count": null, 117 | "metadata": {}, 118 | "outputs": [], 119 | "source": [] 120 | } 121 | ], 122 | "metadata": { 123 | "interpreter": { 124 | "hash": "e0c901c02675fee51393c33bfa502e33ca284fb17fdf86b8a4f5c9070382ed49" 125 | }, 126 | "kernelspec": { 127 | "display_name": "Python 3.7.12 64-bit ('benicml': conda)", 128 | "language": "python", 129 | "name": "python3" 130 | }, 131 | "language_info": { 132 | "codemirror_mode": { 133 | "name": "ipython", 134 | "version": 3 135 | }, 136 | "file_extension": ".py", 137 | "mimetype": "text/x-python", 138 | "name": "python", 139 | "nbconvert_exporter": "python", 140 | "pygments_lexer": "ipython3", 141 | "version": "3.7.12" 142 | }, 143 | "orig_nbformat": 4 144 | }, 145 | "nbformat": 4, 146 | "nbformat_minor": 2 147 | } 148 | -------------------------------------------------------------------------------- /MDN/train.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file serves as a training interface for training the network 3 | """ 4 | # Built in 5 | import glob 6 | import os 7 | import shutil 8 | import sys 9 | sys.path.append('../utils/') 10 | 11 | # Torch 12 | 13 | # Own 14 | import flag_reader 15 | from utils import data_reader 16 | from class_wrapper import Network 17 | # from model_maker import MDN 18 | # from mdn_nips import MDN 19 | # from mdn_tony_duan import MDN 20 | from mdn_manu_joseph import MDN 21 | from utils.helper_functions import put_param_into_folder, write_flags_and_BVE 22 | 23 | def training_from_flag(flags): 24 | """ 25 | Training interface. 1. Read data 2. initialize network 3. train network 4. record flags 26 | :param flag: The training flags read from command line or parameter.py 27 | :return: None 28 | """ 29 | # Get the data 30 | train_loader, test_loader = data_reader.read_data(flags) 31 | print("Making network now") 32 | 33 | # Make Network 34 | ntwk = Network(MDN, flags, train_loader, test_loader) 35 | 36 | print("number of trainable parameters is :") 37 | pytorch_total_params = sum(p.numel() for p in ntwk.model.parameters() if p.requires_grad) 38 | print(pytorch_total_params) 39 | 40 | # Training process 41 | print("Start training now...") 42 | ntwk.train() 43 | 44 | # Do the house keeping, write the parameters and put into folder, also use pickle to save the flags obejct 45 | write_flags_and_BVE(flags, ntwk.best_validation_loss, ntwk.ckpt_dir) 46 | 47 | 48 | def retrain_different_dataset(index): 49 | """ 50 | This function is to evaluate all different datasets in the model with one function call 51 | """ 52 | from utils.helper_functions import load_flags 53 | data_set_list = ["Peurifoy"] 54 | # data_set_list = ["Chen"] 55 | # data_set_list = ["Yang"] 56 | #data_set_list = ["Peurifoy","Chen","Yang_sim"] 57 | for eval_model in data_set_list: 58 | flags = load_flags(os.path.join("models", eval_model+"_best_model")) 59 | flags.model_name = "retrain" + str(index) + eval_model 60 | flags.train_step = 500 61 | flags.test_ratio = 0.2 62 | training_from_flag(flags) 63 | 64 | def hyperswipe(): 65 | """ 66 | This is for doing hyperswiping for the model parameters 67 | """ 68 | reg_scale_list = [0.1, 0.01, 0.001] 69 | # reg_scale_list = [1e-5, 0] 70 | # reg_scale_list = [1e-4, 5e-4, 5e-5, 0] 71 | # layer_size_list = [100, 500, 1000] 72 | layer_size_list = [100, 500, 1000] 73 | num_gauss_list = [4] 74 | # num_gauss_list = [4, 8, 16, 32] 75 | #num_gauss_list = [5, 10, 15, 20, 25, 30] 76 | # dataset = 'Yang_sim' # 'Peurifoy' ## 'Chen' # 77 | # dataset = 'Peurifoy' #'Yang_sim' # # 'Chen' # 78 | dataset = 'Chen' # 'Yang_sim' # 'Peurifoy' ## 79 | linear_start = {'Chen': 256, 'Peurifoy':201, 'Yang_sim':2000} 80 | linear_end = {'Chen':5, 'Peurifoy':8, 'Yang_sim':14} 81 | for reg_scale in reg_scale_list: 82 | # for layer_num in range(5,6): 83 | for layer_num in range(3, 10, 2): 84 | # for layer_num in range(10, 15, 2): 85 | for layer_size in layer_size_list: 86 | for num_gaussian in num_gauss_list: 87 | flags = flag_reader.read_flag() #setting the base case 88 | flags.data_set = dataset 89 | flags.reg_scale = reg_scale 90 | linear = [layer_size for j in range(layer_num)] 91 | linear[0] = linear_start[dataset] 92 | linear[-1] = linear_end[dataset] 93 | flags.linear = linear 94 | flags.num_gaussian = num_gaussian 95 | flags.model_name = flags.data_set + '_gaussian_'+str(num_gaussian) + '_layer_num_' + str(layer_num) + '_unit_' + str(layer_size) + '_lr_' + str(flags.lr) + '_reg_scale_' + str(reg_scale) 96 | training_from_flag(flags) 97 | # quit() 98 | # try: 99 | # training_from_flag(flags) 100 | # except RuntimeError as e: 101 | # print("Failing the device-side assert for MDN mdn.sample function! doing 3 retries now:") 102 | # for j in range(3): 103 | # try: 104 | # print("trying number ", j) 105 | # training_from_flag(flags) 106 | # break; 107 | # except: 108 | # print("Failing again! try again") 109 | 110 | 111 | 112 | 113 | if __name__ == '__main__': 114 | # torch.manual_seed(1) 115 | # torch.cuda.manual_seed(1) 116 | # Read the parameters to be set 117 | flags = flag_reader.read_flag() 118 | 119 | # hyperswipe() 120 | # Call the train from flag function 121 | #training_from_flag(flags) 122 | 123 | # Do the retraining for all the data set to get the training 124 | for i in range(10): 125 | retrain_different_dataset(i) 126 | -------------------------------------------------------------------------------- /INN_FrEIA/flag_reader.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file serves to hold helper functions that is related to the "Flag" object which contains 3 | all the parameters during training and inference 4 | """ 5 | # Built-in 6 | import argparse 7 | import pickle 8 | import os 9 | # Libs 10 | 11 | # Own module 12 | from parameters import * 13 | 14 | # Torch 15 | 16 | def read_flag(): 17 | """ 18 | This function is to write the read the flags from a parameter file and put them in formats 19 | :return: flags: a struct where all the input params are stored 20 | """ 21 | parser = argparse.ArgumentParser() 22 | # Data_Set parameter 23 | parser.add_argument('--data-set', default=DATA_SET, type=str, help='which data set you are chosing') 24 | parser.add_argument('--test-ratio', default=TEST_RATIO, type=float, help='the ratio of the test set') 25 | # VAE model architecture hyper parameters 26 | parser.add_argument('--dim-z', default=DIM_Z, type=int, help='dimension of the latent variable z') 27 | parser.add_argument('--dim-x', default=DIM_X, type=int, help='dimension of the latent variable x') 28 | parser.add_argument('--dim-y', default=DIM_Y, type=int, help='dimension of the latent variable y') 29 | parser.add_argument('--dim-tot', default=DIM_TOT, type=int, help='dimension of the total dimension of both sides') 30 | parser.add_argument('--dim-spec', default=DIM_SPEC, type=int, help='dimension of the spectra encoded conponent') 31 | parser.add_argument('--couple-layer-num', default=COUPLE_LAYER_NUM, type=int, help='The number of coupling blocks to use') 32 | parser.add_argument('--subnet-linear', type=list, default=SUBNET_LINEAR, help='The fc layers units for subnetwork') 33 | parser.add_argument('--linear-se', type=list, default=LINEAR_SE, help='The fc layers units for spectra encoder model') 34 | parser.add_argument('--conv-out-channel-se', type=list, default=CONV_OUT_CHANNEL_SE, help='The output channel of your 1d conv for spectra encoder model') 35 | parser.add_argument('--conv-kernel-size-se', type=list, default=CONV_KERNEL_SIZE_SE, help='The kernel size of your 1d conv for spectra encoder model') 36 | parser.add_argument('--conv-stride-se', type=list, default=CONV_STRIDE_SE, help='The strides of your 1d conv fro spectra encoder model') 37 | # Loss ratio 38 | parser.add_argument('--lambda-mse', type=float, default=LAMBDA_MSE, help='the coefficient for mse loss lambda') 39 | parser.add_argument('--lambda-z', type=float, default=LAMBDA_Z, help='the coefficient for latent variable MMD loss lambda') 40 | parser.add_argument('--lambda-rev', type=float, default=LAMBDA_REV, help='the coefficient for reverse MMD loss lambda') 41 | parser.add_argument('--zeros-noise-scale', type=float, default=ZEROS_NOISE_SCALE, help='the noise scale of random variables for zeros') 42 | parser.add_argument('--y-noise-scale', type=float, default=Y_NOISE_SCALE, help='the noise scale on y') 43 | # Optimization Params 44 | parser.add_argument('--optim', default=OPTIM, type=str, help='the type of optimizer that you want to use') 45 | parser.add_argument('--reg-scale', type=float, default=REG_SCALE, help='#scale for regularization of dense layers') 46 | parser.add_argument('--x-range', type=list, default=X_RANGE, help='columns of input parameters') 47 | parser.add_argument('--y-range', type=list, default=Y_RANGE, help='columns of output parameters') 48 | parser.add_argument('--grad-clamp', default=GRAD_CLAMP, type=float, help='gradient is clamped within [-15, 15]') 49 | parser.add_argument('--batch-size', default=BATCH_SIZE, type=int, help='batch size (100)') 50 | parser.add_argument('--eval-batch-size', default=EVAL_BATCH_SIZE, type=int, help='The Batch size for back propagation') 51 | parser.add_argument('--eval-step', default=EVAL_STEP, type=int, help='# steps between evaluations') 52 | parser.add_argument('--train-step', default=TRAIN_STEP, type=int, help='# steps to train on the dataSet') 53 | parser.add_argument('--verb-step', default=VERB_STEP, type=int, help='# steps to print and check best performance') 54 | parser.add_argument('--lr', default=LEARN_RATE, type=float, help='learning rate') 55 | parser.add_argument('--lr-decay-rate', default=LR_DECAY_RATE, type=float, 56 | help='decay learn rate by multiplying this factor') 57 | parser.add_argument('--stop_threshold', default=STOP_THRESHOLD, type=float, 58 | help='The threshold below which training should stop') 59 | # Data Specific params 60 | parser.add_argument('--model-name', default=MODEL_NAME, type=str, help='name of the model') 61 | parser.add_argument('--data-dir', default=DATA_DIR, type=str, help='data directory') 62 | parser.add_argument('--ckpt-dir', default=CKPT_DIR, type=str, help='ckpt_dir') 63 | parser.add_argument('--normalize-input', default=NORMALIZE_INPUT, type=bool, 64 | help='whether we should normalize the input or not') 65 | parser.add_argument('--geoboundary', default=GEOBOUNDARY, type=tuple, help='the boundary of the geometric data') 66 | # Running specific params 67 | parser.add_argument('--eval-model', default=EVAL_MODEL, type=str, help='the folder name of the model that you want to evaluate') 68 | parser.add_argument('--use-cpu-only', type=bool, default=USE_CPU_ONLY, help='The boolean flag that indicate use CPU only') 69 | flags = parser.parse_args() # This is for command line version of the code 70 | # flags = parser.parse_args(args = [])#This is for jupyter notebook version of the code 71 | # flagsVar = vars(flags) 72 | return flags 73 | 74 | -------------------------------------------------------------------------------- /inverse/modulized_eval.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file serves as a modulized evaluation interface for the network 3 | """ 4 | # Built in 5 | import os 6 | import sys 7 | sys.path.append('../utils/') 8 | # Torch 9 | 10 | # Own 11 | import flag_reader 12 | from class_wrapper import Network 13 | from model_maker import NA 14 | from utils import data_reader 15 | from utils.helper_functions import load_flags 16 | from utils.evaluation_helper import plotMSELossDistrib 17 | from utils.evaluation_helper import get_test_ratio_helper 18 | from utils.plotsAnalysis import get_xpred_ytruth_xtruth_from_folder 19 | from utils.plotsAnalysis import reshape_xpred_list_to_mat 20 | from utils.create_folder_modulized import get_folder_modulized 21 | from utils.create_folder_modulized import check_modulized_yet 22 | # Libs 23 | import numpy as np 24 | import matplotlib.pyplot as plt 25 | from thop import profile, clever_format 26 | 27 | 28 | def modulized_evaluate_from_model(model_dir, operate_dir, FF=False, BP=False): 29 | 30 | """ 31 | Evaluating interface. 1. Retreive the flags 2. get data 3. initialize network 4. eval 32 | :param model_dir: The folder to retrieve the model 33 | :param operate_dir: The directory to operate in (with all the Xpred,Ypred files) 34 | :return: None 35 | """ 36 | # Retrieve the flag object 37 | print("Retrieving flag object for parameters") 38 | if (model_dir.startswith("models")): 39 | model_dir = model_dir[7:] 40 | print("after removing prefix models/, now model_dir is:", model_dir) 41 | print(model_dir) 42 | flags = load_flags(os.path.join("models", model_dir)) 43 | flags.eval_model = model_dir # Reset the eval mode 44 | if BP: 45 | flags.backprop_step = 300 46 | else: 47 | flags.backprop_step = 1 48 | flags.test_ratio = get_test_ratio_helper(flags) 49 | 50 | if flags.data_set == 'meta_material': 51 | save_Simulator_Ypred = False 52 | print("this is MM dataset, there is no simple numerical simulator therefore setting the save_Simulator_Ypred to False") 53 | flags.batch_size = 1 # For backprop eval mode, batchsize is always 1 54 | flags.lr = 0.5 55 | flags.eval_batch_size = 2048 56 | flags.train_step = 500 57 | 58 | print(flags) 59 | 60 | # Make Network 61 | ntwk = Network(NA, flags, train_loader=None, test_loader=None, inference_mode=True, saved_model=flags.eval_model) 62 | 63 | # Set up the files 64 | Xpred_list, Xt, Yt = get_xpred_ytruth_xtruth_from_folder(operate_dir) 65 | X_init_mat = reshape_xpred_list_to_mat(Xpred_list) 66 | 67 | # Evaluation process 68 | print("Start eval now:") 69 | ntwk.modulized_bp_ff(X_init_mat=X_init_mat, Ytruth=Yt, save_dir=operate_dir, save_all=True, FF=FF) 70 | 71 | 72 | def evaluate_all(models_dir="models"): 73 | """ 74 | This function evaluate all the models in the models/. directory 75 | :return: None 76 | """ 77 | for file in os.listdir(models_dir): 78 | if os.path.isfile(os.path.join(models_dir, file, 'flags.obj')): 79 | modulized_evaluate_from_model(os.path.join(models_dir, file)) 80 | return None 81 | 82 | def get_state_of_BP_FF(folder): 83 | """ 84 | This function return 2 flag for BP and FF according to the folder name given 85 | """ 86 | # Get the label of the state of BP and FF 87 | if 'BP_on' in folder: 88 | BP = True 89 | elif 'BP_off' in folder: 90 | BP = False 91 | else: 92 | print("Your folder name does not indicate state of BP: ", folder) 93 | exit() 94 | if 'FF_on' in folder: 95 | FF = True 96 | elif 'FF_off' in folder: 97 | FF = False 98 | else: 99 | print("Your folder name does not indicate state of FF: ", folder) 100 | exit() 101 | return BP, FF 102 | 103 | def modulized_evaluate_different_dataset(gpu=None): 104 | """ 105 | This function is to evaluate all different datasets in the model with one function call 106 | """ 107 | #data_set_list = ["meta_material"] 108 | data_set_list = ["robotic_arm","sine_wave","ballistics",] 109 | folder_list = get_folder_modulized(gpu=gpu) 110 | for folder in folder_list: 111 | # Skip Random for now 112 | #if 'Random' not in folder: 113 | # continue; 114 | BP, FF = get_state_of_BP_FF(folder) 115 | # Nothing is needed if both of them are False 116 | if BP is False and FF is False: 117 | continue; 118 | print("currently working on folder", folder) 119 | # Work on each dataset 120 | for dataset in data_set_list: 121 | if check_modulized_yet(os.path.join(folder, dataset)): 122 | continue; 123 | modulized_evaluate_from_model(model_dir="retrain0" + dataset, 124 | operate_dir=os.path.join(folder, dataset), BP=BP, FF=FF) 125 | 126 | if __name__ == '__main__': 127 | # Read the flag, however only the flags.eval_model is used and others are not used 128 | #eval_flags = flag_reader.read_flag() 129 | 130 | ##################### 131 | # different dataset # 132 | ##################### 133 | # This is to run the single evaluation, please run this first to make sure the current model is well-trained before going to the multiple evaluation code below 134 | #evaluate_different_dataset(multi_flag=False, eval_data_all=False, save_Simulator_Ypred=True, MSE_Simulator=False) 135 | # This is for multi evaluation for generating the Fig 3, evaluating the models under various T values 136 | #evaluate_different_dataset(multi_flag=True, eval_data_all=False, save_Simulator_Ypred=True, MSE_Simulator=False) 137 | 138 | 139 | ##################### 140 | #Modulized eval Here# 141 | ##################### 142 | modulized_evaluate_different_dataset() 143 | -------------------------------------------------------------------------------- /Tandem/model_maker.py: -------------------------------------------------------------------------------- 1 | """ 2 | This is the module where the model is defined. It uses the nn.Module as backbone to create the network structure 3 | """ 4 | # Own modules 5 | 6 | # Built in 7 | import math 8 | # Libs 9 | import numpy as np 10 | 11 | # Pytorch module 12 | import torch.nn as nn 13 | import torch.nn.functional as F 14 | import torch 15 | 16 | class Forward(nn.Module): 17 | def __init__(self, flags): 18 | super(Forward, self).__init__() 19 | """ 20 | This part is the forward model layers definition: 21 | """ 22 | # Linear Layer and Batch_norm Layer definitions here 23 | self.flags = flags 24 | self.linears_f = nn.ModuleList([]) 25 | self.bn_linears_f = nn.ModuleList([]) 26 | for ind, fc_num in enumerate(flags.linear_f[0:-1]): # Excluding the last one as we need intervals 27 | self.linears_f.append(nn.Linear(fc_num, flags.linear_f[ind + 1])) 28 | self.bn_linears_f.append(nn.BatchNorm1d(flags.linear_f[ind + 1])) 29 | 30 | # Conv Layer definitions here 31 | self.convs_f = nn.ModuleList([]) 32 | in_channel = 1 # Initialize the in_channel number 33 | for ind, (out_channel, kernel_size, stride) in enumerate(zip(flags.conv_out_channel_f, 34 | flags.conv_kernel_size_f, 35 | flags.conv_stride_f)): 36 | if stride == 2: # We want to double the number 37 | pad = int(kernel_size/2 - 1) 38 | elif stride == 1: # We want to keep the number unchanged 39 | pad = int((kernel_size - 1)/2) 40 | else: 41 | Exception("Now only support stride = 1 or 2, contact Ben") 42 | 43 | self.convs_f.append(nn.ConvTranspose1d(in_channel, out_channel, kernel_size, 44 | stride=stride, padding=pad)) # To make sure L_out double each time 45 | in_channel = out_channel # Update the out_channel 46 | if self.convs_f: 47 | # Get the channel number down 48 | self.convs_f.append(nn.Conv1d(in_channel, out_channels=1, kernel_size=1, stride=1, padding=0)) 49 | 50 | def forward(self, G): 51 | """ 52 | The forward function which defines how the network is connected 53 | :param G: The input geometry (Since this is a forward network) 54 | :return: S: The 300 dimension spectra 55 | """ 56 | out = G # initialize the out 57 | # For the linear part 58 | for ind, (fc, bn) in enumerate(zip(self.linears_f, self.bn_linears_f)): 59 | if ind != len(self.linears_f) - 1: 60 | # print(out.size()) 61 | out = F.relu(bn(fc(out))) # ReLU + BN + Linear 62 | else: 63 | out = fc(out) 64 | if self.convs_f: 65 | # The normal mode to train without Lorentz 66 | out = out.unsqueeze(1) # Add 1 dimension to get N,L_in, H 67 | # For the conv part 68 | for ind, conv in enumerate(self.convs_f): 69 | out = conv(out) 70 | out = out.squeeze() 71 | return out 72 | 73 | 74 | class Backward(nn.Module): 75 | def __init__(self, flags): 76 | super(Backward, self).__init__() 77 | """ 78 | This part if the backward model layers definition: 79 | """ 80 | # Linear Layer and Batch_norm Layer definitions here 81 | self.flags = flags 82 | self.linears_b = nn.ModuleList([]) 83 | self.bn_linears_b = nn.ModuleList([]) 84 | for ind, fc_num in enumerate(flags.linear_b[0:-1]): # Excluding the last one as we need intervals 85 | self.linears_b.append(nn.Linear(fc_num, flags.linear_b[ind + 1])) 86 | self.bn_linears_b.append(nn.BatchNorm1d(flags.linear_b[ind + 1])) 87 | 88 | # Conv Layer definitions here 89 | self.convs_b = nn.ModuleList([]) 90 | in_channel = 1 # Initialize the in_channel number 91 | for ind, (out_channel, kernel_size, stride) in enumerate(zip(flags.conv_out_channel_b, 92 | flags.conv_kernel_size_b, 93 | flags.conv_stride_b)): 94 | if stride == 2: # We want to double the number 95 | pad = int(kernel_size/2 - 1) 96 | elif stride == 1: # We want to keep the number unchanged 97 | pad = int((kernel_size - 1)/2) 98 | else: 99 | Exception("Now only support stride = 1 or 2, contact Ben") 100 | self.convs_b.append(nn.Conv1d(in_channel, out_channel, kernel_size, 101 | stride=stride, padding=pad)) 102 | in_channel = out_channel # Update the out_channel 103 | if len(self.convs_b): # Make sure there is not en empty one 104 | self.convs_b.append(nn.Conv1d(in_channel, out_channels=1, kernel_size=1, stride=1, padding=0)) 105 | 106 | def forward(self, S): 107 | """ 108 | The backward function defines how the backward network is connected 109 | :param S: The 300-d input spectrum 110 | :return: G: The 8-d geometry 111 | """ 112 | out = S 113 | #if (len(S[0]) == 1) and (self.flags.linear_b[0] != 1): 114 | # # This is when gaussian_mixture data comes in 115 | # out = self.one_hot(out, self.flags.linear_b[0]) 116 | if self.convs_b: 117 | out = out.unsqueeze(1) 118 | # For the Conv Layers 119 | for ind, conv in enumerate(self.convs_b): 120 | out = conv(out) 121 | 122 | out = out.squeeze(1) 123 | # For the linear part 124 | for ind, (fc, bn) in enumerate(zip(self.linears_b, self.bn_linears_b)): 125 | if ind != len(self.linears_b) - 1: 126 | out = F.relu(bn(fc(out))) 127 | else: 128 | out = fc(out) 129 | G = out 130 | return G 131 | -------------------------------------------------------------------------------- /demo/environment.yml: -------------------------------------------------------------------------------- 1 | name: benicml 2 | channels: 3 | - pytorch 4 | - conda-forge 5 | - defaults 6 | dependencies: 7 | - _libgcc_mutex=0.1=conda_forge 8 | - _openmp_mutex=4.5=1_llvm 9 | - absl-py=1.0.0=pyhd8ed1ab_0 10 | - aom=3.2.0=h9c3ff4c_2 11 | - argcomplete=1.12.3=pyhd8ed1ab_2 12 | - backcall=0.2.0=pyh9f0ad1d_0 13 | - backports=1.0=py_2 14 | - backports.functools_lru_cache=1.6.4=pyhd8ed1ab_0 15 | - bzip2=1.0.8=h7f98852_4 16 | - c-ares=1.18.1=h7f98852_0 17 | - ca-certificates=2021.10.8=ha878542_0 18 | - certifi=2021.10.8=py37h89c1867_1 19 | - cffi=1.15.0=py37h036bc23_0 20 | - colorama=0.4.4=pyh9f0ad1d_0 21 | - cudatoolkit=11.3.1=ha36c431_9 22 | - cudnn=8.2.1.32=h86fa8c9_0 23 | - cycler=0.11.0=pyhd8ed1ab_0 24 | - dataclasses=0.8=pyhc8e2a94_3 25 | - debugpy=1.5.1=py37hcd2ae1e_0 26 | - decorator=5.1.0=pyhd8ed1ab_0 27 | - entrypoints=0.3=pyhd8ed1ab_1003 28 | - expat=2.4.1=h9c3ff4c_0 29 | - ffmpeg=4.4.1=h6987444_0 30 | - freetype=2.10.4=h0708190_1 31 | - future=0.18.2=py37h89c1867_4 32 | - gmp=6.2.1=h58526e2_0 33 | - gnutls=3.6.13=h85f3911_1 34 | - grpcio=1.42.0=py37hd5d88b1_0 35 | - icu=69.1=h9c3ff4c_0 36 | - importlib-metadata=4.8.2=py37h89c1867_0 37 | - importlib_metadata=4.8.2=hd8ed1ab_0 38 | - intel-openmp=2021.4.0=h06a4308_3561 39 | - ipykernel=6.5.0=py37h6531663_1 40 | - ipython=7.29.0=py37h6531663_2 41 | - jbig=2.1=h7f98852_2003 42 | - jedi=0.18.1=py37h89c1867_0 43 | - jinja2=3.0.3=pyhd8ed1ab_0 44 | - joblib=1.1.0=pyhd8ed1ab_0 45 | - jpeg=9d=h36c2ea0_0 46 | - jupyter_client=7.0.6=pyhd8ed1ab_0 47 | - jupyter_core=4.9.1=py37h89c1867_1 48 | - kiwisolver=1.3.2=py37h2527ec5_1 49 | - lame=3.100=h7f98852_1001 50 | - lcms2=2.12=hddcbb42_0 51 | - ld_impl_linux-64=2.36.1=hea4e1c9_2 52 | - lerc=3.0=h9c3ff4c_0 53 | - libblas=3.9.0=12_linux64_mkl 54 | - libcblas=3.9.0=12_linux64_mkl 55 | - libdeflate=1.8=h7f98852_0 56 | - libdrm=2.4.108=h7f98852_0 57 | - libffi=3.4.2=h7f98852_5 58 | - libgcc-ng=11.2.0=h1d223b6_11 59 | - libgfortran-ng=11.2.0=h69a702a_11 60 | - libgfortran5=11.2.0=h5c6108e_11 61 | - libiconv=1.16=h516909a_0 62 | - liblapack=3.9.0=12_linux64_mkl 63 | - libllvm11=11.1.0=hf817b99_2 64 | - libnsl=2.0.0=h7f98852_0 65 | - libpciaccess=0.16=h516909a_0 66 | - libpng=1.6.37=h21135ba_2 67 | - libprotobuf=3.18.1=h780b84a_0 68 | - libsodium=1.0.18=h36c2ea0_1 69 | - libstdcxx-ng=11.2.0=he4da1e4_11 70 | - libtiff=4.3.0=h6f004c6_2 71 | - libva=2.13.0=h7f98852_0 72 | - libvpx=1.11.0=h9c3ff4c_3 73 | - libwebp-base=1.2.1=h7f98852_0 74 | - libxml2=2.9.12=h885dcf4_1 75 | - libzlib=1.2.11=h36c2ea0_1013 76 | - llvm-openmp=12.0.1=h4bd325d_1 77 | - lz4-c=1.9.3=h9c3ff4c_1 78 | - magma=2.5.4=h6103c52_2 79 | - markdown=3.3.6=pyhd8ed1ab_0 80 | - markupsafe=2.0.1=py37h5e8e339_1 81 | - matplotlib=3.3.2=0 82 | - matplotlib-base=3.3.2=py37h4f6019d_1 83 | - matplotlib-inline=0.1.3=pyhd8ed1ab_0 84 | - mesalib=18.3.1=h590aaf7_0 85 | - mkl=2021.4.0=h8d4b97c_729 86 | - nccl=2.11.4.1=hdc17891_0 87 | - ncurses=6.2=h58526e2_4 88 | - nest-asyncio=1.5.1=pyhd8ed1ab_0 89 | - nettle=3.6=he412f7d_0 90 | - ninja=1.10.2=h4bd325d_1 91 | - numpy=1.20.3=py37h038b26d_1 92 | - olefile=0.46=pyh9f0ad1d_1 93 | - openh264=2.1.1=h780b84a_0 94 | - openjpeg=2.4.0=hb52868f_1 95 | - openssl=3.0.0=h7f98852_2 96 | - pandas=1.3.4=py37he8f5f7f_1 97 | - parso=0.8.2=pyhd8ed1ab_0 98 | - pexpect=4.8.0=pyh9f0ad1d_2 99 | - pickleshare=0.7.5=py_1003 100 | - pillow=8.4.0=py37h0f21c89_0 101 | - pip=21.3.1=pyhd8ed1ab_0 102 | - prompt-toolkit=3.0.22=pyha770c72_0 103 | - protobuf=3.18.1=py37hcd2ae1e_0 104 | - ptyprocess=0.7.0=pyhd3deb0d_0 105 | - pycparser=2.21=pyhd8ed1ab_0 106 | - pygments=2.10.0=pyhd8ed1ab_0 107 | - pyparsing=3.0.6=pyhd8ed1ab_0 108 | - python=3.7.12=hf930737_100_cpython 109 | - python-dateutil=2.8.2=pyhd8ed1ab_0 110 | - python_abi=3.7=2_cp37m 111 | - pytorch=1.10.0=cuda112py37h3bec1eb_0 112 | - pytorch-gpu=1.10.0=cuda112py37h0bbbad9_0 113 | - pytz=2021.3=pyhd8ed1ab_0 114 | - pyzmq=22.3.0=py37h336d617_1 115 | - readline=8.1=h46c0cb4_0 116 | - scikit-learn=1.0.1=py37hf9e9bfc_2 117 | - scipy=1.7.2=py37hf2a6cf1_0 118 | - seaborn=0.11.2=hd8ed1ab_0 119 | - seaborn-base=0.11.2=pyhd8ed1ab_0 120 | - setuptools=59.2.0=py37h89c1867_0 121 | - six=1.16.0=pyh6c4a22f_0 122 | - sleef=3.5.1=h9b69904_2 123 | - sqlite=3.36.0=h9cd32fc_2 124 | - svt-av1=0.8.7=h9c3ff4c_1 125 | - tbb=2021.4.0=h4bd325d_1 126 | - tensorboard=1.15.0=py37_0 127 | - threadpoolctl=3.0.0=pyh8a188c0_0 128 | - tk=8.6.11=h27826a3_1 129 | - torchaudio=0.10.0=py37_cu113 130 | - tornado=6.1=py37h5e8e339_2 131 | - tqdm=4.62.3=pyhd8ed1ab_0 132 | - traitlets=5.1.1=pyhd8ed1ab_0 133 | - typing_extensions=4.0.0=pyha770c72_0 134 | - wcwidth=0.2.5=pyh9f0ad1d_2 135 | - werkzeug=2.0.1=pyhd8ed1ab_0 136 | - wheel=0.37.0=pyhd8ed1ab_1 137 | - x264=1!161.3030=h7f98852_1 138 | - x265=3.5=h4bd325d_1 139 | - xz=5.2.5=h516909a_1 140 | - zeromq=4.3.4=h9c3ff4c_1 141 | - zipp=3.6.0=pyhd8ed1ab_0 142 | - zlib=1.2.11=h36c2ea0_1013 143 | - zstd=1.5.0=ha95c52a_0 144 | - pip: 145 | - baryrat==1.4.0 146 | - bleach==4.1.0 147 | - bokeh==2.3.3 148 | - cloudpickle==2.0.0 149 | - colorcet==2.0.6 150 | - cython==0.29.23 151 | - dask==2021.9.0 152 | - datashader==0.13.0 153 | - datashape==0.5.2 154 | - distributed==2021.9.0 155 | - dominate==2.6.0 156 | - fsspec==2021.8.1 157 | - heapdict==1.0.1 158 | - holoviews==1.14.5 159 | - imageio==2.9.0 160 | - jsonpatch==1.32 161 | - jsonpointer==2.1 162 | - llvmlite==0.37.0 163 | - locket==0.2.1 164 | - msgpack==1.0.2 165 | - multipledispatch==0.6.0 166 | - networkx==2.6.3 167 | - numba==0.54.0 168 | - packaging==21.0 169 | - panel==0.12.1 170 | - param==1.11.1 171 | - partd==1.2.0 172 | - patsy==0.5.1 173 | - psutil==5.8.0 174 | - pyct==0.4.8 175 | - pynndescent==0.5.4 176 | - pyviz-comms==2.1.0 177 | - pywavelets==1.1.1 178 | - pyyaml==5.4.1 179 | - scikit-image==0.18.3 180 | - sortedcontainers==2.4.0 181 | - statsmodels==0.12.2 182 | - tblib==1.7.0 183 | - thop==0.0.31-2005241907 184 | - tifffile==2021.8.30 185 | - toolz==0.11.1 186 | - torch==1.8.1 187 | - torch-tb-profiler==0.2.1 188 | - torchfile==0.1.0 189 | - torchvision==0.9.1 190 | - umap==0.1.1 191 | - umap-learn==0.5.1 192 | - visdom==0.1.8.9 193 | - webencodings==0.5.1 194 | - websocket-client==1.0.1 195 | - xarray==0.19.0 196 | - zict==2.0.0 197 | prefix: /home/sr365/anaconda3/envs/benicml 198 | -------------------------------------------------------------------------------- /MDN/evaluate.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file serves as a evaluation interface for the network 3 | """ 4 | # Built in 5 | import os 6 | # Torch 7 | 8 | # Own 9 | import flag_reader 10 | from class_wrapper import Network 11 | from mdn_nips import MDN 12 | from utils import data_reader 13 | from utils import helper_functions 14 | from utils.evaluation_helper import plotMSELossDistrib 15 | from utils.evaluation_helper import get_test_ratio_helper 16 | import NA.predict as napredict 17 | 18 | # Libs 19 | import numpy as np 20 | import matplotlib.pyplot as plt 21 | 22 | def predict(model_dir, Ytruth_file ,multi_flag=False): 23 | """ 24 | Predict the output from given spectra 25 | """ 26 | print("Retrieving flag object for parameters") 27 | if (model_dir.startswith("models")): 28 | model_dir = model_dir[7:] 29 | print("after removing prefix models/, now model_dir is:", model_dir) 30 | if model_dir.startswith('/'): # It is a absolute path 31 | flags = helper_functions.load_flags(model_dir) 32 | else: 33 | flags = helper_functions.load_flags(os.path.join("models", model_dir)) 34 | flags.eval_model = model_dir # Reset the eval mode 35 | 36 | ntwk = Network(MDN, flags, train_loader=None, test_loader=None, inference_mode=True, saved_model=flags.eval_model) 37 | print("number of trainable parameters is :") 38 | pytorch_total_params = sum(p.numel() for p in ntwk.model.parameters() if p.requires_grad) 39 | print(pytorch_total_params) 40 | # Evaluation process 41 | pred_file, truth_file = ntwk.predict(Ytruth_file) 42 | if 'Yang' not in flags.data_set and 'Peurifoy' not in flags.data_set: 43 | plotMSELossDistrib(pred_file, truth_file, flags) 44 | 45 | 46 | def predict_different_dataset(multi_flag=False): 47 | """ 48 | This function is to evaluate all different datasets in the model with one function call 49 | """ 50 | step_func_dir = '/home/sr365/MM_Bench/Data/step_func' 51 | for model in os.listdir('models/'): 52 | if 'best' in model: 53 | if 'Yang' in model: 54 | Ytruth_file = os.path.join(step_func_dir, 'Yang'+'step_function.txt') 55 | elif 'Chen' in model: 56 | Ytruth_file = os.path.join(step_func_dir, 'Chen'+'step_function.txt') 57 | elif 'Peurifoy' in model: 58 | Ytruth_file = os.path.join(step_func_dir, 'Peurifoy'+'step_function.txt') 59 | predict(model, Ytruth_file, multi_flag=multi_flag) 60 | 61 | 62 | def evaluate_from_model(model_dir, multi_flag=False, eval_data_all=False, modulized_flag=False): 63 | """ 64 | Evaluating interface. 1. Retreive the flags 2. get data 3. initialize network 4. eval 65 | :param model_dir: The folder to retrieve the model 66 | :param eval_data_all: The switch to turn on if you want to put all data in evaluation data 67 | :return: None 68 | """ 69 | # Retrieve the flag object 70 | print("Retrieving flag object for parameters") 71 | if (model_dir.startswith("models")): 72 | model_dir = model_dir[7:] 73 | print("after removing prefix models/, now model_dir is:", model_dir) 74 | if model_dir.startswith('/'): # It is a absolute path 75 | flags = helper_functions.load_flags(model_dir) 76 | else: 77 | flags = helper_functions.load_flags(os.path.join("models", model_dir)) 78 | flags.eval_model = model_dir # Reset the eval mode 79 | flags.test_ratio = get_test_ratio_helper(flags) 80 | 81 | # Get the data 82 | train_loader, test_loader = data_reader.read_data(flags, eval_data_all=eval_data_all) 83 | print("Making network now") 84 | 85 | # Make Network 86 | ntwk = Network(MDN, flags, train_loader, test_loader, inference_mode=True, saved_model=flags.eval_model) 87 | print(model_dir) 88 | print("number of trainable parameters is :") 89 | pytorch_total_params = sum(p.numel() for p in ntwk.model.parameters() if p.requires_grad) 90 | print(pytorch_total_params) 91 | # Evaluation process 92 | print("Start eval now:") 93 | if multi_flag: 94 | ntwk.evaluate_multiple_time() 95 | else: 96 | pred_file, truth_file = ntwk.evaluate() 97 | 98 | print("Evaluation finished") 99 | 100 | 101 | def evaluate_all(models_dir="models"): 102 | """ 103 | This function evaluate all the models in the models/. directory 104 | :return: None 105 | """ 106 | for file in os.listdir(models_dir): 107 | if os.path.isfile(os.path.join(models_dir, file, 'flags.obj')): 108 | evaluate_from_model(os.path.join(models_dir, file)) 109 | return None 110 | 111 | 112 | def evaluate_different_dataset(multi_flag=False, eval_data_all=False, modulized_flag=False): 113 | """ 114 | This function is to evaluate all different datasets in the model with one function call 115 | """ 116 | for model in os.listdir('models/'): 117 | if 'new_best' in model and 'Peurifoy' in model: 118 | evaluate_from_model(model, multi_flag=multi_flag, 119 | eval_data_all=eval_data_all, modulized_flag=modulized_flag) 120 | 121 | 122 | if __name__ == '__main__': 123 | # Read the flag, however only the flags.eval_model is used and others are not used 124 | useless_flags = flag_reader.read_flag() 125 | 126 | print(useless_flags.eval_model) 127 | ########################## 128 | #Single model evaluation # 129 | ########################## 130 | ### Call the evaluate function from model, this "evaluate_from_model" uses the eval_model field in your 131 | ### "useless_flag" that reads out from your current parameters.py file in case you want to evaluate single model 132 | #evaluate_from_model(useless_flags.eval_model) 133 | #evaluate_from_model(useless_flags.eval_model, multi_flag=True) 134 | #evaluate_from_model(useless_flags.eval_model, multi_flag=False, eval_data_all=True) 135 | 136 | #evaluate_from_model("models/Peurifoy_best_model", multi_flag=False, eval_data_all=True) 137 | 138 | ############################ 139 | #Multiple model evaluation # 140 | ############################ 141 | ### Call the "evaluate_different_dataset" function to evaluate all the models in the "models" folder, the multi_flag is to control whether evaulate across T or only do T=1 (if set to False), make sure you change the model name in function if you have any different model name 142 | # evaluate_different_dataset(multi_flag=False, eval_data_all=False) 143 | evaluate_different_dataset(multi_flag=True, eval_data_all=False) 144 | 145 | ########### 146 | # Predict # 147 | ########### 148 | #predict_different_dataset(multi_flag=False) 149 | -------------------------------------------------------------------------------- /INN_FrEIA/evaluate.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file serves as a evaluation interface for the network 3 | """ 4 | # Built in 5 | import os 6 | # Torch 7 | 8 | # Own 9 | import flag_reader 10 | from class_wrapper import Network 11 | from model_maker import INN 12 | from utils import data_reader 13 | from utils import helper_functions 14 | from utils.evaluation_helper import plotMSELossDistrib 15 | from utils.evaluation_helper import get_test_ratio_helper 16 | from NA import predict 17 | 18 | # Libs 19 | 20 | def predict(model_dir, Ytruth_file ,multi_flag=False): 21 | """ 22 | Predict the output from given spectra 23 | """ 24 | print("Retrieving flag object for parameters") 25 | if (model_dir.startswith("models")): 26 | model_dir = model_dir[7:] 27 | print("after removing prefix models/, now model_dir is:", model_dir) 28 | if model_dir.startswith('/'): # It is a absolute path 29 | flags = helper_functions.load_flags(model_dir) 30 | else: 31 | flags = helper_functions.load_flags(os.path.join("models", model_dir)) 32 | flags.eval_model = model_dir # Reset the eval mode 33 | 34 | ntwk = Network(INN, flags, train_loader=None, test_loader=None, inference_mode=True, saved_model=flags.eval_model) 35 | print("number of trainable parameters is :") 36 | pytorch_total_params = sum(p.numel() for p in ntwk.model.parameters() if p.requires_grad) 37 | print(pytorch_total_params) 38 | # Evaluation process 39 | pred_file, truth_file = ntwk.predict(Ytruth_file) 40 | if 'Yang' not in flags.data_set: 41 | plotMSELossDistrib(pred_file, truth_file, flags) 42 | 43 | 44 | def predict_different_dataset(multi_flag=False): 45 | """ 46 | This function is to evaluate all different datasets in the model with one function call 47 | """ 48 | step_func_dir = '/home/sr365/MM_Bench/Data/step_func' 49 | for model in os.listdir('models/'): 50 | if 'best' in model: 51 | if 'Yang' in model: 52 | Ytruth_file = os.path.join(step_func_dir, 'Yang'+'step_function.txt') 53 | elif 'Chen' in model: 54 | Ytruth_file = os.path.join(step_func_dir, 'Chen'+'step_function.txt') 55 | elif 'Peurifoy' in model: 56 | Ytruth_file = os.path.join(step_func_dir, 'Peurifoy'+'step_function.txt') 57 | predict(model, Ytruth_file, multi_flag=multi_flag) 58 | 59 | 60 | def evaluate_from_model(model_dir, multi_flag=False, eval_data_all=False, modulized_flag=False): 61 | """ 62 | Evaluating interface. 1. Retreive the flags 2. get data 3. initialize network 4. eval 63 | :param model_dir: The folder to retrieve the model 64 | :param eval_data_all: The switch to turn on if you want to put all data in evaluation data 65 | :return: None 66 | """ 67 | # Retrieve the flag object 68 | print("Retrieving flag object for parameters") 69 | if (model_dir.startswith("models")): 70 | model_dir = model_dir[7:] 71 | print("after removing prefix models/, now model_dir is:", model_dir) 72 | flags = helper_functions.load_flags(os.path.join("models", model_dir)) 73 | flags.eval_model = model_dir # Reset the eval mode 74 | 75 | flags.test_ratio = get_test_ratio_helper(flags) 76 | 77 | # Get the data 78 | train_loader, test_loader = data_reader.read_data(flags, eval_data_all=eval_data_all) 79 | print("Making network now") 80 | 81 | # Make Network 82 | ntwk = Network(INN, flags, train_loader, test_loader, inference_mode=True, saved_model=flags.eval_model) 83 | print(ntwk.ckpt_dir) 84 | print("number of trainable parameters is :") 85 | pytorch_total_params = sum(p.numel() for p in ntwk.model.parameters() if p.requires_grad) 86 | print(pytorch_total_params) 87 | # Evaluation process 88 | print("Start eval now:") 89 | if multi_flag: 90 | ntwk.evaluate_multiple_time() 91 | else: 92 | pred_file, truth_file = ntwk.evaluate() 93 | 94 | # # Plot the MSE distribution 95 | # if flags.data_set != 'Yang_sim' and not multi_flag and not modulized_flag: # meta-material does not have simulator, hence no Ypred given 96 | # MSE = plotMSELossDistrib(pred_file, truth_file, flags) 97 | # # Add this MSE back to the folder 98 | # flags.best_validation_loss = MSE 99 | # helper_functions.save_flags(flags, os.path.join("models", model_dir)) 100 | # elif flags.data_set == 'Yang_sim' and not multi_flag and not modulized_flag: 101 | # # Save the current path for getting back in the future 102 | # cwd = os.getcwd() 103 | # abs_path_Xpred = os.path.abspath(pred_file.replace('Ypred','Xpred')) 104 | # # Change to NA dictory to do prediction 105 | # os.chdir('../NA/') 106 | # MSE = predict.ensemble_predict_master('../Data/Yang_sim/state_dicts/', 107 | # abs_path_Xpred, no_plot=False) 108 | # # Add this MSE back to the folder 109 | # flags.best_validation_loss = MSE 110 | # os.chdir(cwd) 111 | # helper_functions.save_flags(flags, os.path.join("models", model_dir)) 112 | print("Evaluation finished") 113 | 114 | def evaluate_all(models_dir="models"): 115 | """ 116 | This function evaluate all the models in the models/. directory 117 | :return: None 118 | """ 119 | for file in os.listdir(models_dir): 120 | if not os.path.isfile(os.path.join(models_dir, file, 'flags.obj')): 121 | continue 122 | try: 123 | evaluate_from_model(os.path.join(models_dir, file)) 124 | except: 125 | print("Your current evalutation failed!") 126 | print(file) 127 | return None 128 | 129 | def evaluate_different_dataset(multi_flag=False, eval_data_all=False, modulized_flag=False): 130 | """ 131 | This function is to evaluate all different datasets in the model with one function call 132 | """ 133 | ## Evaluate all models with "reatrain" and dataset name in models/ 134 | for model in os.listdir('models/'): 135 | if 'new_best' in model and 'Peurifoy' in model: 136 | evaluate_from_model(model, multi_flag=multi_flag, 137 | eval_data_all=eval_data_all, modulized_flag=modulized_flag) 138 | 139 | 140 | if __name__ == '__main__': 141 | # Read the flag, however only the flags.eval_model is used and others are not used 142 | useless_flags = flag_reader.read_flag() 143 | 144 | print(useless_flags.eval_model) 145 | #evaluate_from_model("models/Peurifoy_best_model") 146 | #evaluate_from_model(useless_flags.eval_model, multi_flag=True) 147 | #evaluate_from_model(useless_flags.eval_model, multi_flag=False, eval_data_all=True) 148 | 149 | 150 | ############################################## 151 | # evaluate multiple dataset at the same time!# 152 | ############################################## 153 | #evaluate_different_dataset(multi_flag=False, eval_data_all=False) 154 | # evaluate_different_dataset(multi_flag=False, eval_data_all=False) 155 | evaluate_different_dataset(multi_flag=True, eval_data_all=False) 156 | 157 | #evaluate_different_dataset(modulized_flag=True) 158 | 159 | #evaluate_different_dataset(multi_flag=True) 160 | #evaluate_all("models/Peurifoy_layer_9/") 161 | # evaluate_all("models/Peurifoy_mid_layer_1024/") 162 | 163 | 164 | # Call the evaluate function from model 165 | #evaluate_from_model(useless_flags.eval_model) 166 | 167 | ########### 168 | # Predict # 169 | ########### 170 | #predict_different_dataset(multi_flag=False) 171 | -------------------------------------------------------------------------------- /VAE/evaluate.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file serves as a evaluation interface for the network 3 | """ 4 | # Built in 5 | import os 6 | # Torch 7 | 8 | # Own 9 | import flag_reader 10 | from class_wrapper import Network 11 | from model_maker import VAE 12 | from utils import data_reader 13 | from utils import helper_functions 14 | from utils.evaluation_helper import plotMSELossDistrib 15 | from utils.evaluation_helper import get_test_ratio_helper 16 | # Libs 17 | import NA.predict as napredict 18 | 19 | 20 | def predict(model_dir, Ytruth_file ,multi_flag=False): 21 | """ 22 | Predict the output from given spectra 23 | """ 24 | print("Retrieving flag object for parameters") 25 | if (model_dir.startswith("models")): 26 | model_dir = model_dir[7:] 27 | print("after removing prefix models/, now model_dir is:", model_dir) 28 | if model_dir.startswith('/'): # It is a absolute path 29 | flags = helper_functions.load_flags(model_dir) 30 | else: 31 | flags = helper_functions.load_flags(os.path.join("models", model_dir)) 32 | flags.eval_model = model_dir # Reset the eval mode 33 | 34 | ntwk = Network(VAE, flags, train_loader=None, test_loader=None, 35 | inference_mode=True, saved_model=flags.eval_model) 36 | print("number of trainable parameters is :") 37 | pytorch_total_params = sum(p.numel() for p in ntwk.model.parameters() if p.requires_grad) 38 | print(pytorch_total_params) 39 | # Evaluation process 40 | #pred_file, truth_file = ntwk.predict(Ytruth_file) 41 | #if 'Yang' not in flags.data_set: 42 | # plotMSELossDistrib(pred_file, truth_file, flags) 43 | 44 | 45 | def predict_different_dataset(multi_flag=False): 46 | """ 47 | This function is to evaluate all different datasets in the model with one function call 48 | """ 49 | step_func_dir = '/home/sr365/MM_Bench/Data/step_func' 50 | for model in os.listdir('models/'): 51 | if 'best' in model: 52 | if 'Yang' in model: 53 | Ytruth_file = os.path.join(step_func_dir, 'Yang'+'step_function.txt') 54 | elif 'Chen' in model: 55 | Ytruth_file = os.path.join(step_func_dir, 'Chen'+'step_function.txt') 56 | elif 'Peurifoy' in model: 57 | Ytruth_file = os.path.join(step_func_dir, 'Peurifoy'+'step_function.txt') 58 | predict(model, Ytruth_file, multi_flag=multi_flag) 59 | 60 | 61 | def evaluate_from_model(model_dir, multi_flag=False, eval_data_all=False, modulized_flag=False): 62 | """ 63 | Evaluating interface. 1. Retreive the flags 2. get data 3. initialize network 4. eval 64 | :param model_dir: The folder to retrieve the model 65 | :return: None 66 | """ 67 | # Retrieve the flag object 68 | print("Retrieving flag object for parameters") 69 | if (model_dir.startswith("models")): 70 | model_dir = model_dir[7:] 71 | print("after removing prefix models/, now model_dir is:", model_dir) 72 | flags = helper_functions.load_flags(os.path.join("models", model_dir)) 73 | flags.eval_model = model_dir # Reset the eval mode 74 | 75 | flags.test_ratio = get_test_ratio_helper(flags) 76 | # Get the data 77 | train_loader, test_loader = data_reader.read_data(flags, eval_data_all=eval_data_all) 78 | print("Making network now") 79 | 80 | # Make Network 81 | ntwk = Network(VAE, flags, train_loader, test_loader, inference_mode=True, saved_model=flags.eval_model) 82 | print("number of trainable parameters is :") 83 | pytorch_total_params = sum(p.numel() for p in ntwk.model.parameters() if p.requires_grad) 84 | print(pytorch_total_params) 85 | # Evaluation process 86 | print("Start eval now:") 87 | if multi_flag: 88 | ntwk.evaluate_multiple_time() 89 | else: 90 | pred_file, truth_file = ntwk.evaluate() 91 | 92 | # # Plot the MSE distribution 93 | # if flags.data_set != 'Yang_sim' and not multi_flag and not modulized_flag: # meta-material does not have simulator, hence no Ypred given 94 | # MSE = plotMSELossDistrib(pred_file, truth_file, flags) 95 | # # Add this MSE back to the folder 96 | # flags.best_validation_loss = MSE 97 | # helper_functions.save_flags(flags, os.path.join("models", model_dir)) 98 | # elif flags.data_set == 'Yang_sim' and not multi_flag and not modulized_flag: 99 | # # Save the current path for getting back in the future 100 | # cwd = os.getcwd() 101 | # abs_path_Xpred = os.path.abspath(pred_file.replace('Ypred','Xpred')) 102 | # # Change to NA dictory to do prediction 103 | # os.chdir('../NA/') 104 | # MSE = napredict.ensemble_predict_master('../Data/Yang_sim/state_dicts/', 105 | # abs_path_Xpred, no_plot=False) 106 | # # Add this MSE back to the folder 107 | # flags.best_validation_loss = MSE 108 | # os.chdir(cwd) 109 | # helper_functions.save_flags(flags, os.path.join("models", model_dir)) 110 | print("Evaluation finished") 111 | 112 | 113 | def evaluate_all(models_dir="models"): 114 | """ 115 | This function evaluate all the models in the models/. directory 116 | :return: None 117 | """ 118 | for file in os.listdir(models_dir): 119 | if os.path.isfile(os.path.join(models_dir, file, 'flags.obj')): 120 | evaluate_from_model(os.path.join(models_dir, file)) 121 | return None 122 | 123 | 124 | def evaluate_different_dataset(multi_flag=False, eval_data_all=False, modulized_flag=False): 125 | """ 126 | This function is to evaluate all different datasets in the model with one function call 127 | """ 128 | for model in os.listdir('models/'): 129 | if 'new_best' in model and 'Peurifoy' in model: 130 | evaluate_from_model(model, multi_flag=multi_flag, 131 | eval_data_all=eval_data_all, modulized_flag=modulized_flag) 132 | 133 | if __name__ == '__main__': 134 | # Read the flag, however only the flags.eval_model is used and others are not used 135 | useless_flags = flag_reader.read_flag() 136 | 137 | print(useless_flags.eval_model) 138 | ########################## 139 | #Single model evaluation # 140 | ########################## 141 | ### Call the evaluate function from model, this "evaluate_from_model" uses the eval_model field in your 142 | ### "useless_flag" that reads out from your current parameters.py file in case you want to evaluate single model 143 | #evaluate_from_model(useless_flags.eval_model) 144 | #evaluate_from_model(useless_flags.eval_model, multi_flag=True) 145 | #evaluate_from_model(useless_flags.eval_model, multi_flag=False, eval_data_all=True) 146 | 147 | #evaluate_from_model("models/Peurifoy_best_model") 148 | 149 | ############################ 150 | #Multiple model evaluation # 151 | ############################ 152 | ### Call the "evaluate_different_dataset" function to evaluate all the models in the "models" folder, the multi_flag is to control whether evaulate across T or only do T=1 (if set to False), make sure you change the model name in function if you have any different model name 153 | # evaluate_different_dataset(multi_flag=False, eval_data_all=False) 154 | # evaluate_different_dataset(multi_flag=False, eval_data_all=False) 155 | evaluate_different_dataset(multi_flag=True, eval_data_all=False) 156 | #evaluate_different_dataset(multi_flag=True) 157 | # evaluate_all("models/Yang_sim_large/") 158 | #evaluate_all("models/Peurifoy_4th/") 159 | 160 | ########### 161 | # Predict # 162 | ########### 163 | #predict_different_dataset(multi_flag=False) 164 | -------------------------------------------------------------------------------- /Tandem/evaluate.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file serves as a evaluation interface for the network 3 | """ 4 | # Built in 5 | import os 6 | import sys 7 | sys.path.append('../utils/') 8 | # Torch 9 | 10 | # Own 11 | import flag_reader 12 | from class_wrapper import Network 13 | from model_maker import Forward, Backward 14 | from utils import data_reader,helper_functions 15 | from utils.helper_functions import load_flags 16 | from utils.evaluation_helper import plotMSELossDistrib 17 | from utils.evaluation_helper import get_test_ratio_helper 18 | 19 | # Libs 20 | from NA import predict 21 | 22 | 23 | def predict(model_dir, Ytruth_file ,multi_flag=False): 24 | """ 25 | Predict the output from given spectra 26 | """ 27 | print("Retrieving flag object for parameters") 28 | if (model_dir.startswith("models")): 29 | model_dir = model_dir[7:] 30 | print("after removing prefix models/, now model_dir is:", model_dir) 31 | if model_dir.startswith('/'): # It is a absolute path 32 | flags = helper_functions.load_flags(model_dir) 33 | else: 34 | flags = helper_functions.load_flags(os.path.join("models", model_dir)) 35 | flags.eval_model = model_dir # Reset the eval mode 36 | 37 | ntwk = Network(Forward, Backward, flags, train_loader=None, test_loader=None, inference_mode=True, saved_model=flags.eval_model) 38 | print("number of trainable parameters is :") 39 | pytorch_total_params = sum(p.numel() for p in ntwk.model_b.parameters() if p.requires_grad) 40 | print(pytorch_total_params) 41 | # Evaluation process 42 | pred_file, truth_file = ntwk.predict(Ytruth_file) 43 | if 'Yang' not in flags.data_set: 44 | plotMSELossDistrib(pred_file, truth_file, flags) 45 | 46 | 47 | def predict_different_dataset(multi_flag=False): 48 | """ 49 | This function is to evaluate all different datasets in the model with one function call 50 | """ 51 | step_func_dir = '/home/sr365/MM_Bench/Data/step_func' 52 | for model in os.listdir('models/'): 53 | if 'best' in model: 54 | if 'Yang' in model: 55 | Ytruth_file = os.path.join(step_func_dir, 'Yang'+'step_function.txt') 56 | elif 'Chen' in model: 57 | Ytruth_file = os.path.join(step_func_dir, 'Chen'+'step_function.txt') 58 | elif 'Peurifoy' in model: 59 | Ytruth_file = os.path.join(step_func_dir, 'Peurifoy'+'step_function.txt') 60 | predict(model, Ytruth_file, multi_flag=multi_flag) 61 | 62 | 63 | def evaluate_from_model(model_dir, multi_flag=False, eval_data_all=False, modulized_flag=False): 64 | """ 65 | Evaluating interface. 1. Retreive the flags 2. get data 3. initialize network 4. eval 66 | :param model_dir: The folder to retrieve the model 67 | :param eval_data_all: The switch to turn on if you want to put all data in evaluation data 68 | :return: None 69 | """ 70 | # Retrieve the flag object 71 | print("Retrieving flag object for parameters") 72 | if (model_dir.startswith("models")): 73 | model_dir = model_dir[7:] 74 | print("after removing prefix models/, now model_dir is:", model_dir) 75 | flags = load_flags(os.path.join("models", model_dir)) 76 | flags.eval_model = model_dir # Reset the eval mode 77 | flags.test_ratio = get_test_ratio_helper(flags) 78 | 79 | # Get the data 80 | train_loader, test_loader = data_reader.read_data(flags, eval_data_all=eval_data_all) 81 | print("Making network now") 82 | 83 | # Make Network 84 | ntwk = Network(Forward, Backward, flags, train_loader, test_loader, inference_mode=True, saved_model=flags.eval_model) 85 | print("number of trainable parameters is :") 86 | pytorch_total_params = sum(p.numel() for p in ntwk.model_f.parameters() if p.requires_grad) +\ 87 | sum(p.numel() for p in ntwk.model_b.parameters() if p.requires_grad) 88 | print(pytorch_total_params) 89 | # Evaluation process 90 | print("Start eval now:") 91 | if multi_flag: 92 | ntwk.evaluate_multiple_time() 93 | else: 94 | pred_file, truth_file = ntwk.evaluate() 95 | 96 | # # Plot the MSE distribution 97 | # if flags.data_set != 'Yang_sim' and not multi_flag and not modulized_flag: # meta-material does not have simulator, hence no Ypred given 98 | # MSE = plotMSELossDistrib(pred_file, truth_file, flags) 99 | # # Add this MSE back to the folder 100 | # flags.best_validation_loss = MSE 101 | # helper_functions.save_flags(flags, os.path.join("models", model_dir)) 102 | # elif flags.data_set == 'Yang_sim' and not multi_flag and not modulized_flag: 103 | # # Save the current path for getting back in the future 104 | # cwd = os.getcwd() 105 | # abs_path_Xpred = os.path.abspath(pred_file.replace('Ypred','Xpred')) 106 | # # Change to NA dictory to do prediction 107 | # os.chdir('../NA/') 108 | # MSE = predict.ensemble_predict_master('../Data/Yang_sim/state_dicts/', 109 | # abs_path_Xpred, no_plot=False) 110 | # # Add this MSE back to the folder 111 | # flags.best_validation_loss = MSE 112 | # os.chdir(cwd) 113 | # helper_functions.save_flags(flags, os.path.join("models", model_dir)) 114 | print("Evaluation finished") 115 | 116 | 117 | def evaluate_all(models_dir="models"): 118 | """ 119 | This function evaluate all the models in the models/. directory 120 | :return: None 121 | """ 122 | for file in os.listdir(models_dir): 123 | if os.path.isfile(os.path.join(models_dir, file, 'flags.obj')): 124 | evaluate_from_model(os.path.join(models_dir, file)) 125 | return None 126 | 127 | 128 | def evaluate_different_dataset(multi_flag=False, eval_data_all=False, modulized_flag=False): 129 | """ 130 | This function is to evaluate all different datasets in the model with one function call 131 | """ 132 | for model in os.listdir('models/'): 133 | if 'new_best' in model and 'Peu' in model: 134 | evaluate_from_model(model, multi_flag=multi_flag, 135 | eval_data_all=eval_data_all, modulized_flag=modulized_flag) 136 | 137 | if __name__ == '__main__': 138 | # Read the flag, however only the flags.eval_model is used and others are not used 139 | useless_flags = flag_reader.read_flag() 140 | 141 | print(useless_flags.eval_model) 142 | ########################## 143 | #Single model evaluation # 144 | ########################## 145 | ### Call the evaluate function from model, this "evaluate_from_model" uses the eval_model field in your 146 | ### "useless_flag" that reads out from your current parameters.py file in case you want to evaluate single model 147 | #evaluate_from_model(useless_flags.eval_model) 148 | #evaluate_from_model("models/Peurifoy_best_model") 149 | #evaluate_from_model(useless_flags.eval_model, multi_flag=True) 150 | #evaluate_from_model(useless_flags.eval_model, multi_flag=False, eval_data_all=True) 151 | 152 | ############################ 153 | #Multiple model evaluation # 154 | ############################ 155 | ### Call the "evaluate_different_dataset" function to evaluate all the models in the "models" folder, the multi_flag is to control whether evaulate across T or only do T=1 (if set to False), make sure you change the model name in function if you have any different model name 156 | evaluate_different_dataset(multi_flag=False, eval_data_all=False) 157 | # evaluate_different_dataset(multi_flag=True) 158 | #evaluate_all("models/MM") 159 | 160 | ########### 161 | # Predict # 162 | ########### 163 | #predict_different_dataset(multi_flag=False) 164 | -------------------------------------------------------------------------------- /MDN/mdn_manu_joseph.py: -------------------------------------------------------------------------------- 1 | #Sample Implementation for educational purposes 2 | #For full implementation check out https://github.com/manujosephv/pytorch_tabular 3 | # Adapted from 4 | # https://deep-and-shallow.com/2021/03/20/mixture-density-networks-probabilistic-regression-for-uncertainty-estimation/ 5 | import torch 6 | import math 7 | import torch 8 | import torch.nn as nn 9 | import torch.optim as optim 10 | from torch.autograd import Variable 11 | from torch.distributions import Categorical 12 | import numpy as np 13 | #from numpy.random.Generator import multivariate_normal 14 | from torch.distributions.multivariate_normal import MultivariateNormal 15 | ONEOVERSQRT2PI = 1.0 / math.sqrt(2 * math.pi) 16 | LOG2PI = math.log(2 * math.pi) 17 | 18 | class MDN(nn.Module): 19 | def __init__(self, flags): 20 | self.hparams = flags 21 | self.hparams.input_dim = flags.linear[0] 22 | self.hparams.sigma_bias_flag = True 23 | self.hparams.mu_bias_init = None 24 | 25 | super().__init__() 26 | self._build_network() 27 | 28 | def _build_network(self): 29 | in_features, out_features, num_gaussians = self.hparams.linear[0], self.hparams.linear[-1], self.hparams.num_gaussian 30 | # self.pi = nn.Linear(self.hparams.input_dim, self.hparams.num_gaussian) 31 | # nn.init.normal_(self.pi.weight) 32 | self.sigma = nn.ModuleList([]) 33 | for ind, fc_num in enumerate(self.hparams.linear[:-2]): 34 | self.sigma.append(nn.Linear(fc_num, self.hparams.linear[ind + 1])) 35 | # self.sigma.append(nn.BatchNorm1d(flags.linear[ind + 1])) 36 | self.sigma.append(nn.ELU()) 37 | self.sigma.append(nn.Linear(self.hparams.linear[-2], out_features*num_gaussians)) 38 | self.sigma = nn.Sequential(*self.sigma) 39 | 40 | self.mu = nn.ModuleList([]) 41 | for ind, fc_num in enumerate(self.hparams.linear[:-2]): 42 | self.mu.append(nn.Linear(fc_num, self.hparams.linear[ind + 1])) 43 | # self.mu.append(nn.BatchNorm1d(flags.linear[ind + 1])) 44 | self.mu.append(nn.ELU()) 45 | self.mu.append(nn.Linear(self.hparams.linear[-2], out_features*num_gaussians)) 46 | self.mu = nn.Sequential(*self.mu) 47 | self.pi = nn.ModuleList([]) 48 | for ind, fc_num in enumerate(self.hparams.linear[:-2]): 49 | self.pi.append(nn.Linear(fc_num, self.hparams.linear[ind + 1])) 50 | # self.pi.append(nn.BatchNorm1d(flags.linear[ind + 1])) 51 | self.pi.append(nn.ELU()) 52 | self.pi.append(nn.Linear(self.hparams.linear[-2], num_gaussians)) 53 | self.pi = nn.Sequential(*self.pi) 54 | self.out_features, self.num_gaussians = out_features, num_gaussians 55 | # self.sigma = nn.Linear( 56 | # self.hparams.input_dim, 57 | # self.hparams.num_gaussian, 58 | # bias=self.hparams.sigma_bias_flag, 59 | # ) 60 | # self.mu = nn.Linear(self.hparams.input_dim, self.hparams.num_gaussian) 61 | # nn.init.normal_(self.mu.weight) 62 | # if self.hparams.mu_bias_init is not None: 63 | # for i, bias in enumerate(self.hparams.mu_bias_init): 64 | # nn.init.constant_(self.mu.bias[i], bias) 65 | 66 | def forward(self, x): 67 | pi = self.pi(x) 68 | pi = nn.functional.gumbel_softmax(pi, tau=1, dim=-1) + 1e-15 69 | sigma = self.sigma(x) 70 | # Applying modified ELU activation 71 | sigma = nn.ELU()(sigma) + 1 + 1e-3 72 | sigma = sigma.view(-1, self.num_gaussians, self.out_features) 73 | if torch.any(sigma < 0): 74 | sigma = sigma.view(-1, 1) 75 | print('There is sigma smaller than 0!') 76 | print(sigma[sigma<0]) 77 | quit() 78 | # print(sigma) 79 | mu = self.mu(x) 80 | mu = mu.view(-1, self.num_gaussians, self.out_features) 81 | return pi, sigma, mu 82 | 83 | def gaussian_probability(self, sigma, mu, target, log=False): 84 | """Returns the probability of `target` given MoG parameters `sigma` and `mu`. 85 | 86 | Arguments: 87 | sigma (BxGxO): The standard deviation of the Gaussians. B is the batch 88 | size, G is the number of Gaussians, and O is the number of 89 | dimensions per Gaussian. 90 | mu (BxGxO): The means of the Gaussians. B is the batch size, G is the 91 | number of Gaussians, and O is the number of dimensions per Gaussian. 92 | target (BxI): A batch of target. B is the batch size and I is the number of 93 | input dimensions. 94 | Returns: 95 | probabilities (BxG): The probability of each point in the probability 96 | of the distribution in the corresponding sigma/mu index. 97 | """ 98 | target = target.unsqueeze(1).expand_as(mu) 99 | # target = target. 100 | if log: 101 | ret = ( 102 | -torch.log(sigma) 103 | - 0.5 * LOG2PI 104 | - 0.5 * torch.pow((target - mu) / sigma, 2) 105 | ) 106 | else: 107 | ret = (ONEOVERSQRT2PI / sigma) * torch.exp( 108 | -0.5 * ((target - mu) / sigma) ** 2 109 | ) 110 | if torch.any(torch.isnan(ret)): 111 | print('nan in gaussian probability!!') 112 | ret = ret.view(-1, 1) 113 | print(ret[torch.isnan(ret)]) 114 | print(sigma) 115 | # print(torch.log(sigma)) 116 | # print() 117 | quit() 118 | return torch.sum(ret, dim=-1) # torch.prod(ret, 2) 119 | 120 | def mdn_loss(self, pi, sigma, mu, y): 121 | log_component_prob = self.gaussian_probability(sigma, mu, y, log=True) 122 | # print('size of log component', log_component_prob.size()) 123 | log_mix_prob = torch.log( 124 | pi 125 | # nn.functional.gumbel_softmax(pi, tau=1, dim=-1) + 1e-15 126 | ) 127 | # print('size of log mix prob', log_mix_prob.size()) 128 | return -torch.mean(torch.logsumexp(log_component_prob + log_mix_prob, dim=-1)) 129 | 130 | def sample(self, pi, sigma, mu): 131 | """Draw samples from a MoG.""" 132 | categorical = Categorical(pi) 133 | pis = categorical.sample().unsqueeze(1).unsqueeze(2).expand_as(sigma) 134 | print('pis size',pis.size()) 135 | # sample = Variable(sigma.data.new(sigma.size(0), ).normal_()) 136 | sample = torch.randn_like(sigma[:,0,:]) 137 | print('sample size', sample.size()) 138 | print('sigma size', sigma.size()) 139 | # sigma.gather() 140 | print('sigma gather size', sigma.gather(1, pis).size()) 141 | # Gathering from the n Gaussian Distribution based on sampled indices 142 | sample = sample * sigma.gather(1, pis)[:, 0, :] + mu.gather(1, pis)[:, 0, :] 143 | return sample 144 | 145 | def generate_samples(self, pi, sigma, mu, n_samples=None): 146 | if n_samples is None: 147 | n_samples = self.hparams.n_samples 148 | samples = [] 149 | softmax_pi = nn.functional.gumbel_softmax(pi, tau=1, dim=-1) 150 | assert ( 151 | softmax_pi < 0 152 | ).sum().item() == 0, "pi parameter should not have negative" 153 | for _ in range(n_samples): 154 | samples.append(self.sample(softmax_pi, sigma, mu)) 155 | samples = torch.cat(samples, dim=1) 156 | return samples 157 | 158 | def generate_point_predictions(self, pi, sigma, mu, n_samples=None): 159 | # Sample using n_samples and take average 160 | samples = self.generate_samples(pi, sigma, mu, n_samples) 161 | if self.hparams.central_tendency == "mean": 162 | y_hat = torch.mean(samples, dim=-1) 163 | elif self.hparams.central_tendency == "median": 164 | y_hat = torch.median(samples, dim=-1).values 165 | return y_hat --------------------------------------------------------------------------------