interdependence
Bases: Module
, function
A base class for interdependence computations, extending Module
and function
.
This class defines the structure for interdependence calculations, including attribute and instance interdependencies. It supports preprocessing and postprocessing of data, as well as customizable interdependence configurations.
Notes
Formally, given an input data batch \(\mathbf{X} \in {R}^{b \times m}\) (with \(b\) instances and each instance with \(m\) attributes), the attribute and instance data interdependence functions are defined as:
\[ \begin{equation} \xi_a: {R}^{b \times m} \to {R}^{m \times m'} \text{, and } \xi_i: {R}^{b \times m} \to {R}^{b \times b'}, \end{equation} \]
where \(m'\) and \(b'\) denote the output dimensions of their respective interdependence functions, respectively.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
b
|
int
|
The number of rows (e.g., instances) in the data. |
required |
m
|
int
|
The number of columns (e.g., attributes) in the data. |
required |
name
|
str
|
The name of the interdependence operation, by default 'base_interdependency'. |
'base_interdependency'
|
interdependence_type
|
str
|
The type of interdependence, e.g., 'attribute' or 'instance', by default 'attribute'. |
'attribute'
|
require_data
|
bool
|
Whether the operation requires input data, by default True. |
True
|
require_parameters
|
bool
|
Whether the operation requires trainable parameters, by default False. |
False
|
preprocess_functions
|
list | tuple | callable
|
Functions to preprocess the input tensors, by default None. |
None
|
postprocess_functions
|
list | tuple | callable
|
Functions to postprocess the output tensors, by default None. |
None
|
preprocess_function_configs
|
dict
|
Configuration for instantiating the preprocess functions, by default None. |
None
|
postprocess_function_configs
|
dict
|
Configuration for instantiating the postprocess functions, by default None. |
None
|
device
|
str
|
The device for computations, by default 'cpu'. |
'cpu'
|
*args
|
tuple
|
Additional positional arguments. |
()
|
**kwargs
|
dict
|
Additional keyword arguments. |
{}
|
Source code in tinybig/module/base_interdependence.py
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 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
|
interdependence_type
property
writable
Retrieves the current interdependence type.
Returns:
Type | Description |
---|---|
str
|
The interdependence type. |
__init__(b, m, name='base_interdependency', interdependence_type='attribute', require_data=True, require_parameters=False, preprocess_functions=None, postprocess_functions=None, preprocess_function_configs=None, postprocess_function_configs=None, device='cpu', *args, **kwargs)
Initializes an instance of the interdependence
class.
The interdependence
class is designed to model and process relationships between data features
or instances (rows or columns) using a specific type of interdependence. The class supports
preprocessing and postprocessing functions, and allows for customizable configurations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
b
|
int
|
The number of rows (instances) in the input data. |
required |
m
|
int
|
The number of columns (attributes) in the input data. |
required |
name
|
str
|
A name for the interdependence instance, by default 'base_interdependency'. |
'base_interdependency'
|
interdependence_type
|
str
|
The type of interdependence. Valid values include: - 'row', 'left', 'instance', 'instance_interdependence' for instance-based operations. - 'column', 'right', 'attribute', 'attribute_interdependence' for attribute-based operations. By default, 'attribute'. |
'attribute'
|
require_data
|
bool
|
Specifies whether the |
True
|
require_parameters
|
bool
|
Specifies whether the |
False
|
preprocess_functions
|
list[Callable] | tuple[Callable]
|
A list or tuple of preprocessing functions to apply before the primary operation, by default None. |
None
|
postprocess_functions
|
list[Callable] | tuple[Callable]
|
A list or tuple of postprocessing functions to apply after the primary operation, by default None. |
None
|
preprocess_function_configs
|
dict
|
Configuration dictionary for instantiating preprocessing functions, by default None. |
None
|
postprocess_function_configs
|
dict
|
Configuration dictionary for instantiating postprocessing functions, by default None. |
None
|
device
|
str
|
The device to use for computations (e.g., 'cpu', 'cuda'), by default 'cpu'. |
'cpu'
|
*args
|
Additional positional arguments. |
()
|
|
**kwargs
|
Additional keyword arguments. |
{}
|
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
The name of the interdependence instance. |
interdependence_type |
str
|
The specified type of interdependence. |
b |
int
|
The number of rows (instances) in the input data. |
m |
int
|
The number of columns (attributes) in the input data. |
require_data |
bool
|
Whether the |
require_parameters |
bool
|
Whether the |
preprocess_functions |
list[Callable]
|
Instantiated preprocessing functions. |
postprocess_functions |
list[Callable]
|
Instantiated postprocessing functions. |
A |
Tensor or None
|
The interdependence matrix |
device |
str
|
The computation device. |
Raises:
Type | Description |
---|---|
ValueError
|
If the specified |
Source code in tinybig/module/base_interdependence.py
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 |
|
calculate_A(x=None, w=None, device='cpu', *args, **kwargs)
abstractmethod
Abstract method to calculate the interdependence matrix A
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input data tensor, by default None. |
None
|
w
|
Parameter
|
The trainable parameter matrix, by default None. |
None
|
device
|
str
|
The computational device, by default 'cpu'. |
'cpu'
|
Raises:
Type | Description |
---|---|
NotImplementedError
|
This method must be implemented in subclasses. |
Source code in tinybig/module/base_interdependence.py
calculate_b_prime(b=None)
Calculates the transformed batch b
dimension based on the interdependence type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
b
|
int
|
The original batch |
None
|
Returns:
Type | Description |
---|---|
int
|
The transformed batch |
Warnings
UserWarning If the interdependence type does not involve instances.
Source code in tinybig/module/base_interdependence.py
calculate_l()
Placeholder for calculating the learnable parameter number l
.
Returns:
Type | Description |
---|---|
int
|
The learnable parameter number |
calculate_m_prime(m=None)
Calculates the transformed attribute m
dimension based on the interdependence type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
m
|
int
|
The original attribute |
None
|
Returns:
Type | Description |
---|---|
int
|
The transformed attribute |
Warnings
UserWarning If the interdependence type does not involve attributes.
Source code in tinybig/module/base_interdependence.py
check_A_shape_validity(A)
Checks whether the shape of the interdependence matrix A
is valid.
The validity of the shape is determined by the current interdependence_type
:
- For row-based types, A
should have shape (b, b')
.
- For column-based types, A
should have shape (m, m')
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
A
|
Tensor
|
The interdependence matrix to validate. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If |
AssertionError
|
If the provided matrix |
Source code in tinybig/module/base_interdependence.py
forward(x=None, w=None, kappa_x=None, device='cpu', *args, **kwargs)
Executes the forward pass for the interdependence operation.
Depending on the interdependence_type
, the method calculates the transformation
of the input tensor x
or kappa_x
using the interdependence matrix A
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input data tensor, by default None. |
None
|
w
|
Parameter
|
Trainable parameters for the interdependence calculation, by default None. |
None
|
kappa_x
|
Tensor
|
Alternative input tensor for processing, by default None. |
None
|
device
|
str
|
The device for computations, by default 'cpu'. |
'cpu'
|
Returns:
Type | Description |
---|---|
Tensor
|
The transformed data tensor. |
Raises:
Type | Description |
---|---|
AssertionError
|
If |
ValueError
|
If the |
Notes
- For instance-based interdependence types, the operation transforms rows.
- For attribute-based interdependence types, the operation transforms columns.
Source code in tinybig/module/base_interdependence.py
get_A()
Retrieves the current interdependence matrix A
.
Returns:
Type | Description |
---|---|
Tensor or None
|
The current interdependence matrix |
Warnings
UserWarning
If A
is not set, a warning is issued.
Source code in tinybig/module/base_interdependence.py
get_b()
Retrieves the batch number (b
).
Returns:
Type | Description |
---|---|
int
|
The batch size (i.e., the number of rows). |
get_m()
Retrieves the attribute number (m
).
Returns:
Type | Description |
---|---|
int
|
The number of attributes (i.e., columns). |
post_process(x, device='cpu', *args, **kwargs)
Applies the postprocessing functions to the output tensor.
This method utilizes the functions specified in postprocess_functions
to
transform the output tensor after the primary computation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The output tensor to postprocess. |
required |
device
|
str
|
The device to execute postprocessing on, by default 'cpu'. |
'cpu'
|
Returns:
Type | Description |
---|---|
Tensor
|
The postprocessed tensor. |
Notes
- Postprocessing functions are instantiated during class initialization.
- The exact transformations depend on the specified functions.
Source code in tinybig/module/base_interdependence.py
pre_process(x, device='cpu', *args, **kwargs)
Applies the preprocessing functions to the input tensor.
This method utilizes the functions specified in preprocess_functions
to
transform the input tensor before further processing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
The input tensor to preprocess. |
required |
device
|
str
|
The device to execute preprocessing on, by default 'cpu'. |
'cpu'
|
Returns:
Type | Description |
---|---|
Tensor
|
The preprocessed tensor. |
Notes
- Preprocessing functions are instantiated during class initialization.
- The exact transformations depend on the specified functions.
Source code in tinybig/module/base_interdependence.py
to_config()
Serializes the interdependence instance into a configuration dictionary.
Returns:
Type | Description |
---|---|
dict
|
A dictionary containing the class name, parameters, and configurations. |