# Perl AI Deep Learning Tutorial

## Matrix product calculation

I will explain how to find the product of matrices required for deep learning in Perl.

Necessary when multiplying the weight matrix and the input vector.

There are some conditions for matrix calculation.

The first is that it is a column-first matrix. A column-first matrix means that it has data in the direction of the columns.

```# Matrix representation of mathematics
# 3 rows and 2 columns matrix
# 14
# twenty five
# 3 6

#How to use data with column priority
my \$mat = {
values ​​=> [1, 2, 3, 4, 5, 6],
rows_length => 3,
columns_length => 2,
};;
```

The reason for giving column priority is to be consistent with using a matrix calculation library called BLAS.

First, perform matrix operations in Perl. Port it to SPVM. And finally, we will make it portable to a C language library called BLUS and a library called cuBLUS that can calculate GPU of cuda.

### Matrix product

Let's find the product of the matrix. Find the product of a 3x2 matrix and a 2x1 matrix (that is, a vector).

```use strict;
use warnings;

# Find the product of matrices
sub mat_mul {
my (\$mat1, \$mat2) = @_;

my \$mat1_rows_length = \$mat1->{rows_length};
my \$mat1_columns_length = \$mat1->{columns_length};
my \$mat1_values ​​= \$mat1->{values};

my \$mat2_rows_length = \$mat2->{rows_length};
my \$mat2_columns_length = \$mat2->{columns_length};
my \$mat2_values ​​= \$mat2->{values};

#Calculation of matrix product
my \$mat_out_values ​​= [];
for (my \$row = 0; \$row <\$mat1_rows_length; \$row ++) {
for (my \$col = 0; \$col <\$mat2_columns_length; \$col ++) {
for (my \$incol = 0; \$incol <\$mat1_columns_length; \$incol ++) {
\$mat_out_values->[\$row + \$col * \$mat1_rows_length]
+ = \$mat1_values->[\$row + \$incol * \$mat1_rows_length] * \$mat2_values->[\$incol + \$col * \$mat2_rows_length];
}
}
}

my \$mat_out = {
rows_length => \$mat1_rows_length,
columns_length => \$mat2_columns_length,
values ​​=> \$mat_out_values,
};;

return \$mat_out;
}

#Weight (matrix of 3 rows and 2 columns)
# 14
# twenty five
# 3 6
my \$mat1 = {
values ​​=> [1, 2, 3, 4, 5, 6],
rows_length => 3,
columns_length => 2,
};;

#Input vector (matrix of 2 rows and 1 column)
# 7 9
# 8 10
my \$mat2 = {
values ​​=> [7, 8, 9, 10],
rows_length => 2,
columns_length => 2,
};;

# Method of calculation
# 1 * 7 + 4 * 8 1 * 9 + 4 * 10
# 2 * 7 + 5 * 8 2 * 9 + 5 * 10
# 3 * 7 + 6 * 8 3 * 9 + 6 * 10
my \$outputs_mul = mat_mul (\$mat1, \$mat2);

# rows_length => 3, columns_length => 2, values: [39, 54, 69, 49, 68, 87]
use Data::Dumper;
print Dumper \$outputs_mul;
```

For matrix multiplication calculation in Perl, there was a matrix product calculation algorithm written in C language, so I used it.

Matrix product calculations are typically triple loops.