Commit 6b460aa7 authored by Christian Chapman-Bird's avatar Christian Chapman-Bird
Browse files

latest models (needs cleaned up still). Added optional output directory...

latest models (needs cleaned up still). Added optional output directory location relative to the current working directory
parent 27595f51
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -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
+11 −11
Original line number Diff line number Diff line
@@ -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()

+7 −7
Original line number Diff line number Diff line
@@ -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()


+465 KiB

File added.

No diff preview for this file type.

+24.4 KiB
Loading image diff...
Loading