Tensorflow algorithm

Import tensorflow as TF tf.add(a,b) # Subtract TF.multiply (x,y) # Multiply tF.div (x,y) # Multiply tF.truediv (x,y) # Float number division Tf. Mod (x, y) # take overCopy the code

 

tf.reduce_mean()

The tf.reduce_mean function is used to calculate the mean of tensor tensor along a given dimension of the tensor line. It’s mainly used for reducing dimension or calculating the mean of tensor.

Interface: reduce_mean(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)Copy the code
  • The first parameter input_tensor: the tensor you put in to reduce dimension;
  • The second parameter axis: specifies the axis, or if not, calculates the mean of all elements;
  • Keep_dims: If you set it to True, your output will keep the input tensor shape. If you set it to False, your output will reduce the dimension.
  • The fourth parameter name: the name of the operation;
  • The fifth parameter, reduction_indices: deprecated, used in previous versions to specify an axis;

Here’s an example:

Import tensorflow as tf x = [[1,2,3], [1,2,3] xx = tf.cast(x,tf.float32) mean_all = tf.reduce_mean(xx, keep_dims=False) mean_0 = tf.reduce_mean(xx, axis=0, keep_dims=False) mean_1 = tf.reduce_mean(xx, axis=1, keep_dims=False) with tf.Session() as sess: m_a,m_0,m_1 = sess.run([mean_all, mean_0, mean_1]) print m_a # output: Print m_1 #output: [2. 2.] print m_1 #output: [2.Copy the code

Similar function

  • Tf. reduce_sum: Calculate the sum of all the elements along the tensor specified axis;
  • Tf. reduce_max: Calculate the maximum value of each element along the tensor specified axis;
  • Tf. reduce_all: Calculate the logical sum of the elements along the tensor axis.
  • Tf. reduce_any: Calculate the logic or (or operation) of the elements along the tensor specified axis;

 

Np. Linalg. Norm () – norm

Inalg = Linear (linear) +algebra (norm)

x_norm=np.linalg.norm(x, ord=None, axis=None, keepdims=False)
Copy the code

1. X: represents the matrix (can be one-dimensional)

2. Ord: indicates the norm type

Three kinds of norm of vector:

Three kinds of norm of matrix:

3. Axis: Processing type

4. Keepding: whether to keep the two-dimensional characteristics of the matrix

True preserves the two-dimensional properties of the matrix, False does the opposite

Example:

Import numpy as np x = np.array([[1, 2, 3], [2, 4, 6]]) print" Norm (x, keepdims=True) print "norm(x, keepdims=True) print" : ", np.linalg.norm(x, ord=1,keepdims=True) print" Norm (x, ord=2, keepdims=True) print" Norm (x, ord=np.inf, keepdims=True) print" ", np.linalg.norm(x, axis=1, keepdims=True) print" ", np.linalg.norm(x, axis=0, keepdims=True) print" ", np.linalg.norm(x, ord=1, axis=1, keepdims=True) print" ", np.linalg.norm(x, ord=1, axis=0, keepdims=True)Copy the code

The output is:

Default parameter (matrix 2 norm, without preserving the two-dimensional properties of the matrix) : 8.36660026534 Matrix 2 norm, preserving the two-dimensional properties of the matrix: [[8.36660027]] Matrix 1 norm (maximum value of the column sum) : [[9.]] Matrix 2 norm (compute the eigenvalue, then compute the arithmetic square root of the maximum eigenvalue) : [[8.36660027]] matrix ∞ norm (maximum row sum) : [[12.]]] matrix each row vector to find the 2 norm of the vector: [[3.74165739] [7.48331477]] matrix each column vector to find the 2 norm of the vector: [[2.23606798 4.47213595 6.70820393]] the 1 norm of each row vector of] matrix is calculated: [[6.] [12.]] the 1 norm of each column vector of] matrix is calculated: [[3.6.9.]]Copy the code

 


References:

【 1 】 blog.csdn.net/Liang_xj/ar…

【 2 】 blog.csdn.net/dcrmg/artic…