Skip to content

Commit c2ee72f

Browse files
committed
Visualise actor outputs
1 parent 6b845ae commit c2ee72f

3 files changed

Lines changed: 26 additions & 0 deletions

File tree

src/ppo/actor.pt

20.8 KB
Binary file not shown.

src/ppo/critic.pt

20.1 KB
Binary file not shown.

src/ppo/main.clj

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,32 @@
10461046
(torch/save (py. critic state_dict) "critic.pt")
10471047
(System/exit 0)))
10481048

1049+
;; ## Visualisation of Actor Output
1050+
1051+
;; We can use [dtype-next](https://cnuernber.github.io/dtype-next/) to visualise the output of the actor.
1052+
;; First we need to load additional modules.
1053+
(require '[tech.v3.datatype :as dtype]
1054+
'[tech.v3.tensor :as dtt]
1055+
'[tech.v3.libs.buffered-image :as bufimg]
1056+
'[tech.v3.datatype.functional :as dfn])
1057+
1058+
;; Here we load a pre-trained model and visualise the output of the actor.
1059+
(def actor (Actor 3 64 1))
1060+
(py. actor load_state_dict (torch/load "src/ppo/actor.pt"))
1061+
1062+
(let [angle-values (torch/linspace (- PI) PI 854)
1063+
speed-values (torch/linspace 1.0 -1.0 480)
1064+
grid (torch/meshgrid speed-values angle-values :indexing "ij")
1065+
cos-angle (torch/cos (last grid))
1066+
sin-angle (torch/sin (last grid))
1067+
observations (torch/stack [(py. cos-angle ravel)
1068+
(py. sin-angle ravel)
1069+
(py. (first grid) ravel)]
1070+
:axis 1)
1071+
actions (without-gradient (py. (py. (py. actor deterministic_act observations) reshape 480 854) numpy))
1072+
actions-tensor (dtype/elemwise-cast (dtt/ensure-tensor (py/->jvm actions)) :float32)]
1073+
(bufimg/tensor->image (dfn/* actions-tensor 255)))
1074+
10491075
;; ## Automated Pendulum
10501076
;;
10511077
;; The pendulum implementation can now be updated to use the actor instead of the mouse position as motor input when the mouse button is pressed.

0 commit comments

Comments
 (0)