nonred_out=nOut, d="cuda:0" if enableCuda else "cpu") model.double() if enableCuda: model.cuda() # declare optimizer and gradient and loss function optimizer = optim.Adadelta(model.parameters(), lr=lr_rate) loss = torch.nn.MSELoss(reduction='mean') print("Loading model") model.load_state_dict(torch.load(modelFile)) print("Starting Testing") out_nrgs, test_val = model.test(images, energies, loss) print("Testing Finished") out_nrgs = convertToNumpy(out_nrgs, enableCuda) # scaling energies to mHa energies = 1000 * energies out_nrgs = 1000 * out_nrgs # calculating median absolute error for energies in range (100-400 mHa) abs_err = abs(np.sort(energies) - np.sort(out_nrgs)) median_err = np.median(abs_err) with open(outdir + "median_abs_error.txt", "w+") as f: f.write("Median Absolute Error: " + str(median_err) + " mHa \n") # plotting true energies vs. predicted energies
if not ((epoch + 1) % disp_epochs): print("file name:{}".format(f) + "\tEpoch [{}/{}]".format(epoch+1, num_epochs) + "\tTraining Loss: {:.5f}".format(train_val)) print("Training Finished") pt = files[0][files[0].index("_"):files[0].index("_")+4] tsc = files[0][(files[0].index(" ")+1)+files[0][files[0].index(" ")+1:].index(" ")+1:files[0].index("]")] print("Saving Model") torch.save(model.state_dict(), moddir + "model_{}{}".format(pt,tsc)) print("Starting Testing") out_nrgs, test_val = model.test(t_img, t_nrg, loss) print("Testing Finished") out_nrgs = convertToNumpy(out_nrgs, enableCuda) plt.plot(np.linspace(100, 400), np.linspace(100, 400), color="black") plt.scatter(np.sort(t_nrg)*1000, np.sort(out_nrgs)*1000, s=0.5, c='#FF0000') plt.xlim((100,400)) plt.ylim((100,400)) plt.savefig(outdir + "InitialE.png") plt.close() plt.plot(obj_vals) plt.savefig(outdir + "Training error.png") plt.close()