Python - tensorflow.math.cumprod ()

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

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

Syntax: tensorflow.math.cumprod(   x, axis, exclusive, reverse, name)

Parameters: 

  • x: It’s the input tensor. Allowed dtype for this tensor are  float32, float64, int64, int32, uint8, uint16, int16, int8, complex64, complex128, qint8, quint8, qint32, half.
  • axis(optional): It’s a tensor of type int32. It’s value should  be in the range  A Tensor of type int32 (default: 0). Must be in the range [-rank(x), rank(x)).  Default value is 0.
  • exclusive(optional): It’s of type bool. Default value is False and if set to true then the output for input [a, b, c] will be [1, a, a*b].
  • reverse(optional): It’s of type bool. Default value is False and if set to true then the output for input [a, b, c] will be [a*b*c, a*b, a].
  • name(optional): It’s defines the name for the operation.

Returns: It returns a tensor of same dtype as x.

Пример 1:

Python3

# imporing the library
import tensorflow as tf
  
# initializing the input
a = tf.constant([1, 2, 4, 5], dtype = tf.int32)  
  
# Printing the input
print("Input: ",a)
  
# Cumulative product
res  = tf.math.cumprod(a)
  
# Printing the result
print("Output: ",res)

Выход:

 Вход: tf.Tensor ([1 2 4 5], shape = (4,), dtype = int32)
Вывод: tf.Tensor ([1 2 8 40], shape = (4,), dtype = int32)

Example 2: In this example both reverse and exclusive are set to True.

Python3

# imporing the library
import tensorflow as tf
  
# initializing the input
a = tf.constant([2, 3, 4, 5], dtype = tf.int32)  
  
# Printing the input
print("Input: ",a)
  
# Cumulative product
res  = tf.math.cumprod(a, reverse = True, exclusive = True)
  
# Printing the result
print("Output: ",res)

Выход:

 Вход: tf.Tensor ([2 3 4 5], shape = (4,), dtype = int32)
Вывод: tf.Tensor ([60 20 5 1], shape = (4,), dtype = int32)

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

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