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

More directory fixes, run_on_dataset now correctly normalises and unnormalises

parent bcc8643e
Loading
Loading
Loading
Loading
+9 −9
Original line number Diff line number Diff line
@@ -11,13 +11,13 @@ def model_train_test(data, model, device, n_epochs, n_batches, loss_function, le
    name = model.name
    path = get_script_path()

    np.save(path+'/../models/'+name+'_xdata_mean_std.npy',np.array([xtrain.mean(axis=0), xtrain.std(axis=0)]))
    np.save(path+'/../models/'+name+'_ydata_mean_std.npy',np.array([ytrain.mean(), ytrain.std()]))
    np.save(path+'/../models/'+name+'/xdata_mean_std.npy',np.array([xtrain.mean(axis=0), xtrain.std(axis=0)]))
    np.save(path+'/../models/'+name+'/ydata_mean_std.npy',np.array([ytrain.mean(), ytrain.std()]))

    xtest = torch.from_numpy(norm_inputs(xtest, xtrain)).to(device).float()
    ytest = torch.from_numpy(norm(ytest, ytrain)).to(device).float()
    xtrain = torch.from_numpy(norm_inputs(xtrain, xtrain)).to(device).float()
    ytrain = torch.from_numpy(norm(ytrain, ytrain)).to(device).float()
    xtest = torch.from_numpy(norm_inputs(xtest, ref_dataframe=xtrain)).to(device).float()
    ytest = torch.from_numpy(norm(ytest, ref_dataframe=ytrain)).to(device).float()
    xtrain = torch.from_numpy(norm_inputs(xtrain, ref_dataframe=xtrain)).to(device).float()
    ytrain = torch.from_numpy(norm(ytrain, ref_dataframe=ytrain)).to(device).float()

    ytrainsize = len(ytrain)
    ytestsize = len(ytest)
@@ -89,7 +89,7 @@ def model_train_test(data, model, device, n_epochs, n_batches, loss_function, le

    if verbose:
        print('\nTraining complete - saving.')
    torch.save(model.state_dict(),path+'/'+name+'_model.pth')
    torch.save(model.state_dict(),path+'/'+name+'/model.pth')

    epochs = np.arange(n_epochs)
    plt.semilogy(epochs, train_losses, label='Train')
@@ -98,11 +98,11 @@ def model_train_test(data, model, device, n_epochs, n_batches, loss_function, le
    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+'/../models/'+name+'/losses.png')
    #plt.show()
    plt.close()

    out = (model,)
    if return_losses:
        out += (train_losses, test_losses,)
    return model
    return out
+2 −2
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ def run_on_dataset(model, test_data, n_batches=1, device=None, y_transform_fn=No
    ymeanstd = np.load(get_script_path() + f'/../models/{model.name}_ydata_mean_std.npy')

    test_input = torch.Tensor(xdata)
    normed_input = norm_inputs(test_input, xmeanstd[0], xmeanstd[1]).float().to(device)
    normed_input = norm_inputs(test_input, ref_mean=xmeanstd[0], ref_std=xmeanstd[1]).float().to(device)

    if runtime:
        st = time.perf_counter()
@@ -56,7 +56,7 @@ def run_on_dataset(model, test_data, n_batches=1, device=None, y_transform_fn=No
        per_point = (et - st) / ydata.size

    output = np.concatenate(out)
    out_unnorm = unnorm(output, ymeanstd[0], ymeanstd[1]).flatten()
    out_unnorm = unnorm(output, ref_mean=ymeanstd[0], ref_std=ymeanstd[1]).flatten()

    if y_transform_fn is not None:
        out_unnorm = y_transform_fn(out_unnorm)
Loading