9. Neural Network Layers
9.1
BayesianModule
Abstract base for Bayesian-aware modules in Tensorflow. Provides mechanisms to track if a module is Bayesian and control parameter updates through freezing/unfreezing.
Notes
All derived classes must implement freeze and kl_cost to
handle parameter management and compute the KL divergence cost.
Source code in illia/nn/tf/base.py
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 | |
9.1.1
__init__(**kwargs)
Initialize the Bayesian module with default flags.
Sets frozen to False and is_bayesian to True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Extra arguments passed to the base class. |
{}
|
Returns:
| Type | Description |
|---|---|
None
|
None. |
Source code in illia/nn/tf/base.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
9.1.2
freeze()
abstractmethod
Freeze the module's parameters to stop gradient computation. If weights or biases are not sampled yet, they are sampled first. Once frozen, parameters are not resampled or updated.
Returns:
| Type | Description |
|---|---|
None
|
None. |
Notes
Must be implemented by all subclasses.
Source code in illia/nn/tf/base.py
39 40 41 42 43 44 45 46 47 48 49 50 51 | |
9.1.3
kl_cost()
abstractmethod
Compute the KL divergence cost for all Bayesian parameters.
Returns:
| Type | Description |
|---|---|
tuple[Tensor, int]
|
tuple[tf.Tensor, int]: A tuple containing the KL divergence cost and the total number of parameters in the layer. |
Notes
Must be implemented by all subclasses.
Source code in illia/nn/tf/base.py
64 65 66 67 68 69 70 71 72 73 74 75 | |
9.1.4
unfreeze()
Unfreeze the module by setting its frozen flag to False.
Allows parameters to be sampled and updated again.
Returns:
| Type | Description |
|---|---|
None
|
None. |
Source code in illia/nn/tf/base.py
53 54 55 56 57 58 59 60 61 62 | |
9.2
Conv1d
Bayesian 1D convolutional layer with optional weight and bias priors. Behaves like a standard Conv1d but treats weights and bias as random variables sampled from specified distributions. Parameters become fixed when the layer is frozen.
Source code in illia/nn/tf/conv1d.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 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 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 329 330 331 332 333 334 335 336 337 338 339 340 | |
9.2.1
__init__(input_channels, output_channels, kernel_size, stride=1, padding='VALID', dilation=1, groups=1, data_format='NWC', weights_distribution=None, bias_distribution=None, use_bias=True, **kwargs)
Initializes a Bayesian 1D convolutional layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_channels
|
int
|
Number of channels in the input. |
required |
output_channels
|
int
|
Number of channels produced by the conv. |
required |
kernel_size
|
int
|
Size of the convolution kernel. |
required |
stride
|
int
|
Stride of the convolution. |
1
|
padding
|
str
|
Padding type, 'VALID' or 'SAME'. |
'VALID'
|
dilation
|
int
|
Spacing between kernel elements. |
1
|
groups
|
int
|
Number of blocked connections between input/output. |
1
|
data_format
|
Optional[str]
|
'NWC' or 'NCW' format for input data. |
'NWC'
|
weights_distribution
|
Optional[GaussianDistribution]
|
Distribution for weights sampling. |
None
|
bias_distribution
|
Optional[GaussianDistribution]
|
Distribution for bias sampling. |
None
|
use_bias
|
bool
|
Whether to include a bias term. |
True
|
**kwargs
|
Any
|
Extra arguments passed to the base class. |
{}
|
Returns:
| Type | Description |
|---|---|
None
|
None. |
Notes
Gaussian distributions are used by default if none are provided.
Source code in illia/nn/tf/conv1d.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 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 | |
9.2.2
call(inputs)
Performs a forward pass through the Bayesian Convolution 1D layer. If the layer is not frozen, it samples weights and bias from their respective distributions. If the layer is frozen and the weights or bias are not initialized, it also performs sampling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
Tensor
|
Input tensor to the layer with shape (batch, length, output_channels) if 'data_format' is 'NWC' or (batch, output_channels, length) if 'data_format' is 'NCW' |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor after convolution with optional bias added. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the layer is frozen but weights or bias are undefined. |
Source code in illia/nn/tf/conv1d.py
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 329 330 331 332 333 334 335 336 337 338 339 340 | |
9.2.3
freeze()
Freeze the module's parameters to stop gradient computation. If weights or biases are not sampled yet, they are sampled first. Once frozen, parameters are not resampled or updated.
Returns:
| Type | Description |
|---|---|
None
|
None. |
Source code in illia/nn/tf/conv1d.py
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 | |
9.2.4
kl_cost()
Compute the KL divergence cost for all Bayesian parameters.
Returns:
| Type | Description |
|---|---|
tuple[Tensor, int]
|
tuple[tf.Tensor, int]: A tuple containing the KL divergence cost and the total number of parameters in the layer. |
Source code in illia/nn/tf/conv1d.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
9.3
Conv2d
Bayesian 2D convolutional layer with optional weight and bias priors. Behaves like a standard Conv2d but treats weights and bias as random variables sampled from specified distributions. Parameters become fixed when the layer is frozen.
Source code in illia/nn/tf/conv2d.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 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 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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
9.3.1
__init__(input_channels, output_channels, kernel_size, stride=1, padding='VALID', dilation=None, groups=1, data_format='NHWC', weights_distribution=None, bias_distribution=None, use_bias=True, **kwargs)
Initializes a Bayesian 2D convolutional layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_channels
|
int
|
Number of channels in the input image. |
required |
output_channels
|
int
|
Number of channels produced by the conv. |
required |
kernel_size
|
int | list[int]
|
Convolution kernel size as int or list. |
required |
stride
|
int | list[int]
|
Convolution stride as int or list. |
1
|
padding
|
str | list[int]
|
Padding type 'VALID', 'SAME', or list of ints. |
'VALID'
|
dilation
|
Optional[int | list[int]]
|
Spacing between kernel elements as int or list. |
None
|
groups
|
int
|
Number of blocked connections between input/output. |
1
|
data_format
|
Optional[str]
|
'NHWC' or 'NCHW' format for input data. |
'NHWC'
|
weights_distribution
|
Optional[GaussianDistribution]
|
Distribution for weights sampling. |
None
|
bias_distribution
|
Optional[GaussianDistribution]
|
Distribution for bias sampling. |
None
|
use_bias
|
bool
|
Whether to include a bias term. |
True
|
**kwargs
|
Any
|
Extra arguments passed to the base class. |
{}
|
Returns:
| Type | Description |
|---|---|
None
|
None. |
Notes
Gaussian distributions are used by default if none are provided.
Source code in illia/nn/tf/conv2d.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 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 | |
9.3.2
call(inputs)
Performs a forward pass through the Bayesian Convolution 2D layer. If the layer is not frozen, it samples weights and bias from their respective distributions. If the layer is frozen and the weights or bias are not initialized, it also performs sampling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
Tensor
|
Input tensor with shape [batch, height, width, channels] if NHWC or [batch, channels, height, width] if NCHW. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor after convolution with optional bias added. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the layer is frozen but weights or bias are undefined. |
Source code in illia/nn/tf/conv2d.py
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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
9.3.3
freeze()
Freeze the module's parameters to stop gradient computation. If weights or biases are not sampled yet, they are sampled first. Once frozen, parameters are not resampled or updated.
Returns:
| Type | Description |
|---|---|
None
|
None. |
Source code in illia/nn/tf/conv2d.py
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 | |
9.3.4
kl_cost()
Compute the KL divergence cost for all Bayesian parameters.
Returns:
| Type | Description |
|---|---|
tuple[Tensor, int]
|
tuple[tf.Tensor, int]: A tuple containing the KL divergence cost and the total number of parameters in the layer. |
Source code in illia/nn/tf/conv2d.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | |
9.4
Embedding
Bayesian embedding layer with optional padding and max-norm. Each embedding vector is sampled from a specified distribution. Can be frozen to fix embeddings and stop gradients.
Source code in illia/nn/tf/embedding.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 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 | |
9.4.1
__init__(num_embeddings, embeddings_dim, padding_idx=None, max_norm=None, norm_type=2.0, scale_grad_by_freq=False, sparse=False, weights_distribution=None, **kwargs)
Initializes a Bayesian Embedding layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_embeddings
|
int
|
Size of the embedding dictionary. |
required |
embeddings_dim
|
int
|
Dimensionality of each embedding vector. |
required |
padding_idx
|
Optional[int]
|
Index to exclude from gradient computation. |
None
|
max_norm
|
Optional[float]
|
Maximum norm for embedding vectors. |
None
|
norm_type
|
float
|
p of the p-norm for max_norm. |
2.0
|
scale_grad_by_freq
|
bool
|
Scale gradient by inverse frequency. |
False
|
sparse
|
bool
|
Use sparse gradient updates. |
False
|
weights_distribution
|
Optional[GaussianDistribution]
|
Distribution for embedding weights. |
None
|
**kwargs
|
Any
|
Extra arguments passed to the base class. |
{}
|
Returns:
| Type | Description |
|---|---|
None
|
None. |
Notes
Gaussian distributions are used by default if none are provided.
Source code in illia/nn/tf/embedding.py
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 | |
9.4.2
call(inputs)
Performs embedding lookup using current weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
Tensor
|
Input tensor of indices with shape [batch, *]. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor of embeddings. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the layer is frozen but weights are undefined. |
Source code in illia/nn/tf/embedding.py
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 | |
9.4.3
freeze()
Freeze the module's parameters to stop gradient computation. If weights or biases are not sampled yet, they are sampled first. Once frozen, parameters are not resampled or updated.
Returns:
| Type | Description |
|---|---|
None
|
None. |
Source code in illia/nn/tf/embedding.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
9.4.4
kl_cost()
Compute the KL divergence cost for all Bayesian parameters.
Returns:
| Type | Description |
|---|---|
tuple[Tensor, int]
|
tuple[tf.Tensor, int]: A tuple containing the KL divergence cost and the total number of parameters in the layer. |
Source code in illia/nn/tf/embedding.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
9.5
Linear
Bayesian linear layer (fully connected) with optional weight and bias distributions. Can be frozen to stop gradient updates and fix parameters.
Source code in illia/nn/tf/linear.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 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 | |
9.5.1
__init__(input_size, output_size, weights_distribution=None, bias_distribution=None, use_bias=True, **kwargs)
Initializes a Bayesian Linear layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_size
|
int
|
Number of input features. |
required |
output_size
|
int
|
Number of output features. |
required |
weights_distribution
|
Optional[GaussianDistribution]
|
Distribution for the weights. |
None
|
bias_distribution
|
Optional[GaussianDistribution]
|
Distribution for the bias. |
None
|
use_bias
|
bool
|
Whether to include a bias term. |
True
|
**kwargs
|
Any
|
Extra arguments passed to the base class. |
{}
|
Returns:
| Type | Description |
|---|---|
None
|
None. |
Notes
Gaussian distributions are used by default if none are provided.
Source code in illia/nn/tf/linear.py
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 | |
9.5.2
call(inputs)
Performs forward pass using current weights and bias.
Samples parameters if layer is not frozen. Raises an error if frozen weights are undefined.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
Tensor
|
Input tensor of shape [batch, features]. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor after linear transformation. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the layer is frozen but weights or bias are undefined. |
Source code in illia/nn/tf/linear.py
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 | |
9.5.3
freeze()
Freeze the module's parameters to stop gradient computation. If weights or biases are not sampled yet, they are sampled first. Once frozen, parameters are not resampled or updated.
Returns:
| Type | Description |
|---|---|
None
|
None. |
Source code in illia/nn/tf/linear.py
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 | |
9.5.4
kl_cost()
Compute the KL divergence cost for all Bayesian parameters.
Returns:
| Type | Description |
|---|---|
tuple[Tensor, int]
|
tuple[tf.Tensor, int]: A tuple containing the KL divergence cost and the total number of parameters in the layer. |
Source code in illia/nn/tf/linear.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
9.6
LSTM
Bayesian LSTM layer with embedding and probabilistic weights. All weights and biases are sampled from Gaussian distributions. Freezing the layer fixes parameters and stops gradient computation.
Source code in illia/nn/tf/lstm.py
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 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 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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | |
9.6.1
__init__(num_embeddings, embeddings_dim, hidden_size, output_size, padding_idx=None, max_norm=None, norm_type=2.0, scale_grad_by_freq=False, sparse=False, **kwargs)
Initializes the Bayesian LSTM layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_embeddings
|
int
|
Size of the embedding dictionary. |
required |
embeddings_dim
|
int
|
Dimensionality of each embedding vector. |
required |
hidden_size
|
int
|
Number of hidden units in the LSTM. |
required |
output_size
|
int
|
Size of the final output. |
required |
padding_idx
|
Optional[int]
|
Index to ignore in embeddings. |
None
|
max_norm
|
Optional[float]
|
Maximum norm for embedding vectors. |
None
|
norm_type
|
float
|
Norm type used for max_norm. |
2.0
|
scale_grad_by_freq
|
bool
|
Scale gradient by inverse frequency. |
False
|
sparse
|
bool
|
Use sparse embedding updates. |
False
|
**kwargs
|
Any
|
Extra arguments passed to the base class. |
{}
|
Returns:
| Type | Description |
|---|---|
None
|
None. |
Notes
Gaussian distributions are used by default if none are provided.
Source code in illia/nn/tf/lstm.py
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 | |
9.6.2
call(inputs, init_states=None)
Performs a forward pass through the Bayesian LSTM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
Tensor
|
Input tensor of token indices. Shape: [batch, seq_len, 1]. |
required |
init_states
|
Optional[tuple[Tensor, Tensor]]
|
Optional tuple of initial (hidden, cell) states. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Tensor, tuple[Tensor, Tensor]]
|
Tuple containing: - Output tensor after final linear transformation. - Tuple of final hidden and cell states. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the layer is frozen but weights are undefined. |
Source code in illia/nn/tf/lstm.py
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | |
9.6.3
freeze()
Freeze the module's parameters to stop gradient computation. If weights or biases are not sampled yet, they are sampled first. Once frozen, parameters are not resampled or updated.
Returns:
| Type | Description |
|---|---|
None
|
None. |
Source code in illia/nn/tf/lstm.py
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 | |
9.6.4
kl_cost()
Compute the KL divergence cost for all Bayesian parameters.
Returns:
| Type | Description |
|---|---|
tuple[Tensor, int]
|
tuple[tf.Tensor, int]: A tuple containing the KL divergence cost and the total number of parameters in the layer. |
Source code in illia/nn/tf/lstm.py
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 | |