Python - метод Tensorflow math.accumulate_n ()

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

Tensorflow math.accumulate_n() method performs performs the element-wise sum of a list of passed tensors. The result is a tensor after performing the operation. The operation is done on the representation of a and b. This method belongs to math module.

Syntax: tf.math.accumulate_n( inputs, shape=None, tensor_dtype=None, name=None)

Arguments

  • inputs: This parameter takes a list of Tensor objects, and each of them with same shape and type.
  • shape: This is optional parameter and it defines the expected shape of elements of inputs.
  • dtype: This is optional parameter and it defines the expected data type of inputs.
  • name: This is optional parameter and this is the name of the operation.

Return: It returns a Tensor having the same shape and type as the elements of inputs.

Let’s see this concept with the help of few examples:
Example 1:

Выход:

Вход 1 Тензор ("Const_67: 0", shape = (2, 2), dtype = int32)
[[1 3]
 [6 7]]
Вход 2 Тензор ("Const_68: 0", shape = (2, 2), dtype = int32)
[[5 2]
 [3 8]]
Выход: Tensor ("AccumulateNV2_2: 0", shape = (2, 2), dtype = int32)
[[11 7]
 [12 23]]

Example 2:

# Importing the Tensorflow library 
import tensorflow as tf 
  
# A constant a and b
a = tf.constant([[2, 4], [1, 3]])
b = tf.constant([[5, 3], [4, 6]])  
  
# Applying the accumulate_n() function 
# storing the result in "c" 
c = tf.math.accumulate_n([b, a, b], shape =[2, 2], tensor_dtype = tf.int32)
  
# Initiating a Tensorflow session 
with tf.Session() as sess:
    print("Input 1", a)
    print(sess.run(a))
    print("Input 2", b)
    print(sess.run(b))
    print("Output: ", c)
    print(sess.run(c))

Выход:

Вход 1 Тензор ("Const_73: 0", shape = (2, 2), dtype = int32)
[[2 4]
 [1 3]]
Вход 2 Тензор ("Const_74: 0", shape = (2, 2), dtype = int32)
[[5 3]
 [4 6]]
Выход: Tensor ("AccumulateNV2_5: 0", shape = (2, 2), dtype = int32)
[[12 10]
 [9 15]]

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

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