tf API 研讀2:math

TF API數(shù)學(xué)計(jì)算
tf...... :math
(1)剛開(kāi)始先給一個(gè)運(yùn)行實(shí)例。
tf是基于圖(Graph)的計(jì)算系統(tǒng)。而圖的節(jié)點(diǎn)則是由操作(Operation)來(lái)構(gòu)成的,而圖的各個(gè)節(jié)點(diǎn)之間則是由張量(Tensor)作為邊來(lái)連接在一起的。所以Tensorflow的計(jì)算過(guò)程就是一個(gè)Tensor流圖。Tensorflow的圖則是必須在一個(gè)Session中來(lái)計(jì)算。

import tensorflow as tf  
import numpy as np  
#定義占位符,此處是一個(gè)二維的tensor;這個(gè)在之后構(gòu)建神經(jīng)時(shí)候,對(duì)于model輸入數(shù)據(jù)是要用到的。  
X = tf.placeholder(tf.float32, [None, 1])  
# 構(gòu)建圖。  
out  = tf.abs(X)。  
# 在session中啟動(dòng)圖。  
sess = tf.Session()  
# 給定輸入數(shù)據(jù),直接給定數(shù)組。  
# b = np.array([-23.51])  
# 給定的b是一維數(shù)組,注意下面要加[]  
# b_abs = sess.run(out, feed_dict={X: [b]})  
# 給定的輸入是一個(gè)列表。  
a = []  
a.append(12.3)  
a.append(-34.4)  
b = np.array(a)  
b = b.reshape([-1,1])  
b_abs = sess.run(out, feed_dict={X:b})  
print (a_abs)  
sess.close()  

注意多個(gè)Graph的使用:

import tensorflow as tf  
g1 = tf.Graph()  
with g1.as_default():  
    c1 = tf.constant([1.0])  
with tf.Graph().as_default() as g2:  
    c2 = tf.constant([2.0])  
  
with tf.Session(graph=g1) as sess1:  
    print sess1.run(c1)  
with tf.Session(graph=g2) as sess2:  
    print sess2.run(c2)  
  
# result:  
# [ 1.0 ]  
# [ 2.0 ]  

2)tf.a...API:
tensor可以是一維、二維、.....可以到多維
Arithmetic Operators

1.1 tf.add(x,y,name=None)

功能:對(duì)應(yīng)位置元素的加法運(yùn)算。
輸入:x,y具有相同尺寸的tensor,可以為half, float32, float64, uint8, int8, int16, int32, int64,
complex64, complex128, `string‘類型。
例:
x=tf.constant(1.0)
y=tf.constant(2.0)
z=tf.add(x,y)

z==>(3.0)
1.2 tf.subtract(x,y,name=None)

功能:對(duì)應(yīng)位置元素的減法運(yùn)算。
輸入:x,y具有相同尺寸的tensor,可以為half, float32, float64, int32, int64, complex64, complex128,
`string‘類型。
例:
x=tf.constant([[1.0,-1.0]],tf.float64)
y=tf.constant([[2.2,2.3]],tf.float64)
z=tf.subtract(x,y)

z==>[[-1.2,-3.3]]
1.3 tf.multiply(x,y,name=None)

功能:對(duì)應(yīng)位置元素的乘法運(yùn)算。
輸入:x,y具有相同尺寸的tensor,可以為half, float32, float64, uint8, int8, uint16,int16, int32, int64,
complex64, complex128, `string‘類型。
例:
x=tf.constant([[1.0,-1.0]],tf.float64)
y=tf.constant([[2.2,2.3]],tf.float64)
z=tf.multiply(x,y)

z==>[[2.2,-2.3]]
1.4 tf.scalar_mul(scalar,x)

功能:固定倍率縮放。
輸入:scalar必須為0維元素,x為tensor。
例:
scalar=2.2
x=tf.constant([[1.2,-1.0]],tf.float64)
z=tf.scalar_mul(scalar,x)

z==>[[2.64,-2.2]]
1.5 tf.div(x,y,name=None)[推薦使用tf.divide(x,y)]

功能:對(duì)應(yīng)位置元素的除法運(yùn)算(使用python2.7除法算法,如果x,y有一個(gè)為浮點(diǎn)數(shù),結(jié)果為浮點(diǎn)數(shù);否則為整數(shù),但使用該函數(shù)會(huì)報(bào)錯(cuò))。
輸入:x,y具有相同尺寸的tensor,x為被除數(shù),y為除數(shù)。
例:
x=tf.constant([[1,4,8]],tf.int32)
y=tf.constant([[2,3,3]],tf.int32)
z=tf.div(x,y)

z==>[[0,1,2]]

x=tf.constant([[1,4,8]],tf.int64)
y=tf.constant([[2,3,3]],tf.int64)
z=tf.divide(x,y)

z==>[[0.5,1.33333333,2.66666667]]

x=tf.constant([[1,4,8]],tf.float64)
y=tf.constant([[2,3,3]],tf.float64)
z=tf.div(x,y)

z==>[[0.5,1.33333333,2.66666667]]
1.6 tf.truediv(x,y,name=None)

功能:對(duì)應(yīng)位置元素的除法運(yùn)算。(使用python3除法算法,又叫真除,結(jié)果為浮點(diǎn)數(shù),推薦使用tf.divide)
輸入:x,y具有相同尺寸的tensor,x為被除數(shù),y為除數(shù)。
1.7 tf.floordiv(x,y,name=None)

功能:對(duì)應(yīng)位置元素的地板除法運(yùn)算。返回不大于結(jié)果的最大整數(shù)
輸入:x,y具有相同尺寸的tensor,x為被除數(shù),y為除數(shù)。
例:
x=tf.constant([[2,4,-1]],tf.int64) #float類型運(yùn)行結(jié)果一致,只是類型為浮點(diǎn)型
y=tf.constant([[3,3,3]],tf.int64)
z=tf.floordiv(x,y)

z==>[[0,1,-1]]
1.8 tf.realdiv(x,y,name=None)

功能:對(duì)應(yīng)位置元素的實(shí)數(shù)除法運(yùn)算。實(shí)際情況不非官方描述,與divide結(jié)果沒(méi)區(qū)別,
輸入:x,y具有相同尺寸的tensor,可以為half, float32, float64, uint8, int8, int16, int32, int64,
complex64, complex128, `string‘類型。
例:
x=tf.constant([[2+1j,4+2j,-1+3j]],tf.complex64)
y=tf.constant([[3+3j,3+1j,3+2j]],tf.complex64)
z=tf.realdiv(x,y)

z==>[[0.50000000-0.16666667j 1.39999998+0.2j 0.23076922+0.84615386j]]
1.9 tf.truncatediv(x,y,name=None)

