visualize.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import numpy as np
  2. vis = None
  3. win = None
  4. avg_reward = 0
  5. X = []
  6. Y = []
  7. def visdom_plot(
  8. total_num_steps,
  9. mean_reward
  10. ):
  11. # Lazily import visdom so that people don't need to install visdom
  12. # if they're not actually using it
  13. from visdom import Visdom
  14. global vis
  15. global win
  16. global avg_reward
  17. if vis is None:
  18. vis = Visdom()
  19. assert vis.check_connection()
  20. # Close all existing plots
  21. vis.close()
  22. # Running average for curve smoothing
  23. avg_reward = avg_reward * 0.9 + 0.1 * mean_reward
  24. X.append(total_num_steps)
  25. Y.append(avg_reward)
  26. # The plot with the handle 'win' is updated each time this is called
  27. win = vis.line(
  28. X = np.array(X),
  29. Y = np.array(Y),
  30. opts = dict(
  31. #title = 'All Environments',
  32. xlabel='Total time steps',
  33. ylabel='Reward per episode',
  34. ytickmin=0,
  35. #ytickmax=1,
  36. #ytickstep=0.1,
  37. #legend=legend,
  38. #showlegend=True,
  39. width=900,
  40. height=500
  41. ),
  42. win = win
  43. )