-
Notifications
You must be signed in to change notification settings - Fork 361
/
Copy pathgetting-started-4.py
116 lines (94 loc) · 4.11 KB
/
getting-started-4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""
Get started with logging
========================
**Author**: `Vincent Moens <https://github.com/vmoens>`_
.. _gs_logging:
.. note:: To run this tutorial in a notebook, add an installation cell
at the beginning containing:
.. code-block::
!pip install tensordict
!pip install torchrl
"""
#####################################
# The final chapter of this series before we orchestrate everything in a
# training script is to learn about logging.
#
# Loggers
# -------
#
# Logging is crucial for reporting your results to the outside world and for
# you to check that your algorithm is learning properly. TorchRL has several
# loggers that interface with custom backends such as
# wandb (:class:`~torchrl.record.loggers.wandb.WandbLogger`),
# tensorboard (:class:`~torchrl.record.loggers.tensorboard.TensorBoardLogger`) or a lightweight and
# portable CSV logger (:class:`~torchrl.record.loggers.csv.CSVLogger`) that you can use
# pretty much everywhere.
#
# Loggers are located in the ``torchrl.record`` module and the various classes
# can be found in the :ref:`API reference <ref_loggers>`.
#
# We tried to keep the loggers APIs as similar as we could, given the
# differences in the underlying backends. While execution of the loggers will
# mostly be interchangeable, their instantiation can differ.
#
# Usually, building a logger requires
# at least an experiment name and possibly a logging directory and other
# hyperparameters.
#
from torchrl.record import CSVLogger
logger = CSVLogger(exp_name="my_exp")
#####################################
# Once the logger is instantiated, the only thing left to do is call the
# logging methods! For example, :meth:`~torchrl.record.CSVLogger.log_scalar`
# is used in several places across the training examples to log values such as
# reward, loss value or time elapsed for executing a piece of code.
logger.log_scalar("my_scalar", 0.4)
#####################################
# Recording videos
# ----------------
#
# Finally, it can come in handy to record videos of a simulator. Some
# environments (e.g., Atari games) are already rendered as images whereas
# others require you to create them as such. Fortunately, in most common cases,
# rendering and recording videos isn't too difficult.
#
# Let's first see how we can create a Gym environment that outputs images
# alongside its observations. :class:`~torchrl.envs.GymEnv` accept two keywords
# for this purpose:
# - ``from_pixels=True`` will make the env ``step`` function
# write a ``"pixels"`` entry containing the images corresponding to your
# observations, and
#
# - ``pixels_only=False`` will indicate that you want the
# observations to be returned as well.
#
from torchrl.envs import GymEnv
env = GymEnv("CartPole-v1", from_pixels=True, pixels_only=False)
print(env.rollout(max_steps=3))
from torchrl.envs import TransformedEnv
#####################################
# We now have built an environment that renders images with its observations.
# To record videos, we will need to combine that environment with a recorder
# and the logger (the logger providing the backend to save the video).
# This will happen within a transformed environment, like the one we saw in
# the :ref:`first tutorial <gs_env_ted>`.
from torchrl.record import VideoRecorder
recorder = VideoRecorder(logger, tag="my_video")
record_env = TransformedEnv(env, recorder)
#####################################
# When running this environment, all the ``"pixels"`` entries will be saved in
# a local buffer (i.e. RAM) and dumped in a video on demand (to prevent excessive
# RAM usage, you are advised to call this method whenever appropriate!):
rollout = record_env.rollout(max_steps=3)
# Uncomment this line to save the video on disk:
# recorder.dump()
#####################################
# In this specific case, the video format can be chosen when instantiating
# the CSVLogger.
#
# (If you want to customise how your video is recorded, have a look at :ref:`our knowledge base <ref_knowledge_base>`.)
#
# This is all we wanted to cover in the getting started tutorial.
# You should now be ready to code your
# :ref:`first training loop with TorchRL <gs_first_training>`!
#