“The first duty in life is to be as artificial as possible. What the second duty is no one has as yet discovered.” Oscar Wilde # Matrix operations with pytorch – optimizer – part 2

pytorch – matrix inverse with pytorch optimizer

This blog post is part of a 3 post miniseries.

Today’s post in particular covers the topic pytorch – matrix inverse with pytorch optimizer.

The point of the entire miniseries is to reproduce matrix operations such as matrix inverse and svd using pytorch’s automatic differentiation capability.

These algorithms are already implemented in pytorch itself and other libraries such as scikit-learn. However, we will solve this problem in a general way using gradient descent. We hope that this will provide an understanding of the power of the gradient method in general and the capabilities of pytorch in particular.

To avoid reader fatigue, we present the material in 3 posts:

• A introductory section: pytorch – playing with tensors demonstrates some basic tensor usage​1​. This notebook also shows how to calculate various derivatives.
• A main section: pytorch – matrix inverse with pytorch optimizer shows how to calculate the matrix inverse​2​ using gradient descent.
• An advanced section: SVD with pytorch optimizer shows how to do singular value decomposition​3​ with gradient descent​4​.

Some background information on gradient descent can be found here​5,6​.A post with a similar albeit slightly more mathematical character can be found here​7​. Some more advanced material can be found here​8​.

## Requirements

Hardware

• A computer with at least 4 GB Ram

Software

• The computer can run on Linux, MacOS or Windows

Wetware

• Familiarity with Python and basic linear algebra

Let’s get  started

The code of this post is provided in a jupyter notebook on github:

https://github.com/hfwittmann/matrix-operations-with-pytorch/blob/master/Matrix-operations-with-pytorch-optimizer/01—Matrix-inverse-with-pytorch-optimizer.ipynb

Remark: the following part of the post is directly written in a Jupyter notebook. It is displayed via a very nice wordpress plugin nbconvert​9​.

1. 1.
Matrix operations with pytorch – optimizer – part 1. Arthought. https://arthought.com/matrix-operations-with-pytorch-optimizer-part-1/. Published February 7, 2020. Accessed February 7, 2020.
2. 2.
Invertible matrix. Wikipedia. https://en.wikipedia.org/wiki/Invertible_matrix. Published February 6, 2020. Accessed February 6, 2020.
3. 3.
Singular value decomposition. Wikipedia. https://en.wikipedia.org/wiki/Singular_value_decomposition. Published February 6, 2020. Accessed February 6, 2020.
4. 4.
Matrix operations with pytorch – optimizer – part 3. arthought. https://arthought.com/. Published February 9, 2020. Accessed February 9, 2020.
5. 5.
Gradient Descent. Wikipedia. https://en.wikipedia.org/wiki/Gradient_descent. Published February 8, 2020. Accessed February 8, 2020.
6. 6.