Find the sum of vectors

Let's use Perl to find the sum of the vectors required for deep learning calculations.

In Perl, vectors are represented as arrays. I think that software engineers will have a headache when they hear the word "vector", but when they say "array", it means "what?".

A Perl program that finds the sum of vectors.

use strict;
use warnings;

#Sum of vectors
sub vec_add {
  my ($vec1, $vec2) = @_;

  my $vec_add = [];

  for (my $i = 0; $i <@$vec1; $i ++) {
    $vec_add->[$i] = $vec1->[$i] + $vec2->[$i];
  }
  
  return $vec_add;
}

my $vec1 = [1, 2, 3];
my $vec2 = [4, 5, 6];

#Sum of vectors
my $vec_add = vec_add ($vec1, $vec2);

# 5 7 9
print "@$vec_add\n";

Where do you use the sum of vectors in deep learning calculations?

In deep learning calculations, the matrix product of the weight matrix and the input vector is calculated. The calculated value is a vector (partial calculation 1).

Bias is expressed as a vector.

Finding the sum of the vector of partial calculation 1 and the bias vector gives the output vector.

#Calculation formula for each layer of deep learning
Output vector = weight matrix x input vector + bias vector

Perform the following calculations.

use strict;
use warnings;

# Vector of the result of matrix product of weight matrix and input vector
my $mul_weight_inputs = [4, 5, 6, 7];

#Bias vector
my $biases = [3, 6, 9, 2];

#Output vector
my $outputs = [];

for (my $i = 0; $i <@$mul_weight_inputs; $i ++) {
  $outputs->[$i] = $mul_weight_inputs->[$i] + $biases->[$i];
}

print "@$outputs\n";

Associated Information