亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python中tf.boolean_mask()函數(shù)的使用方法詳解

 更新時間:2023年11月04日 09:52:13   作者:大彤小憶  
這篇文章主要介紹了python中tf.boolean_mask()函數(shù)的使用方法詳解,?tf.boolean_mask()?函數(shù)的作用是通過布爾值對指定的列的元素進行過濾,需要的朋友可以參考下

python中tf.boolean_mask()函數(shù)的使用

tf.boolean_mask() 函數(shù)的作用是通過布爾值對指定的列的元素進行過濾。

語法結(jié)構(gòu)

boolean_mask(tensor, mask, name="boolean_mask", axis=None)

其中,tensor:被過濾的元素 mask:一堆bool值,它的維度不一定等于tensor return:mask為true對應的tensor的元素 當tensor與mask維度一致時,返回一維

1-D example

import numpy as np
import tensorflow as tf
a = [1, 2, 3, 4]
mask = np.array([True, True, False, False])   # mask 與 a 維度相同
b = tf.boolean_mask(a, mask)
 with tf.Session() as sess:
 print(sess.run(b))
 print(b.shape)

[1 2]
(?,)

2-D example

import numpy as np
import tensorflow as tf
a = [[1, 2], [3, 4], [5, 6]]
mask = np.array([True, False, True])   # mask 與 a 維度不同
b = tf.boolean_mask(a, mask)
with tf.Session() as sess:
   print(sess.run(b))
   print(b.shape)

[[1 2]
[5 6]]
(?, 2)

3-D example

import numpy as np
import tensorflow as tf
a = tf.constant([
       [[2, 4], [4, 1]],
       [[6, 8], [2, 1]]], tf.float32)
mask = a > 2   # mask 與 a 維度相同
b = tf.boolean_mask(a, mask)
with tf.Session() as sess:
    print(sess.run(a))
    print(sess.run(mask))
    print(sess.run(b))
    print(b.shape)

[[[2. 4.]
[4. 1.]]

[[6. 8.]
[2. 1.]]]

[[[False True]
[ True False]]

[[ True True]
[False False]]]

[4. 4. 6. 8.]
(?,)

上面的shape有如下的規(guī)則: 假設(shè) tensor.rank=4,維度為(m,n,p,q),則

(1)當mask.shape=(m,n,p,q),結(jié)果返回(?,),表示所有維度都被過濾

(2)當mask.shape=(m,n,p),結(jié)果返回(?,q),表示 q 維度沒有過濾

(3)當mask.shape=(m,n),結(jié)果返回(?,p,q),表示 p,q 維度沒有過濾

(4)當mask.shape=(m),結(jié)果返回(?,n,p,q),表示 n,p,q 維度沒有過濾

tensorflow 使用一種叫tensor的數(shù)據(jù)結(jié)構(gòu)去展示所有的數(shù)據(jù),我們可以把tensor看成是n維的array或者list。在tensorflow的各部分圖形間流動傳遞的只能是tensor。

tensorflow用3種方式描述一個tensor的維數(shù):rank、shape、dimension number (維數(shù)),所以shape和rank的意思的一樣的,只是表達的形式不同。

rankshapedimension
0[ ]0 維
1[ D0 ]1 維
2[ D0, D1 ]2 維
n[ D0, D1, …, Dn-1 ]n 維

到此這篇關(guān)于python中tf.boolean_mask()函數(shù)的使用方法詳解的文章就介紹到這了,更多相關(guān)python的tf.boolean_mask()函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論