Neural Network Utils
Neural Network Utils
cax.nn.pool
Pool module.
Pool
Bases: PyTreeNode
A container for PyTree arrays supporting in-place updates and random sampling.
The pool holds a PyTree of arrays whose first dimension is the pool size. It can be created from a PyTree with leading batch dimension. Sampling returns indices and the sliced batch for the same indices across all leaves.
Attributes:
| Name | Type | Description |
|---|---|---|
size |
int
|
Number of items in the pool (inferred from the leading dimension of the data). |
data |
PyTree
|
PyTree of arrays stacked along the leading dimension. |
Source code in src/cax/nn/pool.py
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 | |
create(data)
classmethod
Create a new Pool instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
PyTree
|
PyTree whose leaves are arrays with shape |
required |
Returns:
| Type | Description |
|---|---|
Pool
|
A new Pool instance with |
Source code in src/cax/nn/pool.py
28 29 30 31 32 33 34 35 36 37 38 39 40 | |
update(idxs, batch)
Update batch in the pool at the specified indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idxs
|
Array
|
Integer indices with shape |
required |
batch
|
PyTree
|
PyTree matching |
required |
Returns:
| Type | Description |
|---|---|
Pool
|
A new Pool instance with the updated batch applied at |
Source code in src/cax/nn/pool.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
sample(key, *, batch_size)
Sample a batch from the pool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array
|
JAX PRNG key. |
required |
batch_size
|
int
|
Number of rows to sample. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
A tuple |
PyTree
|
with each leaf shaped |
Source code in src/cax/nn/pool.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
cax.nn.buffer
Buffer module.
Buffer
Bases: PyTreeNode
A container for PyTree arrays with circular writes and random sampling.
The buffer stores a PyTree of arrays with a fixed capacity along the leading dimension. New batches are written sequentially with wrap-around semantics. Sampling draws indices from the subset of entries that have been written at least once.
Attributes:
| Name | Type | Description |
|---|---|---|
size |
int
|
Maximum number of items stored. |
data |
PyTree
|
PyTree of arrays with leading dimension |
is_full |
Array
|
Boolean mask of shape |
idx |
Array
|
Current write pointer (modulo |
Source code in src/cax/nn/buffer.py
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 | |
create(size, datum)
classmethod
Create a new Buffer instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int
|
Size of the buffer. |
required |
datum
|
PyTree
|
PyTree example whose leaf dtypes/shapes are used to allocate storage. |
required |
Returns:
| Type | Description |
|---|---|
Buffer
|
A new Buffer instance with empty storage of capacity |
Source code in src/cax/nn/buffer.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
add(batch)
Add a batch to the buffer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
PyTree
|
PyTree whose leaves have shape |
required |
Returns:
| Type | Description |
|---|---|
Buffer
|
A new Buffer instance with the batch written at consecutive indices (with wrap-around). |
Source code in src/cax/nn/buffer.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
sample(key, *, batch_size)
Sample a batch from the buffer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Array
|
JAX PRNG key. |
required |
batch_size
|
int
|
Number of rows to sample from initialized entries. |
required |
Returns:
| Type | Description |
|---|---|
PyTree
|
A PyTree with each leaf shaped |
Source code in src/cax/nn/buffer.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
cax.nn.vae
Variational Autoencoder module.
Encoder
Bases: Module
Encoder module for the VAE.
Applies a stack of strided convolutions followed by linear layers to produce mean and log-variance parameters of a diagonal Gaussian in latent space.
Source code in src/cax/nn/vae.py
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 | |
__init__(spatial_dims, features, latent_size, *, rngs)
Initialize the Encoder module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spatial_dims
|
Sequence[int]
|
Spatial dimensions of the input. |
required |
features
|
Sequence[int]
|
Sequence of feature sizes for convolutional layers. |
required |
latent_size
|
int
|
Size of the latent space. |
required |
rngs
|
Rngs
|
rng key. |
required |
Source code in src/cax/nn/vae.py
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 | |
__call__(x)
Forward pass of the encoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Array
|
Input tensor with shape |
required |
Returns:
| Type | Description |
|---|---|
tuple[Array, Array]
|
Tuple |
Source code in src/cax/nn/vae.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
reparameterize(mean, logvar)
Perform the reparameterization trick.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mean
|
Array
|
Mean of the latent distribution. |
required |
logvar
|
Array
|
Log variance of the latent distribution. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Sampled latent vector with shape matching |
Source code in src/cax/nn/vae.py
81 82 83 84 85 86 87 88 89 90 91 92 | |
Decoder
Bases: Module
Decoder module for the VAE.
Maps latent vectors back to image space using a linear layer followed by transposed convolutions.
Source code in src/cax/nn/vae.py
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 | |
__init__(spatial_dims, features, latent_size, rngs)
Initialize the Decoder module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spatial_dims
|
Sequence[int]
|
Spatial dimensions of the output. |
required |
features
|
Sequence[int]
|
Sequence of feature sizes for transposed convolutional layers. |
required |
latent_size
|
int
|
Size of the latent space. |
required |
rngs
|
Rngs
|
rng key. |
required |
Source code in src/cax/nn/vae.py
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 | |
__call__(z)
Forward pass of the decoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
Array
|
Latent vector with shape |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Reconstructed output tensor with shape |
Source code in src/cax/nn/vae.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
VAE
Bases: Module
Variational Autoencoder module.
Combines an encoder and decoder with a reparameterization sampler for training with the evidence lower bound (ELBO).
Source code in src/cax/nn/vae.py
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 | |
__init__(spatial_dims, features, latent_size, rngs)
Initialize the VAE module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spatial_dims
|
tuple[int, int]
|
Spatial dimensions of the input/output. |
required |
features
|
Sequence[int]
|
Sequence of feature sizes for encoder and decoder. |
required |
latent_size
|
int
|
Size of the latent space. |
required |
rngs
|
Rngs
|
rng key. |
required |
Source code in src/cax/nn/vae.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
encode(x)
Encode input to latent space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Array
|
Input tensor with shape |
required |
Returns:
| Type | Description |
|---|---|
tuple[Array, Array, Array]
|
Tuple |
Source code in src/cax/nn/vae.py
191 192 193 194 195 196 197 198 199 200 201 202 | |
decode(z)
Decode latent vector to output space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
Array
|
Latent vector with shape |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Reconstructed output tensor with shape |
Source code in src/cax/nn/vae.py
204 205 206 207 208 209 210 211 212 213 214 | |
generate(z)
Generate output from latent vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
Array
|
Latent vector with shape |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Generated output tensor with shape |
Source code in src/cax/nn/vae.py
216 217 218 219 220 221 222 223 224 225 226 | |
__call__(x)
Forward pass of the VAE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Array
|
Input tensor with shape |
required |
Returns:
| Type | Description |
|---|---|
tuple[Array, Array, Array]
|
Tuple |
Source code in src/cax/nn/vae.py
228 229 230 231 232 233 234 235 236 237 238 239 240 | |
kl_divergence(mean, logvar)
Compute KL divergence between latent distribution and standard normal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mean
|
Array
|
Mean of the latent distribution. |
required |
logvar
|
Array
|
Log variance of the latent distribution. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Scalar KL divergence value (sum over last dimension). |
Source code in src/cax/nn/vae.py
243 244 245 246 247 248 249 250 251 252 253 254 255 | |
binary_cross_entropy_with_logits(logits, labels)
Compute binary cross-entropy loss with logits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits
|
Array
|
Predicted logits. |
required |
labels
|
Array
|
True labels. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Summed Binary Cross-Entropy loss over the last dimension. |
Source code in src/cax/nn/vae.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 | |
vae_loss(logits, labels, mean, logvar)
Compute VAE loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits
|
Array
|
Predicted logits from the decoder. |
required |
labels
|
Array
|
Target labels (e.g., normalized images or one-hot vectors). |
required |
mean
|
Array
|
Mean of the latent distribution. |
required |
logvar
|
Array
|
Log variance of the latent distribution. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Total VAE loss equal to |
Source code in src/cax/nn/vae.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |