import sys import timeit #own file imports import helper_functions import spectroscopy_cnn_model import load_data import numpy as np normalize = sys.argv[1] output_spectra_length = 300 spectra_file = 'spectra.npz' experiment_model = 'exp42' train_loader, test_loader, validation_loader = load_data.load_experiment_data( spectra_file, 'all', 1, 1, 1) model = spectroscopy_cnn_model.create_network(output_spectra_length) model.load_state_dict( torch.load('results/model_' + experiment_model + '.pth', map_location='cpu')) model.eval() predicted_spectra_training = [] predicted_spectra_testing = [] predicted_spectra_validation = [] with torch.no_grad(): print("Predictions training data") for data, target in train_loader:
print("Device is {}".format(device)) #Handle file and output arguments spectra_file = sys.argv[1] coulomb_file = sys.argv[2] runtime = int(sys.argv[3]) experiment_number = sys.argv[4] criterion_string = sys.argv[5] spectra_name = spectra_file[:-4] output_spectra_length = int(sys.argv[6]) train_split_percent = float(sys.argv[7]) print("Running training on Spectra: {} for {} epochs. Experiment number {}". format(spectra_name, runtime, experiment_number)) #Load data train_loader, test_loader, validation_loader = load_data.load_experiment_data( coulomb_file, spectra_file, 'all', train_split=train_split_percent) #Create network network = spectroscopy_cnn_model.create_network(output_spectra_length) #model_parameters = filter(lambda p: p.requires_grad, network.parameters()) #params = sum([np.prod(p.size()) for p in model_parameters]) #print("PARAMS", params) network.to(device) #CUDA or CPU #Set CUDA/CPU, optimizer and loss function criterion = helper_functions.set_criterion(criterion_string) optimizer = optim.Adam(network.parameters(), lr=1e-4) #Define train and test train_loss = [] test_losses = []
import torch import pkg_resources pkg_resources.require("torch==1.0.0") import load_data import helper_functions import spectroscopy_cnn_model validation_loader = load_data.load_experiment_data('spectra_normalized.npz', 'validation') model = spectroscopy_cnn_model.create_network(300) model.load_state_dict(torch.load('results/model_exp17.pth', map_location='cpu')) model.eval() rmse_accuracy, rmse_rmse, rmse_mae, rmse_r2 = helper_functions.get_model_error( validation_loader, model, normalized=True) model.load_state_dict(torch.load('results/model_exp41.pth', map_location='cpu')) model.eval() smooth_accuracy, smooth_rmse, smooth_mae, smooth_r2 = helper_functions.get_model_error( validation_loader, model, normalized=True) model.load_state_dict(torch.load('results/model_exp42.pth', map_location='cpu')) model.eval() logcosh_accuracy, logcosh_rmse, logcosh_mae, logcosh_r2 = helper_functions.get_model_error( validation_loader, model, normalized=True) model.load_state_dict(torch.load('results/model_exp39.pth',
val_test_split = 0.5 coulomb_train, coulomb_test, energies_train, energies_test = train_test_split( coulomb, energies, test_size=1.0 - train_split, random_state=0) coulomb_test, coulomb_val, energies_test, energies_val = train_test_split( coulomb_test, energies_test, test_size=val_test_split, random_state=0) def relative_difference(prediction, label): dE = 30 / len(prediction) #how many eV's one dE is numerator = np.sum(dE * np.power((label - prediction), 2)) denominator = np.sum(dE * label) return 1 - np.sqrt(numerator) / denominator validation_loader = load_data.load_experiment_data('spectra.npz', 'validation') model = spectroscopy_cnn_model.create_network(300) model.load_state_dict(torch.load('results/model_exp17.pth', map_location='cpu')) model.eval() rmse_accuracy = helper_functions.get_model_error(validation_loader, model, True) best_prediction = np.argmin(rmse_accuracy) worst_prediction = np.argmax(rmse_accuracy) median_prediction = rmse_accuracy.index( np.percentile(rmse_accuracy, 50, interpolation='nearest')) index = 1836 index2 = 5670
import torch import pkg_resources import sys import os sys.path.append('..') pkg_resources.require("torch==1.0.0") import load_data import helper_functions import spectroscopy_cnn_model import matplotlib.pyplot as plt import numpy as np validation_loader = load_data.load_experiment_data(os.path.join(os.path.dirname(__file__),'../data/spectra.npz'),'validation', relative_path=True) model = spectroscopy_cnn_model.create_network(300) model.load_state_dict(torch.load(os.path.join(os.path.dirname(__file__),'../results/model_exp17.pth'), map_location='cpu')) model.eval() rmse_accuracy = helper_functions.get_model_error(validation_loader, model, True) best_prediction = np.argmin(rmse_accuracy) worst_prediction = np.argmax(rmse_accuracy) median_prediction = rmse_accuracy.index(np.percentile(rmse_accuracy, 50, interpolation='nearest')) print("Best index = {}\nWorst index = {}\nMedian index = {}".format(best_prediction, worst_prediction, median_prediction)) print("Best accuracy=", max(rmse_accuracy)) bin_width = 0.001