fn
trace
→Tensortrace(input: Tensor)Return the sum of the diagonal entries of a 2-D tensor.
For a matrix (or its non-square
generalisation, where the shorter dimension determines the diagonal
length), trace reduces along the main diagonal.
Parameters
inputTensor2-D tensor. Non-square matrices use the shorter axis for the
diagonal length.
Returns
Tensor0-D tensor (scalar).
Notes
The trace is invariant under cyclic permutations of products: .
Examples
>>> import lucid
>>> A = lucid.tensor([[1.0, 2.0], [3.0, 4.0]])
>>> lucid.trace(A)
Tensor(5.)