TensorFlow2基本操作之合并分割與統(tǒng)計_第1頁
TensorFlow2基本操作之合并分割與統(tǒng)計_第2頁
TensorFlow2基本操作之合并分割與統(tǒng)計_第3頁
TensorFlow2基本操作之合并分割與統(tǒng)計_第4頁
TensorFlow2基本操作之合并分割與統(tǒng)計_第5頁
已閱讀5頁,還剩15頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

第TensorFlow2基本操作之合并分割與統(tǒng)計目錄合并與分割tf.concattf.stacktf.unstacktf.split數(shù)據(jù)統(tǒng)計tf.normreduce_min/max/meanargmax/argmintf.equaltf.unique

合并與分割

tf.concat

tf.concat可以幫助我們實現(xiàn)拼接操作.

格式:

tf.concat(

values,axis,name='concat'

參數(shù):

values:一個tensor或tensorlist

axis:操作的維度

name:數(shù)據(jù)名稱,默認(rèn)為“concat”

例子:

part_1=tf.zeros([5,3])

print(part_1)

part_2=tf.ones([5,3])

print(part_2)

#豎向拼接

result_1=tf.concat([part_1,part_2],axis=0)

print(result_1)

#橫向拼接

result_2=tf.concat([part_1,part_2],axis=1)

print(result_2)

輸出結(jié)果:

tf.Tensor(

[[0.0.0.]

[0.0.0.]

[0.0.0.]

[0.0.0.]

[0.0.0.]],shape=(5,3),dtype=float32)

tf.Tensor(

[[1.1.1.]

[1.1.1.]

[1.1.1.]

[1.1.1.]

[1.1.1.]],shape=(5,3),dtype=float32)

tf.Tensor(

[[0.0.0.]

[0.0.0.]

[0.0.0.]

[0.0.0.]

[0.0.0.]

[1.1.1.]

[1.1.1.]

[1.1.1.]

[1.1.1.]

[1.1.1.]],shape=(10,3),dtype=float32)

tf.Tensor(

[[0.0.0.1.1.1.]

[0.0.0.1.1.1.]

[0.0.0.1.1.1.]

[0.0.0.1.1.1.]

[0.0.0.1.1.1.]],shape=(5,6),dtype=float32)

tf.stack

rf.stack可以創(chuàng)建一個新的維度來合并兩個張量.

格式:

tf.stack(

values,axis=0,name='stack'

參數(shù):

values:一個tensorlist

axis:操作的維度

name:數(shù)據(jù)名稱,默認(rèn)為“stack”

例子:

part_1=tf.zeros([5,3])

print(part_1)

part_2=tf.ones([5,3])

print(part_2)

#頭拼接

result_1=tf.stack([part_1,part_2],axis=0)

print(result_1)

#尾拼接

result_2=tf.stack([part_1,part_2],axis=2)

print(result_2)

輸出結(jié)果:

tf.Tensor(

[[0.0.0.]

[0.0.0.]

[0.0.0.]

[0.0.0.]

[0.0.0.]],shape=(5,3),dtype=float32)

tf.Tensor(

[[1.1.1.]

[1.1.1.]

[1.1.1.]

[1.1.1.]

[1.1.1.]],shape=(5,3),dtype=float32)

tf.Tensor(

[[[0.0.0.]

[0.0.0.]

[0.0.0.]

[0.0.0.]

[0.0.0.]]

[[1.1.1.]

[1.1.1.]

[1.1.1.]

[1.1.1.]

[1.1.1.]]],shape=(2,5,3),dtype=float32)

tf.Tensor(

[[[0.1.]

[0.1.]

[0.1.]]

[[0.1.]

[0.1.]

[0.1.]]

[[0.1.]

[0.1.]

[0.1.]]

[[0.1.]

[0.1.]

[0.1.]]

[[0.1.]

[0.1.]

[0.1.]]],shape=(5,3,2),dtype=float32)

tf.unstack

tf.unstack是一個矩陣分解函數(shù).

格式:

tf.unstack(

value,num=None,axis=0,name='unstack'

)

參數(shù):

values:一個tensor,維度大于0

num:軸的長度

axis:操作的維度

name:數(shù)據(jù)名稱,默認(rèn)為“unstack”

例子:

a=tf.stack([tf.zeros([5,3]),tf.ones([5,3])],axis=0)

print(a)

b=tf.unstack(a,axis=0)

print(b)

輸出結(jié)果:

tf.Tensor(

[[[0.0.0.]

[0.0.0.]

[0.0.0.]

[0.0.0.]

[0.0.0.]]

[[1.1.1.]

[1.1.1.]

[1.1.1.]

[1.1.1.]

[1.1.1.]]],shape=(2,5,3),dtype=float32)

[tf.Tensor:shape=(5,3),dtype=float32,numpy=

array([[0.,0.,0.],

[0.,0.,0.],

[0.,0.,0.],

[0.,0.,0.],

[0.,0.,0.]],dtype=float32),tf.Tensor:shape=(5,3),dtype=float32,numpy=

array([[1.,1.,1.],

[1.,1.,1.],

[1.,1.,1.],

[1.,1.,1.],

[1.,1.,1.]],dtype=float32)]

tf.split

tf.split()可以把一個張量劃分為幾個子張量.

格式:

tf.split(

value,num_or_size_splits,axis=0,num=None,name='split'

參數(shù):

value:待切分的張量

num_or_size_splits:切成幾份

axis:操作的維度

num:num_or_size_splits不能實現(xiàn)的情況下使用

name:數(shù)據(jù)名稱,默認(rèn)為“split”

例子:

#split

a=tf.stack([tf.zeros([5,3]),tf.ones([5,3])],axis=0)

print(a)

b=tf.split(a,2)

print(b)

輸出結(jié)果:

tf.Tensor(

[[[0.0.0.]

[0.0.0.]

[0.0.0.]

[0.0.0.]

[0.0.0.]]

[[1.1.1.]

[1.1.1.]

[1.1.1.]

[1.1.1.]

[1.1.1.]]],shape=(2,5,3),dtype=float32)

[tf.Tensor:shape=(1,5,3),dtype=float32,numpy=

array([[[0.,0.,0.],

[0.,0.,0.],

[0.,0.,0.],

[0.,0.,0.],

[0.,0.,0.]]],dtype=float32),tf.Tensor:shape=(1,5,3),dtype=float32,numpy=

array([[[1.,1.,1.],

[1.,1.,1.],

[1.,1.,1.],

[1.,1.,1.],

[1.,1.,1.]]],dtype=float32)]

數(shù)據(jù)統(tǒng)計

tf.norm

tf.norm可以幫助我們計算向量,矩陣,張量的范數(shù).

格式:

tf.norm(

tensor,ord='euclidean',axis=None,keepdims=None,name=None

參數(shù):

tensor:輸入的張量

ord:范數(shù)的順序

axis:操作的維度

keep_dims:如果為True,則axis中指定的軸將保持為大小1

name:數(shù)據(jù)名稱

例子:

a=tf.fill([2,2],2.0)

print(a)

#sqrt(2^2*4)=sqrt(16)=4

b=tf.norm(a)

print(b)

#[2+2,2+2]=[4,4]

c=tf.norm(a,ord=1,axis=0)

print(c)

#[sqrt(2^2+2^2),sqrt(2^2+2^2)]=[sqrt(8),sqrt(8)]

d=tf.norm(a,ord=2,axis=0)

print(d)

輸出結(jié)果:

tf.Tensor(

[[2.2.]

[2.2.]],shape=(2,2),dtype=float32)

tf.Tensor(4.0,shape=(),dtype=float32)

tf.Tensor([4.4.],shape=(2,),dtype=float32)

tf.Tensor([2.8284272.828427],shape=(2,),dtype=float32)

reduce_min/max/mean

計算一個張量各個維度上元素的最小值/最大值/平均值.

格式:

tf.math.reduce_min/reduce_max/reduce_mean(

input_tensor,axis=None,keepdims=False,name=None

參數(shù):

input_tensor:傳入的張量

axis:維度,默認(rèn)計算所有維度

keepdims:如果為真保留維度,默認(rèn)為False

name:數(shù)據(jù)名稱

例子:

a=tf.reshape(tf.range(9),[3,3])

print(a)

min=tf.reduce_min(a)

print(min)

max=tf.reduce_max(a)

print(max)

輸出結(jié)果:

tf.Tensor(

[[012]

[345]

[678]],shape=(3,3),dtype=int32)

tf.Tensor(0,shape=(),dtype=int32)

tf.Tensor(8,shape=(),dtype=int32)

argmax/argmin

tf.argmax/tf.argmin可以幫我們找到最大/最小值所在的索引(index).

格式:

tf.math.argmax(

input,axis=None,output_type=64,name=None

參數(shù):

input:輸入

axis:操作的維度

output_type:輸出數(shù)據(jù)類型,默認(rèn)為int64

name:數(shù)據(jù)名稱

例子:

#argmax/argmin

a=tf.reshape(tf.range(9),[3,3])

print(a)

max=tf.argmax(a)

print(max)

min=tf.argmin(a)

print(min)

輸出結(jié)果:

tf.Tensor(

[[012]

[345]

[678]],shape=(3,3),dtype=int32)

tf.Tensor([222],shape=(3,),dtype=int64)

tf.Tensor([000],shape=(3,),dtype=int64)

tf.equal

tf.equal可以幫助我們判斷兩個張量是否相等.返回True/False.

格式:

tf.math.equal(

x,y,name=None

例子:

a=tf.zeros(5,dtype=tf.float32)

print(a)

b=tf.range(5,dtype=tf.float32)

print(b)

print(tf.equal(a,b))

輸出結(jié)果:

tf.Tensor([0.0.0.0.0.],shape=(5,),dtype=float32

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論