Thanks to theidioms.com







Gram or Gramian Matrix – Explained with Example in Python

Gram or Gramian Matrix - Explained with Example in Python
Data Science

Gram or Gramian Matrix – Explained with Example in Python

Gram or Gramian Matrix - Explained with Example in PythonGram or Gramian Matrix - Explained with Example in Python

Learn everything about a Gram or Gramian matrix through mathematical examples and Python implementations while discovering its use in Neural Style Transfer.

Gram or Gramian Matrix - Explained with Example in Python

Excited to start learning? Let’s start off by understanding what is a Gram Matrix or a Gramian Matrix.

What is a Gram or Gramian Matrix?

In simple words, a Gram matrix (often referred to as a Gramian matrix) is a matrix created by multiplying a matrix with its own transpose.

For a matrix, V, whose columns are the vectors, v_{k}, its Gramian matrix is defined as,

    \[G  = V^{T}V\]

The Gramian matrix provides a degree of correlation between the vectors of the matrix V since we’re multiplying a matrix with its own transpose.

Mathematical Example of a Gram or Gramian Matrix

Consider a matrix, V, given as follows,

    \[V = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}\]

Then, the transpose of this matrix V^T is computed as,

    \[V^T = \begin{bmatrix} 1 & 3 \\ 2 & 4 \end{bmatrix}\]

So, the gram or gramian matrix G is calculated by taking the matrix product of the two matrices V and V^T as follows,

    \[G = V^{T}V  = \begin{bmatrix} 1 & 3 \\ 2 & 4 \end{bmatrix} \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}  = \begin{bmatrix} 1 \times 1 + 3 \times 3 & 1 \times 2 + 3 \times 4 \\ 2 \times 1 + 4 \times 3 & 2 \times 2 + 4 \times 4 \end{bmatrix} = \begin{bmatrix} 10 & 14 \\ 14 & 20 \end{bmatrix}\]

Here, the matrix G = \begin{bmatrix} 10 & 14 \\ 14 & 20 \end{bmatrix} is the Gram or Gramian matrix.

Gram or Gramian Matrix implementation in Python using NumPy

The Gram or Gramian matrix can be implemented in Python using just 4 lines of code using the NumPy library.

NumPy, or Numerical Python, is an open-source Python library that helps you perform simple as well as complex computations on numerical data. It is the go-to scientific computation library for beginners as well as advanced Python programmers and it is used mostly by statisticians, data scientists, and engineers.

NumPy Logo - Gram or Gramian Matrix - Explained with Example in Python

If you do not have the NumPy library installed, you can run the following command through your command line/terminal to install NumPy using Python’s package manager (pip),

pip install numpy

After you’ve successfully installed NumPy, you can import it in Python using the following line of code,

import numpy as np

Here, we’re importing the NumPy library/package as np since it is a general convention to use the short-hand name. If you are unfamiliar with NumPy and would want to master it, you can take this NumPy for Scientific Computation with Python course.

Now, that you know how to install and import NumPy in Python, you can write the following lines of code to implement a Gram or Gramian matrix in Python,

# Importing the NumPy library as np
import numpy as np

# Creating a matrix V
V = np.array([[1, 2], [3, 4]])

# Creating the Gram Matrix G
G = V.T.dot(V)

# Printing the matrix G
print(G)
[[10 14] 
 [14 20]]

Nice! You’ve successfully implemented the Gram or Gramian matrix in Python using NumPy.

Gram or Gramian Matrix in Neural Style Transfer

The Gram or Gramian Matrix is used in Neural Style Transfer, a Machine Learning use case for transferring the style of image into another.

Gram or Gramian Matrix in Neural Style Transfer

Since the Gram or Gramian matrix provides a degree of correlation between the vectors of a matrix, Machine Learning engineers can find the correlation between parameters of different Convolutional Filters in a Convolutional Neural Network.

You can watch this video to learn more about it in detail:

In Conclusion

You now know what is a Gram or Gramian matrix and how to implement it in Python. If you have any questions, please feel free to write to us in the comments and we will get back to you.


Are you interested in working with data? Get a free trial month of LinkedIn Learning and learn Data Science and Machine Learning courses using Python, R, and SQL from top instructors.

Enroll in any one of the courses from the following fields today on LinkedIn Learning and start your free month of learning:

Disclaimer: When you subscribe to LinkedIn Learning, we may earn a small commission as an affiliate. We love the platform so much so that we have been using it ourselves at The Click Reader.

Gram or Gramian Matrix - Explained with Example in PythonGram or Gramian Matrix - Explained with Example in Python

Leave your thought here

Your email address will not be published. Required fields are marked *