Author: Chen_H wechat & QQ: 862251340 wechat official account: Coderpai My blog: please click here
We plan to do a tutorial on the Python API in TensorFlow for future learning. The original link
This chapter introduces apis for manipulating mathematical symbols
-
The first part
-
The second part
Arithmetic operator
TensorFlow provides some operations that you can add to your charts using basic arithmetic operators.
tf.add(x, y, name = None)
Explanation: This function returns the sum of x and y element by element.
Note: The tf.add operation supports broadcast, but the tf.add_n operation does not.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import tensorflow as tf
a = tf.constant(2)
b = tf.constant(3)
c = tf.add(a, b)
sess = tf.Session()
print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: a Tensor, your data type must be one of the following: float32, float64, INT8, INT16, Int32, complex64, int64.
-
Y: A Tensor. You have to have the same type as X.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as X.
tf.sub(x, y, name = None)
Explanation: This function returns the subtraction of x and y element by element.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorflow as tf a = tf.constant([1,2]) b = tf.constant(2) c = tf.sub(a, b) sess = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: a Tensor, your data type must be one of the following: float32, float64, INT8, INT16, Int32, complex64, int64.
-
Y: A Tensor. You have to have the same type as X.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as X.
tf.mul(x, y, name = None)
Explanation: This function returns the element-by-element multiplication of x and y.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorflow as tf a = tf.constant([1,2]) b = tf.constant(2) c = tf.mul(a, b) sess = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: a Tensor, your data type must be one of the following: float32, float64, INT8, INT16, Int32, complex64, int64.
-
Y: A Tensor. You have to have the same type as X.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as X.
tf.div(x, y, name = None)
Explanation: This function returns the division of x and y element by element.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorflow as tf a = tf.constant([1,2]) b = tf.constant(2) c = tf.div(a, b) sess = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: a Tensor, your data type must be one of the following: float32, float64, INT8, INT16, Int32, complex64, int64.
-
Y: A Tensor. You have to have the same type as X.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as X.
tf.mod(x, y, name = None)
Explanation: This function returns the mod of x and y element by element.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorflow as tf a = tf.constant([1,2]) b = tf.constant(2) c = tf.mod(a, b) sess = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, the data type must be one of the following: INT16, INT32, float32, float64.
-
Y: A Tensor. You have to have the same type as X.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as X.
Basic mathematical functions
TensorFlow provides some operations that you can add to your charts using basic mathematical functions.
tf.add_n(inputs, name = None)
This function adds the corresponding positions of elements in the Inputs list.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorflow as tf a = tf.constant([1,2], tf.int32) b = tf.constant([3,4], tf.int32) c = tf.add_n([a,b]) sess = tf.Session()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
Inputs: a list of at least one Tensor. The data type must be one of the following: float32, float64, Int64, Int32, Uint8, Int16, Int8, complex64, qint8, quint8, quint32. And every Tensor in the list has to have the same data dimension.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor has the same type as inputs.
tf.abs(x, name = None)
Explanation: This function returns the absolute value of x.
So given x, it’s a real Tensor. This operation returns a tensor, and every value in that tensor corresponds to every absolute value of value in x.
If you need to handle the absolute value of a complex number, use the tf.plex_abs () function.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import tensorflow as tf
a = tf.constant([1,-2])
c = tf.abs(a)
sess = tf.Session()
print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, the data type has to be one of the following: float, double, int64 or int32.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type, same data dimension as X.
tf.neg(x, name = None)
Explanation: This function yields the negative value of every value in x, that is, y = -x
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import tensorflow as tf
a = tf.constant([1,-2])
c = tf.neg(a)
sess = tf.Session()
print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, your data type must be one of the following: float32, float64, INT32, complex64, int64.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as X.
tf.sign(x, name = None)
Explanation: This function is a symbolic function that converts each value in x according to the following rules.
If x < 0, y = sign(x) = -1; If x == 0, y = sign(x) = 0; If x > 0, y = sign(x) = 1;
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorFlow as tf a = tf.constant([1,-2,0]) c = tf.sign(a) sess = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, the data type must be one of the following: float32, float64, int32, int64.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as X.
tf.inv(x, name = None)
Explanation: This function computes the reciprocal of each element in x, i.e., y = 1/x.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorflow as tf a = tf.constant(7.0) c = tf.inv(a) sess = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, your data type must be one of the following: float32, float64, INT32, complex64, int64.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as X.
Translator’s note: I tested the API, but it seems that x only works if the data type is float.
tf.square(x, name = None)
Explanation: This function computes the square of each element in x, i.e. Y = x*x = x^2.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorflow as tf a = tf.constant([2.0,7.0]) c = tf.square(a) sess = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, your data type must be one of the following: float32, float64, INT32, complex64, int64.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as X.
tf.round(x, name = None)
Explanation: This function is to get the nearest integer to each element of x.
Such as:
# 'a' is [0.9, 2.5, 2.3, -4.4]Tf.round (a) ==> [1.0, 3.0, 2.0, -4.0]Copy the code
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorflow as tf a = tf.constant([2.9, 0.0, -2.1, 2.0, 7.2]) c = tf.round(a) sess = tf.session () tensorflow as tf a = tf.constant([2.9, 0.0, -2.1, 2.0, 7.2]) c = tf.round(a) sess = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, it’s a float or a double.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as X, same data dimension as X.
tf.sqrt(x, name = None)
Explanation: This function yields the square root of each element in x, that is, y = x^{1/2}.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import tensorflow as tf
a = tf.constant([2, 3], tf.float32)
c = tf.sqrt(a)
sess = tf.Session()
print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, the values must be one of the following: float32, float64, INT32, complex64, INT64.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as X.
tf.rsqrt(x, name = None)
Explanation: This function is the derivative of the square root of each element in x, y = 1/x^{1/2}.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import tensorflow as tf
a = tf.constant([2, 3], tf.float32)
c = tf.rsqrt(a)
sess = tf.Session()
print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, the values must be one of the following: float32, float64, INT32, complex64, INT64.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as X.
tf.pow(x, y, name = None)
Explanation: This function computes powers.
Given an x and y, for each value of x and y, compute x to the y.
Such as:
# tensor 'x' is [[2, 2]], [3, 3]]
# tensor 'y' is [[8, 16], [2, 3]]
tf.pow(x, y) ==> [[256, 65536], [9, 27]]Copy the code
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import tensorflow as tf
a = tf.constant([2, 3])
b = tf.constant([2, 3])
c = tf.pow(a, b)
sess = tf.Session()
print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, the data type must be one of the following: float, double, INT32, complex64, int64.
-
Y: A Tensor, you have to have one of the following types: float, double, int32, complex64, int64.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor.
tf.exp(x, name = None)
Explanation: This function evaluates the exponent of each element of x, y = e^x.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorFlow as tf a = tf.constant([2.0, 1], tf.float32) c = tf.exp(a) sess = tf.session () tensorFlow as tf a = tf.constant([2.0, 1], tf.float32) c = tf.exp(a) sess = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, the values must be one of the following: float32, float64, INT32, complex64, INT64.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as X.
tf.log(x, name = None)
Explanation: This function computes the natural log of each element in x, that is, y = log(x).
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorFlow as tf a = tf.constant([2.0, 1], tf.float32) c = tf.log(a) sess = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, the values must be one of the following: float32, float64, INT32, complex64, INT64.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as X.
tf.ceil(x, name = None)
Explanation: This function returns the smallest integer not less than every element in x.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorFlow as tf a = tf.constant([2.2, -1.8], tf.float32) c = tf.ceil(a) sess = tf.session () tensorFlow as tf a = tf.constant([2.2, -1.8], tf.float32) C = tf.ceil(a) sess = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, the data type must be one of the following: float32, float64.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as X.
tf.floor(x, name = None)
Explanation: This function returns the largest integer not greater than each element in x.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorFlow as tf a = tf.constant([2.2, -1.8], tf.float32) c = tf.floor(a) sess = tf.session () tensorFlow as TF a = tf.constant([2.2, -1.8], tF.float32) C = tf.floor(a) sess = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, the data type must be one of the following: float32, float64.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as X.
tf.maximum(x, y, name = None)
Explanation: This function compares the values of x and y one by one and finds the maximum value, x > y? X: y. This function supports broadcast form.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorflow as tf a = tf.constant([2.2, -1.8, 0.0]) b = tf.constant(1.0) C = tf.maximum(a, b) sess = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, the values must be one of the following: float32, float64, int32, int64.
-
Y: Yes, a Tensor. That’s the same kind as X.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as X.
tf.minimum(x, y, name = None)
Explanation: This function compares the values of x and y one by one and finds the lowest value, which is x < y? X: y. This function supports broadcast form.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorFlow as tf a = tf.constant([2.2, -1.8, 0.0]) b = tf.constant(1.0) c = tf.minimum(a, b) sess = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, the values must be one of the following: float32, float64, int32, int64.
-
Y: Yes, a Tensor. That’s the same kind as X.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as X.
tf.cos(x, name = None)
Explanation: This function computes the cosine of each element in x.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorflow as tf a = tf.constant([2.2, -1.8, 0.0]) c = tf.cos(a) sess = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, the values must be one of the following: float32, float64, INT32, complex64, INT64.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as X.
tf.sin(x, name = None)
Explanation: This function evaluates the sine of each element in x.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorFlow as tf a = tf.constant([2.2, -1.8, 0.0]) c = tf.sin(a) sess = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, the values must be one of the following: float32, float64, INT32, complex64, INT64.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as X.
Matrix mathematical function
TensorFlow provides some operations you can use to add basic matrix math functions to your graphs.
tf.diag(diagonal, name = None)
This function is given a diagonal diagonal and returns a diagonal tensor.
Given a diagonal diagonal value, this operation returns a diagonal tensor, with the diagonal value on top being the diagonal, and the rest filled with zeros.
Let’s say that the dimension of the diagonal is [D1, D2… Dk], then the rank of the output tensor is 2k and the dimension is [D1, D2… Dk, D1, D2… Dk], as follows:
The output [i1, i2,..., ik, i1, i2,..., ik] = diagonal [i1,.., ik], the remaining value is 0.Copy the code
Such as:
# 'diagonal' is [1, 2, 3, 4]
tf.diag(diagonal) ==> [[1, 0, 0, 0]
[0, 2, 0, 0]
[0, 0, 3, 0]
[0, 0, 0, 4]]Copy the code
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorflow as tf a = tf.constant([2.2, -1.8, 1.0]) c = tf.diag(a) sess = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
Diagonal: A Tensor, and the data type must be one of the following: float32, float64, INT32, int64. Its rank is at most 3.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as diagonal.
tf.transpose(a, perm = None, name = 'transpose')
Explanation: Transpose a and rearrange the output dimensions according to perm parameters.
The i-th dimension of the output tensor will be specified by Perm [I]. For example, if perm is not given, then the default is perm = [n-1, n-2… 0], where rank(a) = n. By default, for two-dimensional input data, this is just normal matrix transpose.
Such as:
input_data.dims = (1, 4, 3)
perm = [1, 2, 0]
Dims [0] = input_data.dims[perm[0]]
Dims [1] = input_data.dims[perm[1]]
Dims [2] = input_data.dims[perm[2]]
Dims = (4, 3, 1)
output_data.dims = (4, 3, 1)Copy the code
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorflow as tf sess = tf.session () input_data = tf.constant([[1,2,3],[4,5,6]])print sess.run(tf.transpose(input_data))
print sess.run(input_data)
printSess. Run (tf) transpose (input_data, perm = (1, 0])) input_data = tf. The constant ([[[1, 2, 3], [4 and 6],,8,9 [7], the final three [10]]])print 'input_data shape: ', sess.run(tf.shape(input_data))
output_data = tf.transpose(input_data, perm=[1, 2, 0])
print 'output_data shape: ', sess.run(tf.shape(output_data))
print sess.run(output_data)
sess.close()Copy the code
Input parameters:
-
A: A Tensor.
-
Perm: A rearrangement of dimensions of A.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor that’s flipped.
tf.matmul(a, b, transpose_a = False, transpose_b = False, a_is_sparse = False, b_is_sparse = False, name = None)
Explanation: Multiply matrix A and matrix B to get matrix A *b.
The input data must be a two-dimensional matrix, with or without transpose, and the internal dimensions must match.
The input matrices must have the same data type: float, double, INT32, complex64.
Matrices can be set to transpose operations, i.e., transpose_A = True and transpose_b = True. By default, this flag is set to False.
If there are lots of zeros in the matrix, use sparse notation, a_IS_SPARSE = True and B_IS_SPARSE = True. By default, this flag is set to False.
Such as:
# 2-D tensor `a`
a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3]) => [[1. 2. 3.]
[4. 5. 6.]]
# 2-D tensor `b`
b = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2]) => [[7. 8.]
[9. 10.]
[11. 12.]]
c = tf.matmul(a, b) => [[58 64]
[139 154]]Copy the code
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorflow as tf import numpy as np a = tf.constant(np.random.rand(2,3)) b = tf.constant(np.random.rand(1,3)) c = tf.matmul(a, b, transpose_b = True) sess = tf.Session()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
A Tensor, your data type is float, double, INT32 or complex64.
-
B: A Tensor, same kind of data as a.
-
Transpose_a: If the value is True, then a is transposed before the matrix is evaluated.
-
Transpose_b: If the value is True, then b is transposed before the matrix is evaluated.
-
A_is_sparse: If this value is True, consider A as a sparse matrix during matrix calculation.
-
B_is_sparse: If this value is True, consider B as a sparse matrix during matrix calculations.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as A.
tf.batch_matmul(x, y, adj_x = None, adj_y = None, name = None)
Explanation: This function multiplies two tensors by batch slices.
Slice the tensors x and y (each slice is an element of a batch), then multiply each slice of the corresponding x and y, rearranging the result according to the size of the original batch. If we set adj_x or adj_y to True, each individual slice can form its conjugate (which is equivalent to transpose) before multiplication.
Input x and y is the three dimensional tensor, or higher dimension [… and r_x c_x] and [… and r_y c_y].
Output tensor is three-dimensional, or higher dimensional [… r_O c_O], where:
r_o = c_x if adj_x else r_x
c_o = r_y if adj_y else c_yCopy the code
The calculation process is as follows:
out[..., :, :] = matrix(x[..., :, :]) * matrix(y[..., :, :])Copy the code
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
a = tf.constant(np.random.rand(2, 2, 3))
b = tf.constant(np.random.rand(3, 3, 1))
c = tf.batch_matmul(a, b)
sess = tf.Session()
print sess.run(c)
sess.close()Copy the code
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
a = tf.constant(np.random.rand(3, 2, 3, 1))
b = tf.constant(np.random.rand(3, 2, 3, 1))
c = tf.batch_matmul(a, b, adj_x = False, adj_y = True )
sess = tf.Session()
print sess.run(c)
print sess.run(tf.shape(c))
sess.close()Copy the code
Input parameters:
-
X: a Tensor, the data type is float32, float64, INT32 or complex64, the data dimension is three dimensional or higher [… r_x c_x].
-
Y: A Tensor, you have the same kind of data as X, you have three dimensions, maybe higher dimensions.
-
Adj_x: This is an optional Boolean value, False by default. If we set it to True, each slice of x will be transposed.
-
Adj_y: This is an optional Boolean value, False by default. If we set it to True, each slice of y will be transposed.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, your data type is the same as X, your data dimension is three dimensional, or higher.
tf.matrix_determinant(input, name=None)
Explanation: This function computes the determinant of a matrix of order n.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
a = tf.constant(np.random.rand(3, 3))
c = tf.matrix_determinant(a)
sess = tf.Session()
print sess.run(c)
sess.close()Copy the code
Input parameters:
-
Input: a Tensor, the values are float32 or float64, the values are [M, M].
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor scalar, same type as input.
tf.batch_matrix_determinant(input, name=None)
Explanation: This function computes the determinant of the n-order matrix for each batch (slice).
The input tensor has to have a dimension of M, M, M, which has to have a two-dimensional square matrix inside it, and for all the submatrices, the output has to be a one-dimensional tensor.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
a = tf.constant(np.random.rand(4, 2, 3, 3))
c = tf.batch_matrix_determinant(a)
sess = tf.Session()
print sess.run(c)
sess.close()Copy the code
Input parameters:
-
Input: a Tensor, the values are float32 or float64, the values are [… M, M].
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as input, data dimension […] .
tf.matrix_inverse(input, name=None)
Explanation: this function computes the inverse of the n-order matrix and checks for invertibility.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
a = tf.constant(np.random.rand(3, 3))
c = tf.matrix_inverse(a)
sess = tf.Session()
print sess.run(c)
d = tf.matmul(a, c)
print sess.run(d)
e = tf.matrix_determinant(d)
print sess.run(e)
sess.close()Copy the code
Input parameters:
-
Input: a Tensor, the values are float32 or float64, the values are [M, M].
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as input, data dimension [M, M].
tf.batch_matrix_inverse(input, name=None)
This function computes the inverse of the n-order matrix for each batch (slice) and checks for reversibility.
The input tensor’s data type is [… M, M], where the interior has to be a two-dimensional square matrix, and for each submatrix, the inverse of the output matrix has the same data dimension as the input.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
a = tf.constant(np.random.rand(2, 3, 3))
c = tf.batch_matrix_inverse(a)
sess = tf.Session()
print sess.run(c)
sess.close()Copy the code
Input parameters:
-
Input: a Tensor, the values are float32 or float64, the values are [… M, M].
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same type as input, dimension [… M, M].
tf.cholesky(input, name=None)
Explanation: This function is used to compute the Cholesky factorization of n-order matrices.
The input data must be a symmetric positive definite matrix, and for this operation we only read the lower triangle of the matrix, not the upper triangle of the matrix.
The output result is a lower triangular real matrix with positive diagonal elements after Cholesky’s decomposition.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
a = tf.constant([[2, np.random.rand()], [-2, 5]], tf.float32)
c = tf.cholesky(a)
sess = tf.Session()
print sess.run(c)
sess.close()Copy the code
Input parameters:
-
Input: a Tensor, the values are float32 or float64, the values are [M, M].
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same data type as input, data dimension [M, M].
tf.batch_cholesky(input, name=None)
Explanation: This function is used to compute the Cholesky factorization of the n-order matrix for each batch (slice).
The data type for the tensor is […, M, M], where the interior has to be a two-dimensional square matrix that meets the conditions of Cholesky’s decomposition. The output tensor has the same data type as the input data, and each slice is the value that Cholesky has decomposed.
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
a = tf.constant([[[2, np.random.rand()], [-2, 5]], [[2, np.random.rand()], [-2, 5]]], tf.float32)
c = tf.batch_cholesky(a)
sess = tf.Session()
print sess.run(c)
sess.close()Copy the code
Input parameters:
-
Input: a Tensor, the values are float32 or float64, the values are [… M, M].
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, same type as input, dimension [… M, M].
The plural function
TensorFlow provides complex number functions that allow you to manipulate complex numbers and you can add them to your diagrams.
tf.complex(real, imag, name=None)
Explanation: This function converts two real numbers into a complex number.
The operation is to compute the complex number A + BJ, where a comes from the input data real, representing the real part, and b comes from the input data imag, representing the imaginary part.
Input data Real and IMAG must have the same data dimension.
Such as:
# tensor 'real' is [2.25, 3.25]
# tensor `imag` is [4.75, 5.75]Tf.com place (real, imag) = = > [2.25 + 4.74 [j], 3.25 + 5.75 [j]]Copy the code
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorflow as tf import numpy as NP a = tf.constant([2.25, 3.25]) b = tf.constant([4.75, 5.75]) c = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
Real: A Tensor, the data type is float.
-
Imag: A Tensor, the data type is float.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, the data type is complex64.
tf.complex_abs(x, name=None)
Explanation: The function is to compute the absolute value of a complex number.
Given a complex number tensor x, this operation evaluates the absolute value of each value in x and returns a tensor of type float. All complex numbers in x must be of the form a + BJ, so the absolute value is calculated as follows:
Such as:
# tensor 'x' is [[-2.25 + 4.75j], [-3.25 + 5.75j]]Tf.com plex_abs (x) = = > [5.25594902, 6.60492229]Copy the code
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorflow as tf import numpy as NP a = tf.constant([2.25 + 3.25j]) c = tf.plex_abs (a) sess = tf.session () import tensorflow as TF import numpy as NP a = tf.constant([2.25 + 3.25j]) c = tf.plex_abs (a) sess = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, the data type is complex64.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, the data type is float32.
tf.conj(in_, name=None)
Explanation: This function computes the complex conjugate of complex numbers.
Given a complex number tensor in_, the operation computes the complex conjugate of every complex number in in_. All complex numbers in in_ must be of the form a + bj, where a is the real part and b is the imaginary part.
After this operation, the return form of the complex conjugate is a-bj.
Such as:
# tensor 'in' is [-2.25 + 4.75j, 3.25 + 5.75j]
tf.conj(in) ==> [-2.25-4.75j, 3.25-5.75j]Copy the code
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorflow as tf import numpy as NP a = tf.constant([2.25 + 3.25j]) C = tf.conj(a) sess = tf.session () import tensorflow as tf import numpy as NP a = tf.constant([2.25 + 3.25j]) C = tf.conj(a) sess = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, the data type is complex64.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, the data type is complex64.
tf.imag(in_, name=None)
Explanation: This function returns the imaginary part of a complex number.
Given a complex number tensor in_, this operation returns the imaginary part of each complex number in in_. All complex numbers in in_ must be of the form a + bj, where a is the real part and b is the imaginary part.
Such as:
# tensor 'in' is [-2.25 + 4.75j, 3.25 + 5.75j]
tf.imag(in) = = > [4.75, 5.75]Copy the code
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorflow as tf import numpy as NP a = tf.constant([2.25 + 3.25j]) c = tf.imag(a) sess = tf.session () import tensorflow as tf import numpy as NP a = tf.constant([2.25 + 3.25j]) C = tf.imag(a) sess = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, the data type is complex64.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, the data type is float32.
tf.real(in_, name=None)
Explanation: This function returns the real part of the complex number.
Given a complex number tensor in_, this operation returns the real part of every complex number in in_. All complex numbers in in_ must be of the form a + bj, where a is the real part and b is the imaginary part.
Such as:
# tensor 'in' is [-2.25 + 4.75j, 3.25 + 5.75j]
tf.real(in) = = > [2.25, 3.25]Copy the code
Use examples:
#! /usr/bin/env python
# -*- coding: utf-8 -*-Import tensorflow as tf import numpy as NP a = tf.constant([2.25 + 3.25j]) C = tf.real(a) sess = tf.session () import tensorflow as tf import numpy as NP a = tf.constant([2.25 + 3.25j]) C = tf.real(a) sess = tf.session ()print sess.run(c)
sess.close()Copy the code
Input parameters:
-
X: A Tensor, the data type is complex64.
-
Name :(optional) give this operation a name.
Output parameters:
-
A Tensor, the data type is float32.
CoderPai is a platform that focuses on algorithms in action, from basic algorithms to artificial intelligence algorithms. If you are interested in algorithms in action, please follow us. Join AI combat wechat group, AI combat QQ group, ACM algorithm wechat group, ACM algorithm QQ group. For details, please pay attention to the wechat account CoderPai.