Skip to content

tensor(*spec, *, initializer=None, optimizable=None, prefer=None) ΒΆ

Construct an abstract tensor using spec.

Parameters:

Name Type Description Default
spec multiple

Formats supported:

  • symbol, size...: a alphanumeric symbol followed by the size for each dimension.
  • size...: size of each dimension.
  • symbol, tensor: a alphanumeric symbol followed by a concrete tensor such as np.eye(3) or rand(10, 7).
  • tensor: a concrete tensor.
()
initializer callable

Initialization distribution

None
optimizable boolean

True/False flag indicating if a tensor leaf should be optimized. The default behavior is dependent on the input for spec:

  • if a size for each dimension is provided in spec, optimizable is True by default
  • if a concrete tensor is provided in spec, optimizable is False by default

The default behavior can be overriden by user input.

None
prefer callable

Condition evaluated on tensor as a penalty term. Only considered if optimizable is set to True or defaults to True, otherwise it is ignored.

None

Returns:

Type Description
TsrEx

A tensor expression representing an abstract tensor object.

Source code in funfact/lang/_tsrex.py
def tensor(*spec, initializer=None, optimizable=None, prefer=None):
    '''Construct an abstract tensor using `spec`.

    Args:
        spec (multiple):
            Formats supported:

            * `symbol, size...`: a alphanumeric symbol followed by the size for
            each dimension.
            * `size...`: size of each dimension.
            * `symbol, tensor`: a alphanumeric symbol followed by a concrete
            tensor such as ``np.eye(3)`` or ``rand(10, 7)``.
            * `tensor`: a concrete tensor.

        initializer (callable):
            Initialization distribution

        optimizable (boolean):
            True/False flag indicating if a tensor leaf should be optimized.
            The default behavior is dependent on the input for `spec`:

            * if a size for each dimension is provided in `spec`, optimizable
            is True by default
            * if a concrete tensor is provided in `spec`, optimizable is False
            by default

            The default behavior can be overriden by user input.

        prefer (callable):
            Condition evaluated on tensor as a penalty term. Only considered if
            optimizable is set to True or defaults to True, otherwise it is
            ignored.

    Returns:
        TsrEx: A tensor expression representing an abstract tensor object.
    '''
    if len(spec) == 2 and isinstance(spec[0], str) and ab.is_tensor(spec[1]):
        # name + concrete tensor
        symbol = spec[0]
        initializer = spec[1]
        size = initializer.shape
        if optimizable is None:
            optimizable = False
    elif len(spec) == 1 and ab.is_tensor(spec[0]):
        # concrete tensor only
        symbol = None
        initializer = spec[0]
        size = initializer.shape
        if optimizable is None:
            optimizable = False
    elif len(spec) >= 1 and isinstance(spec[0], str):
        # name + size
        symbol, *size = spec
        if optimizable is None:
            optimizable = True
    else:
        # size only
        symbol = None
        size = spec
        if optimizable is None:
            optimizable = True

    for d in size:
        if not (isinstance(d, int) and d > 0):
            raise RuntimeError(
                f'Tensor size must be positive integer, got {d} instead.'
            )
    if optimizable and prefer is None:
        prefer = NoCondition()

    return TsrEx(
        P.tensor(
            AbstractTensor(
                *size, symbol=symbol, initializer=initializer,
                optimizable=optimizable, prefer=prefer
            )
        )
    )
Back to top