欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

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

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

其中,tensor:被過濾的元素 mask:一堆bool值,它的維度不一定等于tensor return:mask為true對(duì)應(yīng)的tensor的元素 當(dāng)tensor與mask維度一致時(shí),返回一維

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)當(dāng)mask.shape=(m,n,p,q),結(jié)果返回(?,),表示所有維度都被過濾

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

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

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

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

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

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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論