Tensorflow Glossary Part 1 - Mathematical Functions

This is a summary and brief write up of common (and not so common mathematical functions in Tensorflow) - just a few lines specifying the syntax and how they should be written in TensorFlow.

Its obviously available at the documentation link given below, but writing this in this post forces me to go through each one of them one by one, which I am too lazy to do in a documentation. Also, this lis excludes a lot of not-so-common functions

Tensorflow Documentation for Math

Basic Mathematics

  • tf.add
  • tf.subtract
  • tf.multiply
  • tf.scalar_mul
    tf.scalar_mul(
      scalar,
      x
    )
    

    Multiplies a scalar times a Tensor or IndexedSlices object.

Intended for use in gradient code which might deal with IndexedSlices objects, which are easy to multiply by a scalar but more expensive to multiply with arbitrary tensors.

  • tf.div
  • tf.divide
  • tf.floor - Returns element-wise remainder of division. When x < 0 xor y < 0 is true, this follows Python semantics in that the result here is consistent with a flooring divide. E.g. floor(x / y) * y + mod(x, y) = x.
  • tf.mod tf.cross - Compute the pairwise cross product. a and b must be the same shape; they can either be simple 3-element vectors, or any shape where the innermost dimension is 3. In the latter case, each pair of corresponding 3-element vectors is cross-multiplied independently.

Basic Mathematical Operations

  • tf.add_n
  • tf.abs
  • tf.negative
  • tf.sign
  • tf.reciprocal
  • tf.square
  • tf.round
  • tf.sqrt
  • tf.rsqrt - Computes reciprocal of square root of x element-wise. (1/sqrt(x))
  • tf.pow
  • tf.exp
  • tf.expm1
  • tf.log
  • tf.log1p
  • tf.ceil
  • tf.floor
  • tf.maximum - Returns the max of x and y (i.e. x > y ? x : y) element-wise.
  • tf.minimum
  • tf.squared_difference - Returns (x - y)(x - y) element-wise.

Matrix Operations

  • tf.diag - Returns a diagonal tensor with a given diagonal values.
  • tf.diag_part - Returns the diagonal part of the tensor.
  • tf.trace - Compute the trace of a tensor x. 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, i, …, l, :, :])
  • tf.transpose
tf.transpose(
    a,
    perm=None,
    name='transpose',
    conjugate=False
)

Transposes a. Permutes the dimensions according to perm.

  • tf.eye
tf.eye(
    num_rows,
    num_columns=None,
    batch_shape=None,
    dtype=tf.float32,
    name=None
)

Construct an identity matrix, or a batch of matrices.

  • tf.matrix_transpose
    tf.matrix_transpose(
      a,
      name='matrix_transpose',
      conjugate=False
    )
    

    Transposes last two dimensions of tensor a.

  • tf.matmul
    tf.matmul(
      a,
      b,
      transpose_a=False,
      transpose_b=False,
      adjoint_a=False,
      adjoint_b=False,
      a_is_sparse=False,
      b_is_sparse=False,
      name=None
    )
    

    Multiplies matrix a by matrix b, producing a * b.

  • tf.norm
    tf.norm(
      tensor,
      ord='euclidean',
      axis=None,
      keepdims=None,
      name=None,
      keep_dims=None
    )
    

    Computes the norm of vectors, matrices, and tensors. (deprecated arguments)

  • tf.matrix_determinant
    tf.matrix_determinant(
      input,
      name=None
    )
    

    Computes the determinant of one or more square matrices.

  • tf.matrix_inverse
    tf.matrix_inverse(
      input,
      adjoint=False,
      name=None
    )
    

Computes the inverse of one or more square invertible matrices or their adjoints (conjugate transposes).

Reduction

  • tf.reduce_sum
    Computes the sum of elements across dimensions of a tensor. (deprecated arguments)
    x = tf.constant([[1, 1, 1], [1, 1, 1]])
    tf.reduce_sum(x)  # 6
    tf.reduce_sum(x, 0)  # [2, 2, 2]
    tf.reduce_sum(x, 1)  # [3, 3]
    tf.reduce_sum(x, 1, keepdims=True)  # [[3], [3]]
    tf.reduce_sum(x, [0, 1])  # 6
    
  • tf.reduce_prod
    Computes the product of elements across dimensions of a tensor. (deprecated arguments)
tf.reduce_prod(
    input_tensor,
    axis=None,
    keepdims=None,
    name=None,
    reduction_indices=None,
    keep_dims=None
)
  • tf.reduce_min - Computes the minimum of elements across dimensions of a tensor. (deprecated arguments)
  • tf.reduce_max - Computes the maximum of elements across dimensions of a tensor. (deprecated arguments)
  • tf.reduce_mean
    Computes the mean of elements across dimensions of a tensor. (deprecated arguments)
x = tf.constant([[1., 1.], [2., 2.]])
tf.reduce_mean(x)  # 1.5
tf.reduce_mean(x, 0)  # [1.5, 1.5]
tf.reduce_mean(x, 1)  # [1.,  2.]
  • tf.reduce_all
    Computes the “logical and” of elements across dimensions of a tensor. (deprecated arguments)
x = tf.constant([[True,  True], [False, False]])
tf.reduce_all(x)  # False
tf.reduce_all(x, 0)  # [False, False]
tf.reduce_all(x, 1)  # [True, False]
  • tf.reduce_any
    Computes the “logical or” of elements across dimensions of a tensor. (deprecated arguments)

  • tf.count_nonzero
    Computes number of nonzero elements across dimensions of a tensor. (deprecated arguments)

Returns the element-wise sum of a list of tensors.

  • tf.accumulate_n
a = tf.constant([[1, 2], [3, 4]])
b = tf.constant([[5, 0], [0, 6]])
tf.accumulate_n([a, b, a])  

[[7, 4], [6, 14]]

Explicitly pass shape and type
tf.accumulate_n([a, b, a], shape=[2, 2], tensor_dtype=tf.int32)
                                                               [[7,  4],
                                                               [6, 14]]

Scan

  • tf.cumsum Compute the cumulative sum of the tensor x along axis. ```python tf.cumsum([a, b, c]) # [a, a + b, a + b + c] tf.cumsum([a, b, c], reverse=True) # [a + b + c, b + c, c]
* tf.cumprod
Compute the cumulative product of the tensor x along axis.

```python
tf.cumprod([a, b, c])  

[a, a * b, a * b * c]
tf.cumprod([a, b, c], reverse=True)  

[a * b * c, b * c, c]

Sequence Comparison and Indexing

  • tf.argmin Returns the index with the smallest value across axes of a tensor. (deprecated arguments)
tf.argmin(
    input,
    axis=None,
    name=None,
    dimension=None,
    output_type=tf.int64
)

  • tf.argmax Returns the index with the largest value across axes of a tensor. (deprecated arguments)
    tf.argmax(
      input,
      axis=None,
      name=None,
      dimension=None,
      output_type=tf.int64
    )
    
  • tf.where Return the elements, either from x or y, depending on the condition.
tf.where(
    condition,
    x=None,
    y=None,
    name=None
)
  • tf.unique

Finds unique elements in a 1-D tensor.

#tensor 'x' is [1, 1, 2, 4, 4, 4, 7, 8, 8]
y, idx = unique(x)
y ==> [1, 2, 4, 7, 8]
idx ==> [0, 0, 1, 2, 2, 2, 3, 4, 4]
Written on April 23, 2018
]