功能:對(duì)應(yīng)位置元素的截?cái)喑ㄟ\(yùn)算,獲取整數(shù)部分。(和手冊(cè)功能描述不符,符號(hào)位并不能轉(zhuǎn)為0)
輸入:x,y具有相同尺寸的tensor,可以為uint8, int8, int16, int32, int64,類型。(只能為整型,浮點(diǎn)型等并未注冊(cè),和手冊(cè)不符)
例:
x=tf.constant([[2,4,-7]],tf.int64)
y=tf.constant([[3,3,3]],tf.int64)
z=tf.truncatediv(x,y)

z==>[[0 1 -2]]
1.10 tf.floor_div(x,y,name=None)

功能:對(duì)應(yīng)位置元素的地板除法運(yùn)算。(和tf.floordiv運(yùn)行結(jié)果一致,只是內(nèi)部實(shí)現(xiàn)方式不一樣)
輸入:x,y具有相同尺寸的tensor,可以為half, float32, float64, uint8, int8, int16, int32, int64,
complex64, complex128, `string‘類型。
1.11 tf.truncatemod(x,y,name=None)

功能:對(duì)應(yīng)位置元素的截?cái)喑ㄈ∮噙\(yùn)算。
輸入:x,y具有相同尺寸的tensor,可以為float32,float64,int32,int64`類型。
例:
x=tf.constant([[2.1,4.1,-1.1]],tf.float64)
y=tf.constant([[3,3,3]],tf.float64)
z=tf.truncatemod(x,y)

z==>[[2.1 1.1 -1.1]]
1.12 tf.floormod(x,y,name=None)

