def plot_contour_trajectory(surf_file, dir_file, proj_file, surf_name='loss_vals', vmin=0.1, vmax=10, vlevel=0.5, show=False): """2D contour + trajectory""" assert exists(surf_file) and exists(proj_file) and exists(dir_file) # plot contours f = h5py.File(surf_file,'r') x = np.array(f['xcoordinates'][:]) y = np.array(f['ycoordinates'][:]) X, Y = np.meshgrid(x, y) if surf_name in f.keys(): Z = np.array(f[surf_name][:]) fig = plt.figure() CS1 = plt.contour(X, Y, Z, levels=np.arange(vmin, vmax, vlevel)) CS2 = plt.contour(X, Y, Z, levels=np.logspace(1, 8, num=8)) # plot trajectories pf = h5py.File(proj_file, 'r') plt.plot(pf['proj_xcoord'], pf['proj_ycoord'], marker='.') # plot red points when learning rate decays # for e in [150, 225, 275]: # plt.plot([pf['proj_xcoord'][e]], [pf['proj_ycoord'][e]], marker='.', color='r') # add PCA notes df = h5py.File(dir_file,'r') ratio_x = df['explained_variance_ratio_'][0] ratio_y = df['explained_variance_ratio_'][1] plt.xlabel('1st PC: %.2f %%' % (ratio_x*100), fontsize='xx-large') plt.ylabel('2nd PC: %.2f %%' % (ratio_y*100), fontsize='xx-large') df.close() plt.clabel(CS1, inline=1, fontsize=6) plt.clabel(CS2, inline=1, fontsize=6) fig.savefig(proj_file + '_' + surf_name + '_2dcontour_proj.pdf', dpi=300, bbox_inches='tight', format='pdf') pf.close() if show: plt.show()