swarmrl.networks.flax_network Module API Reference¶
Jax model for reinforcement learning.
FlaxModel
¶
Bases: Network
, ABC
Class for the Flax model in ZnRND.
Attributes¶
epoch_count : int Current epoch stage. Used in saving the models.
Source code in swarmrl/networks/flax_network.py
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 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 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 |
|
__call__(params, episode_features)
¶
vmaped version of the model call function. Operates on a batch of episodes.
Parameters¶
parmas : dict Parameters of the model. episode_features: np.ndarray (n_steps, n_agents, observable_dimension) Features of the episode. This contains the features of all agents, for all time steps in the episode.
Returns¶
logits : np.ndarray Output of the network.
Source code in swarmrl/networks/flax_network.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
|
__init__(flax_model, input_shape, optimizer=None, exploration_policy=RandomExploration(probability=0.0), sampling_strategy=GumbelDistribution(), rng_key=None, deployment_mode=False)
¶
Constructor for a Flax model.
Parameters¶
flax_model : nn.Module Flax model as a neural network. optimizer : Callable optimizer to use in the training. OpTax is used by default and cross-compatibility is not assured. input_shape : tuple Shape of the NN input. rng_key : int Key to seed the model with. Default is a randomly generated key but the parameter is here for testing purposes. deployment_mode : bool If true, the model is a shell for the network and nothing else. No training can be performed, this is only used in deployment.
Source code in swarmrl/networks/flax_network.py
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 |
|
compute_action(observables)
¶
Compute and action from the action space.
This method computes an action on all colloids of the relevant type.
Parameters¶
observables : List (n_agents, observable_dimension) Observable for each colloid for which the action should be computed.
Returns¶
tuple : (np.ndarray, np.ndarray) The first element is an array of indices corresponding to the action taken by the agent. The value is bounded between 0 and the number of output neurons. The second element is an array of the corresponding log_probs (i.e. the output of the network put through a softmax).
Source code in swarmrl/networks/flax_network.py
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 |
|
export_model(filename='model', directory='Models')
¶
Export the model state to a directory.
Parameters¶
filename : str (default=models) Name of the file the models are saved in. directory : str (default=Models) Directory in which to save the models. If the directory is not in the currently directory, it will be created.
Source code in swarmrl/networks/flax_network.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
|
reinitialize_network()
¶
Initialize the neural network.
Source code in swarmrl/networks/flax_network.py
123 124 125 126 127 128 129 130 |
|
restore_model_state(filename, directory)
¶
Restore the model state from a file.
Parameters¶
filename : str Name of the model state file directory : str Path to the model state file.
Returns¶
Updates the model state.
Source code in swarmrl/networks/flax_network.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
|
update_model(grads)
¶
Train the model.
See the parent class for a full doc-string.
Source code in swarmrl/networks/flax_network.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|