Matrix Computation With NumPy | Towards Data Science

Performance is really an important aspect when we are solving problems using Machine Learning, especially Deep Learning. Using a neural network for calculating things can become a complex calculation because it involves matrix and vectors to it.
Suppose we want to calculate a 25 x 25 matrix that is being multiplied by 5. At the first time you are being introduced to programming, You will calculate vectors or matrices that look like this,
for i in range(25):    for j in range(25):        x[i][j] *= 5
If we use this method, it will make our computation longer and we will not get the result at the time that we desired. Thankfully, in Python, we have NumPy library to solve this matrix and vector calculation.
By using NumPy library, we can make that 3-lines of code become 1-line of code like this one below,
import numpy as np# Make it as NumPy array firstx = np.array(x)x = x * 5
And if we compare the time, for this case, the conventional way will take around 0.000224 and the NumPy method is just 0.000076. The NumPy is almost 3 times faster than the conventional one, but it also simplifies your code at the same time!
Just imagine when you want to calculate a bigger matrix than in this example here and imagine how much the time that will you save. This calculation is classified as vectorization where the calculation has occurred on a matrix or vector representation. Therefore, with that computation method, it will save your time for the same result.
How is that happen? What is the magic of NumPy so it can calculate the matrix simpler and faster? This article will introduce you to the concepts of Single Instruction Multiple Data (SIMD) and Broadcasting. Without further, let’s get to it.
What is Single Instruction Multiple Data (SIMD)? Basically, SIMD (pronounced as “sim-dee” not spell it by the alphabet) is a parallel computation method to make the data inside the computer can be calculated at the same time (concurrently) by the hardware.
For example, we have the 2-input vectors and we want to calculate the vector by each element arithmetically. The hardware will, so each data will be distributed to a different component of the hardware (i.e. the GPU-core) and calculate each element of both vectors at the same time. Because of that, the vectors can be calculated faster because of it.
In NumPy, the calculation works when meeting these requirements, they are:
The dimensions on both objects are the same, orOn of them have dimension’s value 1
The dimensions on both objects are the same, or
On of them have dimension’s value 1
If the objects do not meet with the requirements above, it cannot be calculated. If there some objects that met the 2nd requirement, therefore that object will be broadcasted.
Broadcasting is being used basically on vectors that have not the same shape on it. Recall the calculation above between the matrix and the scalar element, which is 5, the scalar will be broadcasted so the scalar will have the same shape as the matrix. Example, when you have these objects,
A 5 x 1 vectors in example 2 [1 2 3 4 5]
A 1 x 1 scalar in the example is 5
To make this happen, the scalar will have the same shape as the vector and the value will be broadcast across the new vector. So we can have [5 5 5 5 5] vector. With that matrix, we can calculate the vector and scalar that is being converted to a vector by using the principle of SIMD. Therefore, the result will be [5 10 15 20 25] as illustrated below,
In conclusion, the computation for machine learning is not separated from vectors and matrices. Understanding Linear Algebra will be useful if you want to develop some machine learning model from scratch, especially on the vectorization part. By doing so, we can make our computation more efficient and simpler thanks to the concepts of SIMD and Broadcasting.
[1] João M.P. Cardoso, José Gabriel F. Coutinho and Pedro C. Diniz, Embedded Computing for High Performance (2017), Elsevier Inc.[2] Stéfan van der Walt, S. Chris Colbert and Gaël Varoquaux. The NumPy Array: A Structure for Efficient Numerical Computation (2011), Computing in Science & Engineering, vol. 13, no. 2, pp. 22–30.