Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster plot #23

Merged
merged 3 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 17 additions & 42 deletions maestroflame/maestroflame/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def make_movie(file_list, save_dir='./', var='enuc', movie_name="flame.mp4"):
os.system("rm movie_imag*")
Video("movie.mp4", embed=True)


class plotting_standard:
#class to make it easy to plot things from driver. Set up all the data
#then just call the methods for whatever plots you want.
Expand Down Expand Up @@ -45,7 +44,6 @@ def __init__(self, model, fields, test_loader, cost_per_epoc,
def do_prediction_vs_solution_plot(self):
############ Prediction Vs Solution Plot Should fall one y=x line.


plt.figure()
#N = react_data.output_data.shape[1]
colors = matplotlib.cm.rainbow(np.linspace(0, 1, self.nnuc+1))
Expand All @@ -67,48 +65,29 @@ def do_prediction_vs_solution_plot(self):

#for batch_idx, (data, targets) in enumerate(self.test_loader):
pred = self.model(data_whole)
#print(pred.shape)

if self.LOG_MODE:
# convert all mass fractions back from their log form
data_whole[:,:self.nnuc] = torch.exp(-0.5/data_whole[:,:self.nnuc])
targets_whole[:,:self.nnuc] = torch.exp(-0.5/targets_whole[:,:self.nnuc])
pred[:,:self.nnuc] = torch.exp(-0.5/pred[:,:self.nnuc])

for i in range(pred.shape[0]):
if i == 0:
for j in range(pred.shape[1]):
plt.scatter(pred[i,j], targets_whole[i,j], color=colors[j], label=self.fields[j])
else:
plt.scatter(pred[i, :], targets_whole[i, :self.nnuc+1], c=colors)
for i in range(pred.shape[1]):
plt.scatter(pred[:, i], targets_whole[:, i], color=colors[i], label=self.fields[i])
plt.plot(np.linspace(0, 1), np.linspace(0,1), '--', color='orange')
#plt.legend(yt.load(react_data.output_files[0]).field_list, colors=colors)
plt.legend(bbox_to_anchor=(1, 1))
plt.xlabel('Prediction')
plt.ylabel('Solution')
plt.savefig(self.output_dir + "/prediction_vs_solution.png", bbox_inches='tight')

plt.yscale("log")
plt.xscale("log")
plt.savefig(self.output_dir + "/prediction_vs_solution_log.png", bbox_inches='tight')

self.model.train()
# plt.figure()
# #N = react_data.output_data.shape[1]
# colors = matplotlib.cm.rainbow(np.linspace(0, 1, self.N_fields))
# #fields = [field[1] for field in yt.load(react_data.output_files[0]).field_list]
# with torch.no_grad():
# losses = []
# for batch_idx, (data, targets) in enumerate(self.test_loader):
# pred = self.model(data)
# for i in range(pred.shape[0]):
# if i == 0 and batch_idx == 0:
# for j in range(pred.shape[1]):
# plt.scatter(pred[i,j], targets[i,j], color=colors[j], label=self.fields[j])
# else:
# plt.scatter(pred[i, :], targets[i, :], c=colors)
# # if batch_idx == 100:
# # break
plt.plot(np.linspace(0, 1), np.linspace(0,1), '--', color='orange')
#plt.legend(yt.load(react_data.output_files[0]).field_list, colors=colors)
plt.legend(bbox_to_anchor=(1, 1))
plt.xlabel('Prediction')
plt.ylabel('Solution')
plt.savefig(self.output_dir + "/prediction_vs_solution.png", bbox_inches='tight')

plt.yscale("log")
plt.xscale("log")
plt.savefig(self.output_dir + "/prediction_vs_solution_log.png", bbox_inches='tight')


plt.close()

Expand Down Expand Up @@ -239,7 +218,7 @@ def do_prediction_vs_solution_plot(self):
colors = matplotlib.cm.rainbow(np.linspace(0, 1, self.nnuc+1))
#fields = [field[1] for field in yt.load(react_data.output_files[0]).field_list]
self.model.eval()

with torch.no_grad():
losses = []

Expand All @@ -255,13 +234,9 @@ def do_prediction_vs_solution_plot(self):

#for batch_idx, (data, targets) in enumerate(self.test_loader):
pred = self.model(data_whole)
#print(pred.shape)
for i in range(pred.shape[0]):
if i == 0:
for j in range(pred.shape[1]):
plt.scatter(pred[i,j], targets_whole[i,j], color=colors[j], label=self.fields[j])
else:
plt.scatter(pred[i, :self.nnuc+1], targets_whole[i, :self.nnuc+1], c=colors)

for i in range(self.nnuc+1):
plt.scatter(pred[:, i], targets_whole[:, i], color=colors[i], label=self.fields[i])

self.model.train()

Expand Down
82 changes: 43 additions & 39 deletions maestroflame/maestroflame/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,22 +257,6 @@ def train(self, model, optimizer, num_epochs, criterion, save_every_N=np.Inf):
self.component_losses_train.append(component_loss/batch_idx)
self.cost_per_epoc.append(sum(plotting_losses) / len(plotting_losses))

with torch.no_grad():
if epoch % save_every_N == 0 and epoch != 0:
directory = self.output_dir+'intermediate_output/epoch'+str(epoch)+'/'
os.mkdir(directory)


torch.save(model.state_dict(), directory+'my_model.pt')
np.savetxt(directory + "/cost_per_epoch.txt", self.cost_per_epoc)
np.savetxt(directory + "/component_losses_test.txt", self.component_losses_test)
np.savetxt(directory + "/component_losses_train.txt", self.component_losses_train)


plot_class = plotting_standard(model, self.fields, self.test_loader, self.cost_per_epoc, np.array(self.component_losses_test),
np.array(self.component_losses_train), self.cost_per_epoc_test, directory)

plot_class.do_all_plots()


if self.DO_PLOTTING:
Expand Down Expand Up @@ -302,9 +286,30 @@ def train(self, model, optimizer, num_epochs, criterion, save_every_N=np.Inf):

model.train()


with torch.no_grad():
if epoch % save_every_N == 0 and epoch != 0:
directory = self.output_dir+'intermediate_output/epoch'+str(epoch)+'/'
os.mkdir(directory)


torch.save(model.state_dict(), directory+'my_model.pt')
np.savetxt(directory + "/cost_per_epoch.txt", self.cost_per_epoc)
np.savetxt(directory + "/component_losses_test.txt", self.component_losses_test)
np.savetxt(directory + "/component_losses_train.txt", self.component_losses_train)


plot_class = plotting_standard(model, self.fields, self.test_loader, self.cost_per_epoc, np.array(self.component_losses_test),
np.array(self.component_losses_train), self.cost_per_epoc_test, directory)

plot_class.do_all_plots()


self.component_losses_test = np.array(self.component_losses_test)
self.component_losses_train = np.array(self.component_losses_train)



self.model = model

if self.SAVE_MODEL:
Expand Down Expand Up @@ -649,29 +654,6 @@ def criterion_plotting(prediction, targets):
diff_losses = np.array(diff_losses)
self.different_loss_metrics.append(diff_losses.sum(axis=0)/len(losses))

with torch.no_grad():
if epoch % save_every_N == 0 and epoch != 0:
directory = self.output_dir+'intermediate_output/epoch'+str(epoch)+'/'
os.mkdir(directory)


torch.save(model.state_dict(), directory+'my_model.pt')
np.savetxt(directory + "/cost_per_epoch.txt", self.cost_per_epoc)
np.savetxt(directory + "/component_losses_test.txt", self.component_losses_test)
np.savetxt(directory + "/component_losses_train.txt", self.component_losses_train)
np.savetxt(self.output_dir + "/d_component_losses_test.txt", self.d_component_losses_test)
np.savetxt(self.output_dir + "/d_component_losses_train.txt", self.d_component_losses_train)


plot_class = plotting_pinn(self.model, self.fields, self.test_loader, self.cost_per_epoc,
np.array(self.component_losses_test), np.array(self.component_losses_train),
np.array(self.d_component_losses_test), np.array(self.d_component_losses_train),
self.cost_per_epoc_test, np.array(self.different_loss_metrics),
self.output_dir)

plot_class.do_all_plots()




if self.DO_PLOTTING:
Expand Down Expand Up @@ -722,6 +704,28 @@ def criterion_plotting(prediction, targets):
self.component_losses_test.append(component_loss/batch_idx)
self.d_component_losses_test.append(d_component_loss/batch_idx)

with torch.no_grad():
if epoch % save_every_N == 0 and epoch != 0:
directory = self.output_dir+'intermediate_output/epoch'+str(epoch)+'/'
os.mkdir(directory)


torch.save(model.state_dict(), directory+'my_model.pt')
np.savetxt(directory + "/cost_per_epoch.txt", self.cost_per_epoc)
np.savetxt(directory + "/component_losses_test.txt", self.component_losses_test)
np.savetxt(directory + "/component_losses_train.txt", self.component_losses_train)
np.savetxt(self.output_dir + "/d_component_losses_test.txt", self.d_component_losses_test)
np.savetxt(self.output_dir + "/d_component_losses_train.txt", self.d_component_losses_train)


plot_class = plotting_pinn(self.model, self.fields, self.test_loader, self.cost_per_epoc,
np.array(self.component_losses_test), np.array(self.component_losses_train),
np.array(self.d_component_losses_test), np.array(self.d_component_losses_train),
self.cost_per_epoc_test, np.array(self.different_loss_metrics),
self.output_dir)

plot_class.do_all_plots()


self.component_losses_train = np.array(self.component_losses_train)
self.component_losses_test = np.array(self.component_losses_test)
Expand Down