Python - метод tensorflow.boolean_mask ()

Опубликовано: 29 Марта, 2022

TensorFlow - это библиотека Python с открытым исходным кодом, разработанная Google для разработки моделей машинного обучения и нейронных сетей глубокого обучения. boolean_mask () - это метод, используемый для применения логической маски к тензору.

Syntax: tensorflow.boolean_mask(tensor, mask, axis, name)

Parameters:

  • tensor: It’s a N-dimensional input tensor.
  • mask: It’s a boolean tensor with k-dimensions where k<=N and k  is know statically.
  • axis: It’s a 0-dimensional tensor which represets the axis from which mask should be applied. Default value for axis is zero and k+axis<=N.
  • name: It’s an optional parameter that defines the name for the operation.

Return: It returns (N-K+1)-dimensional tensor which have the values that are populated against the True values in mask.

Пример 1: В этом примере ввод 1-D.

Python3

# 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)

Выход:

 Тензор: [1, 2, 3]
Маска: [Ложь, Верно, Верно]
Результат: tf.Tensor ([2 3], shape = (2,), dtype = int32)

Example 2: In this example 2-D input is taken.

Python3

# 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)

Выход:

 Тензор: [[1, 2], [10, 14], [9, 7]]
Маска: [Ложь, Верно, Верно]
Результат: tf.Tensor (
[[10 14]
 [9 7]], shape = (2, 2), dtype = int32)

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.