TensorFLow 數學運算
一、Tensor 之間的運算規則
- 相同大小 Tensor 之間的任何算術運算都會將運算應用到元素級
- 不同大小 Tensor(要求dimension 0 必須相同) 之間的運算叫做廣播(broadcasting)
- Tensor 與 Scalar(0維 tensor) 間的算術運算會將那個標量值傳播到各個元素
- Note: TensorFLow 在進行數學運算時,一定要求各個 Tensor 資料型別一致
二、常用操作符和基本數學函式
大多數運算子都進行了過載操作,使我們可以快速使用 (+ - * /) 等,但是有一點不好的是使用過載操作符後就不能為每個操作命名了。
# 算術操作符:+ - * / %
tf.add(x, y, name=None ) # 加法(支援 broadcasting)
tf.subtract(x, y, name=None) # 減法
tf.multiply(x, y, name=None) # 乘法
tf.divide(x, y, name=None) # 浮點除法, 返回浮點數(python3 除法)
tf.mod(x, y, name=None) # 取餘
# 冪指對數操作符:^ ^2 ^0.5 e^ ln
tf.pow(x, y, name=None) # 冪次方
tf.square(x, name=None) # 平方
tf.sqrt(x, name=None ) # 開根號,必須傳入浮點數或複數
tf.exp(x, name=None) # 計算 e 的次方
tf.log(x, name=None) # 以 e 為底,必須傳入浮點數或複數
# 取符號、負、倒數、絕對值、近似、兩數中較大/小的
tf.negative(x, name=None) # 取負(y = -x).
tf.sign(x, name=None) # 返回 x 的符號
tf.reciprocal(x, name=None) # 取倒數
tf.abs(x, name=None) # 求絕對值
tf.round(x, name=None) # 四捨五入
tf.ceil(x, name=None) # 向上取整
tf.floor(x, name=None) # 向下取整
tf.rint(x, name=None) # 取最接近的整數
tf.maximum(x, y, name=None) # 返回兩tensor中的最大值 (x > y ? x : y)
tf.minimum(x, y, name=None) # 返回兩tensor中的最小值 (x < y ? x : y)
# 三角函式和反三角函式
tf.cos(x, name=None)
tf.sin(x, name=None)
tf.tan(x, name=None)
tf.acos(x, name=None)
tf.asin(x, name=None)
tf.atan(x, name=None)
# 其它
tf.div(x, y, name=None) # python 2.7 除法, x/y-->int or x/float(y)-->float
tf.truediv(x, y, name=None) # python 3 除法, x/y-->float
tf.floordiv(x, y, name=None) # python 3 除法, x//y-->int
tf.realdiv(x, y, name=None)
tf.truncatediv(x, y, name=None)
tf.floor_div(x, y, name=None)
tf.truncatemod(x, y, name=None)
tf.floormod(x, y, name=None)
tf.cross(x, y, name=None)
tf.add_n(inputs, name=None) # inputs: A list of Tensor objects, each with same shape and type
tf.squared_difference(x, y, name=None)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
三、矩陣數學函式
# 矩陣乘法(tensors of rank >= 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)
# 轉置,可以通過指定 perm=[1, 0] 來進行軸變換
tf.transpose(a, perm=None, name='transpose')
# 在張量 a 的最後兩個維度上進行轉置
tf.matrix_transpose(a, name='matrix_transpose')
# Matrix with two batch dimensions, x.shape is [1, 2, 3, 4]
# tf.matrix_transpose(x) is shape [1, 2, 4, 3]
# 求矩陣的跡
tf.trace(x, name=None)
# 計算方陣行列式的值
tf.matrix_determinant(input, name=None)
# 求解可逆方陣的逆,input 必須為浮點型或複數
tf.matrix_inverse(input, adjoint=None, name=None)
# 奇異值分解
tf.svd(tensor, full_matrices=False, compute_uv=True, name=None)
# QR 分解
tf.qr(input, full_matrices=None, name=None)
# 求張量的範數(預設2)
tf.norm(tensor, ord='euclidean', axis=None, keep_dims=False, name=None)
# 構建一個單位矩陣, 或者 batch 個矩陣,batch_shape 以 list 的形式傳入
tf.eye(num_rows, num_columns=None, batch_shape=None, dtype=tf.float32, name=None)
# Construct one identity matrix.
tf.eye(2)
==> [[1., 0.],
[0., 1.]]
# Construct a batch of 3 identity matricies, each 2 x 2.
# batch_identity[i, :, :] is a 2 x 2 identity matrix, i = 0, 1, 2.
batch_identity = tf.eye(2, batch_shape=[3])
# Construct one 2 x 3 "identity" matrix
tf.eye(2, num_columns=3)
==> [[ 1., 0., 0.],
[ 0., 1., 0.]]
# 構建一個對角矩陣,rank = 2*rank(diagonal)
tf.diag(diagonal, name=None)
# '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]]
# 其它
tf.diag_part
tf.matrix_diag
tf.matrix_diag_part
tf.matrix_band_part
tf.matrix_set_diag
tf.cholesky
tf.cholesky_solve
tf.matrix_solve
tf.matrix_triangular_solve
tf.matrix_solve_ls
tf.self_adjoint_eig
tf.self_adjoint_eigvals
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
四、Reduction:reduce various dimensions of a tensor
# 計算輸入 tensor 所有元素的和,或者計算指定的軸所有元素的和
tf.reduce_sum(input_tensor, axis=None, keep_dims=False, name=None)
# 'x' is [[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, keep_dims=True) ==> [[3], [3]] # 維度不縮減
tf.reduce_sum(x, [0, 1]) ==> 6
# 計算輸入 tensor 所有元素的均值/最大值/最小值/積/邏輯與/或
# 或者計算指定的軸所有元素的均值/最大值/最小值/積/邏輯與/或(just like reduce_sum)
tf.reduce_mean(input_tensor, axis=None, keep_dims=False, name=None)
tf.reduce_max(input_tensor, axis=None, keep_dims=False, name=None)
tf.reduce_min(input_tensor, axis=None, keep_dims=False, name=None)
tf.reduce_prod(input_tensor, axis=None, keep_dims=False, name=None)
tf.reduce_all(input_tensor, axis=None, keep_dims=False, name=None) # 全部滿足條件
tf.reduce_any(input_tensor, axis=None, keep_dims=False, name=None) #至少有一個滿足條件
-------------------------------------------
# 分界線以上和 Numpy 中相應的用法完全一致
-------------------------------------------
# inputs 為一 list, 計算 list 中所有元素的累計和,
# tf.add(x, y, name=None)只能計算兩個元素的和,此函式相當於擴充套件了其功能
tf.accumulate_n(inputs, shape=None, tensor_dtype=None, name=None)
# Computes log(sum(exp(elements across dimensions of a tensor)))
tf.reduce_logsumexp(input_tensor, axis=None, keep_dims=False, name=None)
# Computes number of nonzero elements across dimensions of a tensor
tf.count_nonzero(input_tensor, axis=None, keep_dims=False, name=None)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
五、Scan:perform scans (running totals) across one axis of a tensor
# Compute the cumulative sum of the tensor x along axis
tf.cumsum(x, axis=0, exclusive=False, reverse=False, name=None)
# Eg:
tf.cumsum([a, b, c]) # => [a, a + b, a + b + c]
tf.cumsum([a, b, c], exclusive=True) # => [0, a, a + b]
tf.cumsum([a, b, c], reverse=True) # => [a + b + c, b + c, c]
tf.cumsum([a, b, c], exclusive=True, reverse=True) # => [b + c, c, 0]
# Compute the cumulative product of the tensor x along axis
tf.cumprod(x, axis=0, exclusive=False, reverse=False, name=None)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
六、Segmentation
沿著第一維(x 軸)根據 segment_ids(list)分割好相應的資料後再進行操作
# Computes the sum/mean/max/min/prod along segments of a tensor
tf.segment_sum(data, segment_ids, name=None)
# Eg:
m = tf.constant([5,1,7,2,3,4,1,3])
s_id = [0,0,0,1,2,2,3,3]
s.run(tf.segment_sum(m, segment_ids=s_id))
>array([13, 2, 7, 4], dtype=int32)
tf.segment_mean(data, segment_ids, name=None)
tf.segment_max(data, segment_ids, name=None)
tf.segment_min(data, segment_ids, name=None)
tf.segment_prod(data, segment_ids, name=None)
# 其它
tf.unsorted_segment_sum
tf.sparse_segment_sum
tf.sparse_segment_mean
tf.sparse_segment_sqrt_n
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
七、 序列比較與索引提取
# 比較兩個 list 或者 string 的不同,並返回不同的值和索引
tf.setdiff1d(x, y, index_dtype=tf.int32, name=None)
# 返回 x 中的唯一值所組成的tensor 和原 tensor 中元素在現 tensor 中的索引
tf.unique(x, out_idx=None, name=None)
# x if condition else y, condition 為 bool 型別的,可用tf.equal()等來表示
# x 和 y 的形狀和資料型別必須一致
tf.where(condition, x=None, y=None, name=None)
# 返回沿著座標軸方向的最大/最小值的索引
tf.argmax(input, axis=None, name=None, output_type=tf.int64)
tf.argmin(input, axis=None, name=None, output_type=tf.int64)
# x 的值當作 y 的索引,range(len(x)) 索引當作 y 的值
# y[x[i]] = i for i in [0, 1, ..., len(x) - 1]
tf.invert_permutation(x, name=None)
# 其它
tf.edit_distance
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
八、參考資料
http://blog.csdn.net/mzpmzk/article/details/77337851
相關推薦
TensorFLow 數學運算
一、Tensor 之間的運算規則 相同大小 Tensor 之間的任何算術運算都會將運算應用到元素級不同大小 Tensor(要求dimension 0 必須相同) 之間的運算叫做廣播(broadcasting)Tensor 與 Scalar(0維 tensor) 間的算術運
學習筆記(5)---數學運算
mat tla fix bsp matlab 循環 支持 -- oot 一.開n次方 比如-8的立方根,用nthroot(-8,-3),不建議用(-8)^(1/3) 二. 乘除 向0取整數:clear all;clc;fix(7/2)ans = 3-----------
Shell腳本(二)數學運算
style code ech 數學 代碼 incr ++ echo pre 直接上代碼。 #!/bin/bash no1=4 no2=5 echo "using let ..." let result=no1+no2 echo "result is: ${result
當VR踏入足球賽事會是如何?用數學運算又是如何?
足球 數學 運算 vr 虛擬實境 我們可以一起想像一下開賽後第一球進網時,所有人的VR 裝置全切換到進球球員的視角,剎那間觀眾從前鋒眼中看見球門,接著畫面切換至守門員視角,最後停在球門後方視野。沒有VR 裝置的觀眾則透過智能型手機投影3D 全像,回放射門精彩畫面;而當球員沖向場邊與球迷一同
day01_05.數學運算符
lan com 答案 希望 運算符 .com target php ref 數學運算符 $zhang = 100; $lisi = 50; echo $zhang+$lisi; 答案:150 $zhang = 50; $lisi = 40; echo $zh
Python學習21:Python中函數的用法,使用函數進行簡單的數學運算
Python 函數 錯誤 今天學習了Python函數的用法,了解了使用Python如何定義一個函數。而且代碼編寫過程中也遇到了一些小小的錯誤,特此記錄一下,以方便以後在遇到同樣錯誤時能夠快速找到問題的點。 # --coding: utf-8 -- # 定義4個簡單的函數,分別是加、減、乘、除,定義
行測(爆發篇)之數學運算怎能輕言放棄?!
經濟 規律 全部 自己 概率問題 排列 分享 我們 不能 一、必考的行程問題 1、行程問題的簡單考法 【註】上下坡問題其實就是等距離求平均速度的問題。 2、相遇問題 直線相遇 環線相遇
理解數據類型與數學運算:求和、溫度轉換
style div 輸入 pan pre 輸入一個數 sum 兩個 求和 a = input(‘請輸入一個數‘) b = input(‘請輸入第二個數‘) sum2=int(a) + int(b) print(‘兩個數的和是:{}‘.format(sum2)) a
理解數據類型與數學運算
sum 轉換 ima img pre inpu .com pri format n1=input(‘請輸入第一個數:‘) n2=input(‘請輸入第二個數:‘) sum2=int(n1)+int(n2) print(‘兩個數的和是:{}‘.format(sum2))
解數據類型與數學運算:求和、溫度轉換
數學運算 華氏溫度 color nbsp 溫度 求和 mat 轉換 數據類型 c = input(‘請輸入攝氏溫度:‘) f = float(c)*9/5+32 print(‘{}攝氏溫度轉為華氏溫度是{}‘.format(c,f)) 解數據類型與數學運算:求和、溫度
理解數據類型與數學運算:求和、溫度轉換2
pan style alt 數學運算 inpu int com img png a = int(input(‘攝氏溫度轉換為華氏溫度請按 1\n華氏溫度轉換為攝氏溫度請按 2\n‘)) if a==1: c = float(input(‘請輸入攝氏溫度:‘))
理解數據類型與數學運算。求和、溫度轉換。
一個數 http src PE sum str image format 輸入一個數 >>> a=input(‘請輸入一個數‘) 請輸入一個數5 >>> print(a) 5 >>> a=input(‘請輸入一個數;‘
R: 基本的數學運算
TP 一個 uniq 並集 ron 存在 AR 相等 tro ################################################### 問題:基本數學運算 18.4.30 R語言用於初等數學的計算,都怎麽表示??加減乘除、
Julia - 數學運算
python 值類型 按位異或 運算 基礎 復制 算術 基本 == 算術運算符 算術運算符適用於所有的基本數值類型 +x,一元加法,就是 x 本身 -x,一元減法,x 的相反數 x + y,二元加法,做加法運算 x - y,二元減法,做減法運算 x * y,乘法
【Linux】shell數學運算
變量名 right 浮點型 border 測試 solid 操作符 font pad 在Bash shell環境中,可以利用let、(())和[]執行基本的算術操作。而在進行高級操作時,expr和bc這兩個工具就特別有用 let的使用 Script01.sh #!/bin/
Linux編程 22 shell編程(輸出和輸入重定向,管道,數學運算命令,退出腳本狀態碼)
tab test $? per width 可能 註意 ble 保存 1. 輸出重定向 最基本的重定向是將命令的輸出發送到一個文件中。在bash shell中用大於號(>) ,格式如下:command > inputfile。例如:將date命令的輸出內容,
10.22 使用DateUtils對日期進行數學運算
span mat lan 分享圖片 pid pen play -- ons 1.1、pom <dependency> <groupId>org.apache.commons</groupId> <artifactId&g
C++ 數學運算, <cmath>
C++ 數學運算 在 C++ 中,除了可以建立各種函式,還包含了各種有用的函式供您使用。這些函式寫在標準 C 和 C++ 庫中,叫做內建函式。您可以在程式中引用這些函式。 C++ 內建了豐富的數學函式,可對各種數字進行運算。下表列出了 C++ 中一些有用的內建的數學函式。 為了利用這
Python: 複數的數學運算
寫的最新的網路認證方案程式碼遇到了一個難題,唯一的解決辦法就是使用複數空間,需要使用複數來執行一些計算操作。 複數可以用使用函式complex(real, imag) 或者是帶有後綴j 的浮點數來指定。比如: >>> a = complex(2, 4) >>> b
linux shell 指令碼【2】 ---- 數學運算
在Bash shell環境中,可以利用 let、 (()) 、 [ ]、 expr 、 bc 進行數學運算 用法直接上程式碼,如下: #! /bin/bash echo "數學運算篇 START" #基本運算----let a=5; b=4; echo "a = $a