“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 1

pytorch – playing with tensors

This blog post is part of a 3 post miniseries.

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. This also shows how to calculate various derivatives.
• A main section: pytorch – matrix inverse with pytorch optimizer shows how to calculate the matrix inverse​1​ using gradient descent.
• An advanced section: SVD with pytorch optimizer shows how to do singular value decomposition​1,2​ with gradient descent.

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/00—pytorch-playing-with-tensors.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​3​.

1. 1.
Invertible matrix. Wikipedia. https://en.wikipedia.org/wiki/Invertible_matrix. Published February 6, 2020. Accessed February 6, 2020.
2. 2.
Singular value decomposition. Wikipedia. https://en.wikipedia.org/wiki/Singular_value_decomposition. Published February 6, 2020. Accessed February 6, 2020.
3. 3.
Challis A. PHP: nbconvert – A wordpress plugin for Jupyter notebooks.   . https://www.andrewchallis.co.uk/. Published May 1, 2019. Accessed February 6, 2020.