velora.buffer¶
Documentation
Storage buffers for all algorithms.
BatchExperience
dataclass
¶
Storage container for a batch agent experiences.
Attributes:
Name | Type | Description |
---|---|---|
states |
torch.Tensor
|
a batch of environment observations |
actions |
torch.Tensor
|
a batch of agent actions taken in the states |
rewards |
torch.Tensor
|
a batch of rewards obtained for taking the actions |
next_states |
torch.Tensor
|
a batch of newly generated environment observations following the actions taken |
dones |
torch.Tensor
|
a batch of environment completion statuses |
hiddens |
torch.Tensor
|
a batch of prediction network hidden states (e.g., Actor) |
Source code in velora/buffer/experience.py
Python | |
---|---|
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
BufferBase
¶
A base class for all buffers.
Stores experiences (states, actions, rewards, next_states, dones)
as
individual items in tensors.
Source code in velora/buffer/base.py
Python | |
---|---|
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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
|
__init__(capacity, state_dim, action_dim, hidden_dim, *, device=None)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
capacity
|
int
|
the total capacity of the buffer |
required |
state_dim
|
int
|
dimension of state observations |
required |
action_dim
|
int
|
dimension of actions |
required |
hidden_dim
|
int
|
dimension of hidden state |
required |
device
|
torch.device
|
the device to perform computations on |
None
|
Source code in velora/buffer/base.py
Python | |
---|---|
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 |
|
__len__()
¶
Gets the current size of the buffer.
Returns:
Name | Type | Description |
---|---|---|
size |
int
|
the current size of the buffer. |
Source code in velora/buffer/base.py
Python | |
---|---|
157 158 159 160 161 162 163 164 |
|
add(state, action, reward, next_state, done, hidden)
¶
Adds a single experience to the buffer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
torch.Tensor
|
current state observation |
required |
action
|
torch.Tensor
|
action taken |
required |
reward
|
float
|
reward received |
required |
next_state
|
torch.Tensor
|
next state observation |
required |
done
|
bool
|
whether the episode ended |
required |
hidden
|
torch.Tensor
|
Actor hidden state (prediction network) |
required |
Source code in velora/buffer/base.py
Python | |
---|---|
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 |
|
add_multi(states, actions, rewards, next_states, dones, hiddens)
¶
Adds a set of experience to the buffer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
states
|
torch.Tensor
|
current state observations |
required |
actions
|
torch.Tensor
|
action takens |
required |
rewards
|
torch.Tensor
|
rewards received |
required |
next_states
|
torch.Tensor
|
next state observations |
required |
dones
|
torch.Tensor
|
whether the episode ended |
required |
hiddens
|
torch.Tensor
|
Actor hidden states (prediction network) |
required |
Source code in velora/buffer/base.py
Python | |
---|---|
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 |
|
load(state_path, metadata)
classmethod
¶
Restores the buffer from a saved state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state_path
|
str | Path
|
the filepath to the buffer state |
required |
metadata
|
Dict[str, Any]
|
a dictionary of metadata already
loaded from a |
required |
Returns:
Name | Type | Description |
---|---|---|
buffer |
Self
|
a new buffer instance with the saved state restored |
Source code in velora/buffer/base.py
Python | |
---|---|
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 |
|
metadata()
¶
Gets the metadata of the buffer.
Includes:
capacity
- the maximum capacity of the buffer.state_dim
- state dimension.action_dim
- action dimension.hidden_dim
- hidden state dimension.position
- current buffer position.size
- current size of buffer.device
- the device used for computations.
Returns:
Name | Type | Description |
---|---|---|
metadata |
Dict[str, Any]
|
the buffers metadata |
Source code in velora/buffer/base.py
Python | |
---|---|
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 |
|
sample()
abstractmethod
¶
Samples experience from the buffer.
Returns:
Name | Type | Description |
---|---|---|
batch |
BatchExperience
|
an object of samples with the attributes ( All items have the same shape |
Source code in velora/buffer/base.py
Python | |
---|---|
145 146 147 148 149 150 151 152 153 154 155 |
|
save(dirpath, prefix='buffer_')
¶
Saves a buffers state_dict()
to a safetensors
file.
Includes:
<prefix>metadata.json
- the buffers metadata<prefix>state.safetensors
- the buffer state
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dirpath
|
str | Path
|
the folder path to save the buffer state |
required |
prefix
|
str
|
a name prefix for the files |
'buffer_'
|
Source code in velora/buffer/base.py
Python | |
---|---|
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
|
state_dict()
¶
Return a dictionary containing the buffers state.
Includes:
states
- tensor of states.actions
- tensor of actions.rewards
- tensor of rewards.next_states
- tensor of next states.dones
- tensor of dones.hiddens
- tensor of Actor hidden states (prediction network).
Returns:
Name | Type | Description |
---|---|---|
state_dict |
Dict[str, torch.Tensor]
|
the current state of the buffer |
Source code in velora/buffer/base.py
Python | |
---|---|
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
|
ReplayBuffer
¶
Bases: BufferBase
A Buffer for storing agent experiences. Used for Off-Policy agents.
First introduced in Deep RL in the Deep Q-Network paper: Player Atari with Deep Reinforcement Learning.
Source code in velora/buffer/replay.py
Python | |
---|---|
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 117 118 119 120 121 122 123 124 125 |
|
__init__(capacity, state_dim, action_dim, hidden_dim, *, device=None)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
capacity
|
int
|
the total capacity of the buffer |
required |
state_dim
|
int
|
dimension of state observations |
required |
action_dim
|
int
|
dimension of actions |
required |
hidden_dim
|
int
|
dimension of hidden state |
required |
device
|
torch.device
|
the device to perform computations on |
None
|
Source code in velora/buffer/replay.py
Python | |
---|---|
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
config()
¶
Creates a buffer config model.
Returns:
Name | Type | Description |
---|---|---|
config |
BufferConfig
|
a config model with buffer details. |
Source code in velora/buffer/replay.py
Python | |
---|---|
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
sample(batch_size)
¶
Samples a random batch of experiences from the buffer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch_size
|
int
|
the number of items to sample |
required |
Returns:
Name | Type | Description |
---|---|---|
batch |
BatchExperience
|
an object of samples with the attributes ( All items have the same shape |
Source code in velora/buffer/replay.py
Python | |
---|---|
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 |
|
warm(agent, n_samples, num_envs=8)
¶
Warms the buffer to fill it to a number of samples by generating them
from an agent using a vectorized
copy of the environment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
agent
|
Any
|
the agent to generate samples with |
required |
n_samples
|
int
|
the maximum number of samples to generate |
required |
num_envs
|
int
|
number of vectorized environments. Cannot
be smaller than |
8
|
Source code in velora/buffer/replay.py
Python | |
---|---|
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 |
|