Python – tensorflow.boolean_mask()方法
TensorFlow是谷歌设计的开源Python库,用于开发机器学习模型和深度学习神经网络。 boolean_mask()是用于对Tensor应用布尔掩码的方法。
语法: tensorflow.boolean_mask(tensor, mask, axis, name)
参数:
- tensor: 它是一个N维的输入张量。
- mask: 它是一个具有k维度的布尔张量,其中k<=N,k是静态的。
- axis: 它是一个0维的张量,代表应用掩码的轴。轴的默认值是0,k+轴<=N。
- name: 这是一个可选的参数,定义了操作的名称。
返回:它返回(N-K+1)维的张量,这些张量的值与掩码中的真值相对应。
例子1:在这个例子中,输入是1-D。
# importing the library import tensorflow as tf # initializing the inputs tensor = [1,2,3] mask = [False, True, True] # printing the input print('Tensor: ',tensor) print('Mask: ',mask) # applying the mask result = tf.boolean_mask(tensor, mask) # printing the result print('Result: ',result)
输出:
Tensor: [1, 2, 3] Mask: [False, True, True] Result: tf.Tensor([2 3], shape=(2,), dtype=int32)
例2:在这个例子中,采取的是2-D输入。
# importing the library import tensorflow as tf # initializing the inputs tensor = [[1, 2], [10, 14], [9, 7]] mask = [False, True, True] # printing the input print('Tensor: ',tensor) print('Mask: ',mask) # applying the mask result = tf.boolean_mask(tensor, mask) # printing the result print('Result: ',result)
输出:
Tensor: [[1, 2], [10, 14], [9, 7]] Mask: [False, True, True] Result: tf.Tensor( [[10 14] [ 9 7]], shape=(2, 2), dtype=int32)
版权声明:本页面内容旨在传播知识,为用户自行发布,若有侵权等问题请及时与本网联系,我们将第一时间处理。E-mail:284563525@qq.com