Answers.Body: <p>The concepts of trace and tensor also appear in other contexts outside of machine learning (ML), like quantum computing, so an answer to your question may be given independently of ML, but that may not be useful, as these concepts may be defined and implemented differently in the context of ML, which seems to be the case.</p>
  <p>The concept of trace, in mathematics, is apparently known as <a href="https://en.wikipedia.org/wiki/Tensor_contraction" rel="nofollow noreferrer"><strong>tensor contraction</strong></a>. I don't know if that definition is consistent with the definition(s)/implementation(s) of trace used in machine learning.</p>
  <p>I found at least 2 (different) definitions of tensor trace in machine learning. The first definition is provided by the TensorFlow <a href="https://github.com/tensorflow/tensorflow/blob/v2.7.0/tensorflow/python/ops/math_ops.py#L3431" rel="nofollow noreferrer">implementation</a> of the trace of a tensor. For completeness, let me write here <a href="https://www.tensorflow.org/api_docs/python/tf/linalg/trace" rel="nofollow noreferrer">their definition of the trace</a>.</p>
  <blockquote>
  <p><code>trace(x)</code> returns the sum along the main diagonal of each inner-most matrix in <code>x</code>. If <code>x</code> is of rank <code>k</code> with shape <code>[I, J, K, ..., L, M, N]</code>, then output is a tensor of rank <code>k-2</code> with dimensions <code>[I, J, K, ..., L]</code> where <code>output[i, j, k, ..., l] = trace(x[i, j, k, ..., l, :, :])</code>.</p>
  </blockquote>
  <p>So, essentially, for each most inner 2d matrix in this tensor, you compute the trace (for that matrix), then return the result as another tensor, which has 2 fewer dimensions than the original tensor (because a matrix has 2 dimensions, and, by computing the tensor of a matrix, you reduce a matrix to a number, which is a 0-dimensional tensor).</p>
  <p>They give these examples</p>
  <pre><code>x = tf.constant([[1, 2], [3, 4]])
  tf.linalg.trace(x)  # 5
  
  x = tf.constant([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])
  tf.linalg.trace(x)  # 15
  
  x = tf.constant([[[1, 2, 3],
                    [4, 5, 6],
                    [7, 8, 9]],
                   [[-1, -2, -3],
                    [-4, -5, -6],
                    [-7, -8, -9]]])
  tf.linalg.trace(x)  # [15, -15]
  </code></pre>
  <p>I think this definition is easy to understand, but I don't remember having ever used it, but I could be wrong.</p>
  <p>In fact, PyTorch does not seem to implement the trace for tensors, but only matrices. If you executed the following code, you should get an error that tells you that's not possible.</p>
  <pre><code>import torch  # install also numpy
  
  x = torch.tensor([[[1, 2, 3],
                     [4, 5, 6],
                     [7, 8, 9]],
                    [[-1, -2, -3],
                     [-4, -5, -6],
                     [-7, -8, -9]]])
  
  print(torch.trace(x))  # [15, -15]? No, you get an error.
  </code></pre>
  <p>So, as I was suspecting, the tensor trace may not be terribly useful in ML, at least, for tensors with more than 2 dimensions.</p>
  <p>By the way, in the paper <a href="https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&amp;arnumber=8884203" rel="nofollow noreferrer">A Survey on Tensor Techniques and Applications in Machine Learning</a> (2019) Yuwang Ji et al. (<a href="https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&amp;arnumber=8884203#page=6" rel="nofollow noreferrer">p. 6 of the pdf</a>), you find another definition of tensor trace, which I don't think it's equivalent to the definition used by the TF implementation.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-12-02T11:26:14.877Z
Answers.LastActivityDate: 2021-12-02T13:01:08.913Z
Answers.LastEditDate: 2021-12-02T13:01:08.913Z
Answers.UserDisplayName: nbro
Body: <p>Tensor is a multi-dimensional ordered collection of elements, which is used in deep learning to store and process data as well as intermediate steps.</p>
  <p>We are aware of the trace of a two-dimensional tensor i.e. matrix. It is defined as the sum of the diagonal elements of the matrix.</p>
  <p>Is there any definition for the trace of a tensor?</p>
  
Tags: <machine-learning><deep-learning><math><definitions><tensor>
Title: What is the definition of a trace of a tensor?
UserDisplayName: hanugm
ThreadText: # What is the definition of a trace of a tensor?
  
  (Asked by: username_0 on 2021-12-01)
  
  Tensor is a multi-dimensional ordered collection of elements, which is used in deep learning to store and process data as well as intermediate steps.
  
  We are aware of the trace of a two-dimensional tensor i.e. matrix. It is defined as the sum of the diagonal elements of the matrix.
  
  Is there any definition for the trace of a tensor?
  
  **Tags:** machine-learning, deep-learning, math, definitions, tensor
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-12-02. Score: 1
  
  The concepts of trace and tensor also appear in other contexts outside of machine learning (ML), like quantum computing, so an answer to your question may be given independently of ML, but that may not be useful, as these concepts may be defined and implemented differently in the context of ML, which seems to be the case.
  
  The concept of trace, in mathematics, is apparently known as [**tensor contraction**](https://en.wikipedia.org/wiki/Tensor_contraction). I don't know if that definition is consistent with the definition(s)/implementation(s) of trace used in machine learning.
  
  I found at least 2 (different) definitions of tensor trace in machine learning. The first definition is provided by the TensorFlow [implementation](https://github.com/tensorflow/tensorflow/blob/v2.7.0/tensorflow/python/ops/math_ops.py#L3431) of the trace of a tensor. For completeness, let me write here [their definition of the trace](https://www.tensorflow.org/api_docs/python/tf/linalg/trace).
  
  > `trace(x)` returns the sum along the main diagonal of each inner-most matrix in `x`. If `x` is of rank `k` with shape `[I, J, K, ..., L, M, N]`, then output is a tensor of rank `k-2` with dimensions `[I, J, K, ..., L]` where `output[i, j, k, ..., l] = trace(x[i, j, k, ..., l, :, :])`.
  
  So, essentially, for each most inner 2d matrix in this tensor, you compute the trace (for that matrix), then return the result as another tensor, which has 2 fewer dimensions than the original tensor (because a matrix has 2 dimensions, and, by computing the tensor of a matrix, you reduce a matrix to a number, which is a 0-dimensional tensor).
  
  They give these examples
  
  ```
  x = tf.constant([[1, 2], [3, 4]])
  tf.linalg.trace(x)  # 5
  
  x = tf.constant([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])
  tf.linalg.trace(x)  # 15
  
  x = tf.constant([[[1, 2, 3],
                    [4, 5, 6],
                    [7, 8, 9]],
                   [[-1, -2, -3],
                    [-4, -5, -6],
                    [-7, -8, -9]]])
  tf.linalg.trace(x)  # [15, -15]
  
  ```
  
  I think this definition is easy to understand, but I don't remember having ever used it, but I could be wrong.
  
  In fact, PyTorch does not seem to implement the trace for tensors, but only matrices. If you executed the following code, you should get an error that tells you that's not possible.
  
  ```
  import torch  # install also numpy
  
  x = torch.tensor([[[1, 2, 3],
                     [4, 5, 6],
                     [7, 8, 9]],
                    [[-1, -2, -3],
                     [-4, -5, -6],
                     [-7, -8, -9]]])
  
  print(torch.trace(x))  # [15, -15]? No, you get an error.
  
  ```
  
  So, as I was suspecting, the tensor trace may not be terribly useful in ML, at least, for tensors with more than 2 dimensions.
  
  By the way, in the paper [A Survey on Tensor Techniques and Applications in Machine Learning](https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8884203) (2019) Yuwang Ji et al. ([p. 6 of the pdf](https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=8884203#page=6)), you find another definition of tensor trace, which I don't think it's equivalent to the definition used by the TF implementation.
  
  
  ---
  
  