velora.models.lnn¶
Documentation
Liquid Neural Network building blocks.
NCPLiquidCell
¶
A Liquid Time-Constant (LTC) cell using a Closed-form (CfC) approach.
The LTC cell follows the closed-form continuous-depth (CFC; Equation 10) solution from the paper: Closed-form Continuous-time Neural Models.
Equation: $$ x(t) = \sigma(-f(x, I, θ_f), t) \; g(x, I, θ_g) + \left[ 1 - \sigma(-[\;f(x, I, θ_f)\;]\;t) \right] \; h(x, I, θ_h) $$
Source code in velora/models/lnn/cell.py
Python | |
---|---|
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 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 |
|
__init__(in_features, n_hidden, mask, *, init_type='kaiming_uniform', device=None)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_features
|
int
|
number of input nodes. |
required |
n_hidden
|
int
|
number of hidden nodes. |
required |
mask
|
torch.Tensor
|
a matrix of sparse connections
usually containing a combination of |
required |
init_type
|
str
|
the type of weight initialization |
'kaiming_uniform'
|
device
|
torch.device
|
the device to load tensors on. |
None
|
Source code in velora/models/lnn/cell.py
Python | |
---|---|
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 |
|
forward(x, hidden)
¶
Performs a forward pass through the cell.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
torch.Tensor
|
input values. |
required |
hidden
|
torch.Tensor
|
current hidden state. |
required |
Returns:
Name | Type | Description |
---|---|---|
y_pred |
torch.Tensor
|
the cell prediction. |
h_state |
torch.Tensor
|
the hidden state. |
Source code in velora/models/lnn/cell.py
Python | |
---|---|
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
update_mask(mask)
¶
Updates the sparsity mask with a new one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mask
|
torch.Tensor
|
new mask |
required |
Source code in velora/models/lnn/cell.py
Python | |
---|---|
138 139 140 141 142 143 144 145 |
|
LiquidNCPNetwork
¶
A CfC Liquid Neural Circuit Policy (NCP) Network with three layers:
- Inter (input) - a
SparseLinear
layer - Command (hidden) - a
NCPLiquidCell
layer - Motor (output) - a
SparseLinear
layer
Decision nodes
inter
and command
neurons are automatically calculated using:
Python | |
---|---|
1 2 |
|
Combines a Liquid Time-Constant (LTC) cell with Ordinary Neural Circuits (ONCs). Paper references:
Source code in velora/models/lnn/ncp.py
Python | |
---|---|
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 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 |
|
active_params
property
¶
Gets the network's active parameter count.
Returns:
Name | Type | Description |
---|---|---|
count |
int
|
the active parameter count. |
total_params
property
¶
Gets the network's total parameter count.
Returns:
Name | Type | Description |
---|---|---|
count |
int
|
the total parameter count. |
__init__(in_features, n_neurons, out_features, *, sparsity_level=0.5, init_type='kaiming_uniform', device=None)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_features
|
int
|
number of inputs (sensory nodes) |
required |
n_neurons
|
int
|
number of decision nodes (inter and command nodes) |
required |
out_features
|
int
|
number of out features (motor nodes) |
required |
sparsity_level
|
float
|
controls the connection sparsity between neurons. Must be a value between
|
0.5
|
init_type
|
str
|
the type of weight initialization |
'kaiming_uniform'
|
device
|
torch.device
|
the device to load tensors on |
None
|
Source code in velora/models/lnn/ncp.py
Python | |
---|---|
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 |
|
forward(x, h_state=None)
¶
Performs a forward pass through the network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
torch.Tensor
|
an input tensor of shape:
|
required |
h_state
|
torch.Tensor
|
initial hidden state of the RNN with
shape:
|
None
|
Returns:
Name | Type | Description |
---|---|---|
y_pred |
torch.Tensor
|
the network prediction. When |
h_state |
torch.Tensor
|
the final hidden state. Output shape is |
Source code in velora/models/lnn/ncp.py
Python | |
---|---|
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 |
|
NCPNetwork
¶
A Neural Circuit Policy (NCP) Network with three layers:
- Inter (input) - a
SparseLinear
layer - Command (hidden) - a
SparseLinear
layer - Motor (output) - a
SparseLinear
layer
Uses the Mish activation function between each layer.
Decision nodes
inter
and command
neurons are automatically calculated using:
Python | |
---|---|
1 2 |
|
Uses an Ordinary Neural Circuit (ONC) architecture without Liquid dynamics. Paper references:
- Reinforcement Learning with Ordinary Neural Circuits
- Mish: A Self Regularized Non-Monotonic Activation Function
Source code in velora/models/lnn/ncp.py
Python | |
---|---|
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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
|
active_params
property
¶
Gets the network's active parameter count.
Returns:
Name | Type | Description |
---|---|---|
count |
int
|
the active parameter count. |
total_params
property
¶
Gets the network's total parameter count.
Returns:
Name | Type | Description |
---|---|---|
count |
int
|
the total parameter count. |
__init__(in_features, n_neurons, out_features, *, sparsity_level=0.5, init_type='kaiming_uniform', device=None)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_features
|
int
|
number of inputs (sensory nodes) |
required |
n_neurons
|
int
|
number of decision nodes (inter and command nodes) |
required |
out_features
|
int
|
number of out features (motor nodes) |
required |
sparsity_level
|
float
|
controls the connection sparsity between neurons. Must be a value between
|
0.5
|
init_type
|
str
|
the type of weight initialization |
'kaiming_uniform'
|
device
|
torch.device
|
the device to load tensors on |
None
|
Source code in velora/models/lnn/ncp.py
Python | |
---|---|
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 |
|
forward(x)
¶
Performs a forward pass through the network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
torch.Tensor
|
an input tensor of shape:
|
required |
Returns:
Name | Type | Description |
---|---|---|
y_pred |
torch.Tensor
|
the network prediction. When |
Source code in velora/models/lnn/ncp.py
Python | |
---|---|
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
|
SparseLinear
¶
A torch.nn.Linear
layer with sparsely weighted connections.
Source code in velora/models/lnn/sparse.py
Python | |
---|---|
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 |
|
__init__(in_features, out_features, mask, *, init_type='kaiming_uniform', bias=True, device=None)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_features
|
int
|
number of input features |
required |
out_features
|
int
|
number of output features |
required |
mask
|
torch.Tensor
|
sparsity mask tensor of shape
|
required |
init_type
|
str
|
the type of weight initialization |
'kaiming_uniform'
|
bias
|
bool
|
a flag to enable additive bias |
True
|
device
|
torch.device
|
device to perform computations on |
None
|
Source code in velora/models/lnn/sparse.py
Python | |
---|---|
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 |
|
extra_repr()
¶
String representation of layer parameters.
Source code in velora/models/lnn/sparse.py
Python | |
---|---|
83 84 85 |
|
forward(x)
¶
Perform a forward pass through the layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
torch.Tensor
|
input tensor with shape |
required |
Returns:
Name | Type | Description |
---|---|---|
y_pred |
torch.Tensor
|
layer prediction with sparsity applied with shape |
Source code in velora/models/lnn/sparse.py
Python | |
---|---|
71 72 73 74 75 76 77 78 79 80 81 |
|
reset_parameters(style)
¶
Initializes weights and biases using an initialization method.
Source code in velora/models/lnn/sparse.py
Python | |
---|---|
55 56 57 58 59 60 |
|
update_mask(mask)
¶
Updates the sparsity mask with a new one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mask
|
torch.Tensor
|
new mask |
required |
Source code in velora/models/lnn/sparse.py
Python | |
---|---|
62 63 64 65 66 67 68 69 |
|