matrix
Compute the nth power of a square matrix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mx
|
Tensor
|
The input matrix, must be square (2D tensor). |
required |
n
|
int
|
The exponent to raise the matrix to. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The matrix raised to the nth power. |
Notes
- If n = 0, the identity matrix of the same size as
mx
is returned. - If n = 1, the input matrix
mx
is returned. - For n > 1, recursive computation is used.
Raises:
Type | Description |
---|---|
AssertionError
|
If the input matrix is not 2D. |
Source code in tinybig/koala/linear_algebra/matrix.py
Compute the sum of matrix powers from 1 to n.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mx
|
Tensor
|
The input matrix, must be square (2D tensor). |
required |
n
|
int
|
The highest power to sum up to. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The accumulative matrix power, i.e., mx + mx^2 + ... + mx^n. |
Raises:
Type | Description |
---|---|
AssertionError
|
If the input matrix is not 2D. |
Source code in tinybig/koala/linear_algebra/matrix.py
Normalize a matrix based on its row or column sums.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mx
|
Tensor
|
The input matrix, can be dense or sparse. |
required |
mode
|
str
|
The normalization mode: - "row": Normalize each row by its sum. - "column": Normalize each column by its sum. - "row_column": Normalize both rows and columns by their respective sums. |
'row'
|
Returns:
Type | Description |
---|---|
Tensor
|
The degree-normalized matrix. |
Raises:
Type | Description |
---|---|
ValueError
|
If an invalid mode is specified. |
AssertionError
|
If the input matrix is not 2D. |
Source code in tinybig/koala/linear_algebra/matrix.py
Normalize a matrix using a custom operator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mx
|
Tensor
|
The input matrix, can be dense or sparse. |
required |
mask_zeros
|
bool
|
Whether to mask zero elements before normalization. |
False
|
rescale_factor
|
float
|
A scaling factor to adjust the matrix values. |
1.0
|
operator
|
callable
|
A function applied for normalization (e.g., |
softmax
|
mode
|
str
|
The normalization mode: - "row": Apply the operator row-wise. - "column": Apply the operator column-wise. - "row_column": Apply the operator first row-wise, then column-wise. |
'row'
|
Returns:
Type | Description |
---|---|
Tensor
|
The operator-normalized matrix. |
Raises:
Type | Description |
---|---|
ValueError
|
If an invalid mode is specified. |
AssertionError
|
If the input matrix is not 2D. |
Source code in tinybig/koala/linear_algebra/matrix.py
Normalize a matrix based on its mean and standard deviation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mx
|
Tensor
|
The input matrix. |
required |
mode
|
str
|
The normalization mode: - "row": Normalize each row. - "column": Normalize each column. - "row_column" or "all": Normalize based on global mean and standard deviation. |
'column'
|
Returns:
Type | Description |
---|---|
Tensor
|
The normalized matrix. |
Raises:
Type | Description |
---|---|
ValueError
|
If an invalid mode is specified. |
Source code in tinybig/koala/linear_algebra/matrix.py
Convert a scipy sparse matrix to a PyTorch sparse tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sparse_mx
|
scipy.sparse matrix
|
The input sparse matrix in scipy format. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
The converted PyTorch sparse tensor. |