Transductive Methods
Transductive Methods¶
These methods update policy parameters during online testing to improve the solutions of a specific instance.
Active Search (AS)¶
Classes:
-
ActiveSearch
–Active Search for Neural Combination Optimization from Bello et al. (2016).
ActiveSearch
¶
ActiveSearch(
env,
policy,
dataset: Union[Dataset, str],
batch_size: int = 1,
max_iters: int = 200,
augment_size: int = 8,
augment_dihedral: bool = True,
num_parallel_runs: int = 1,
max_runtime: int = 86400,
save_path: str = None,
optimizer: Union[str, Optimizer, partial] = "Adam",
optimizer_kwargs: dict = {
"lr": 0.00026,
"weight_decay": 1e-06,
},
**kwargs
)
Bases: TransductiveModel
Active Search for Neural Combination Optimization from Bello et al. (2016). Fine-tunes the whole policy network (encoder + decoder) on a batch of instances. Reference: https://arxiv.org/abs/1611.09940
Parameters:
-
env
–RL4CO environment to be solved
-
policy
–policy network
-
dataset
(Union[Dataset, str]
) –dataset to be used for training
-
batch_size
(int
, default:1
) –batch size for training
-
max_iters
(int
, default:200
) –maximum number of iterations
-
augment_size
(int
, default:8
) –number of augmentations per state
-
augment_dihedral
(bool
, default:True
) –whether to augment with dihedral rotations
-
parallel_runs
–number of parallel runs
-
max_runtime
(int
, default:86400
) –maximum runtime in seconds
-
save_path
(str
, default:None
) –path to save solution checkpoints
-
optimizer
(Union[str, Optimizer, partial]
, default:'Adam'
) –optimizer to use for training
-
optimizer_kwargs
(dict
, default:{'lr': 0.00026, 'weight_decay': 1e-06}
) –keyword arguments for optimizer
-
**kwargs
–additional keyword arguments
Methods:
-
setup
–Setup base class and instantiate:
-
on_train_batch_start
–Called before training (i.e. search) for a new batch begins.
-
training_step
–Main search loop. We use the training step to effectively adapt to a
batch
of instances. -
on_train_batch_end
–We store the best solution and reward found.
-
on_train_epoch_end
–Called when the training ends.
Source code in rl4co/models/zoo/active_search/search.py
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 |
|
setup
¶
setup(stage='fit')
Setup base class and instantiate:
- augmentation
- instance solutions and rewards
- original policy state dict
Source code in rl4co/models/zoo/active_search/search.py
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 |
|
on_train_batch_start
¶
Called before training (i.e. search) for a new batch begins. We re-load the original policy state dict and configure the optimizer.
Source code in rl4co/models/zoo/active_search/search.py
100 101 102 103 104 105 |
|
training_step
¶
training_step(batch, batch_idx)
Main search loop. We use the training step to effectively adapt to a batch
of instances.
Source code in rl4co/models/zoo/active_search/search.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
|
on_train_batch_end
¶
We store the best solution and reward found.
Source code in rl4co/models/zoo/active_search/search.py
177 178 179 180 181 182 183 184 185 186 |
|
on_train_epoch_end
¶
on_train_epoch_end() -> None
Called when the training ends. If the epoch ends, it means we have finished searching over the instances, thus the trainer should stop.
Source code in rl4co/models/zoo/active_search/search.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
Efficent Active Search (EAS)¶
Classes:
-
EAS
–Efficient Active Search for Neural Combination Optimization from Hottung et al. (2022).
-
EASEmb
–EAS with embedding adaptation
-
EASLay
–EAS with layer adaptation
EAS
¶
EAS(
env,
policy,
dataset: Union[Dataset, str],
use_eas_embedding: bool = True,
use_eas_layer: bool = False,
eas_emb_cache_keys: List[str] = ["logit_key"],
eas_lambda: float = 0.013,
batch_size: int = 2,
max_iters: int = 200,
augment_size: int = 8,
augment_dihedral: bool = True,
num_parallel_runs: int = 1,
baseline: str = "multistart",
max_runtime: int = 86400,
save_path: str = None,
optimizer: Union[str, Optimizer, partial] = "Adam",
optimizer_kwargs: dict = {
"lr": 0.0041,
"weight_decay": 1e-06,
},
verbose: bool = True,
**kwargs
)
Bases: TransductiveModel
Efficient Active Search for Neural Combination Optimization from Hottung et al. (2022). Fine-tunes a subset of parameters (such as node embeddings or newly added layers) thus avoiding expensive re-encoding of the problem. Reference: https://openreview.net/pdf?id=nO5caZwFwYu
Parameters:
-
env
–RL4CO environment to be solved
-
policy
–policy network
-
dataset
(Union[Dataset, str]
) –dataset to be used for training
-
use_eas_embedding
(bool
, default:True
) –whether to use EAS embedding (EASEmb)
-
use_eas_layer
(bool
, default:False
) –whether to use EAS layer (EASLay)
-
eas_emb_cache_keys
(List[str]
, default:['logit_key']
) –keys to cache in the embedding
-
eas_lambda
(float
, default:0.013
) –lambda parameter for IL loss
-
batch_size
(int
, default:2
) –batch size for training
-
max_iters
(int
, default:200
) –maximum number of iterations
-
augment_size
(int
, default:8
) –number of augmentations per state
-
augment_dihedral
(bool
, default:True
) –whether to augment with dihedral rotations
-
parallel_runs
–number of parallel runs
-
baseline
(str
, default:'multistart'
) –REINFORCE baseline type (multistart, symmetric, full)
-
max_runtime
(int
, default:86400
) –maximum runtime in seconds
-
save_path
(str
, default:None
) –path to save solution checkpoints
-
optimizer
(Union[str, Optimizer, partial]
, default:'Adam'
) –optimizer to use for training
-
optimizer_kwargs
(dict
, default:{'lr': 0.0041, 'weight_decay': 1e-06}
) –keyword arguments for optimizer
-
verbose
(bool
, default:True
) –whether to print progress for each iteration
Methods:
-
setup
–Setup base class and instantiate:
-
on_train_batch_start
–Called before training (i.e. search) for a new batch begins.
-
training_step
–Main search loop. We use the training step to effectively adapt to a
batch
of instances. -
on_train_batch_end
–We store the best solution and reward found.
-
on_train_epoch_end
–Called when the train ends.
Source code in rl4co/models/zoo/eas/search.py
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 |
|
setup
¶
setup(stage='fit')
Setup base class and instantiate:
- augmentation
- instance solutions and rewards
- original policy state dict
Source code in rl4co/models/zoo/eas/search.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
|
on_train_batch_start
¶
Called before training (i.e. search) for a new batch begins. We re-load the original policy state dict and configure all parameters not to require gradients. We do the rest in the training step.
Source code in rl4co/models/zoo/eas/search.py
126 127 128 129 130 131 132 133 134 135 |
|
training_step
¶
training_step(batch, batch_idx)
Main search loop. We use the training step to effectively adapt to a batch
of instances.
Source code in rl4co/models/zoo/eas/search.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
|
on_train_batch_end
¶
We store the best solution and reward found.
Source code in rl4co/models/zoo/eas/search.py
283 284 285 286 287 288 289 290 |
|
on_train_epoch_end
¶
on_train_epoch_end() -> None
Called when the train ends.
Source code in rl4co/models/zoo/eas/search.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
|
EASEmb
¶
EASEmb(*args, **kwargs)
Bases: EAS
EAS with embedding adaptation
Source code in rl4co/models/zoo/eas/search.py
314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
|
EASLay
¶
EASLay(*args, **kwargs)
Bases: EAS
EAS with layer adaptation
Source code in rl4co/models/zoo/eas/search.py
333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
|
Functions:
-
forward_pointer_attn_eas_lay
–Add layer to the forward pass of logit attention, i.e.
-
forward_eas
–Forward pass of the decoder
forward_pointer_attn_eas_lay
¶
forward_pointer_attn_eas_lay(
self, query, key, value, logit_key, mask
)
Add layer to the forward pass of logit attention, i.e. Single-head attention.
Source code in rl4co/models/zoo/eas/decoder.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
forward_eas
¶
forward_eas(
self,
td: TensorDict,
cached_embeds,
best_solutions,
iter_count: int = 0,
env: Union[str, RL4COEnvBase] = None,
decode_type: str = "multistart_sampling",
num_starts: int = None,
mask_logits: bool = True,
temperature: float = 1.0,
tanh_clipping: float = 0,
**decode_kwargs
)
Forward pass of the decoder Given the environment state and the pre-computed embeddings, compute the logits and sample actions
Parameters:
-
td
(TensorDict
) –Input TensorDict containing the environment state
-
embeddings
–Precomputed embeddings for the nodes. Can be already precomputed cached in form of q, k, v and
-
env
(Union[str, RL4COEnvBase]
, default:None
) –Environment to use for decoding. If None, the environment is instantiated from
env_name
. Note that it is more efficient to pass an already instantiated environment each time for fine-grained control -
decode_type
(str
, default:'multistart_sampling'
) –Type of decoding to use. Can be one of:
- "sampling": sample from the logits
- "greedy": take the argmax of the logits
- "multistart_sampling": sample as sampling, but with multi-start decoding
- "multistart_greedy": sample as greedy, but with multi-start decoding
-
num_starts
(int
, default:None
) –Number of multi-starts to use. If None, will be calculated from the action mask
-
calc_reward
–Whether to calculate the reward for the decoded sequence
Source code in rl4co/models/zoo/eas/decoder.py
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 117 118 119 120 121 122 123 124 125 126 127 128 |
|
Classes:
-
EASLayerNet
–Instantiate weights and biases for the added layer.
EASLayerNet
¶
Bases: Module
Instantiate weights and biases for the added layer.
The layer is defined as: h = relu(emb * W1 + b1); out = h * W2 + b2.
Wrapping in nn.Parameter
makes the parameters trainable and sets gradient to True.
Parameters:
Methods:
-
forward
–emb: [num_instances, group_num, emb_dim]
Source code in rl4co/models/zoo/eas/nn.py
15 16 17 18 19 20 21 22 23 |
|
forward
¶
forward(*args)
emb: [num_instances, group_num, emb_dim]
Source code in rl4co/models/zoo/eas/nn.py
25 26 27 28 29 30 |
|