Python - tensorflow.math.count_nonzero ()
TensorFlow - это библиотека Python с открытым исходным кодом, разработанная Google для разработки моделей машинного обучения и нейронных сетей глубокого обучения.
count_nonzero () используется для подсчета количества ненулевых элементов в тензоре.
Syntax: tf.math.count_nonzero( input, axis, keepdim, dtype, name)
Parameters:
- input: It’s a Tensor that need to be reduced.
- axis(optional): It defines the axis along which input need to be reduced. Allowed range for this is [-rank(input), rank(input)). If no value is given then default is none i.e. input will be reduced along all axis.
- keepdim(optional): If it is true, it will retain the reduced dimensions with length 1.
- dtype(optional): It defines the output dtype. Default if int32.
- name(optional): It defines the name for the operation.
Returns:
It returns a tensor that contains the number of non-zero values.
Example 1:
Python3
# imporing the libraryimport tensorflow as tf # initializing the inputa = tf.constant([1,0,2,5,0], dtype = tf.int32) # 3 non-zero # Printing the inputprint("Input: ",a) # Counting non-zerores = tf.math.count_nonzero(a) # Printing the resultprint("No of non-zero elements: ",res) |
Выход:
Вход: tf.Tensor ([1 0 2 5 0], shape = (5,), dtype = int32). Нет ненулевых элементов: tf.Tensor (3, shape = (), dtype = int64)
Example 2: When input tensor is of type string, “” is considered as empty string. ” ” is non zero.
Python3
# imporing the libraryimport tensorflow as tf # initializing the inputa = tf.constant([""," ","a","b"]) # 3 non-zero # Printing the inputprint("Input: ",a) # Counting non-zerores = tf.math.count_nonzero(a) # Printing the resultprint("No of non-zero elements: ",res) |
Выход:
Ввод: tf.Tensor ([b '' b '' b'a 'b'b'], shape = (4,), dtype = string) Нет ненулевых элементов: tf.Tensor (3, shape = (), dtype = int64)
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.