tensorflow基礎.數學運算_邏輯&判斷&條件&分支&跳轉


PS:AutoGraph 是很方便,但是 那是 tensorflow的事情了...

1、邏輯&判斷&條件&分支&跳轉

 1.1、Tensorflow-API :對於tensor特定值進行邏輯判斷和操作 - 簡書.html(https://www.jianshu.com/p/9fe26908ac31

  tf.where(condition,x=None,y=None,name=None)

   tf.gather_nd  ZC:這個是干嘛的??

  (1)、比較函數

   1) #判斷每一個數是否大於threshold
      greater = tf.greater(x, threshold)
   2) #判斷每一個數是否小於threshold
      less = tf.less(x,threshold)
   3) #判斷每一個數是否大於等於threshold
      greater_equal=tf.greater_equal(x, threshold)
   4) #判斷每一個數是否小於等於threshold
      less_equal=tf.less_equal(x, threshold)
   5) #判斷每一個數是否等於threshold
      equal = tf.equal(x, threshold)
   6) #判斷每一個數是否不等於threshold
      not_equal=tf.not_equal(x, threshold)

  (2)、這些比較函數會輸出bool類型的tensor,那么就可以配合tf.where來使用

 

 1.2、tensorflow條件語句-tf.case.html(http://www.mamicode.com/info-detail-2558652.html

  tf.case(...)

 

 1.3、Tensorflow之分支結構_PYTandFA的博客-CSDN博客.html(https://blog.csdn.net/PYTandFA/article/details/85233744

   Learning Tensorflow(8)---條件判斷語句_CHAO'S NOTE-CSDN博客.html(https://blog.csdn.net/jinzhichaoshuiping/article/details/84615753

  tf.cond(...)

 

 1.4、tf.where_loop(...)

 

 1.5、

 

2、TensorFLow 數學運算_zywvvd的博客-CSDN博客.html(https://blog.csdn.net/zywvvd/article/details/78593618

一、Tensor 之間的運算規則
  相同大小 Tensor 之間的任何算術運算都會將運算應用到元素級
  不同大小 Tensor(要求dimension 0 必須相同) 之間的運算叫做廣播(broadcasting)
  Tensor 與 Scalar(0維 tensor) 間的算術運算會將那個標量值傳播到各個元素
  Note: TensorFLow 在進行數學運算時,一定要求各個 Tensor 數據類型一致
 1 # 算術操作符:+ - * / % 
 2 tf.add(x, y, name=None)        # 加法(支持 broadcasting)
 3 tf.subtract(x, y, name=None)   # 減法
 4 tf.multiply(x, y, name=None)   # 乘法
 5 tf.divide(x, y, name=None)     # 浮點除法, 返回浮點數(python3 除法)
 6 tf.mod(x, y, name=None)        # 取余
 7  
 8  
 9 # 冪指對數操作符:^ ^2 ^0.5 e^ ln 
10 tf.pow(x, y, name=None)        # 冪次方
11 tf.square(x, name=None)        # 平方
12 tf.sqrt(x, name=None)          # 開根號,必須傳入浮點數或復數
13 tf.exp(x, name=None)           # 計算 e 的次方
14 tf.log(x, name=None)           # 以 e 為底,必須傳入浮點數或復數
15  
16  
17 # 取符號、負、倒數、絕對值、近似、兩數中較大/小的
18 tf.negative(x, name=None)      # 取負(y = -x).
19 tf.sign(x, name=None)          # 返回 x 的符號
20 tf.reciprocal(x, name=None)    # 取倒數
21 tf.abs(x, name=None)           # 求絕對值
22 tf.round(x, name=None)         # 四舍五入
23 tf.ceil(x, name=None)          # 向上取整
24 tf.floor(x, name=None)         # 向下取整
25 tf.rint(x, name=None)          # 取最接近的整數 
26 tf.maximum(x, y, name=None)    # 返回兩tensor中的最大值 (x > y ? x : y)
27 tf.minimum(x, y, name=None)    # 返回兩tensor中的最小值 (x < y ? x : y)
28  
29  
30 # 三角函數和反三角函數
31 tf.cos(x, name=None)    
32 tf.sin(x, name=None)    
33 tf.tan(x, name=None)    
34 tf.acos(x, name=None)
35 tf.asin(x, name=None)
36 tf.atan(x, name=None)   
37  
38  
39 # 其它
40 tf.div(x, y, name=None)  # python 2.7 除法, x/y-->int or x/float(y)-->float
41 tf.truediv(x, y, name=None) # python 3 除法, x/y-->float
42 tf.floordiv(x, y, name=None)  # python 3 除法, x//y-->int
43 tf.realdiv(x, y, name=None)
44 tf.truncatediv(x, y, name=None)
45 tf.floor_div(x, y, name=None)
46 tf.truncatemod(x, y, name=None)
47 tf.floormod(x, y, name=None)
48 tf.cross(x, y, name=None)
49 tf.add_n(inputs, name=None)  # inputs: A list of Tensor objects, each with same shape and type
50 tf.squared_difference(x, y, name=None) 

 

 矩陣數學函數

 1 # 矩陣乘法(tensors of rank >= 2)
 2 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)
 3  
 4 # 轉置,可以通過指定 perm=[1, 0] 來進行軸變換
 5 tf.transpose(a, perm=None, name='transpose')
 6  
 7 # 在張量 a 的最后兩個維度上進行轉置
 8 tf.matrix_transpose(a, name='matrix_transpose')
 9 # Matrix with two batch dimensions, x.shape is [1, 2, 3, 4]
10 # tf.matrix_transpose(x) is shape [1, 2, 4, 3]
11  
12 # 求矩陣的跡
13 tf.trace(x, name=None)
14  
15 # 計算方陣行列式的值
16 tf.matrix_determinant(input, name=None)
17  
18 # 求解可逆方陣的逆,input 必須為浮點型或復數
19 tf.matrix_inverse(input, adjoint=None, name=None)
20  
21 # 奇異值分解
22 tf.svd(tensor, full_matrices=False, compute_uv=True, name=None)
23  
24 # QR 分解
25 tf.qr(input, full_matrices=None, name=None)
26  
27 # 求張量的范數(默認2)
28 tf.norm(tensor, ord='euclidean', axis=None, keep_dims=False, name=None)
29  
30  
31 # 構建一個單位矩陣, 或者 batch 個矩陣,batch_shape 以 list 的形式傳入
32 tf.eye(num_rows, num_columns=None, batch_shape=None, dtype=tf.float32, name=None)
33 # Construct one identity matrix.
34 tf.eye(2)
35 ==> [[1., 0.],
36      [0., 1.]]
37  
38 # Construct a batch of 3 identity matricies, each 2 x 2.
39 # batch_identity[i, :, :] is a 2 x 2 identity matrix, i = 0, 1, 2.
40 batch_identity = tf.eye(2, batch_shape=[3])
41  
42 # Construct one 2 x 3 "identity" matrix
43 tf.eye(2, num_columns=3)
44 ==> [[ 1.,  0.,  0.],
45      [ 0.,  1.,  0.]]
46  
47  
48 # 構建一個對角矩陣,rank = 2*rank(diagonal)
49 tf.diag(diagonal, name=None)
50 # 'diagonal' is [1, 2, 3, 4]
51 tf.diag(diagonal) ==> [[1, 0, 0, 0]
52                        [0, 2, 0, 0]
53                        [0, 0, 3, 0]
54                        [0, 0, 0, 4]]
55  
56  
57 # 其它
58 tf.diag_part
59 tf.matrix_diag
60 tf.matrix_diag_part
61 tf.matrix_band_part
62 tf.matrix_set_diag
63 tf.cholesky
64 tf.cholesky_solve
65 tf.matrix_solve
66 tf.matrix_triangular_solve
67 tf.matrix_solve_ls
68 tf.self_adjoint_eig
69 tf.self_adjoint_eigvals

 

 Reduction:reduce various dimensions of a tensor

 1 # 計算輸入 tensor 所有元素的和,或者計算指定的軸所有元素的和
 2 tf.reduce_sum(input_tensor, axis=None, keep_dims=False, name=None)
 3 # 'x' is [[1, 1, 1]
 4 #         [1, 1, 1]]
 5 tf.reduce_sum(x) ==> 6
 6 tf.reduce_sum(x, 0) ==> [2, 2, 2]
 7 tf.reduce_sum(x, 1) ==> [3, 3]
 8 tf.reduce_sum(x, 1, keep_dims=True) ==> [[3], [3]]  # 維度不縮減
 9 tf.reduce_sum(x, [0, 1]) ==> 6
10  
11  
12 # 計算輸入 tensor 所有元素的均值/最大值/最小值/積/邏輯與/或
13 # 或者計算指定的軸所有元素的均值/最大值/最小值/積/邏輯與/或(just like reduce_sum)
14 tf.reduce_mean(input_tensor, axis=None, keep_dims=False, name=None)
15 tf.reduce_max(input_tensor, axis=None, keep_dims=False, name=None)
16 tf.reduce_min(input_tensor, axis=None, keep_dims=False, name=None)
17 tf.reduce_prod(input_tensor, axis=None, keep_dims=False, name=None)
18 tf.reduce_all(input_tensor, axis=None, keep_dims=False, name=None)  # 全部滿足條件
19 tf.reduce_any(input_tensor, axis=None, keep_dims=False, name=None) #至少有一個滿足條件
20  
21  
22 -------------------------------------------
23 # 分界線以上和 Numpy 中相應的用法完全一致
24 -------------------------------------------
25  
26  
27  
28 # inputs 為一 list, 計算 list 中所有元素的累計和,
29 # tf.add(x, y, name=None)只能計算兩個元素的和,此函數相當於擴展了其功能
30 tf.accumulate_n(inputs, shape=None, tensor_dtype=None, name=None)
31  
32  
33 # Computes log(sum(exp(elements across dimensions of a tensor)))
34 tf.reduce_logsumexp(input_tensor, axis=None, keep_dims=False, name=None)
35  
36  
37 # Computes number of nonzero elements across dimensions of a tensor
38 tf.count_nonzero(input_tensor, axis=None, keep_dims=False, name=None)

 

 Scan:perform scans (running totals) across one axis of a tensor

 1 # Compute the cumulative sum of the tensor x along axis
 2 tf.cumsum(x, axis=0, exclusive=False, reverse=False, name=None)
 3 # Eg:
 4 tf.cumsum([a, b, c])  # => [a, a + b, a + b + c]
 5 tf.cumsum([a, b, c], exclusive=True)  # => [0, a, a + b]
 6 tf.cumsum([a, b, c], reverse=True)  # => [a + b + c, b + c, c]
 7 tf.cumsum([a, b, c], exclusive=True, reverse=True)  # => [b + c, c, 0]
 8  
 9  
10 # Compute the cumulative product of the tensor x along axis
11 tf.cumprod(x, axis=0, exclusive=False, reverse=False, name=None)

 

 Segmentation

沿着第一維(x 軸)根據 segment_ids(list)分割好相應的數據后再進行操作

  

 1 # Computes the sum/mean/max/min/prod along segments of a tensor
 2 tf.segment_sum(data, segment_ids, name=None)
 3 # Eg:
 4 m = tf.constant([5,1,7,2,3,4,1,3])
 5 s_id = [0,0,0,1,2,2,3,3]
 6 s.run(tf.segment_sum(m, segment_ids=s_id))
 7 >array([13,  2,  7,  4], dtype=int32)
 8  
 9 tf.segment_mean(data, segment_ids, name=None)
10 tf.segment_max(data, segment_ids, name=None)
11 tf.segment_min(data, segment_ids, name=None)
12 tf.segment_prod(data, segment_ids, name=None)
13  
14  
15 # 其它
16 tf.unsorted_segment_sum
17 tf.sparse_segment_sum
18 tf.sparse_segment_mean
19 tf.sparse_segment_sqrt_n

 

 序列比較與索引提取

 1 # 比較兩個 list 或者 string 的不同,並返回不同的值和索引
 2 tf.setdiff1d(x, y, index_dtype=tf.int32, name=None)
 3  
 4  
 5 # 返回 x 中的唯一值所組成的tensor 和原 tensor 中元素在現 tensor 中的索引
 6 tf.unique(x, out_idx=None, name=None)
 7  
 8  
 9 # x if condition else y, condition 為 bool 類型的,可用tf.equal()等來表示
10 # x 和 y 的形狀和數據類型必須一致
11 tf.where(condition, x=None, y=None, name=None)
12  
13  
14 # 返回沿着坐標軸方向的最大/最小值的索引
15 tf.argmax(input, axis=None, name=None, output_type=tf.int64)
16 tf.argmin(input, axis=None, name=None, output_type=tf.int64)
17  
18  
19 # x 的值當作 y 的索引,range(len(x)) 索引當作 y 的值
20 # y[x[i]] = i for i in [0, 1, ..., len(x) - 1]
21 tf.invert_permutation(x, name=None)
22  
23  
24 # 其它
25 tf.edit_distance

 

 八、參考資料

  1、https://www.tensorflow.org/api_guides/python/math_ops

  2、TensorFlow 函數查詢列表_數值計算

 

3、

4、

5、

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM