conditions
¶
UpperTriangular
¶
Checks an upper triangular condition for a 2-way leaf tensor (matrix).
Usage:
T = funfact.tensor(...,
prefer=funfact.conditions.UpperTriangular(
weight=1.0,
elementwise='mse',
reduction='mean'
)
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
weight |
float |
Hyperparameter that determines weight of triangular condition. |
required |
elementwise |
str |
If 'mse', the mean-squared error is evaluated elementwise. If 'l1', the L1 error is evaluated elementwise. |
required |
reduction |
str |
If 'mean', the mean of the elementwise condition is returned. If 'sum', the sum over the elementwise condition is returned. |
required |
Source code in funfact/conditions.py
class UpperTriangular(_MatrixCondition):
''' Checks an upper triangular condition for a 2-way leaf tensor (matrix).
**Usage:**
```py
T = funfact.tensor(...,
prefer=funfact.conditions.UpperTriangular(
weight=1.0,
elementwise='mse',
reduction='mean'
)
)
```
Args:
weight (float): Hyperparameter that determines weight of triangular
condition.
elementwise (str):
If 'mse', the mean-squared error is evaluated elementwise.
If 'l1', the L1 error is evaluated elementwise.
reduction (str):
If 'mean', the mean of the elementwise condition is returned.
If 'sum', the sum over the elementwise condition is returned.
'''
def _condition(self, data):
return ab.tril(data, -1)
Unitary
¶
Checks a unitary condition for a 2-way leaf tensor (matrix).
Usage:
T = funfact.tensor(...,
prefer=funfact.conditions.Unitary(
weight=1.0,
elementwise='mse',
reduction='mean'
)
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
weight |
float |
Hyperparameter that determines weight of unitary condition. |
required |
elementwise |
str |
If 'mse', the mean-squared error is evaluated elementwise. If 'l1', the L1 error is evaluated elementwise. |
required |
reduction |
str |
If 'mean', the mean of the elementwise condition is returned. If 'sum', the sum over the elementwise condition is returned. |
required |
Source code in funfact/conditions.py
class Unitary(_MatrixCondition):
''' Checks a unitary condition for a 2-way leaf tensor (matrix).
**Usage:**
```py
T = funfact.tensor(...,
prefer=funfact.conditions.Unitary(
weight=1.0,
elementwise='mse',
reduction='mean'
)
)
```
Args:
weight (float): Hyperparameter that determines weight of unitary
condition.
elementwise (str):
If 'mse', the mean-squared error is evaluated elementwise.
If 'l1', the L1 error is evaluated elementwise.
reduction (str):
If 'mean', the mean of the elementwise condition is returned.
If 'sum', the sum over the elementwise condition is returned.
'''
def _condition(self, data):
return ab.subtract(
ab.matmul(data, ab.conj(ab.transpose(data, (1, 0)))),
ab.eye(data.shape[0])
)
Diagonal
¶
Checks a diagonal condition for a 2-way leaf tensor (matrix).
Usage:
T = funfact.tensor(...,
prefer=funfact.conditions.Diagonal(
weight=1.0,
elementwise='mse',
reduction='mean'
)
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
weight |
float |
Hyperparameter that determines weight of diagonal condition. |
required |
elementwise |
str |
If 'mse', the mean-squared error is evaluated elementwise. If 'l1', the L1 error is evaluated elementwise. |
required |
reduction |
str |
If 'mean', the mean of the elementwise condition is returned. If 'sum', the sum over the elementwise condition is returned. |
required |
Source code in funfact/conditions.py
class Diagonal(_MatrixCondition):
''' Checks a diagonal condition for a 2-way leaf tensor (matrix).
**Usage:**
```py
T = funfact.tensor(...,
prefer=funfact.conditions.Diagonal(
weight=1.0,
elementwise='mse',
reduction='mean'
)
)
```
Args:
weight (float): Hyperparameter that determines weight of diagonal
condition.
elementwise (str):
If 'mse', the mean-squared error is evaluated elementwise.
If 'l1', the L1 error is evaluated elementwise.
reduction (str):
If 'mean', the mean of the elementwise condition is returned.
If 'sum', the sum over the elementwise condition is returned.
'''
def _condition(self, data):
return ab.add(ab.triu(data, 1), ab.tril(data, -1))
NonNegative
¶
Checks a non-negative condition for a leaf tensor.
Usage:
T = funfact.tensor(...,
prefer=funfact.conditions.NonNegative(
weight=1.0,
elementwise='mse',
reduction='mean'
)
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
weight |
float |
Hyperparameter that determines weight of nonnegative condition. |
required |
elementwise |
str |
If 'mse', the mean-squared error is evaluated elementwise. If 'l1', the L1 error is evaluated elementwise. |
required |
reduction |
str |
If 'mean', the mean of the elementwise condition is returned. If 'sum', the sum over the elementwise condition is returned. |
required |
Source code in funfact/conditions.py
class NonNegative(_Condition):
''' Checks a non-negative condition for a leaf tensor.
**Usage:**
```py
T = funfact.tensor(...,
prefer=funfact.conditions.NonNegative(
weight=1.0,
elementwise='mse',
reduction='mean'
)
)
```
Args:
weight (float): Hyperparameter that determines weight of
nonnegative condition.
elementwise (str):
If 'mse', the mean-squared error is evaluated elementwise.
If 'l1', the L1 error is evaluated elementwise.
reduction (str):
If 'mean', the mean of the elementwise condition is returned.
If 'sum', the sum over the elementwise condition is returned.
'''
def _condition(self, data):
return ab.relu(-data)
NoCondition
¶
No condition enforced for a leaf tensor.
Usage:
vmap(condition, append=True)
¶
Vectorizes a condtion.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
condition |
callable |
non-vectorized condition. |
required |
append |
bool |
If True, the last index of shape is considered the vectorizing index. If False, the first index of shape tuple is considered the vectorizing index. |
True |
Source code in funfact/conditions.py
def vmap(condition, append: bool = True):
'''Vectorizes a condtion.
Args:
condition (callable): non-vectorized condition.
append (bool):
If True, the last index of shape is considered the vectorizing
index. If False, the first index of shape tuple is considered
the vectorizing index.
'''
def wrapper(data, sum_vec: bool = True):
'''Vectorized condition
Args:
data (tensor): vectorized tensor leaf.
sum_vec (bool):
If True, the conditions are summed over the vectorizing
dimension. If False, the conditions per instance in the
vectorized model are returned.
'''
shape = data.shape
nvec = shape[-1] if append else shape[0]
def _get_instance(i):
return data[..., i] if append else data[i, ...]
conditions = ab.stack([condition(_get_instance(i)) for i in
range(nvec)], 0)
return ab.sum(conditions) if sum_vec else conditions
return wrapper