######################################################################################### # plot spectrum fig = plt.figure(fig_no); fig_no += 1 # evaluate spectrum spectralnrm = [] rhs_spectrum = [] x = dataset[:5][0] for m in model: if isinstance(m, theta_solver): spectrum_i = [] m.ind_out = torch.arange(m.num_steps+1) odesol = m(x) m.ind_out = None for t in range(odesol.size(1)-1): spectrum_i.append( eigenvalues( lambda x: m.rhs(t,x), (1-m.theta.item())*odesol[:,t,...]+m.theta.item()*odesol[:,t+1,...]) ) spectralnrm.append(np.amax(ex_setup.theta_stab(spectrum_i[-1][:,0]+1j*spectrum_i[-1][:,1],m.theta.item()))) rhs_spectrum.append(np.concatenate(np.array(spectrum_i))) # plot eigenvalues xmax = ymax = 4 plt.gca().clear() ex_setup.plot_stab(m.theta.item(), xlim=(-xmax,xmax), ylim=(-ymax,ymax)) # print(rhs_spectrum[-1].shape, np.amax(np.abs(rhs_spectrum[-1]))) plt.plot(rhs_spectrum[-1][:,0], rhs_spectrum[-1][:,1], 'o', markersize=4) #, markerfacecolor='none') plt.savefig(images_output+"_%s_spectrum.jpg"%(m.name), bbox_inches='tight', pad_inches=0.0, dpi=300) else: spectralnrm.append( spectralnorm(m,x).mean().item() ) x = m(x) # save spectral norm of layers fname = Path(paths['out_data'],('spectral_norm.txt'))
np.moveaxis(np.stack((t_learned_ode, y_learned_ode)), 0, 1).reshape((-1, 1001)).T, delimiter=',') ############################################### fig = plt.figure(fig_no) fig_no += 1 # evaluate spectrum spectrum_train = [] for i in range(t_learned.shape[1] - 1): y = (1 - args.theta ) * y_learned[:, i] + args.theta * y_learned[:, i + 1] y = torch.from_numpy(y).unsqueeze(1) t = torch.from_numpy(t_learned[:, i]) spectrum_train.append(eigenvalues(lambda x: rhs_obj(t, x), y)) spectrum_train = np.concatenate(spectrum_train) # plot spectrum # ex_setup.plot_stab(args.theta, xlim=(-25,-15), ylim=(-5,5)) plt.plot(spectrum_train[:, 0], spectrum_train[:, 1], 'bo', markersize=4) plt.savefig(images_output + "_spectrum.pdf", bbox_inches='tight', pad_inches=0.0) ############################################### # plot vector field
for i in range(1, len(model) - 1): plt.cla() # evaluate trjectory model[i].ind_out = torch.arange(model[i].num_steps + 1) ytrain = model[i](out_train) out_train = ytrain0[:, -1, :] model[i].ind_out = None # evaluate spectrum spectrum_train = [] xmax = ymax = 4 for t in range(model[i].num_steps): y = (1 - model[i].theta ) * ytrain[:, t, :] + model[i].theta * ytrain[:, t + 1, :] spectrum_train.append( eigenvalues(lambda x: model[i].rhs(t, x), y)) xmax = max(np.amax(np.abs(spectrum_train[-1][:, 0])), xmax) ymax = max(np.amax(np.abs(spectrum_train[-1][:, 1])), ymax) spectrum_train = np.concatenate(spectrum_train) # plot spectrum ex_setup.plot_stab(model[i].theta, xlim=(-xmax, xmax), ylim=(-ymax, ymax)) plt.plot(spectrum_train[:, 0], spectrum_train[:, 1], 'bo', markersize=4) ex_setup.savefig(images_output + "_spectrum_%d" % (i), format='jpg', aspect='equal')
savefig(images_output + '_learned_ode_trajectories_%dperiods.pdf' % (periods), bbox_inches='tight', pad_inches=0.0) ############################################### fig = plt.figure(fig_no) fig_no += 1 # evaluate correct spectrum along the true trajectories spectrum_exact = [] for traj in y_ode[:, :1000, :]: spectrum_traj = [] for i, t in enumerate(t_ode[:1000]): spectrum_traj.append( eigenvalues(lambda x: correct_rhs(t, x), torch.from_numpy(traj[i:i + 1, :]))) spectrum_exact.append(np.concatenate(spectrum_traj)) # plot correct spectrum # ex_setup.plot_stab(args.theta, xlim=(-2,2), ylim=(-2,2)) for i in range(0, len(spectrum_exact), 2): plt.plot(spectrum_exact[i][:, 0], spectrum_exact[i][:, 1], 'r.', markersize=1) plt.plot(spectrum_exact[i + 1][:, 0], spectrum_exact[i + 1][:, 1], 'b.', markersize=1) plt.legend(['validation', 'training'], loc='upper right') # plt.gca().axis('off')
# plt.plot(xtrain, ytrain,'o') # plt.show() ############################################### fig = plt.figure(fig_no) fig_no += 1 # evaluate spectrum spectrum_train = [] spectrum_test = [] xmax = ymax = 4 for t in range(len(ode_output_train) - 1): y = (1 - args.theta ) * ode_output_train[t] + args.theta * ode_output_train[t + 1] spectrum_train.append( eigenvalues(lambda x: rhs_obj(t, x), torch.from_numpy(y))) xmax = max(np.amax(np.abs(spectrum_train[-1][:, 0])), xmax) ymax = max(np.amax(np.abs(spectrum_train[-1][:, 1])), ymax) y = (1 - args.theta ) * ode_output_test[t] + args.theta * ode_output_test[t + 1] spectrum_test.append( eigenvalues(lambda x: rhs_obj(t, x), torch.from_numpy(y))) xmax = max(np.amax(np.abs(spectrum_test[-1][:, 0])), xmax) ymax = max(np.amax(np.abs(spectrum_test[-1][:, 1])), ymax) spectrum_train = np.concatenate(spectrum_train) spectrum_test = np.concatenate(spectrum_test) # plot spectrum ex_setup.plot_stab(args.theta, xlim=(-xmax, xmax), ylim=(-ymax, ymax)) plt.plot(spectrum_test[:, 0], spectrum_test[:, 1], 'bo',