REINFORCE
REINFORCE
¶
REINFORCE(
env: RL4COEnvBase,
policy: Module,
baseline: REINFORCEBaseline | str = "rollout",
baseline_kwargs: dict = {},
reward_scale: str = None,
**kwargs
)
Bases: RL4COLitModule
REINFORCE algorithm, also known as policy gradients.
See superclass RL4COLitModule for more details.
Parameters:
-
env(RL4COEnvBase) –Environment to use for the algorithm
-
policy(Module) –Policy to use for the algorithm
-
baseline(REINFORCEBaseline | str, default:'rollout') –REINFORCE baseline
-
baseline_kwargs(dict, default:{}) –Keyword arguments for baseline. Ignored if baseline is not a string
-
**kwargs–Keyword arguments passed to the superclass
Methods:
-
calculate_loss–Calculate loss for REINFORCE algorithm.
-
on_train_epoch_end–Callback for end of training epoch: we evaluate the baseline
-
wrap_dataset–Wrap dataset from baseline evaluation. Used in greedy rollout baseline
-
set_decode_type_multistart–Set decode type to
multistartfor train, val and test in policy. -
load_from_checkpoint–Load model from checkpoint/
Source code in rl4co/models/rl/reinforce/reinforce.py
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 | |
calculate_loss
¶
calculate_loss(
td: TensorDict,
batch: TensorDict,
policy_out: dict,
reward: Tensor | None = None,
log_likelihood: Tensor | None = None,
)
Calculate loss for REINFORCE algorithm.
Parameters:
-
td(TensorDict) –TensorDict containing the current state of the environment
-
batch(TensorDict) –Batch of data. This is used to get the extra loss terms, e.g., REINFORCE baseline
-
policy_out(dict) –Output of the policy network
-
reward(Tensor | None, default:None) –Reward tensor. If None, it is taken from
policy_out -
log_likelihood(Tensor | None, default:None) –Log-likelihood tensor. If None, it is taken from
policy_out
Source code in rl4co/models/rl/reinforce/reinforce.py
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 | |
on_train_epoch_end
¶
on_train_epoch_end()
Callback for end of training epoch: we evaluate the baseline
Source code in rl4co/models/rl/reinforce/reinforce.py
123 124 125 126 127 128 129 130 131 132 133 134 | |
wrap_dataset
¶
wrap_dataset(dataset)
Wrap dataset from baseline evaluation. Used in greedy rollout baseline
Source code in rl4co/models/rl/reinforce/reinforce.py
136 137 138 139 140 141 142 143 | |
set_decode_type_multistart
¶
set_decode_type_multistart(phase: str)
Set decode type to multistart for train, val and test in policy.
For example, if the decode type is greedy, it will be set to multistart_greedy.
Parameters:
-
phase(str) –Phase to set decode type for. Must be one of
train,valortest.
Source code in rl4co/models/rl/reinforce/reinforce.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
load_from_checkpoint
classmethod
¶
load_from_checkpoint(
checkpoint_path: _PATH | IO,
map_location: _MAP_LOCATION_TYPE = None,
hparams_file: _PATH | None = None,
strict: bool = False,
load_baseline: bool = True,
**kwargs: Any
) -> Self
Load model from checkpoint/
Note
This is a modified version of load_from_checkpoint from pytorch_lightning.core.saving.
It deals with matching keys for the baseline by first running setup
Source code in rl4co/models/rl/reinforce/reinforce.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
REINFORCEBaseline
¶
REINFORCEBaseline(*args, **kw)
Bases: Module
Base class for REINFORCE baselines
Methods:
-
wrap_dataset–Wrap dataset with baseline-specific functionality
-
eval–Evaluate baseline
-
epoch_callback–Callback at the end of each epoch
-
setup–To be called before training during setup phase
Source code in rl4co/models/rl/reinforce/baselines.py
22 23 24 | |
wrap_dataset
¶
wrap_dataset(dataset: Dataset, *args, **kw)
Wrap dataset with baseline-specific functionality
Source code in rl4co/models/rl/reinforce/baselines.py
26 27 28 | |
eval
abstractmethod
¶
eval(
td: TensorDict,
reward: Tensor,
env: RL4COEnvBase = None,
**kwargs
)
Evaluate baseline
Source code in rl4co/models/rl/reinforce/baselines.py
30 31 32 33 | |
epoch_callback
¶
epoch_callback(*args, **kw)
Callback at the end of each epoch For example, update baseline parameters and obtain baseline values
Source code in rl4co/models/rl/reinforce/baselines.py
35 36 37 38 39 | |
setup
¶
setup(*args, **kw)
To be called before training during setup phase This follow PyTorch Lightning's setup() convention
Source code in rl4co/models/rl/reinforce/baselines.py
41 42 43 44 45 | |
NoBaseline
¶
NoBaseline(*args, **kw)
Bases: REINFORCEBaseline
No baseline: return 0 for baseline and neg_los
Methods:
-
eval–Evaluate baseline
Source code in rl4co/models/rl/reinforce/baselines.py
22 23 24 | |
eval
¶
eval(td, reward, env=None)
Evaluate baseline
Source code in rl4co/models/rl/reinforce/baselines.py
51 52 | |
SharedBaseline
¶
SharedBaseline(*args, **kw)
Bases: REINFORCEBaseline
Shared baseline: return mean of reward as baseline
Methods:
-
eval–Evaluate baseline
Source code in rl4co/models/rl/reinforce/baselines.py
22 23 24 | |
eval
¶
eval(td, reward, env=None, on_dim=1)
Evaluate baseline
Source code in rl4co/models/rl/reinforce/baselines.py
58 59 | |
ExponentialBaseline
¶
ExponentialBaseline(beta=0.8, **kw)
Bases: REINFORCEBaseline
Exponential baseline: return exponential moving average of reward as baseline
Parameters:
-
beta–Beta value for the exponential moving average
Methods:
-
eval–Evaluate baseline
Source code in rl4co/models/rl/reinforce/baselines.py
69 70 71 72 73 | |
eval
¶
eval(td, reward, env=None)
Evaluate baseline
Source code in rl4co/models/rl/reinforce/baselines.py
75 76 77 78 79 80 81 | |
MeanBaseline
¶
MeanBaseline(*args, **kw)
Bases: REINFORCEBaseline
Mean baseline: return mean of reward as baseline
Source code in rl4co/models/rl/reinforce/baselines.py
22 23 24 | |
WarmupBaseline
¶
WarmupBaseline(
baseline, n_epochs=1, warmup_exp_beta=0.8, **kw
)
Bases: REINFORCEBaseline
Warmup baseline: return convex combination of baseline and exponential baseline
Parameters:
-
baseline–Baseline to use after warmup
-
n_epochs–Number of epochs to warmup
-
warmup_exp_beta–Beta value for the exponential baseline during warmup
Methods:
-
wrap_dataset–Wrap dataset with baseline-specific functionality
-
setup–To be called before training during setup phase
-
eval–Evaluate baseline
-
epoch_callback–Callback at the end of each epoch
Source code in rl4co/models/rl/reinforce/baselines.py
100 101 102 103 104 105 106 107 | |
wrap_dataset
¶
wrap_dataset(dataset, *args, **kw)
Wrap dataset with baseline-specific functionality
Source code in rl4co/models/rl/reinforce/baselines.py
109 110 111 112 | |
setup
¶
setup(*args, **kw)
To be called before training during setup phase This follow PyTorch Lightning's setup() convention
Source code in rl4co/models/rl/reinforce/baselines.py
114 115 | |
eval
¶
eval(td, reward, env=None)
Evaluate baseline
Source code in rl4co/models/rl/reinforce/baselines.py
117 118 119 120 121 122 123 124 125 126 127 128 | |
epoch_callback
¶
epoch_callback(*args, **kw)
Callback at the end of each epoch For example, update baseline parameters and obtain baseline values
Source code in rl4co/models/rl/reinforce/baselines.py
130 131 132 133 134 135 | |
CriticBaseline
¶
CriticBaseline(critic: CriticNetwork = None, **unused_kw)
Bases: REINFORCEBaseline
Critic baseline: use critic network as baseline
Parameters:
-
critic(CriticNetwork, default:None) –Critic network to use as baseline. If None, create a new critic network based on the environment
Methods:
Source code in rl4co/models/rl/reinforce/baselines.py
145 146 147 | |
setup
¶
setup(policy, env, **kwargs)
To be called before training during setup phase This follow PyTorch Lightning's setup() convention
Source code in rl4co/models/rl/reinforce/baselines.py
149 150 151 152 | |
eval
¶
eval(x, c, env=None)
Evaluate baseline
Source code in rl4co/models/rl/reinforce/baselines.py
154 155 156 157 | |
RolloutBaseline
¶
RolloutBaseline(bl_alpha=0.05, **kw)
Bases: REINFORCEBaseline
Rollout baseline: use greedy rollout as baseline
Parameters:
-
bl_alpha–Alpha value for the baseline T-test
Methods:
-
setup–To be called before training during setup phase
-
eval–Evaluate rollout baseline
-
epoch_callback–Challenges the current baseline with the policy and replaces the baseline policy if it is improved
-
rollout–Rollout the policy on the given dataset
-
wrap_dataset–Wrap the dataset in a baseline dataset
Source code in rl4co/models/rl/reinforce/baselines.py
167 168 169 | |
setup
¶
setup(*args, **kw)
To be called before training during setup phase This follow PyTorch Lightning's setup() convention
Source code in rl4co/models/rl/reinforce/baselines.py
171 172 | |
eval
¶
eval(td, reward, env)
Evaluate rollout baseline
Warning
This is not differentiable and should only be used for evaluation.
Also, it is recommended to use the rollout method directly instead of this method.
Source code in rl4co/models/rl/reinforce/baselines.py
189 190 191 192 193 194 195 196 197 198 | |
epoch_callback
¶
epoch_callback(
policy,
env,
batch_size=64,
device="cpu",
epoch=None,
dataset_size=None,
)
Challenges the current baseline with the policy and replaces the baseline policy if it is improved
Source code in rl4co/models/rl/reinforce/baselines.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
rollout
¶
rollout(
policy, env, batch_size=64, device="cpu", dataset=None
)
Rollout the policy on the given dataset
Source code in rl4co/models/rl/reinforce/baselines.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
wrap_dataset
¶
wrap_dataset(
dataset, env, batch_size=64, device="cpu", **kw
)
Wrap the dataset in a baseline dataset
Note
This is an alternative to eval that does not require the policy to be passed
at every call but just once. Values are added to the dataset. This also allows for
larger batch sizes since we evauate the policy without gradients.
Source code in rl4co/models/rl/reinforce/baselines.py
239 240 241 242 243 244 245 246 247 248 | |
get_reinforce_baseline
¶
get_reinforce_baseline(name, **kw)
Get a REINFORCE baseline by name The rollout baseline default to warmup baseline with one epoch of exponential baseline and the greedy rollout
Source code in rl4co/models/rl/reinforce/baselines.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |