aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/ops/math_ops.py
diff options
context:
space:
mode:
Diffstat (limited to 'tensorflow/python/ops/math_ops.py')
-rw-r--r--tensorflow/python/ops/math_ops.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/tensorflow/python/ops/math_ops.py b/tensorflow/python/ops/math_ops.py
index 6c388ae9b2..3ba9d509fd 100644
--- a/tensorflow/python/ops/math_ops.py
+++ b/tensorflow/python/ops/math_ops.py
@@ -63,6 +63,8 @@ TensorFlow provides several operations that you can use to add basic
mathematical functions for matrices to your graph.
@@diag
+@@diag_part
+@@trace
@@transpose
@@matmul
@@ -921,6 +923,39 @@ def reduce_any(input_tensor, reduction_indices=None, keep_dims=False,
keep_dims, name=name)
+def trace(x, name=None):
+ """ Compute the trace of a tensor `x`.
+
+ `trace(x)` returns the sum of along the diagonal.
+
+ For example:
+
+ ```python
+ # 'x' is [[1, 1],
+ # [1, 1]]
+ tf.trace(x) ==> 2
+
+ # 'x' is [[1,2,3],
+ # [4,5,6],
+ # [7,8,9]]
+ tf.trace(x) ==> 15
+ ```
+
+ Args:
+ input_tensor: 2-D tensor.
+ name: A name for the operation (optional).
+
+ Returns:
+ The trace of input tensor.
+ """
+ with ops.op_scope([x], name, "Trace") as name:
+ x = ops.convert_to_tensor(x, name="x")
+ if len(x.get_shape()) != 2:
+ raise ValueError("Expected a tensor with rank 2, rank %d tensor received"
+ % len(x.get_shape()))
+ return reduce_sum(array_ops.diag_part(x), name=name)
+
+
def matmul(a, b,
transpose_a=False, transpose_b=False,
a_is_sparse=False, b_is_sparse=False,