plot_2d_3.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. def plot_contour_trajectory(surf_file, dir_file, proj_file, surf_name='loss_vals',
  2. vmin=0.1, vmax=10, vlevel=0.5, show=False):
  3. """2D contour + trajectory"""
  4. assert exists(surf_file) and exists(proj_file) and exists(dir_file)
  5. # plot contours
  6. f = h5py.File(surf_file,'r')
  7. x = np.array(f['xcoordinates'][:])
  8. y = np.array(f['ycoordinates'][:])
  9. X, Y = np.meshgrid(x, y)
  10. if surf_name in f.keys():
  11. Z = np.array(f[surf_name][:])
  12. fig = plt.figure()
  13. CS1 = plt.contour(X, Y, Z, levels=np.arange(vmin, vmax, vlevel))
  14. CS2 = plt.contour(X, Y, Z, levels=np.logspace(1, 8, num=8))
  15. # plot trajectories
  16. pf = h5py.File(proj_file, 'r')
  17. plt.plot(pf['proj_xcoord'], pf['proj_ycoord'], marker='.')
  18. # plot red points when learning rate decays
  19. # for e in [150, 225, 275]:
  20. # plt.plot([pf['proj_xcoord'][e]], [pf['proj_ycoord'][e]], marker='.', color='r')
  21. # add PCA notes
  22. df = h5py.File(dir_file,'r')
  23. ratio_x = df['explained_variance_ratio_'][0]
  24. ratio_y = df['explained_variance_ratio_'][1]
  25. plt.xlabel('1st PC: %.2f %%' % (ratio_x*100), fontsize='xx-large')
  26. plt.ylabel('2nd PC: %.2f %%' % (ratio_y*100), fontsize='xx-large')
  27. df.close()
  28. plt.clabel(CS1, inline=1, fontsize=6)
  29. plt.clabel(CS2, inline=1, fontsize=6)
  30. fig.savefig(proj_file + '_' + surf_name + '_2dcontour_proj.pdf', dpi=300,
  31. bbox_inches='tight', format='pdf')
  32. pf.close()
  33. if show: plt.show()