cornucopia.base
Transform
Base class for all transforms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
[list or dict of] str
|
Which tensors to return. Can be a nested structure.
Most transforms accept |
None
|
append
|
bool | str
|
Append the (structure of) returned tensors to the parent structure. Warning This option does not keep the input tensors in the returned
structure! To preserve the input tensors, you should
use Example
If it is a |
False
|
prefix
|
bool | str
|
If If
|
True
|
include
|
[list of] str | re.Pattern
|
List of keys to which the transform should apply. Default: all.
|
None
|
exclude
|
[list of] str | re.Pattern
|
List of keys to which the transform should not apply. Default: none.
|
None
|
consume
|
[list of] str | re.Pattern
|
List of keys to remove from the output after applying the transform. Default: none.
|
None
|
is_final
property
Returns:
| Type | Description |
|---|---|
bool
|
Whether the transform is final (i.e., deterministic) or not. |
get_prm
Get the parameters of the transform, for use in subtransforms.
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary containing the attributes
|
forward
Apply the transform recursively.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*a
|
[nested list or dict of] tensor
|
Input tensors, with shape |
()
|
**k
|
[nested list or dict of] tensor
|
Input tensors, with shape |
()
|
Returns:
| Type | Description |
|---|---|
[nested list or dict of] tensor
|
Output tensors. with shape |
xform
Apply the transform to a tensor.
Non-final transforms do not implement this method in general.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
(C_inp, *spatial_inp) tensor
|
A single input tensor |
required |
args
|
Arguments
|
The original inputs arguments to the transform, in case they are needed. |
NoArguments()
|
Returns:
| Name | Type | Description |
|---|---|---|
y |
Returned | (C_out, *spatial_out) tensor
|
A single output tensor, or a |
final
Generate the final version of the transform.
Some transforms save the output type of this function in their
Final attribute.
Added
final method in v0.5.
Before this, one had to use make_final(x, max_depth=inf).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
tensor
|
A single input tensor, with shape |
required |
args
|
Arguments
|
The original inputs arguments to the transform, in case they are needed. |
NoArguments()
|
Returns:
| Type | Description |
|---|---|
FinalTransform
|
A final version of the transform. |
next
Generate the next version of the transform.
Some transforms save the output type of this function in their
Next attribute.
Added
next method in v0.5.
Before this, one had to use make_final(x, max_depth=1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
tensor
|
A single input tensor, with shape |
required |
args
|
Arguments
|
The original inputs arguments to the transform, in case they are needed. |
NoArguments()
|
Returns:
| Type | Description |
|---|---|
Transform
|
A more specialized version of the transform. |
unroll
Generate the next (i.e., more final) version(s) of the transform.
- To completely finalize a transform,
call
unroll(x, max_depth=inf)orfinal(). - To get the the next version of a transform,
call
unroll(x, max_depth=1)ornext().
Added
unroll method in v0.5.
Before this, it was named make_final.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
tensor
|
A single input tensor, with shape |
required |
max_depth
|
int | {inf}
|
Maximum depth to apply |
inf
|
args
|
Arguments
|
The original inputs arguments to the transform, in case they are needed. |
NoArguments()
|
Returns:
| Type | Description |
|---|---|
Transform
|
A more specialized version of the transform. |
inverse
Apply the inverse transform recursively
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*a
|
[nested list or dict of] tensor
|
Input tensors, with shape |
()
|
**k
|
[nested list or dict of] tensor
|
Input tensors, with shape |
()
|
Returns:
| Type | Description |
|---|---|
[nested list or dict of] tensor
|
Output tensors. with shape |
FinalTransform
FinalTransform(*, returns=None, append=False, prefix=True, include=None, exclude=None, consume=None)
Bases: Transform
Base class for determinstic transforms.
Final transforms must implement the xform method.
NonFinalTransform
Bases: _SharedMixin, Transform
Transforms whose parameters depend on features of the input transform (shape, dtype, etc).
Non-final transforms implement unroll, and do not implement
xform. Their aim is to generate a more-specialized transform
at call time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shared
|
(channels, tensors, channels + tensor, '')
|
|
'channels'
|
SpecialTransform
SpecialTransform(*, returns=None, append=False, prefix=True, include=None, exclude=None, consume=None)
Bases: Transform
Base class for transforms that act on other transforms.
Such transforms cannot be easily classified as "final" or "non-final", because this characeteristic depends on the transforms that they embed.
They all implement unroll, but some may also implement a
"fast-track" xform that is applied in simple cases (e.g., when
the transform is not shared across tensors) for efficiency.
Added
SpecialTransform class in v0.5.
Before this, special transforms inherited directly from Transform.