**Greetings!**Some links on this site are affiliate links. That means that, if you choose to make a purchase, The Click Reader may earn a small commission

*at no extra cost to you*. We greatly appreciate your support!

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

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

[latexpage]## 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.

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.

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.

Do you want to learn Python, Data Science, and Machine Learning while getting certified? Here are some best selling Datacamp courses that we recommend you enroll in:

- Introduction to Python (Free Course) - 1,000,000+ students already enrolled!
- Introduction to Data Science in Python- 400,000+ students already enrolled!
- Introduction to TensorFlow for Deep Learning with Python - 90,000+ students already enrolled!
- Data Science and Machine Learning Bootcamp with R - 70,000+ students already enrolled!