MXNet in Perlmanipulate data with NDArraythis page replicates
script for this pageoutline of this pageAs an introduction to MXNet, this page discusses NDArray, the primary tool for storing and transforming data in MXNet. First, we must use the module in our Perl script: use AI::MXNet qw(mx); which imports the AI::MXNet::NDArray module, so it is not necessary to list it separately. getting startedTo begin, let's create a twodimensional array (better known as a "matrix"), with values: [1, 2, 3] and [5, 6, 7]. my $ndarray = nd>array([[1,2,3],[5,6,7]]); print $ndarray>aspdl; which yields:
Next, let's create a matrix of the same shape (two rows and three columns), but fill it with ones: my $xx = nd>ones([2,3]); print $xx>aspdl; which yields:
And let's create an array and populate it with randomly sampled values. In the case below, we create an array of the same shape and populate it with uniformly distributed values between 1 and 1. my $yy = nd>random>uniform(1,1,[2,3]); print $yy>aspdl; which yields:
Finally, let's create one more array of the same shape and fill it with a particular value: my $zz = nd>full([2,3],2); print $zz>aspdl; which yields:
To access the dimensions of each NDArray, we can call the shape method. The size method return the product of the shape components. And the dtype method returns the data type of the stored values. print "shape: ". '['. join(",", @{$ndarray>shape}) .']'."\n"; print "size: ". $ndarray>size ."\n"; print "dtype: ". $ndarray>dtype ."\n"; which yields:
operationsIn this section, we'll explore NDArray's support for standard mathematical operations. We can perform elementwise multiplication: my $zy = $zz * $yy; print $zy>aspdl; which yields:
Exponentiation: my $ye = $yy>exp; print $ye>aspdl; which yields:
And we can transpose a matrix to compute a matrixmatrix product. my $zyt = nd>dot($zz,$yy>T); print $zyt>aspdl; which yields:
indexingMXNet NDArrays support slicing, so that you can access particular parts of your data. As a first example, let's return a onedimensional array containing the element in the last row, last column: print $yy>slice(1,2)>aspdl; which yields:
We can change that element: $yy>slice(1,2) .= 7; print $yy>aspdl; which yields:
We can change every value in a row: $yy>slice(0,) .= 14; print $yy>aspdl; which yields:
While preparing these notes, I was unable to change every value in a column and I was unable to select a range of rows or columns. Hopefully, this bug will be fixed in a future release. In the meantime, we can work around the issue by converting betweeen NDArray and PDL. converting between NDArray and PDLThe one thing that you must remember when working with the Perl Data Language is that it's columnmajor. So instead of reading elements rowbyrow into a matrix, it reads them in columnbycolumn. What this means in practice is that matrices appear transposed, so to access the second and third columns (i.e. columns 1 and 2), you would write 1:2 and follow it with a comma, so that: print $yy>aspdl>slice('1:2,:'); returns:
So to change the value of the second and third columns, we can convert to PDL and then back to NDArray: { my $tmp = $yy>aspdl; } print $yy>aspdl; which yields:
And similarly, we can change the values in the first two columns of the second row: { my $tmp = $yy>aspdl; } print $yy>aspdl; which yields:
Copyright © 20022020 Eryk Wdowiak 
