3. Neural Network
This module contains the code for the bayesian Conv1d.
3.1
Conv1d(input_channels, output_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, weights_distribution=None, bias_distribution=None, use_bias=True, rngs=nnx.Rngs(0), **kwargs)
This class is the bayesian implementation of the Conv1d class.
Definition of a Bayesian Convolution 1D layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_channels
|
int
|
Number of input feature channels. |
required |
output_channels
|
int
|
Number of output feature channels. |
required |
kernel_size
|
int
|
Size of the convolutional kernel. |
required |
stride
|
int
|
Stride of the convolution operation. |
1
|
padding
|
int
|
Amount of zero-padding added to both sides. |
0
|
dilation
|
int
|
Spacing between kernel elements. |
1
|
groups
|
int
|
Number of blocked connections between input and output. |
1
|
weights_distribution
|
Optional[GaussianDistribution]
|
Distribution to initialize weights. |
None
|
bias_distribution
|
Optional[GaussianDistribution]
|
Distribution to initialize bias. |
None
|
use_bias
|
bool
|
Whether to include a bias term. |
True
|
rngs
|
Rngs
|
Random number generators for reproducibility. |
Rngs(0)
|
Returns:
Type | Description |
---|---|
None
|
None. |
Source code in illia/nn/jax/conv1d.py
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 |
|
3.1.1
__call__(inputs)
Applies the convolution operation to the inputs using current weights and bias. If the model is not frozen, samples new weights and bias before computation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
Array
|
Input array to be convolved. |
required |
Returns:
Type | Description |
---|---|
Array
|
Output array after applying convolution and bias. |
Source code in illia/nn/jax/conv1d.py
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 |
|
3.1.2
freeze()
Freezes the current module and all submodules that are instances of BayesianModule. Sets the frozen state to True.
Returns:
Type | Description |
---|---|
None
|
None. |
Source code in illia/nn/jax/conv1d.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
3.1.3
kl_cost()
Computes the Kullback-Leibler (KL) divergence cost for the layer's weights and bias.
Returns:
Type | Description |
---|---|
Array
|
Tuple containing KL divergence cost and total number of |
int
|
parameters. |
Source code in illia/nn/jax/conv1d.py
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 |
|
This module contains the code for the bayesian Conv2d.
3.2
Conv2d(input_channels, output_channels, kernel_size, stride=(1, 1), padding=(0, 0), dilation=(1, 1), groups=1, weights_distribution=None, bias_distribution=None, use_bias=True, rngs=nnx.Rngs(0), **kwargs)
This class is the bayesian implementation of the Conv2d class.
Definition of a Bayesian Convolution 2D layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_channels
|
int
|
Number of input feature channels. |
required |
output_channels
|
int
|
Number of output feature channels. |
required |
kernel_size
|
int | tuple[int, int]
|
Size of the convolutional kernel. |
required |
stride
|
tuple[int, int]
|
Stride of the convolution operation. |
(1, 1)
|
padding
|
tuple[int, int]
|
Tuple for zero-padding on both sides. |
(0, 0)
|
dilation
|
tuple[int, int]
|
Spacing between kernel elements. |
(1, 1)
|
groups
|
int
|
Number of blocked connections between input and output. |
1
|
weights_distribution
|
Optional[GaussianDistribution]
|
Distribution to initialize weights. |
None
|
bias_distribution
|
Optional[GaussianDistribution]
|
Distribution to initialize bias. |
None
|
use_bias
|
bool
|
Whether to include a bias term. |
True
|
rngs
|
Rngs
|
Random number generators for reproducibility. |
Rngs(0)
|
Returns:
Type | Description |
---|---|
None
|
None. |
Source code in illia/nn/jax/conv2d.py
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 |
|
3.2.1
__call__(inputs)
Applies the convolution operation to the inputs using current weights and bias. If the model is not frozen, samples new weights and bias before computation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
Array
|
Input array to be convolved. |
required |
Returns:
Type | Description |
---|---|
Array
|
Output array after applying convolution and bias. |
Source code in illia/nn/jax/conv2d.py
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 |
|
3.2.2
freeze()
Freezes the current module and all submodules that are instances of BayesianModule. Sets the frozen state to True.
Returns:
Type | Description |
---|---|
None
|
None. |
Source code in illia/nn/jax/conv2d.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
|
3.2.3
kl_cost()
Computes the Kullback-Leibler (KL) divergence cost for the layer's weights and bias.
Returns:
Type | Description |
---|---|
Array
|
Tuple containing KL divergence cost and total number of |
int
|
parameters. |
Source code in illia/nn/jax/conv2d.py
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 |
|
This module contains the code for bayesian Embedding layer.
3.3
Embedding(num_embeddings, embeddings_dim, padding_idx=None, max_norm=None, norm_type=2.0, scale_grad_by_freq=False, weights_distribution=None, rngs=nnx.Rngs(0), **kwargs)
This class is the bayesian implementation of the Embedding class.
This method is the constructor of the embedding class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_embeddings
|
int
|
size of the dictionary of embeddings. |
required |
embeddings_dim
|
int
|
the size of each embedding vector. |
required |
padding_idx
|
Optional[int]
|
If specified, the entries at padding_idx do not contribute to the gradient. |
None
|
max_norm
|
Optional[float]
|
If given, each embedding vector with norm larger than max_norm is renormalized to have norm max_norm. |
None
|
norm_type
|
float
|
The p of the p-norm to compute for the max_norm option. |
2.0
|
scale_grad_by_freq
|
bool
|
If given, this will scale gradients by the inverse of frequency of the words in the mini-batch. |
False
|
weights_distribution
|
Optional[GaussianDistribution]
|
distribution for the weights of the layer. |
None
|
rngs
|
Rngs
|
Random number generators for reproducibility. |
Rngs(0)
|
Returns:
Type | Description |
---|---|
None
|
None. |
Source code in illia/nn/jax/embedding.py
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 |
|
3.3.1
__call__(inputs)
This method is the forward pass of the layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
Array
|
input tensor. Dimensions: [*]. |
required |
Returns:
Type | Description |
---|---|
Array
|
outputs tensor. Dimension: [*, embedding dim]. |
Source code in illia/nn/jax/embedding.py
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 |
|
3.3.2
freeze()
Freezes the current module and all submodules that are instances of BayesianModule. Sets the frozen state to True.
Returns:
Type | Description |
---|---|
None
|
None. |
Source code in illia/nn/jax/embedding.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
3.3.3
kl_cost()
Computes the Kullback-Leibler (KL) divergence cost for the layer's weights and bias.
Returns:
Type | Description |
---|---|
Array
|
Tuple containing KL divergence cost and total number of |
int
|
parameters. |
Source code in illia/nn/jax/embedding.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
|
This module contains the code for Linear Bayesian layer.
3.4
Linear(input_size, output_size, weights_distribution=None, bias_distribution=None, use_bias=True, precision=None, dot_general=lax.dot_general, rngs=nnx.Rngs(0), **kwargs)
This class is the bayesian implementation of the Linear class.
This is the constructor of the Linear class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_size
|
int
|
Size of the input features. |
required |
output_size
|
int
|
Size of the output features. |
required |
weights_distribution
|
Optional[GaussianDistribution]
|
Prior distribution of the weights. |
None
|
bias_distribution
|
Optional[GaussianDistribution]
|
Prior distribution of the bias. |
None
|
use_bias
|
bool
|
Whether to include a bias term in the layer. |
True
|
precision
|
PrecisionLike
|
Precision used in dot product operations. |
None
|
dot_general
|
DotGeneralT
|
Function for computing generalized dot products. |
dot_general
|
Returns:
Type | Description |
---|---|
None
|
None. |
Source code in illia/nn/jax/linear.py
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 |
|
3.4.1
__call__(inputs)
This method is the forward pass of the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
Array
|
Inputs of the model. Dimensions: [*, input size]. |
required |
Returns:
Type | Description |
---|---|
Array
|
Output tensor. Dimension: [*, output size]. |
Source code in illia/nn/jax/linear.py
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 |
|
3.4.2
freeze()
Freezes the current module and all submodules that are instances of BayesianModule. Sets the frozen state to True.
Returns:
Type | Description |
---|---|
None
|
None. |
Source code in illia/nn/jax/linear.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 |
|
3.4.3
kl_cost()
Computes the Kullback-Leibler (KL) divergence cost for the layer's weights and bias.
Returns:
Type | Description |
---|---|
Array
|
Tuple containing KL divergence cost and total number of |
int
|
parameters. |
Source code in illia/nn/jax/linear.py
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 |
|
This module contains the code for the bayesian LSTM.
3.5
LSTM(num_embeddings, embeddings_dim, hidden_size, output_size, padding_idx=None, max_norm=None, norm_type=2.0, scale_grad_by_freq=False, rngs=nnx.Rngs(0), **kwargs)
This class is the bayesian implementation of the TensorFlow LSTM layer.
summary
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_embeddings
|
int
|
description |
required |
embeddings_dim
|
int
|
description |
required |
hidden_size
|
int
|
description |
required |
output_size
|
int
|
description |
required |
padding_idx
|
Optional[int]
|
description. Defaults to None. |
None
|
max_norm
|
Optional[float]
|
description. Defaults to None. |
None
|
norm_type
|
float
|
description. Defaults to 2.0. |
2.0
|
scale_grad_by_freq
|
bool
|
description. Defaults to False. |
False
|
rngs
|
Rngs
|
description. Defaults to nnx.Rngs(0). |
Rngs(0)
|
Returns:
Type | Description |
---|---|
None
|
None. |
Source code in illia/nn/jax/lstm.py
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 |
|
3.5.1
__call__(inputs, init_states=None)
Performs a forward pass through the Bayesian LSTM layer. If the layer is not frozen, it samples weights and bias from their respective distributions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
Array
|
Input tensor with token indices. Shape: [batch, seq_len, 1] |
required |
init_states
|
Optional[tuple[Array, Array]]
|
Optional initial hidden and cell states |
None
|
Returns:
Type | Description |
---|---|
tuple[Array, tuple[Array, Array]]
|
Tuple of (output, (hidden_state, cell_state)) |
Source code in illia/nn/jax/lstm.py
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 |
|
3.5.2
freeze()
Freezes the current module and all submodules that are instances of BayesianModule. Sets the frozen state to True.
Returns:
Type | Description |
---|---|
None
|
None. |
Source code in illia/nn/jax/lstm.py
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 |
|
3.5.3
kl_cost()
Computes the Kullback-Leibler (KL) divergence cost for the layer's weights and bias.
Returns:
Type | Description |
---|---|
Array
|
tuple containing KL divergence cost and total number of |
int
|
parameters. |
Source code in illia/nn/jax/lstm.py
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 |
|