功能:對(duì)應(yīng)位置元素的地板除法取余運(yùn)算。
輸入:x,y具有相同尺寸的tensor,可以為float32,float64,int32,int64`類型。
例:
x=tf.constant([[2.1,4.1,-1.1]],tf.float64)
y=tf.constant([[3,3,3]],tf.float64)
z=tf.truncatemod(x,y)

z==>[[2.1 1.1 1.9]]
1.13 tf.mod(x,y,name=None)

功能:對(duì)應(yīng)位置元素的除法取余運(yùn)算。若x和y只有一個(gè)小于0,則計(jì)算‘floor(x/y)*y+mod(x,y)’。
輸入:x,y具有相同尺寸的tensor,可以為float32, float64, int32, int64類型。
例:
x=tf.constant([[2.1,4.1,-1.1]],tf.float64)
y=tf.constant([[3,3,3]],tf.float64)
z=tf.mod(x,y)

z==>[[2.1 1.1 1.9]]
1.14 tf.cross(x,y,name=None)

功能:計(jì)算叉乘。最大維度為3。
輸入:x,y具有相同尺寸的tensor,包含3個(gè)元素的向量
例:
x=tf.constant([[1,2,-3]],tf.float64)
y=tf.constant([[2,3,4]],tf.float64)
z=tf.cross(x,y)

z==>[[17. -10. -1]]#2×4-(-3)×3=17,-(1×4-(-3)×2)=-10,1×3-2×2=-1。
Basic Math Functions

1.15 tf.add_n(inputs,name=None)

功能:將所有輸入的tensor進(jìn)行對(duì)應(yīng)位置的加法運(yùn)算
輸入:inputs:一組tensor,必須是相同類型和維度。
例:
x=tf.constant([[1,2,-3]],tf.float64)
y=tf.constant([[2,3,4]],tf.float64)
z=tf.constant([[1,4,3]],tf.float64)
xyz=[x,y,z]
z=tf.add_n(xyz)

z==>[[4. 9. 4.]]
1.16 tf.abs(x,name=None)

功能:求x的絕對(duì)值。
輸入:x為張量或稀疏張量,可以為float32, float64, int32, int64類型。
例:
x=tf.constant([[1.1,2,-3]],tf.float64)
z=tf.abs(x)

z==>[[1.1 2. 3.]]
1.17 tf.negative(x,name=None)

功能:求x的負(fù)數(shù)。
輸入:x為張量或稀疏張量,可以為half,float32, float64, int32, int64,complex64,complex128類型。
例:
x=tf.constant([[1.1,2,-3]],tf.float64)
z=tf.negative(x)

z==>[[-1.1. -2. 3.]]
1.18 tf.sign(x,name=None)

功能:求x的符號(hào),x>0,則y=1;x<0則y=-1;x=0則y=0。
輸入:x,為張量,可以為half,float32, float64, int32, int64,complex64,complex128類型。
例:
x=tf.constant([[1.1,0,-3]],tf.float64)
z=tf.sign(x)

z==>[[1. 0. -1.]]
1.19 tf.reciprocal(x,name=None)

功能:求x的倒數(shù)。
輸入:x為張量,可以為half,float32, float64, int32, int64,complex64,complex128類型。
例:
x=tf.constant([[2,0,-3]],tf.float64)
z=tf.reciprocal(x)

z==>[[0.5 inf -0.33333333]]
1.20 tf.square(x,name=None)

功能:計(jì)算x各元素的平方。
輸入:x為張量或稀疏張量,可以為half,float32, float64, int32, int64,complex64,complex128類型。
例:
x=tf.constant([[2,0,-3]],tf.float64)
z=tf.square(x)

z==>[[4. 0. 9.]]
1.21 tf.round(x,name=None)

功能:計(jì)算x各元素的距離其最近的整數(shù),若在中間,則取偶數(shù)值。
輸入:x為張量,可以為float32, float64類型。
例:
x=tf.constant([[0.9,1.1,1.5,-4.1,-4.5,-4.9]],tf.float64)
z=tf.round(x)

z==>[[1. 1. 2. -4. -4. -5.]]
1.22 tf.sqrt(x,name=None)

功能:計(jì)算x各元素的平方。
輸入:x為張量,可以為half,float32, float64,complex64,complex128類型。
例:
x=tf.constant([[2,3,-5]],tf.float64)
z=tf.sqrt(x)

z==>[[1.41421356 1.73205081 nan]]
1.23 tf.rsqrt(x,name=None)

功能:計(jì)算x各元素的平方根的倒數(shù)。
輸入:x為張量或稀疏張量,可以為half,float32, float64,complex64,complex128類型。
例:
x=tf.constant([[2,3,5]],tf.float64)
z=tf.rsqrt(x)

z==>[[0.70710678 0.57735027 0.4472136]]
1.24 tf.pow(x,y,name=None)

功能:計(jì)算x各元素的y次方。
輸入:x,y為張量,可以為float32, float64, int32, int64,complex64,complex128類型。
例:
x=tf.constant([[2,3,5]],tf.float64)
y=tf.constant([[2,3,4]],tf.float64)
z=tf.pow(x,y)

z==>[[4. 27. 625.]]
1.25 tf.exp(x,name=None)

功能:計(jì)算x各元素的自然指數(shù),即e^x。
輸入:x為張量,可以為half,float32, float64,complex64,complex128類型。
例:
x=tf.constant([[0,1,-1]],tf.float64)
z=tf.exp(x)

z==>[[1. 2.71828183 0.36787944]]
1.26 tf.expm1(x,name=None)

功能:計(jì)算x各元素的自然指數(shù)減1,即e^x-1。
輸入:x為張量,可以為half,float32, float64,complex64,complex128類型。
例:
x=tf.constant([[0,1,-1]],tf.float64)
z=tf.expm1(x)

z==>[[0. 1.71828183 -0.63212056]]
1.27 tf.log(x,name=None)

功能:計(jì)算x各元素的自然對(duì)數(shù)。
輸入:x為張量,可以為half,float32, float64,complex64,complex128類型。
例:
x=tf.constant([[1,2.71828183,10]],tf.float64)
z=tf.log(x)

z==>[[0. 1. 2.30258509]]
1.28 tf.log1p(x,name=None)

功能:計(jì)算x各元素加1后的自然對(duì)數(shù)。
輸入:x為張量,可以為half,float32, float64,complex64,complex128類型。
例:
x=tf.constant([[0,1.71828183,9]],tf.float64)
z=tf.log1p(x)

z==>[[0. 1. 2.30258509]]
1.29 tf.ceil(x,name=None)

功能:計(jì)算x各元素比x大的最小整數(shù)。
輸入:x為張量,可以為half,float32, float64類型。
例:
x=tf.constant([[0.2,0.8,-0.7]],tf.float64)
z=tf.ceil(x)

z==>[[1. 1. -0.]]
1.30 tf.floor(x,name=None)

功能:計(jì)算x各元素比其小的最大整數(shù)。
輸入:x為張量,可以為half,float32, float64類型。
例:
x=tf.constant([[0.2,0.8,-0.7]],tf.float64)
z=tf.floor(x)

z==>[[0. 0. -1.]]
1.31 tf.maximum(x,y,name=None)

功能:計(jì)算x,y對(duì)應(yīng)位置元素較大的值。
輸入:x,y為張量,可以為half,float32, float64, int32, int64類型。
例:
x=tf.constant([[0.2,0.8,-0.7]],tf.float64)
y=tf.constant([[0.2,0.5,-0.3]],tf.float64)
z=tf.maximum(x,y)

z==>[[0.2 0.8 -0.3]]
1.32 tf.minimum(x,y,name=None)

功能:計(jì)算x,y對(duì)應(yīng)位置元素較小的值。
輸入:x,y為張量,可以為half,float32, float64, int32, int64類型。
例:
x=tf.constant([[0.2,0.8,-0.7]],tf.float64)
y=tf.constant([[0.2,0.5,-0.3]],tf.float64)
z=tf.maximum(x,y)

z==>[[0.2 0.5 -0.7]]
1.33 tf.cos(x,name=None)

功能:計(jì)算x的余弦值。
輸入:x為張量,可以為half,float32, float64, complex64, complex128類型。
例:
x=tf.constant([[0,3.1415926]],tf.float64)
z=tf.cos(x)

z==>[[1. -1.]]
1.34 tf.sin(x,name=None)

功能:計(jì)算x的正弦值。
輸入:x為張量,可以為half,float32, float64, complex64, complex128類型。
例:
x=tf.constant([[0,1.5707963]],tf.float64)
z=tf.sin(x)

z==>[[0. 1.]]
1.35 tf.lbeta(x,name=None)

功能:計(jì)算ln(|Beta(x)|),并以最末尺度進(jìn)行歸納。
最末尺度z = [z_0,...,z_{K-1}],則Beta(z) = \prod_j Gamma(z_j) / Gamma(\sum_j z_j)
輸入:x為秩為n+1的張量,可以為'float','double'類型。
例:
x=tf.constant([[4,3,3],[2,3,2]],tf.float64)
z=tf.lbeta(x)

z==>[-9.62377365 -5.88610403]

ln(gamma(4)gamma(3)gamma(3)/gamma(4+3+3))=ln(622/362880)=-9.62377365

ln(gamma(2)gamma(3)gamma(2)/gamma(2+3+2))=ln(2/720)=-5.88610403

1.36 tf.tan(x,name=None)

功能:計(jì)算tan(x)。
輸入:x為張量,可以為half,float32, float64, int32, int64,complex64, complex128類型。
例:
x=tf.constant([[0,0.785398163]],tf.float64)
z=tf.tan(x)

z==>[[0. 1.]]
1.37 tf.acos(x,name=None)

功能:計(jì)算acos(x)。
輸入:x為張量,可以為half,float32, float64, int32, int64,complex64, complex128類型。
例:
x=tf.constant([[0,1,-1]],tf.float64)
z=tf.acos(x)

z==>[[1.57079633 0. 3.14159265]]
1.38 tf.asin(x,name=None)

功能:計(jì)算asin(x)。
輸入:x為張量,可以為half,float32, float64, int32, int64,complex64, complex128類型。
例:
x=tf.constant([[0,1,-1]],tf.float64)
z=tf.asin(x)

z==>[[0. 1.57079633 -1.57079633]]
1.39 tf.atan(x,name=None)

功能:計(jì)算atan(x)。
輸入:x為張量,可以為half,float32, float64, int32, int64,complex64, complex128類型。
例:
x=tf.constant([[0,1,-1]],tf.float64)

z=tf.atan(x)

z==>[[0. 0.78539816 -0.78539816]]
1.40 tf.lgamma(x,name=None)

功能:計(jì)算ln(gamma(x))。
輸入:x為張量,可以為half,float32, float64類型。
例:
x=tf.constant([[1,2,3]],tf.float64)
z=tf.lgamma(x)

z==>[[0. 0. 0.69314718]]
1.41 tf.digamma(x,name=None)

功能:計(jì)算lgamma的導(dǎo)數(shù),即gamma‘/gamma。
輸入:x,y為張量,可以為half,float32, float64類型。
例:
x=tf.constant([[1,2,3]],tf.float64)
z=tf.digamma(x)

z==>[[-0.57721566 0.42278434 0.92278434]]
1.42 tf.erf(x,name=None)

功能:計(jì)算x的高斯誤差。
輸入:x為張量或稀疏張量,可以為half,float32, float64類型。
例:
x=tf.constant([[-1,0,1,2,3]],tf.float64)
z=tf.erf(x)

z==>[[-0.84270079 0. 0.84270079 0.99532227 0.99997791]]
1.43 tf.erfc(x,name=None)

功能:計(jì)算x高斯互補(bǔ)誤差。
輸入:x為張量,可以為half,float32, float64類型。
例:
x=tf.constant([[-1,0,1,2,3]],tf.float64)
z=tf.erfc(x)

z==>[[1.84270079 1.00000000 0.15729920 4.67773498e-03 2.20904970e-05]]
1.44 tf.squared_difference(x,y,name=None)

功能:計(jì)算(x-y)(x-y)。
輸入:x為張量,可以為half,float32, float64類型。
例:
x=tf.constant([[-1,0,2]],tf.float64)
y=tf.constant([[2,3,4,]],tf.float64)
z=tf.squared_difference(x,y)

z==>[[9. 9. 4.]]
1.45 tf.igamma(a,x,name=None)

功能:計(jì)算gamma(a,x)/gamma(a),gamma(a,x)=\intergral_from_0_to_x t(a-1)*exp(-t)dt。
輸入:x為張量,可以為float32, float64類型。
例:
a=tf.constant(1,tf.float64)
x=tf.constant([[1,2,3,4]],tf.float64)
z=tf.igamma(a,x)

z==>[[0.63212056 0.86466472 0.95021293 0.98168436]]
1.46 tf.igammac(a,x,name=None)

功能:計(jì)算gamma(a,x)/gamma(a),gamma(a,x)=\intergral_from_x_to_inf t(a-1)*exp(-t)dt。
輸入:x為張量,可以為float32, float64類型。
例:
x=tf.constant([[-1,0,1,2,3]],tf.float64)
z=tf.erf(x)

z==>[[-0.84270079 0. 0.84270079 0.99532227 0.99997791]]
1.47 tf.zeta(x,q,name=None)

功能:計(jì)算Hurwitz zeta函數(shù)。
輸入:x為張量或稀疏張量,可以為float32, float64類型。
例:
a=tf.constant(1,tf.float64)
x=tf.constant([[1,2,3,4]],tf.float64)
z=tf.zeta(x,a)

z==>[[inf 1.64493407 1.2020569 1.08232323]]
1.48 tf.polygamma(a,x,name=None)

功能:計(jì)算psi{(a)}(x),psi{(a)}(x) = ({da}/{dxa})*psi(x),psi即為polygamma。
輸入:x為張量,可以為float32, float64類型。a=tf.constant(1,tf.float64)
例:
x=tf.constant([[1,2,3,4]],tf.float64)z=tf.polygamma(a,x)

z==>[[1.64493407 0.64493407 0.39493407 0.28382296]]
1.49 tf.betainc(a,b,x,name=None)

功能:計(jì)算I_x(a, b)。I_x(a, b) = {B(x; a, b)}/{B(a, b)}。
B(x; a, b) = \intergral_from_0_to_x t^{a-1} (1 - t)^{b-1} dt。
B(a, b) = \intergral_from_0_to_1 t^{a-1} (1 - t)^{b-1} dt。即完全beta函數(shù)。
輸入:x為張量,可以為float32, float64類型。a,b與x同類型。
例:
a=tf.constant(1,tf.float64)b=tf.constant(1,tf.float64)x=tf.constant([[0,0.5,1]],tf.float64)

z==>[[0. 0.5 1.]]
1.50 tf.rint(x,name=None)

功能:計(jì)算離x最近的整數(shù),若為中間值,取偶數(shù)值。
輸入:x為張量,可以為half,float32, float64類型。
例:
x=tf.constant([[-1.7,-1.5,-1.1,0.1,0.5,0.4,1.5]],tf.float64)
z=tf.rint(x)

z==>[[-2. -2. -1. 0. 0. 0. 2.]]

矩陣數(shù)學(xué)函數(shù):
1.51 tf.diag(diagonal, name=None)

功能:返回對(duì)角陣。
輸入:tensor,秩為k<=3。
例:
a=tf.constant([1,2,3,4])
z=tf.diag(a)

z==>[[1 0 0 0
0 2 0 0
0 0 3 0
0 0 0 4]]
1.52 tf.diag_part(input,name=None)

功能:返回對(duì)角陣的對(duì)角元素。
輸入:tensor,且維度必須一致。
例:
a=tf.constant([[1,5,0,0],[0,2,0,0],[0,0,3,0],[0,0,0,4]])
z=tf.diag_part(a)

z==>[1,2,3,4]
1.53 tf.trace(x,name=None)

功能:返回矩陣的跡。
輸入:tensor
例:
a=tf.constant([[[1,2,3],[4,5,6],[7,8,9]],[[10,11,12],[13,14,15],[16,17,18]]])
z=tf.trace(a)

z==>[15 42]
1.54 tf.transpose(a,perm=None,name='transpose')

功能:矩陣轉(zhuǎn)置。
輸入:tensor,perm代表轉(zhuǎn)置后的維度排列,決定了轉(zhuǎn)置方法,默認(rèn)為[n-1,....,0],n為a的維度。
例:
a=tf.constant([[1,2,3],[4,5,6]])
z=tf.transpose(a)#perm為[1,0],即0維和1維互換。

z==>[[1 4]
[2 5]
[3 6]]
1.55 tf.eye(num_rows, num_columns=None, batch_shape=None, dtype=tf.float32, name=None)

功能:返回單位陣。
輸入:num_rows:矩陣的行數(shù);num_columns:矩陣的列數(shù),默認(rèn)與行數(shù)相等
batch_shape:若提供值,則返回batch_shape的單位陣。
例:
z=tf.eye(2,batch_shape=[2])

z==>[[[1. 0.]
[0. 1.]]
[[1. 0.]
[0. 1.]]]
1.56 tf.matrix_diag(diagonal,name=None)

功能:根據(jù)對(duì)角值返回一批對(duì)角陣
輸入:對(duì)角值
例:
a=tf.constant([[1,2,3],[4,5,6]])
z=tf.matrix_diag(a)

z==>[[[1 0 0]
[0 2 0]
[0 0 3]]
[[4 0 0]
[0 5 0]
[0 0 6]]]
1.57 tf.matrix_diag_part(input,name=None)

功能:返回批對(duì)角陣的對(duì)角元素
輸入:tensor,批對(duì)角陣
例:
a=tf.constant([[[1,3,0],[0,2,0],[0,0,3]],[[4,0,0],[0,5,0],[0,0,6]]])
z=tf.matrix_diag_part(a)

z==>[[1 2 3]
[4 5 6]]
1.58 tf.matrix_band_part(input,num_lower,num_upper,name=None)

功能:復(fù)制一個(gè)矩陣,并將規(guī)定帶之外的元素置為0。
假設(shè)元素坐標(biāo)為(m,n),則in_band(m, n) = (num_lower < 0 || (m-n) <= num_lower)) &&
(num_upper < 0 || (n-m) <= num_upper)。
band(m,n)=in_band(m,n)*input(m,n)。
特殊情況:
tf.matrix_band_part(input, 0, -1) ==> 上三角陣.
tf.matrix_band_part(input, -1, 0) ==> 下三角陣.
tf.matrix_band_part(input, 0, 0) ==> 對(duì)角陣.
輸入:num_lower:如果為負(fù),則結(jié)果右上空三角陣;
num_lower:如果為負(fù),則結(jié)果左下為空三角陣。
例:
a=tf.constant([[0,1,2,3],[-1,0,1,2],[-2,-1,0,1],[-3,-2,-1,0]])
z=tf.matrix_band_part(a,1,-1)

z==>[[0 1 2 3]
[-1 0 1 2]
[0 -1 0 1]
[0 0 -1 0]]
1.59 tf.matrix_set_diag(input,diagonal,name=None)

功能:將輸入矩陣的對(duì)角元素置換為對(duì)角元素。
輸入:input:矩陣,diagonal:對(duì)角元素。
例:
a=tf.constant([[0,1,2,3],[-1,0,1,2],[-2,-1,0,1],[-3,-2,-1,0]])
z=tf.matrix_set_diag(a,[10,11,12,13])

z==>[[10 1 2 3]
[-1 11 1 2]
[0 -1 12 1]
[0 0 -1 13]]
1.60 tf.matrix_transpose(a,name='matrix_transpose')

功能:進(jìn)行矩陣轉(zhuǎn)置。只對(duì)低維度的2維矩陣轉(zhuǎn)置,功能同tf.transpose(a,perm=[0,1,3,2])。(若a為4維)
輸入:矩陣。
1.61 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)

功能:矩陣乘法。配置后的矩陣a,b必須滿足矩陣乘法對(duì)行列的要求。
輸入:transpose_a,transpose_b:運(yùn)算前是否轉(zhuǎn)置;
adjoint_a,adjoint_b:運(yùn)算前進(jìn)行共軛;
a_is_sparse,b_is_sparse:a,b是否當(dāng)作稀疏矩陣進(jìn)行運(yùn)算。
例:
a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3])
b = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2])
z = tf.matmul(a, b)

z==>[[ 58 64]
[139 154]]
1.62 tf.norm(tensor, ord='euclidean', axis=None, keep_dims=False, name=None)

功能:求取范數(shù)。
輸入:ord:范數(shù)類型,默認(rèn)為‘euclidean’,支持的有‘fro’,‘euclidean’,‘0’,‘1’,‘2’,‘np.inf’;
axis:默認(rèn)為‘None’,tensor為向量。
keep_dims:默認(rèn)為‘None’,結(jié)果為向量,若為True,保持維度。
例:
a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3],dtype=tf.float32)
z = tf.norm(a)
z2=tf.norm(a,ord=1)
z3=tf.norm(a,ord=2)
z4=tf.norm(a,ord=1,axis=0)
z5=tf.norm(a,ord=1,axis=1)
z6=tf.norm(a,ord=1,axis=1,keep_dims=True)

z==>9.53939
z2==>21.0
z3==>9.53939
z4==>[5. 7. 9.]
z5==>[6. 15.]
z6==>[[6.]
[15.]]
1.63 tf.matrix_determinant(input, name=None)

功能:求行列式。
輸入:必須是float32,float64類型。
例:
a = tf.constant([1, 2, 3, 4],shape=[2,2],dtype=tf.float32)
z = tf.matrix_determinant(a)

z==>-2.0
1.64 tf.matrix_inverse(input, adjoint=None, name=None)

功能:求矩陣的逆。
輸入:輸入必須是float32,float64類型。adjoint表示計(jì)算前求轉(zhuǎn)置
例:
a = tf.constant([1, 2, 3, 4],shape=[2,2],dtype=tf.float64)
z = tf.matrix_inverse(a)

z==>[[-2. 1.]
[1.5 -0.5]]
1.65 tf.cholesky(input, name=None)

功能:進(jìn)行cholesky分解。
輸入:注意輸入必須是正定矩陣。
例:
a = tf.constant([2, -2, -2, 5],shape=[2,2],dtype=tf.float64)
z = tf.cholesky(a)

z==>[[ 1.41421356 0. ]
[-1.41421356 1.73205081]]
1.66 tf.cholesky_solve(chol, rhs, name=None)

功能:對(duì)方程‘AX=RHS’進(jìn)行cholesky求解。
輸入:chol=tf.cholesky(A)。
例:
a = tf.constant([2, -2, -2, 5],shape=[2,2],dtype=tf.float64)
chol = tf.cholesky(a)
RHS=tf.constant([3,10],shape=[2,1],dtype=tf.float64)
z=tf.cholesky_solve(chol,RHS)

z==>[[5.83333333]
[4.33333333]] #A*X=RHS
1.67 tf.matrix_solve(matrix, rhs, adjoint=None, name=None)

功能:求線性方程組,matrix*X=rhs。
輸入:adjoint:是否對(duì)matrix轉(zhuǎn)置。
例:
a = tf.constant([2, -2, -2, 5],shape=[2,2],dtype=tf.float64)
RHS=tf.constant([3,10],shape=[2,1],dtype=tf.float64)
z=tf.matrix_solve(a,RHS)

z==>[[5.83333333]
[4.33333333]]
1.68 tf.matrix_triangular_solve(matrix, rhs, lower=None, adjoint=None, name=None)

功能:求解matrix×X=rhs,matrix為上三角或下三角陣。
輸入:lower:默認(rèn)為None,matrix上三角元素為0;若為True,matrix下三角元素為0;
adjoint:轉(zhuǎn)置
例:
a = tf.constant([2, 4, -2, 5],shape=[2,2],dtype=tf.float64)
RHS=tf.constant([3,10],shape=[2,1],dtype=tf.float64)
z=tf.matrix_triangular_solve(a,RHS)

z==>[[1.5]
[2.6]]
1.69 tf.matrix_solve_ls(matrix, rhs, l2_regularizer=0.0, fast=True, name=None)

功能:求解多個(gè)線性方程的最小二乘問(wèn)題。
輸入:。
例:
a = tf.constant([2, 4, -2, 5],shape=[2,2],dtype=tf.float64)
RHS=tf.constant([3,10],shape=[2,1],dtype=tf.float64)
z=tf.matrix_solve_ls(a,RHS)

z==>[[-1.38888889]
[1.44444444]]
1.70 tf.qr(input, full_matrices=None, name=None)

功能:對(duì)矩陣進(jìn)行qr分解。
輸入:。
例:
a = tf.constant([1,2,2,1,0,2,0,1,1],shape=[3,3],dtype=tf.float64)
q,r=tf.qr(a)

q==>[[-0.70710678 0.57735027 -0.40824829]
[-0.70710678 -0.57735027 0.40824829]
[0. 0.57735027 0.81649658 ]]
r==>[[-1.41421356 -1.41421356 -2.82842712]
[0. 1.73205081 0.57735027]
[0. 0. 0.81649658]]
1.71 tf.self_adjoint_eig(tensor, name=None)

功能:求取特征值和特征向量。
輸入:
例:
a = tf.constant([3,-1,-1,3],shape=[2,2],dtype=tf.float64)
e,v=tf.self_adjoint_eig(a)

e==>[2. 4.]
v==>[[0.70710678 0.70710678]
[0.70710678 -0.70710678]]
1.72 tf.self_adjoint_eigvals(tensor, name=None)

功能:計(jì)算多個(gè)矩陣的特征值。
輸入:shape=[....,N,N]。
1.73 tf.svd(tensor, full_matrices=False, compute_uv=True, name=None)

功能:進(jìn)行奇異值分解。tensor=u×diag(s)×transpose(v)
輸入:
例:
a = tf.constant([3,-1,-1,3],shape=[2,2],dtype=tf.float64)
s,u,v=tf.svd(a)

s==>[4. 2.]
u==>[[0.70710678 0.70710678]
[-0.70710678 0.70710678]]
v==>[[0.70710678 0.70710678]
[-0.70710678 0.70710678]]

Tensor Math Function:

1.74 tf.tensordot(a, b, axes, name=None)

功能:同numpy.tensordot,根據(jù)axis計(jì)算點(diǎn)乘。
輸入:axes=1或axes=[[1],[0]],即為矩陣乘。
例:
a = tf.constant([1,2,3,4],shape=[2,2],dtype=tf.float64)
b = tf.constant([1,2,3,4],shape=[2,2],dtype=tf.float64)
z=tf.tensordot(a,b,axes=[[1],[1]])

z==>[[5. 11.]
[11. 25.]]
Complex Number Functions

1.75 tf.complex(real, imag, name=None)

功能:將實(shí)數(shù)轉(zhuǎn)化為復(fù)數(shù)。
輸入:real,imag:float32或float64。
例:
real = tf.constant([1,2],dtype=tf.float64)
imag = tf.constant([3,4],dtype=tf.float64)
z=tf.complex(real,imag)

z==>[1.+3.j 2.+4.j]
1.76 tf.conj(x, name=None)

功能:返回x的共軛復(fù)數(shù)。
輸入:
例:
a = tf.constant([1+2j,2-3j])
z=tf.conj(a)

z==>[1.-2.j 2.+3.j]
1.77 tf.imag(input, name=None)

功能:返回虛數(shù)部分。
輸入:complex64,complex128類型。
例:
a = tf.constant([1+2j,2-3j])
z=tf.imag(a)

z==>[2. -3.]
1.78 tf.real(input,name=None)

功能:返回實(shí)數(shù)部分。
輸入:complex64,complex128類型。
例:
a = tf.constant([1+2j,2-3j])
z=tf.real(a)

z==>[1. 2.]
1.79 ~1.84 fft變換

函數(shù):
tf.fft(input, name=None)
tf.ifft(input, name=None)
tf.fft2d(input, name=None)
tf.ifft2d(input, name=None)
tf.fft3d(input, name=None)
tf.ifft3d(input, name=None)
Reduction

1.85 tf.reduce_sum(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)

功能:沿著維度axis計(jì)算元素和,除非keep_dims=True,輸出tensor保持維度為1。
輸入:axis:默認(rèn)為None,即沿所有維度求和。
例:
a = tf.constant([[1,2,3],[4,5,6]])
z=tf.reduce_sum(a)
z2=tf.reduce_sum(a,0)
z3=tf.reduce_sum(a,1)

z==>21
z2==>[5 7 9]
z3==>[6 15]
1.86 tf.reduce_prod(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)

功能:沿著維度axis計(jì)算元素積,除非keep_dims=True,輸出tensor保持維度為1。
輸入:axis:默認(rèn)為None,即沿所有維度求和。
例:
a = tf.constant([[1,2,3],[4,5,6]])
z=tf.reduce_prod(a)
z2=tf.reduce_prod(a,0)
z3=tf.reduce_prod(a,1)

z==>720
z2==>[4 10 18]
z3==>[6 120]
1.87 tf.reduce_min(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)

功能:沿著維度axis計(jì)算最小值,除非keep_dims=True,輸出tensor保持維度為1。
輸入:axis:默認(rèn)為None,即沿所有維度求和。
例:
a = tf.constant([[1,2,3],[4,5,6]])
z=tf.reduce_min(a)
z2=tf.reduce_min(a,0)
z3=tf.reduce_min(a,1)

z==>1
z2==>[1 2 3]
z3==>[1 4]
1.88 tf.reduce_max(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)

功能:沿著維度axis計(jì)算最大值,除非keep_dims=True,輸出tensor保持維度為1。
輸入:axis:默認(rèn)為None,即沿所有維度求和。
例:
a = tf.constant([[1,2,3],[4,5,6]])
z=tf.reduce_max(a)
z2=tf.reduce_max(a,0)
z3=tf.reduce_max(a,1)

z==>6
z2==>[4 5 6]
z3==>[3 6]
1.89 tf.reduce_mean(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)

功能:沿著維度axis計(jì)算平均值,除非keep_dims=True,輸出tensor保持維度為1。
輸入:axis:默認(rèn)為None,即沿所有維度求和。
例:
a = tf.constant([[1,2,3],[4,5,6]],dtype=tf.float64)
z=tf.reduce_mean(a)
z2=tf.reduce_mean(a,0)
z3=tf.reduce_mean(a,1)

z==>3.5
z2==>[2.5 3.5 4.5]
z3==>[2. 5.]
1.90 tf.reduce_all(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)

功能:沿著維度axis計(jì)算邏輯與,除非keep_dims=True,輸出tensor保持維度為1。
輸入:axis:默認(rèn)為None,即沿所有維度求和。
例:
a = tf.constant([[True,True,False,False],[True,False,False,True]])
z=tf.reduce_all(a)
z2=tf.reduce_all(a,0)
z3=tf.reduce_all(a,1)

z==>False
z2==>[True False False False]
z3==>[False False]
1.91 tf.reduce_any(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)

功能:沿著維度axis計(jì)算邏輯或,除非keep_dims=True,輸出tensor保持維度為1。
輸入:axis:默認(rèn)為None,即沿所有維度求和。
例:
a = tf.constant([[True,True,False,False],[True,False,False,True]])
z=tf.reduce_any(a)
z2=tf.reduce_any(a,0)
z3=tf.reduce_any(a,1)

z==>True
z2==>[True True False True]
z3==>[True True]
1.92 tf.reduce_logsumexp(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)

功能:沿著維度axis計(jì)算log(sum(exp())),除非keep_dims=True,輸出tensor保持維度為1。
輸入:axis:默認(rèn)為None,即沿所有維度求和。
例:
a = tf.constant([[0,0,0],[0,0,0]],dtype=tf.float64)
z=tf.reduce_logsumexp(a)
z2=tf.reduce_logsumexp(a,0)
z3=tf.reduce_logsumexp(a,1)

z==>1.79175946923#log(6)
z2==>[0.69314718 0.69314718 0.69314718]#[log(2) log(2) log(2)]
z3==>[1.09861229 1.09861229]#[log(3) log(3)]
1.93 tf.count_nonzero(input_tensor, axis=None, keep_dims=False, dtype=tf.int64, name=None, reduction_indices=None)

功能:沿著維度axis計(jì)算非0個(gè)數(shù),除非keep_dims=True,輸出tensor保持維度為1。
輸入:axis:默認(rèn)為None,即沿所有維度求和。
例:
a = tf.constant([[0,0,0],[0,1,2]],dtype=tf.float64)
z=tf.count_nonzero(a)
z2=tf.count_nonzero(a,0)
z3=tf.count_nonzero(a,1)

z==>2
z2==>[0 1 1]
z3==>[0 2]
1.94 tf.accumulate_n(inputs, shape=None, tensor_dtype=None, name=None)

功能:對(duì)應(yīng)位置元素相加。如果輸入是訓(xùn)練變量,不要使用,應(yīng)使用tf.add_n。
輸入:shape,tensor_dtype:類型檢查
例:
a = tf.constant([[1,2],[3,4]])
b = tf.constant([[5,6],[7,8]])
z=tf.accumulate_n([a,b])

z==>[[6 8]
[10 12]]
1.95 tf.einsum(equation, *inputs)

功能:通過(guò)equation進(jìn)行矩陣乘法。
輸入:equation:乘法算法定義。

矩陣乘

einsum('ij,jk->ik', m0, m1) # output[i,k] = sum_j m0[i,j] * m1[j, k]

點(diǎn)乘

einsum('i,i->', u, v) # output = sum_i u[i]*v[i]

向量乘

einsum('i,j->ij', u, v) # output[i,j] = u[i]*v[j]

轉(zhuǎn)置

einsum('ij->ji', m) # output[j,i] = m[i,j]

批量矩陣乘

einsum('aij,ajk->aik', s, t) # out[a,i,k] = sum_j s[a,i,j] * t[a, j, k]
例:
a = tf.constant([[1,2],[3,4]])
b = tf.constant([[5,6],[7,8]])
z=tf.einsum('ij,jk->ik',a,b)

z==>[[19 22]
[43 50]]
Scan

1.96 tf.cumsum(x, axis=0, exclusive=False, reverse=False, name=None)

功能:沿著維度axis進(jìn)行累加。
輸入:axis:默認(rèn)為0
reverse:默認(rèn)為False,若為True,累加反向相反。
例:
a = tf.constant([[1,2,3],[4,5,6],[7,8,9]])
z=tf.cumsum(a)
z2=tf.cumsum(a,axis=1)
z3=tf.cumsum(a,reverse=True)

z==>[[1 2 3]
[5 7 9]
[12 15 18]]
z2==>[[1 3 6]
[4 9 15]
[7 15 24]]
z3==>[[12 15 18]
[11 13 15]
[7 8 9]]
1.97 tf.cumprod(x, axis=0, exclusive=False, reverse=False, name=None)

功能:沿著維度axis進(jìn)行累積。
輸入:axis:默認(rèn)為0
reverse:默認(rèn)為False,若為True,累加反向相反。
例:
a = tf.constant([[1,2,3],[4,5,6],[7,8,9]])
z=tf.cumprod(a)
z2=tf.cumprod(a,axis=1)
z3=tf.cumprod(a,reverse=True)

z==>[[ 1 2 3]
[ 5 10 18]
[28 80 162]]
z2==>[[ 1 2 6]
[ 4 20 120]
[ 7 56 504]]
z3==>[[ 28 80 162]
[ 28 40 54]
[ 7 8 9]]
Segmentation

1.98 tf.segment_sum(data, segment_ids, name=None)

功能:tensor進(jìn)行拆分后求和。
輸入:segment_ids:必須是整型,1維向量,向量數(shù)目與data第一維的數(shù)量一致。
必須從0開(kāi)始,且以1進(jìn)行遞增。
例:
a = tf.constant([[1,2,3],[4,5,6],[7,8,9]])
z=tf.segment_sum(a,[0,0,1])

z==>[[5 7 9]
[7 8 9]]
1.99 tf.segment_prod(data, segment_ids, name=None)

功能:tensor進(jìn)行拆分后求積。
輸入:segment_ids:必須是整型,1維向量,向量數(shù)目與data第一維的數(shù)量一致。
必須從0開(kāi)始,且以1進(jìn)行遞增。
例:
a = tf.constant([[1,2,3],[4,5,6],[7,8,9]])
z=tf.segment_prod(a,[0,0,1])

z==>[[4 10 18]
[7 8 9]]
1.100 tf.segment_min(data, segment_ids, name=None)

功能:tensor進(jìn)行拆分后求最小值。
輸入:segment_ids:必須是整型,1維向量,向量數(shù)目與data第一維的數(shù)量一致。
必須從0開(kāi)始,且以1進(jìn)行遞增。
例:
a = tf.constant([[1,2,3],[4,5,6],[7,8,9]])
z=tf.segment_min(a,[0,0,1])

z==>[[1 2 3]
[7 8 9]]
1.101 tf.segment_max(data, segment_ids, name=None)

功能:tensor進(jìn)行拆分后求最大值。
輸入:segment_ids:必須是整型,1維向量,向量數(shù)目與data第一維的數(shù)量一致。
必須從0開(kāi)始,且以1進(jìn)行遞增。
例:
a = tf.constant([[1,2,3],[4,5,6],[7,8,9]])
z=tf.segment_max(a,[0,0,1])

z==>[[4 5 6]
[7 8 9]]
1.102 tf.segment_mean(data, segment_ids, name=None)

功能:tensor進(jìn)行拆分后求平均值。
輸入:segment_ids:必須是整型,1維向量,向量數(shù)目與data第一維的數(shù)量一致。
必須從0開(kāi)始,且以1進(jìn)行遞增。
例:
a = tf.constant([[1,2,3],[4,5,6],[7,8,9]])
z=tf.segment_mean(a,[0,0,1])

z==>[[2 3 4]
[7 8 9]]
1.103 tf.unsorted_segment_sum(data, segment_ids, num_segments, name=None)

功能:tensor進(jìn)行拆分后求和。不同于sugementsum,segmentids不用按照順序排列
輸入:segment_ids:必須是整型,1維向量,向量數(shù)目與data第一維的數(shù)量一致。
num_segments:分類總數(shù),若多余ids匹配的數(shù)目,則置0。
例:
a = tf.constant([[1,2,3],[4,5,6],[7,8,9]])
z=tf.unsorted_segment_sum(a,[0,1,0],2)
z2=tf.unsorted_segment_sum(a,[0,0,0],2)

z==>[[8 10 12]
[4 5 6]]
z2==>[[12 15 18]
[0 0 0]]
1.104 tf.unsorted_segment_max(data, segment_ids, num_segments, name=None)

功能:tensor進(jìn)行拆分后求最大值。不同于sugementmax,segmentids不用按照順序排列
輸入:segment_ids:必須是整型,1維向量,向量數(shù)目與data第一維的數(shù)量一致。
num_segments:分類總數(shù),若多余ids匹配的數(shù)目,則置0。
例:
a = tf.constant([[1,2,3],[4,5,6],[7,8,9]])
z=tf.unsorted_segment_max(a,[0,1,0],2)
z2=tf.unsorted_segment_max(a,[0,0,0],2)

z==>[[8 10 12]
[4 5 6]]
z2==>[[12 15 18]
[0 0 0]]
1.105 tf.sparse_segment_sum(data, indices, segment_ids, name=None)

功能:tensor進(jìn)行拆分后求和。和segment_sum類似,只是segment_ids的rank數(shù)可以小于‘data’第0維度數(shù)。
輸入:indices:選擇第0維度參與運(yùn)算的編號(hào)。
例:
a = tf.constant([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
z=tf.sparse_segment_sum(a, tf.constant([0, 1]), tf.constant([0, 0]))
z2=tf.sparse_segment_sum(a, tf.constant([0, 1]), tf.constant([0, 1]))
z3=tf.sparse_segment_sum(a, tf.constant([0, 2]), tf.constant([0, 1]))
z4=tf.sparse_segment_sum(a, tf.constant([0, 1,2]), tf.constant([0, 0,1]))

z==>[[6 8 10 12]]
z2==>[[1 2 3 4]
[5 6 7 8]]
z3==>[[1 2 3 4]
[9 10 11 12]]
z4==>[[6 8 10 12]
[9 10 11 12]]
1.106 tf.sparse_segment_mean(data, indices, segment_ids, name=None)

功能:tensor進(jìn)行拆分后求平均值。和segment_mean類似,只是segment_ids的rank數(shù)可以小于‘data’第0維度數(shù)。
輸入:indices:選擇第0維度參與運(yùn)算的編號(hào)。
例:
a = tf.constant([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
z=tf.sparse_segment_mean(a, tf.constant([0, 1]), tf.constant([0, 0]))
z2=tf.sparse_segment_mean(a, tf.constant([0, 1]), tf.constant([0, 1]))
z3=tf.sparse_segment_mean(a, tf.constant([0, 2]), tf.constant([0, 1]))
z4=tf.sparse_segment_mean(a, tf.constant([0, 1,2]), tf.constant([0, 0,1]))

z==>[[3. 4. 5. 6.]]
z2==>[[1. 2. 3. 4.]
[5. 6. 7. 8.]]
z3==>[[1. 2. 3. 4.]
[9. 10. 11. 12.]]
z4==>[[3. 4. 5. 6.]
[9. 10. 11. 12.]]
1.107 tf.sparse_segment_sqrt_n(data, indices, segment_ids, name=None)

功能:tensor進(jìn)行拆分后求和再除以N的平方根。N為reduce segment數(shù)量。
和segment_mean類似,只是segment_ids的rank數(shù)可以小于‘data’第0維度數(shù)。
輸入:indices:選擇第0維度參與運(yùn)算的編號(hào)。
例:
a = tf.constant([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
z=tf.sparse_segment_sqrt_n(a, tf.constant([0, 1]), tf.constant([0, 0]))
z2=tf.sparse_segment_sqrt_n(a, tf.constant([0, 1]), tf.constant([0, 1]))
z3=tf.sparse_segment_sqrt_n(a, tf.constant([0, 2]), tf.constant([0, 1]))
z4=tf.sparse_segment_sqrt_n(a, tf.constant([0, 1,2]), tf.constant([0, 0,1]))

z==>[[4.24264069 5.65685424 7.07106781 8.48528137]]
z2==>[[1. 2. 3. 4.]
[5. 6. 7. 8.]]
z3==>[[1. 2. 3. 4.]
[9. 10. 11. 12.]]
z4==>[[4.24264069 5.65685424 7.07106781 8.48528137]
[9. 10. 11. 12.]]
Sequence Comparison and Indexing

1.108 tf.argmin(input, axis=None, name=None, dimension=None)

功能:返回沿axis維度最小值的下標(biāo)。
輸入:
例:
a = tf.constant([[1,2,3,4], [5,6,7,8], [9,10,11,12]],tf.float64)
z1=tf.argmin(a,axis=0)
z2=tf.argmin(a,axis=1)

z1==>[0 0 0 0]
z2==>[0 0 0]
1.109 tf.argmax(input, axis=None, name=None, dimension=None)

功能:返回沿axis維度最大值的下標(biāo)。
輸入:
例:
a = tf.constant([[1,2,3,4], [5,6,7,8], [9,10,11,12]],tf.float64)
z1=tf.argmin(a,axis=0)
z2=tf.argmax(a,axis=1)

z1==>[2 2 2 2]
z2==>[3 3 3]
1.110 tf.setdiff1d(x, y, index_dtype=tf.int32, name=None)

功能:返回在x里不在y里的元素值和下標(biāo),
輸入:
例:
a = tf.constant([1,2,3,4])
b=tf.constant([1,4])
out,idx=tf.setdiff1d(a,b)

out==>[2 3]
idx==>[1 2]
1.111 tf.where(condition, x=None, y=None, name=None)

功能:若x,y都為None,返回condition值為True的坐標(biāo);
若x,y都不為None,返回condition值為True的坐標(biāo)在x內(nèi)的值,condition值為False的坐標(biāo)在y內(nèi)的值
輸入:condition:bool類型的tensor;
例:
a=tf.constant([True,False,False,True])
x=tf.constant([1,2,3,4])
y=tf.constant([5,6,7,8])
z=tf.where(a)
z2=tf.where(a,x,y)

z==>[[0]
[3]]
z2==>[ 1 6 7 4]
1.112 tf.unique(x, out_idx=None, name=None)

功能:羅列非重復(fù)元素及其編號(hào)。
輸入:
例:
a = tf.constant([1,1,2,4,4,4,7,8,9,1])
y,idx=tf.unique(a)

y==>[1 2 4 7 8 9]
idx==>[0 0 1 2 2 2 3 4 5 0]
1.113 tf.edit_distance(hypothesis, truth, normalize=True, name='edit_distance')

功能:計(jì)算Levenshtein距離。
輸入:hypothesis:'SparseTensor';
truth:'SparseTensor'.
例:
hypothesis = tf.SparseTensor(
[[0, 0, 0],
[1, 0, 0]],
["a", "b"],
(2, 1, 1))
truth = tf.SparseTensor(
[[0, 1, 0],
[1, 0, 0],
[1, 0, 1],
[1, 1, 0]],
["a", "b", "c", "a"],
(2, 2, 2))
z=tf.edit_distance(hypothesis,truth)

z==>[[inf 1.]
[0.5 1.]]
1.114 tf.invert_permutation(x, name=None)

功能:轉(zhuǎn)換坐標(biāo)與值。y(i)=x(i)的坐標(biāo) (for i in range (lens(x))。
輸入:
例:
a=tf.constant([3,4,0,2,1])
z=tf.invert_permutation(a)

z==>[2 4 3 0 1]

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容