Apply a linear alphabet map to every tensor axis of one level.
Parameters:
| Name |
Type |
Description |
Default |
level
|
Tensor
|
Tensor with shape (*shared, in_width, ..., in_width).
|
required
|
matrix
|
Tensor
|
Linear map with shape (in_width, out_width). If v is a row
vector of coordinates, the mapped vector is v @ matrix.
|
required
|
shared_dims
|
int
|
Number of leading axes in level that are not tensor-coordinate axes.
|
required
|
Returns:
| Type |
Description |
Tensor
|
Tensor with shape (*shared, out_width, ..., out_width).
|
Source code in src/tensorsig/tensor_ops/linear.py
| def apply_linear_map_to_tensor_level(
level: Tensor,
matrix: Tensor,
shared_dims: int,
) -> Tensor:
"""Apply a linear alphabet map to every tensor axis of one level.
Parameters
----------
level : Tensor
Tensor with shape ``(*shared, in_width, ..., in_width)``.
matrix : Tensor
Linear map with shape ``(in_width, out_width)``. If ``v`` is a row
vector of coordinates, the mapped vector is ``v @ matrix``.
shared_dims : int
Number of leading axes in ``level`` that are not tensor-coordinate axes.
Returns
-------
Tensor
Tensor with shape ``(*shared, out_width, ..., out_width)``.
"""
if isinstance(shared_dims, bool) or not isinstance(shared_dims, int):
raise TypeError("shared_dims must be an integer.")
if shared_dims < 0:
raise ValueError("shared_dims must be non-negative.")
if level.ndim <= shared_dims:
raise ValueError("level must contain at least one tensor-coordinate axis.")
if matrix.ndim != 2:
raise ValueError("matrix must have shape (in_width, out_width).")
if matrix.dtype != level.dtype:
raise ValueError("matrix and level must have the same dtype.")
if matrix.device != level.device:
raise ValueError("matrix and level must be on the same device.")
degree = level.ndim - shared_dims
in_width = matrix.shape[0]
for axis_size in level.shape[shared_dims:]:
if axis_size != in_width:
raise ValueError(
f"Expected tensor-coordinate axis size {in_width}, got {axis_size}."
)
out = level
for axis in range(shared_dims, shared_dims + degree):
out = torch.tensordot(out, matrix, dims=([axis], [0]))
out = out.movedim(-1, axis)
return out
|