plot_2d.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. """
  2. 2D plotting funtions
  3. """
  4. from mpl_toolkits.mplot3d import Axes3D
  5. from matplotlib import pyplot as plt
  6. from matplotlib import cm
  7. import h5py
  8. import argparse
  9. import numpy as np
  10. from os.path import exists
  11. import seaborn as sns
  12. def plot_2d_contour(surf_file, surf_name='train_loss', vmin=0.1, vmax=10, vlevel=0.5, show=False):
  13. """Plot 2D contour map and 3D surface."""
  14. f = h5py.File(surf_file, 'r')
  15. x = np.array(f['xcoordinates'][:])
  16. y = np.array(f['ycoordinates'][:])
  17. X, Y = np.meshgrid(x, y)
  18. if surf_name in f.keys():
  19. Z = np.array(f[surf_name][:])
  20. elif surf_name == 'train_err' or surf_name == 'test_err' :
  21. Z = 100 - np.array(f[surf_name][:])
  22. else:
  23. print ('%s is not found in %s' % (surf_name, surf_file))
  24. print('------------------------------------------------------------------')
  25. print('plot_2d_contour')
  26. print('------------------------------------------------------------------')
  27. print("loading surface file: " + surf_file)
  28. print('len(xcoordinates): %d len(ycoordinates): %d' % (len(x), len(y)))
  29. print('max(%s) = %f \t min(%s) = %f' % (surf_name, np.max(Z), surf_name, np.min(Z)))
  30. print(Z)
  31. if (len(x) <= 1 or len(y) <= 1):
  32. print('The length of coordinates is not enough for plotting contours')
  33. return
  34. # --------------------------------------------------------------------
  35. # Plot 2D contours
  36. # --------------------------------------------------------------------
  37. fig = plt.figure()
  38. CS = plt.contour(X, Y, Z, cmap='summer', levels=np.arange(vmin, vmax, vlevel))
  39. plt.clabel(CS, inline=1, fontsize=8)
  40. fig.savefig(surf_file + '_' + surf_name + '_2dcontour' + '.pdf', dpi=300,
  41. bbox_inches='tight', format='pdf')
  42. fig = plt.figure()
  43. print(surf_file + '_' + surf_name + '_2dcontourf' + '.pdf')
  44. CS = plt.contourf(X, Y, Z, cmap='summer', levels=np.arange(vmin, vmax, vlevel))
  45. fig.savefig(surf_file + '_' + surf_name + '_2dcontourf' + '.pdf', dpi=300,
  46. bbox_inches='tight', format='pdf')
  47. # --------------------------------------------------------------------
  48. # Plot 2D heatmaps
  49. # --------------------------------------------------------------------
  50. fig = plt.figure()
  51. sns_plot = sns.heatmap(Z, cmap='viridis', cbar=True, vmin=vmin, vmax=vmax,
  52. xticklabels=False, yticklabels=False)
  53. sns_plot.invert_yaxis()
  54. sns_plot.get_figure().savefig(surf_file + '_' + surf_name + '_2dheat.pdf',
  55. dpi=300, bbox_inches='tight', format='pdf')
  56. # --------------------------------------------------------------------
  57. # Plot 3D surface
  58. # --------------------------------------------------------------------
  59. fig = plt.figure()
  60. ax = Axes3D(fig)
  61. surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm, linewidth=0, antialiased=False)
  62. fig.colorbar(surf, shrink=0.5, aspect=5)
  63. fig.savefig(surf_file + '_' + surf_name + '_3dsurface.pdf', dpi=300,
  64. bbox_inches='tight', format='pdf')
  65. f.close()
  66. if show: plt.show()
  67. def plot_trajectory(proj_file, dir_file, show=False):
  68. """ Plot optimization trajectory on the plane spanned by given directions."""
  69. assert exists(proj_file), 'Projection file does not exist.'
  70. f = h5py.File(proj_file, 'r')
  71. fig = plt.figure()
  72. plt.plot(f['proj_xcoord'], f['proj_ycoord'], marker='.')
  73. plt.tick_params('y', labelsize='x-large')
  74. plt.tick_params('x', labelsize='x-large')
  75. f.close()
  76. if exists(dir_file):
  77. f2 = h5py.File(dir_file,'r')
  78. if 'explained_variance_ratio_' in f2.keys():
  79. ratio_x = f2['explained_variance_ratio_'][0]
  80. ratio_y = f2['explained_variance_ratio_'][1]
  81. plt.xlabel('1st PC: %.2f %%' % (ratio_x*100), fontsize='xx-large')
  82. plt.ylabel('2nd PC: %.2f %%' % (ratio_y*100), fontsize='xx-large')
  83. f2.close()
  84. fig.savefig(proj_file + '.pdf', dpi=300, bbox_inches='tight', format='pdf')
  85. if show: plt.show()
  86. def plot_contour_trajectory(surf_file, dir_file, proj_file, surf_name='loss_vals',
  87. vmin=0.1, vmax=10, vlevel=0.5, show=False):
  88. """2D contour + trajectory"""
  89. assert exists(surf_file) and exists(proj_file) and exists(dir_file)
  90. # plot contours
  91. f = h5py.File(surf_file,'r')
  92. x = np.array(f['xcoordinates'][:])
  93. y = np.array(f['ycoordinates'][:])
  94. X, Y = np.meshgrid(x, y)
  95. if surf_name in f.keys():
  96. Z = np.array(f[surf_name][:])
  97. fig = plt.figure()
  98. CS1 = plt.contour(X, Y, Z, levels=np.arange(vmin, vmax, vlevel))
  99. CS2 = plt.contour(X, Y, Z, levels=np.logspace(1, 8, num=8))
  100. # plot trajectories
  101. pf = h5py.File(proj_file, 'r')
  102. plt.plot(pf['proj_xcoord'], pf['proj_ycoord'], marker='.')
  103. # plot red points when learning rate decays
  104. # for e in [150, 225, 275]:
  105. # plt.plot([pf['proj_xcoord'][e]], [pf['proj_ycoord'][e]], marker='.', color='r')
  106. # add PCA notes
  107. df = h5py.File(dir_file,'r')
  108. ratio_x = df['explained_variance_ratio_'][0]
  109. ratio_y = df['explained_variance_ratio_'][1]
  110. plt.xlabel('1st PC: %.2f %%' % (ratio_x*100), fontsize='xx-large')
  111. plt.ylabel('2nd PC: %.2f %%' % (ratio_y*100), fontsize='xx-large')
  112. df.close()
  113. plt.clabel(CS1, inline=1, fontsize=6)
  114. plt.clabel(CS2, inline=1, fontsize=6)
  115. fig.savefig(proj_file + '_' + surf_name + '_2dcontour_proj.pdf', dpi=300,
  116. bbox_inches='tight', format='pdf')
  117. pf.close()
  118. if show: plt.show()
  119. def plot_2d_eig_ratio(surf_file, val_1='min_eig', val_2='max_eig', show=False):
  120. """ Plot the heatmap of eigenvalue ratios, i.e., |min_eig/max_eig| of hessian """
  121. print('------------------------------------------------------------------')
  122. print('plot_2d_eig_ratio')
  123. print('------------------------------------------------------------------')
  124. print("loading surface file: " + surf_file)
  125. f = h5py.File(surf_file,'r')
  126. x = np.array(f['xcoordinates'][:])
  127. y = np.array(f['ycoordinates'][:])
  128. X, Y = np.meshgrid(x, y)
  129. Z1 = np.array(f[val_1][:])
  130. Z2 = np.array(f[val_2][:])
  131. # Plot 2D heatmaps with color bar using seaborn
  132. abs_ratio = np.absolute(np.divide(Z1, Z2))
  133. print(abs_ratio)
  134. fig = plt.figure()
  135. sns_plot = sns.heatmap(abs_ratio, cmap='viridis', vmin=0, vmax=.5, cbar=True,
  136. xticklabels=False, yticklabels=False)
  137. sns_plot.invert_yaxis()
  138. sns_plot.get_figure().savefig(surf_file + '_' + val_1 + '_' + val_2 + '_abs_ratio_heat_sns.pdf',
  139. dpi=300, bbox_inches='tight', format='pdf')
  140. # Plot 2D heatmaps with color bar using seaborn
  141. ratio = np.divide(Z1, Z2)
  142. print(ratio)
  143. fig = plt.figure()
  144. sns_plot = sns.heatmap(ratio, cmap='viridis', cbar=True, xticklabels=False, yticklabels=False)
  145. sns_plot.invert_yaxis()
  146. sns_plot.get_figure().savefig(surf_file + '_' + val_1 + '_' + val_2 + '_ratio_heat_sns.pdf',
  147. dpi=300, bbox_inches='tight', format='pdf')
  148. f.close()
  149. if show: plt.show()
  150. if __name__ == '__main__':
  151. parser = argparse.ArgumentParser(description='Plot 2D loss surface')
  152. parser.add_argument('--surf_file', '-f', default='', help='The h5 file that contains surface values')
  153. parser.add_argument('--dir_file', default='', help='The h5 file that contains directions')
  154. parser.add_argument('--proj_file', default='', help='The h5 file that contains the projected trajectories')
  155. parser.add_argument('--surf_name', default='train_loss', help='The type of surface to plot')
  156. parser.add_argument('--vmax', default=10, type=float, help='Maximum value to map')
  157. parser.add_argument('--vmin', default=0.1, type=float, help='Miminum value to map')
  158. parser.add_argument('--vlevel', default=0.5, type=float, help='plot contours every vlevel')
  159. parser.add_argument('--zlim', default=10, type=float, help='Maximum loss value to show')
  160. parser.add_argument('--show', action='store_true', default=False, help='show plots')
  161. args = parser.parse_args()
  162. if exists(args.surf_file) and exists(args.proj_file) and exists(args.dir_file):
  163. plot_contour_trajectory(args.surf_file, args.dir_file, args.proj_file,
  164. args.surf_name, args.vmin, args.vmax, args.vlevel, args.show)
  165. elif exists(args.proj_file) and exists(args.dir_file):
  166. plot_trajectory(args.proj_file, args.dir_file, args.show)
  167. elif exists(args.surf_file):
  168. plot_2d_contour(args.surf_file, args.surf_name, args.vmin, args.vmax, args.vlevel, args.show)