diff --git a/EMRI_DET/nn/model_creation.py b/EMRI_DET/nn/model_creation.py index 42ae684ff5b64c17db6b9b83ea49333355210b8f..02040bd283fe6e01c7a07790ca11a91c86fefd24 100644 --- a/EMRI_DET/nn/model_creation.py +++ b/EMRI_DET/nn/model_creation.py @@ -37,7 +37,7 @@ class LinearModel(nn.Module): self.initial(m.weight) -def create_mlp(input_features, output_features, neurons, layers, activation, model_name, out_activation=None, init=xavier_uniform_, device=None, norm_type='z-score', use_dropout=False,drop_p=0.25, use_bn=False): +def create_mlp(input_features, output_features, neurons, layers, activation, model_name, out_activation=None, init=xavier_uniform_, device=None, norm_type='z-score', use_dropout=False,drop_p=0.25, use_bn=False, outdir='../models'): if isinstance(neurons, list): if len(neurons) != layers: raise RuntimeError('Length of neuron vector does not equal number of hidden layers.') @@ -45,8 +45,8 @@ def create_mlp(input_features, output_features, neurons, layers, activation, mod neurons = [neurons, ] model = LinearModel(input_features, output_features, neurons, layers, activation, model_name, initialisation=init, use_dropout=use_dropout,drop_p=drop_p,use_bn=use_bn, out_activation=out_activation) model.norm_type=norm_type - Path(get_script_path()+f'/../models/{model_name}/').mkdir(parents=True, exist_ok=True) - pickle.dump(model, open(get_script_path()+f'/../models/{model_name}/function.pickle', "wb"), pickle.HIGHEST_PROTOCOL) # save blank model + Path(get_script_path()+f'/{outdir}/{model_name}/').mkdir(parents=True, exist_ok=True) + pickle.dump(model, open(get_script_path()+f'/{outdir}/{model_name}/function.pickle', "wb"), pickle.HIGHEST_PROTOCOL) # save blank model if device is not None: model = model.to(device) @@ -54,7 +54,7 @@ def create_mlp(input_features, output_features, neurons, layers, activation, mod def load_mlp(model_name, device, get_state_dict=False): - model = pickle.load(open(get_script_path()+f'/../models/{model_name}/function.pickle', "rb")) # load blank model + model = pickle.load(open(get_script_path()+f'/{outdir}/{model_name}/function.pickle', "rb")) # load blank model if get_state_dict: - model.load_state_dict(torch.load(open(get_script_path()+f'/../models/{model_name}/model.pth', "rb"), map_location=device)) + model.load_state_dict(torch.load(open(get_script_path()+f'/{outdir}/{model_name}/model.pth', "rb"), map_location=device)) return model diff --git a/EMRI_DET/nn/model_train_test.py b/EMRI_DET/nn/model_train_test.py index 23b61c919c39e07331081f3e96f28eec644b44da..bbe7de5e9d0216057380a1723ee521676731ddd8 100644 --- a/EMRI_DET/nn/model_train_test.py +++ b/EMRI_DET/nn/model_train_test.py @@ -6,7 +6,7 @@ from sys import stdout from pathlib import Path -def model_train_test(data, model, device, n_epochs, n_batches, loss_function, optimizer, verbose=False, return_losses=False, update_every=None, n_test_batches=None, save_best=False, scheduler=None): +def model_train_test(data, model, device, n_epochs, n_batches, loss_function, optimizer, verbose=False, return_losses=False, update_every=None, n_test_batches=None, save_best=False, scheduler=None, outdir='../models'): if n_test_batches is None: n_test_batches = n_batches @@ -17,13 +17,13 @@ def model_train_test(data, model, device, n_epochs, n_batches, loss_function, op name = model.name path = get_script_path() norm_type = model.norm_type - Path(get_script_path()+f'/../models/{name}/').mkdir(parents=True, exist_ok=True) + Path(get_script_path()+f'/{outdir}/{name}/').mkdir(parents=True, exist_ok=True) if norm_type == 'z-score': - np.save(path+'/../models/'+name+'/xdata_inputs.npy',np.array([xtrain.mean(axis=0), xtrain.std(axis=0)])) - np.save(path+'/../models/'+name+'/ydata_inputs.npy',np.array([ytrain.mean(), ytrain.std()])) + np.save(path+f'/{outdir}/{name}/xdata_inputs.npy',np.array([xtrain.mean(axis=0), xtrain.std(axis=0)])) + np.save(path+f'/{outdir}/{name}/ydata_inputs.npy',np.array([ytrain.mean(), ytrain.std()])) elif norm_type == 'uniform': - np.save(path+'/../models/'+name+'/xdata_inputs.npy',np.array([np.min(xtrain,axis=0), np.max(xtrain,axis=0)])) - np.save(path+'/../models/'+name+'/ydata_inputs.npy',np.array([np.min(ytrain), np.max(ytrain)])) + np.save(path+f'/{outdir}/{name}/xdata_inputs.npy',np.array([np.min(xtrain,axis=0), np.max(xtrain,axis=0)])) + np.save(path+f'/{outdir}/{name}/ydata_inputs.npy',np.array([np.min(ytrain), np.max(ytrain)])) xtest = torch.from_numpy(norm_inputs(xtest, ref_dataframe=xtrain, norm_type=norm_type)).to(device).float() ytest = torch.from_numpy(norm(ytest, ref_dataframe=ytrain, norm_type=norm_type)).to(device).float() xtrain = torch.from_numpy(norm_inputs(xtrain, ref_dataframe=xtrain, norm_type=norm_type)).to(device).float() @@ -92,7 +92,7 @@ def model_train_test(data, model, device, n_epochs, n_batches, loss_function, op if test_losses[-1] < lowest_loss: lowest_loss = test_losses[-1] if save_best: - torch.save(model.state_dict(),path+'/../models/'+name+'/model.pth') + torch.save(model.state_dict(),path+f'/{outdir}/{name}/model.pth') # if epoch >= cutoff_LR: # scheduler.step() @@ -110,19 +110,19 @@ def model_train_test(data, model, device, n_epochs, n_batches, loss_function, op plt.xlabel('Epochs') plt.ylabel('Loss') plt.title('Train and Test Loss Across Train Epochs') - plt.savefig(path+'/../models/'+name+'/losses.png') + plt.savefig(path+f'/{outdir}/{name}/losses.png') #plt.show() plt.close() if not save_best: - torch.save(model.state_dict(),path+'/../models/'+name+'/model.pth') + torch.save(model.state_dict(),path+f'/{outdir}/{name}/model.pth') if verbose: print('\nTraining complete - saving.') if not save_best: - torch.save(model.state_dict(),path+'/../models/'+name+'/model.pth') + torch.save(model.state_dict(),path+f'/{outdir}/{name}/model.pth') epochs = np.arange(n_epochs) plt.semilogy(epochs, train_losses, label='Train') @@ -131,7 +131,7 @@ def model_train_test(data, model, device, n_epochs, n_batches, loss_function, op plt.xlabel('Epochs') plt.ylabel('Loss') plt.title('Train and Test Loss Across Train Epochs') - plt.savefig(path+'/../models/'+name+'/losses.png') + plt.savefig(path+f'/{outdir}/{name}/losses.png') #plt.show() plt.close() diff --git a/EMRI_DET/validate.py b/EMRI_DET/validate.py index e4e511a6f7af21520d8c386783257d3d23f3d685..7437ecb4737981f4f42589320e2a5e39e1e7c7fe 100644 --- a/EMRI_DET/validate.py +++ b/EMRI_DET/validate.py @@ -6,7 +6,7 @@ from EMRI_DET.utilities import norm_inputs, unnorm, get_script_path import seaborn as sns -def run_on_dataset(model, test_data, distances=None, n_batches=1, device=None, y_transform_fn=None, runtime=False): +def run_on_dataset(model, test_data, distances=None, n_batches=1, device=None, y_transform_fn=None, runtime=False, outdir='../models'): """ Get the re-processed output of the supplied model on a set of supplied test data. @@ -36,8 +36,8 @@ def run_on_dataset(model, test_data, distances=None, n_batches=1, device=None, y xdata, ydata = test_data - xscalevals = np.load(get_script_path() + f'/../models/{model.name}/xdata_inputs.npy') - yscalevals = np.load(get_script_path() + f'/../models/{model.name}/ydata_inputs.npy') + xscalevals = np.load(get_script_path() + f'/{outdir}/{model.name}/xdata_inputs.npy') + yscalevals = np.load(get_script_path() + f'/{outdir}/{model.name}/ydata_inputs.npy') test_input = torch.Tensor(xdata) normed_input = norm_inputs(test_input, ref_inputs=xscalevals,norm_type=model.norm_type).float().to(device) @@ -108,7 +108,7 @@ def test_threshold_accuracy(comparison_sets, threshold, confusion_matrix=False): return (1-np.mean(np.abs(out_classified-truth_classified)),confmat) def plot_histograms(comparison_sets, model_name, xlabel, title=None, title_kwargs={}, xlabel_kwargs={}, log=True, - fig_kwargs={}, plot_kwargs={}, save_kwargs={}, legend_kwargs={}): + fig_kwargs={}, plot_kwargs={}, save_kwargs={}, legend_kwargs={}, outdir='../models'): truth, pred = comparison_sets if log: truth = np.log10(truth) @@ -130,12 +130,12 @@ def plot_histograms(comparison_sets, model_name, xlabel, title=None, title_kwarg ax.set_title(title, **title_kwargs) ax.legend(**legend_kwargs) - plt.savefig(get_script_path() + f'/../models/{model_name}/hists.png', **save_kwargs) + plt.savefig(get_script_path() + f'/{outdir}/{model_name}/hists.png', **save_kwargs) plt.close() def plot_difference_histogram(comparison_sets, model_name, xlabel, title=None, title_kwargs={}, xlabel_kwargs={}, - log=True, one_sided=False, ratio=False, fig_kwargs={}, plot_kwargs={}, save_kwargs={}): + log=True, one_sided=False, ratio=False, fig_kwargs={}, plot_kwargs={}, save_kwargs={}, outdir='../models'): truth, pred = comparison_sets if log: truth = np.log10(truth) @@ -175,7 +175,7 @@ def plot_difference_histogram(comparison_sets, model_name, xlabel, title=None, t else: logname = '' - plt.savefig(get_script_path() + f'/../models/{model_name}/hist_{logname}{diff}diff.png', **save_kwargs) + plt.savefig(get_script_path() + f'/{outdir}/{model_name}/hist_{logname}{diff}diff.png', **save_kwargs) plt.close() diff --git a/emri_data/models/prograde_SiLU_linearOut/function.pickle b/emri_data/models/prograde_SiLU_linearOut/function.pickle new file mode 100644 index 0000000000000000000000000000000000000000..ae18343665091620f58fd63db132eb2432c110a3 Binary files /dev/null and b/emri_data/models/prograde_SiLU_linearOut/function.pickle differ diff --git a/emri_data/models/prograde_SiLU_linearOut/losses.png b/emri_data/models/prograde_SiLU_linearOut/losses.png new file mode 100644 index 0000000000000000000000000000000000000000..71e39db5b9a55ee2cdd0b4da86d6172fe6e9ffc7 Binary files /dev/null and b/emri_data/models/prograde_SiLU_linearOut/losses.png differ diff --git a/emri_data/models/prograde_SiLU_linearOut/model.pth b/emri_data/models/prograde_SiLU_linearOut/model.pth new file mode 100644 index 0000000000000000000000000000000000000000..d27a312ce180f8c08f2378badea1a9dde6483355 Binary files /dev/null and b/emri_data/models/prograde_SiLU_linearOut/model.pth differ diff --git a/emri_data/models/prograde_SiLU_linearOut/xdata_inputs.npy b/emri_data/models/prograde_SiLU_linearOut/xdata_inputs.npy new file mode 100644 index 0000000000000000000000000000000000000000..c79128481bbe6607f0be4113a310a424fa27e04c Binary files /dev/null and b/emri_data/models/prograde_SiLU_linearOut/xdata_inputs.npy differ diff --git a/emri_data/models/prograde_SiLU_linearOut/ydata_inputs.npy b/emri_data/models/prograde_SiLU_linearOut/ydata_inputs.npy new file mode 100644 index 0000000000000000000000000000000000000000..5c1f51724a89eaa8eac280cfe6b6fcc8903bbafa Binary files /dev/null and b/emri_data/models/prograde_SiLU_linearOut/ydata_inputs.npy differ diff --git a/emri_data/models/retrograde_SiLU_linearOut/function.pickle b/emri_data/models/retrograde_SiLU_linearOut/function.pickle new file mode 100644 index 0000000000000000000000000000000000000000..02fc2cc12cd4531534f7fb08e597b7ab49dcd5a4 Binary files /dev/null and b/emri_data/models/retrograde_SiLU_linearOut/function.pickle differ diff --git a/emri_data/models/retrograde_SiLU_linearOut/losses.png b/emri_data/models/retrograde_SiLU_linearOut/losses.png new file mode 100644 index 0000000000000000000000000000000000000000..aa7d02d0ec042840c6b48955c0b6f23a380007c1 Binary files /dev/null and b/emri_data/models/retrograde_SiLU_linearOut/losses.png differ diff --git a/emri_data/models/retrograde_SiLU_linearOut/model.pth b/emri_data/models/retrograde_SiLU_linearOut/model.pth new file mode 100644 index 0000000000000000000000000000000000000000..a226478f8160513d65cf26091d5e72d8fd0704a6 Binary files /dev/null and b/emri_data/models/retrograde_SiLU_linearOut/model.pth differ diff --git a/emri_data/models/retrograde_SiLU_linearOut/xdata_inputs.npy b/emri_data/models/retrograde_SiLU_linearOut/xdata_inputs.npy new file mode 100644 index 0000000000000000000000000000000000000000..07e66c9590178752b2317ed0be1ac2e91da20062 Binary files /dev/null and b/emri_data/models/retrograde_SiLU_linearOut/xdata_inputs.npy differ diff --git a/emri_data/models/retrograde_SiLU_linearOut/ydata_inputs.npy b/emri_data/models/retrograde_SiLU_linearOut/ydata_inputs.npy new file mode 100644 index 0000000000000000000000000000000000000000..ccde9526f74f239d55c3e7169741f8a446835fb9 Binary files /dev/null and b/emri_data/models/retrograde_SiLU_linearOut/ydata_inputs.npy differ diff --git a/emri_data/schwarz_negY_fix/samp_dataframe.csv b/emri_data/retrograde/samp_dataframe.csv similarity index 100% rename from emri_data/schwarz_negY_fix/samp_dataframe.csv rename to emri_data/retrograde/samp_dataframe.csv diff --git a/emri_data/schwarz_negY_fix/samp_dataframe_fixed.csv b/emri_data/retrograde/samp_dataframe_fixed.csv similarity index 100% rename from emri_data/schwarz_negY_fix/samp_dataframe_fixed.csv rename to emri_data/retrograde/samp_dataframe_fixed.csv diff --git a/emri_data/schwarz_negY_fix/samp_position.npy b/emri_data/retrograde/samp_position.npy similarity index 100% rename from emri_data/schwarz_negY_fix/samp_position.npy rename to emri_data/retrograde/samp_position.npy diff --git a/emri_data/schwarz_negY_fix/samp_processing_times.npy b/emri_data/retrograde/samp_processing_times.npy similarity index 100% rename from emri_data/schwarz_negY_fix/samp_processing_times.npy rename to emri_data/retrograde/samp_processing_times.npy diff --git a/emri_data/schwarz_negY_fix/samp_waveform_times.npy b/emri_data/retrograde/samp_waveform_times.npy similarity index 100% rename from emri_data/schwarz_negY_fix/samp_waveform_times.npy rename to emri_data/retrograde/samp_waveform_times.npy diff --git a/emri_data/scripts/model_train.py b/emri_data/scripts/model_train.py index c7352a7cef54d98623790fd248b900bcde339161..7ed860143e14928b4946c04a4f2e979518a48810 100644 --- a/emri_data/scripts/model_train.py +++ b/emri_data/scripts/model_train.py @@ -6,7 +6,7 @@ from EMRI_DET.nn.model_creation import create_mlp import pandas as pd if __name__ == '__main__': - device = "cuda:2" + device = "cuda:2" fp = '../prograde/{}' fp2 = '../prograde_pop_spin075/{}' #['logM', 'logq', 'a', 'p0', 'e', 'Y0', 'thetaS', 'phiS', 'thetaK', 't', 'SNR'] @@ -16,6 +16,7 @@ if __name__ == '__main__': test_inds2 = [11,12] traindata = pd.read_csv(fp.format('samp_dataframe.csv')) + traindata['logq'] = -traindata['logq'] xtrain = traindata.iloc[:,train_inds].to_numpy() # ytrain = np.log(traindata.iloc[:,test_inds].to_numpy().flatten()) ytrain = traindata.iloc[:,test_inds].to_numpy() @@ -26,23 +27,22 @@ if __name__ == '__main__': xtrain = xtrain[np.where(~np.isnan(ytrain[:,1]))[0],:] ytrain = ytrain[np.where(~np.isnan(ytrain)[:,1])[0],:] - inds = np.arange(ytrain.shape[0]) np.random.shuffle(inds) xtrain = xtrain[inds,:] ytrain = ytrain[inds,:] - testdata = pd.read_csv(fp2.format('samp_dataframe.csv')) - dists = testdata.iloc[:,6].to_numpy().flatten() - xtest = testdata.iloc[:, train_inds2].to_numpy() - ytest = testdata.iloc[:, test_inds2].to_numpy()*dists[:,None]/0.5 +# testdata = pd.read_csv(fp2.format('samp_dataframe.csv')) +# dists = testdata.iloc[:,6].to_numpy().flatten() +# xtest = testdata.iloc[:, train_inds2].to_numpy() +# ytest = testdata.iloc[:, test_inds2].to_numpy()*dists[:,None]/0.5 -# cut = 0.99 -# xtest = xtrain[int(cut*xtrain.shape[0]):,:] -# ytest = ytrain[int(cut*ytrain.shape[0]):,:] -# xtrain = xtrain[:int(cut*xtrain.shape[0]),:] -# ytrain = ytrain[:int(cut*ytrain.shape[0]),:] + cut = 0.99 + xtest = xtrain[int(cut*xtrain.shape[0]):,:] + ytest = ytrain[int(cut*ytrain.shape[0]):,:] + xtrain = xtrain[:int(cut*xtrain.shape[0]),:] + ytrain = ytrain[:int(cut*ytrain.shape[0]),:] print(ytrain.shape[0]) print(ytest.shape[0]) @@ -52,8 +52,8 @@ if __name__ == '__main__': layers = 8 neurons = np.array(np.ones(layers,dtype=np.int32)*128).tolist()#[256,256,256,256,256] activation = nn.SiLU - out_activation=nn.ReLU - model = create_mlp(input_features=in_features,output_features=out_features,neurons=neurons,layers=layers,activation=activation,out_activation=out_activation, device=device, model_name='prograde_SILU_RELUout',use_bn=False) + out_activation=None#nn.ReLU + model = create_mlp(input_features=in_features,output_features=out_features,neurons=neurons,layers=layers,activation=activation,out_activation=out_activation, device=device, model_name='prograde_SiLU_linearOut',use_bn=False) data = [xtrain, ytrain, xtest, ytest] diff --git a/emri_data/scripts/retrograde_model_train.py b/emri_data/scripts/retrograde_model_train.py new file mode 100644 index 0000000000000000000000000000000000000000..f09ee0973d1c535c9ae0627cc15ded892a4a44d6 --- /dev/null +++ b/emri_data/scripts/retrograde_model_train.py @@ -0,0 +1,53 @@ +from torch import nn +import torch +import numpy as np +from EMRI_DET.nn.model_train_test import model_train_test +from EMRI_DET.nn.model_creation import create_mlp +import pandas as pd + +if __name__ == '__main__': + device = "cuda:1" + fp = '../retrograde/{}' + #['logM', 'logq', 'a', 'p0', 'e', 'Y0', 'thetaS', 'phiS', 'thetaK', 't', 'SNR'] + train_inds = [0,1,2,4,5,6,7,8,9] + test_inds = [10] + + traindata = pd.read_csv(fp.format('samp_dataframe.csv')) + traindata['logq'] = -traindata['logq'] + xtrain = traindata.iloc[:,train_inds].to_numpy() +# ytrain = np.log(traindata.iloc[:,test_inds].to_numpy().flatten()) + ytrain = traindata.iloc[:,test_inds].to_numpy() + + xtrain = xtrain[np.where(~np.isnan(ytrain))[0],:] + ytrain = ytrain[np.where(~np.isnan(ytrain))[0],:] + + inds = np.arange(ytrain.shape[0]) + np.random.shuffle(inds) + xtrain = xtrain[inds,:] + ytrain = ytrain[inds,:] + + cut = 0.99 + xtest = xtrain[int(cut*xtrain.shape[0]):,:] + ytest = ytrain[int(cut*ytrain.shape[0]):,:] + xtrain = xtrain[:int(cut*xtrain.shape[0]),:] + ytrain = ytrain[:int(cut*ytrain.shape[0]),:] + + print(ytrain.shape[0]) + print(ytest.shape[0]) + + in_features = 9 + out_features = 1 + layers = 8 + neurons = np.array(np.ones(layers,dtype=np.int32)*128).tolist()#[256,256,256,256,256] + activation = nn.SiLU + out_activation=None#nn.ReLU + model = create_mlp(input_features=in_features,output_features=out_features,neurons=neurons,layers=layers,activation=activation,out_activation=out_activation, device=device, model_name='retrograde_SiLU_linearOut',use_bn=False) + + data = [xtrain, ytrain, xtest, ytest] + + loss_function = nn.L1Loss() + LR = 5e-5 + optimizer = torch.optim.Adam(model.parameters(), lr=LR) + sched = torch.optim.lr_scheduler.CyclicLR(optimizer,base_lr=5e-5,max_lr=1e-3, step_size_up=10000,mode='triangular2', cycle_momentum=False) + + model_train_test(data, model, device,n_epochs=100000,n_batches=1, loss_function=loss_function,optimizer=optimizer, verbose=True,update_every=500, n_test_batches=1,save_best=True, scheduler=sched)