{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "

Table of Contents

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Array operations in PyTorch

\n", "

(Szabó Sándor, 27. May 2020)

\n", "\n", "

We will consider some common operations for matrices:

\n", "

\n", "

\n", "

" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Import torch and other required modules\n", "import torch" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Multiplication

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

\n", " Since multiplication is not commutative, we need to take care of the order.\n", "

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 1.

" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[ 0.1024, -0.6093, 0.2240, -0.1838],\n", " [-0.7241, -0.7749, 0.2628, 1.9635]])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Example 1\n", "A = torch.randn(2, 3)\n", "B = torch.randn(3, 4)\n", "torch.mm(A, B)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 2.

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

However if you change the order you obtain

\n", "
" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "ename": "RuntimeError", "evalue": "size mismatch, m1: [3 x 4], m2: [2 x 3] at C:\\Users\\builder\\AppData\\Local\\Temp\\pip-req-build-9msmi1s9\\aten\\src\\TH/generic/THTensorMath.cpp:197", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mRuntimeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Example 2 - breaking\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmm\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mB\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mA\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mRuntimeError\u001b[0m: size mismatch, m1: [3 x 4], m2: [2 x 3] at C:\\Users\\builder\\AppData\\Local\\Temp\\pip-req-build-9msmi1s9\\aten\\src\\TH/generic/THTensorMath.cpp:197" ] } ], "source": [ "# Example 2 - breaking\n", "torch.mm(B, A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

\n", " torch.mm does not broadcast. \n", " For broadcasting matrix products, see torch.matmul().\n", "

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 3.

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

If your keyboard has the character '@', then you can write

\n", "
" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[-0.5379, -0.1539, -0.2042, -0.5444],\n", " [ 0.0280, -0.8160, 0.5760, 2.8664]])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Example 3\n", "A @ B" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Transpose

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

When we transpose a matrix we transform rows to columns, and columns to rows.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 1.

" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[-0.7157, 1.2350],\n", " [ 0.0141, -1.2724],\n", " [ 0.1572, 0.7450]])" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Example 1 \n", "torch.t(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 2.

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Again, you should thoughtful, because $(AB)^T\\neq A^T B^T$, indeed

\n", "
" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[ 1.5273, 2.2196, 0.6171],\n", " [-0.0384, -1.0361, -1.0175],\n", " [ 2.1029, -0.2353, -0.4913]])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Example 2\n", "C = torch.randn(3, 3)\n", "D = torch.randn(3, 3)\n", "torch.t(C @ D) - (torch.t(C) @ torch.t(D))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

The correct result is $(AB)^T=B^T A^T$.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 3.

" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[0., 0., 0.],\n", " [0., 0., 0.],\n", " [0., 0., 0.]])" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Example 3\n", "torch.t(C @ D) - (torch.t(D) @ torch.t(C))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Gradient

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

In machine learning we should minimize different type \n", " of error function.

\n", "

\n", " The derivative of a multivariable scalar valued function is a matrix, the so-called \n", " \n", " Jacobi matrix.\n", "

\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "
\n", "

If you want to calculate the derivative, you should give this option \n", " in the definition of the tensor, using requires_grad=True

\n", "

In Example 1 we use the vector norm (in fact, the Frobenius norm, which gives the \n", " Eucledian vector norm in case of vectors).

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 1.

" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A: \n", " tensor([[-0.3062, -0.5272, 1.2869],\n", " [-1.8781, -1.9212, 0.1393],\n", " [-1.0218, 1.2361, -0.1057]], requires_grad=True)\n", "x: \n", " tensor([[ 0.5530],\n", " [-0.6164],\n", " [ 0.8250]], requires_grad=True)\n", "b: \n", " tensor([[-1.0424],\n", " [-1.3509],\n", " [ 0.8377]], requires_grad=True)\n", "y: \n", " tensor(3.5743, grad_fn=)\n" ] } ], "source": [ "# Example 1\n", "# Create tensors.\n", "A = torch.randn(3, 3, requires_grad=True)\n", "x = torch.randn(3, 1, requires_grad=True)\n", "b = torch.randn(3, 1, requires_grad=True)\n", "y = torch.norm(A @ x - b, p='fro')\n", "print(\"A: \\n\", A)\n", "print(\"x: \\n\", x)\n", "print(\"b: \\n\", b)\n", "print(\"y: \\n\", y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Now we calculate the derivatives $\\dfrac{\\partial y}{\\partial A}$, \n", " $\\dfrac{\\partial y}{\\partial x}$, $\\dfrac{\\partial y}{\\partial b}$.

\n", "

To compute the derivatives, we call the .backward \n", " method on our result $y$.

\n", "
" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "∂y/∂A: \n", " tensor([[ 0.3496, -0.3897, 0.5216],\n", " [ 0.2493, -0.2780, 0.3720],\n", " [-0.3484, 0.3884, -0.5198]])\n", "∂y/∂x: \n", " tensor([[-0.3966],\n", " [-1.9784],\n", " [ 0.9430]])\n", "∂y/∂b: \n", " tensor([[-0.6322],\n", " [-0.4509],\n", " [ 0.6301]])\n" ] } ], "source": [ "# Compute derivatives\n", "y.backward()\n", "\n", "# Display gradients\n", "print('∂y/∂A: \\n', A.grad)\n", "print('∂y/∂x: \\n', x.grad)\n", "print('∂y/∂b: \\n', b.grad)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 2.

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Instead of Frobenius norm we can choose any $1\\leq p<\\infty$ norm.\n", " In the next example we choose $p=1$.

\n", "
" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "∂w/∂A: \n", " tensor([[ 0.9026, -1.0062, 1.3467],\n", " [ 0.8023, -0.8944, 1.1970],\n", " [-0.9014, 1.0048, -1.3449]])\n", "∂w/∂x: \n", " tensor([[-1.5591],\n", " [-5.6630],\n", " [ 2.4750]])\n", "∂w/∂b: \n", " tensor([[-1.6322],\n", " [-1.4509],\n", " [ 1.6301]])\n" ] } ], "source": [ "# Example 2 \n", "w = torch.norm(A @ x - b, p=1)\n", " \n", "# Compute derivatives\n", "w.backward()\n", "\n", "# Display gradients\n", "print('∂w/∂A: \\n', A.grad)\n", "print('∂w/∂x: \\n', x.grad)\n", "print('∂w/∂b: \\n', b.grad)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 3.

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

At this moment the infinity norm $p=\\infty$ does not work.

\n", "
" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'inf' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Example 3 - breaking\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mz\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnorm\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mA\u001b[0m \u001b[1;33m@\u001b[0m \u001b[0mx\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mp\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0minf\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;31m# Compute derivatives\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mz\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbackward\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mNameError\u001b[0m: name 'inf' is not defined" ] } ], "source": [ "# Example 3 - breaking \n", "z = torch.norm(A @ x - b, p=inf)\n", " \n", "# Compute derivatives\n", "z.backward()\n", "\n", "# Display gradients\n", "print('∂z/∂A: \\n', A.grad)\n", "print('∂z/∂x: \\n', x.grad)\n", "print('∂z/∂b: \\n', b.grad)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Eigenvalues, eigenvectors

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

When we work on linear operators (matrices) many times is a must to \n", " determine their eigenvalues and eigenvectors. \n", " To do this, we use torch.eig

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 1.

" ] }, { "cell_type": "code", "execution_count": 111, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "eigenvalue: tensor([1.1316, 0.0000], grad_fn=)\n", "eigvector: tensor([-0.9053, 0.4609, 0.0952], grad_fn=)\n", "\n", "\n", "eigenvalue: tensor([-0.2300, 0.0000], grad_fn=)\n", "eigvector: tensor([-0.1106, -0.8415, -0.8702], grad_fn=)\n", "\n", "\n", "eigenvalue: tensor([-0.9825, 0.0000], grad_fn=)\n", "eigvector: tensor([-0.4100, 0.2818, -0.4834], grad_fn=)\n", "\n", "\n" ] } ], "source": [ "# Example 1 \n", "(eigvalues, eigvectors) = torch.eig(A, eigenvectors=True)\n", "for i in range(3):\n", " print('eigenvalue: ', eigvalues[i])\n", " print('eigvector: ', eigvectors[i])\n", " print('\\n')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Since an eigenvalue can be complex, the first element in the tensor is \n", " the real part and the second element is the imaginary part of it.\n", "

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 2.

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

\n", " Here is an example when the tensor has one eigenvalue with three different \n", " eigenvectors.\n", "

\n", "
" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "eigenvalue: tensor([2., 0.])\n", "eigenvector: tensor([ 0.0000, -0.4472, 0.4082])\n", "\n", "\n", "eigenvalue: tensor([2.0000, 0.0000])\n", "eigenvector: tensor([ 0.0000, 0.8944, -0.8165])\n", "\n", "\n", "eigenvalue: tensor([2.0000, 0.0000])\n", "eigenvector: tensor([ 1.0000, 0.0000, -0.4082])\n", "\n", "\n" ] } ], "source": [ "# Example 2 \n", "C = torch.tensor([[0., -1., 0], [4., 4., 0], [2., 1., 2.]])\n", "\n", "(eigvalues, eigvectors) = torch.eig(C, eigenvectors=True)\n", "for i in range(3):\n", " print('eigenvalue: ', eigvalues[i])\n", " print('eigenvector: ', eigvectors[i])\n", " print('\\n')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 3.

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Only square matrices can have eigenvalues.

\n", "
" ] }, { "cell_type": "code", "execution_count": 114, "metadata": {}, "outputs": [ { "ename": "RuntimeError", "evalue": "invalid argument 1: A should be square at C:\\Users\\builder\\AppData\\Local\\Temp\\pip-req-build-9msmi1s9\\aten\\src\\TH/generic/THTensorLapack.cpp:195", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mRuntimeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0mD\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtensor\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0.\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m-\u001b[0m\u001b[1;36m1.\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m4.\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m4.\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[1;33m(\u001b[0m\u001b[0meigvalues\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0meigvectors\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0meig\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mD\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0meigenvectors\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mRuntimeError\u001b[0m: invalid argument 1: A should be square at C:\\Users\\builder\\AppData\\Local\\Temp\\pip-req-build-9msmi1s9\\aten\\src\\TH/generic/THTensorLapack.cpp:195" ] } ], "source": [ "# Example 3 \n", "D = torch.tensor([[0., -1., 0], [4., 4., 0]])\n", "\n", "(eigvalues, eigvectors) = torch.eig(D, eigenvectors=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Least square norm

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

Many times we should minimize the quantity $\\Vert AX-B\\Vert_2$, \n", " where $A,B$ are given matrices, and $X$ is the one we want to calculate. \n", " We use the torch.lstsq function.\n", "

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 1.

" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A: \n", " tensor([[-0.2769, 1.0674, 0.6662, -1.5984],\n", " [ 0.8249, 1.3140, -0.0192, -0.3846],\n", " [ 2.4818, 0.5684, -1.7736, 0.6815]])\n", "B: \n", " tensor([[ 0.4494],\n", " [-0.9212],\n", " [-1.1869]])\n", "\n", "\n", "The solution of the least square problem is: \n", "\n", " tensor([[-0.2753],\n", " [-0.7950],\n", " [-0.3150],\n", " [-0.8957]])\n", "\n", "\n", "The error is: tensor(1.8849e-07)\n" ] } ], "source": [ "# Example 1\n", "A = torch.randn(3, 4)\n", "B = torch.randn(3, 1) # a vector\n", "print(\"A: \\n\", A)\n", "print(\"B: \\n\", B)\n", "print(\"\\n\")\n", "\n", "X, _ = torch.lstsq(B, A)\n", "\n", "error = torch.norm(A @ X - B, p=2)\n", "print(\"The solution of the least square problem is: \\n\\n\", X)\n", "print(\"\\n\")\n", "print(\"The error is: \", error)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 2.

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

\n", " Now consider the previous example, but with $p=1$ norm.\n", "

\n", "
" ] }, { "cell_type": "code", "execution_count": 132, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A: \n", " tensor([[-0.0644, -0.7350, 0.9334, -0.2362],\n", " [-0.5515, -0.2604, 0.7796, -1.1156],\n", " [-1.2727, 1.4277, 1.5215, 1.3682]])\n", "B: \n", " tensor([[-1.8821],\n", " [-0.5725],\n", " [-0.3240]])\n", "\n", "\n", "The solution of the least square problem is: \n", "\n", " tensor([[-0.3108],\n", " [ 1.2100],\n", " [-1.1999],\n", " [-0.4541]])\n", "\n", "\n", "The error is: tensor(8.3447e-07)\n" ] } ], "source": [ "# Example 2 \n", "A = torch.randn(3, 4)\n", "B = torch.randn(3, 1) # a vector\n", "print(\"A: \\n\", A)\n", "print(\"B: \\n\", B)\n", "print(\"\\n\")\n", "\n", "X, _ = torch.lstsq(B, A)\n", "\n", "error = torch.norm(A @ X - B, p=1)\n", "print(\"The solution of the least square problem is: \\n\\n\", X)\n", "print(\"\\n\")\n", "print(\"The error is: \", error)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

The error is greater.

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Example 3.

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

If we give matrices of wrong dimensions, we obtain \n", " a 'size mismatch' error message.

\n", "
" ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "A: \n", " tensor([[-0.7085, -0.5104, 0.8651, -1.5750],\n", " [-0.6783, -0.8917, -0.3745, 0.8802],\n", " [-0.1210, 0.4588, 1.7196, -1.7847],\n", " [ 0.5258, -0.0043, 1.3598, -0.2012],\n", " [-0.3974, 0.0806, 0.2112, -0.7372]])\n", "B: \n", " tensor([[ 2.1218],\n", " [ 2.0587],\n", " [ 0.4716],\n", " [-0.7330],\n", " [ 1.3271]])\n", "\n", "\n" ] }, { "ename": "RuntimeError", "evalue": "size mismatch, m1: [5 x 4], m2: [5 x 1] at C:\\Users\\builder\\AppData\\Local\\Temp\\pip-req-build-9msmi1s9\\aten\\src\\TH/generic/THTensorMath.cpp:197", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mRuntimeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlstsq\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mB\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mA\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m \u001b[0merror\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnorm\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mA\u001b[0m \u001b[1;33m@\u001b[0m \u001b[0mX\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0mB\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mp\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 11\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"The solution of the least square problem is: \\n\\n\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mX\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 12\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"\\n\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mRuntimeError\u001b[0m: size mismatch, m1: [5 x 4], m2: [5 x 1] at C:\\Users\\builder\\AppData\\Local\\Temp\\pip-req-build-9msmi1s9\\aten\\src\\TH/generic/THTensorMath.cpp:197" ] } ], "source": [ "# Example 3 - breaking\n", "A = torch.randn(5, 4)\n", "B = torch.randn(5, 1) # a vector\n", "print(\"A: \\n\", A)\n", "print(\"B: \\n\", B)\n", "print(\"\\n\")\n", "\n", "X, _ = torch.lstsq(B, A)\n", "\n", "error = torch.norm(A @ X - B, p=1)\n", "print(\"The solution of the least square problem is: \\n\\n\", X)\n", "print(\"\\n\")\n", "print(\"The error is: \", error)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Summary

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "

We considered some basic linear algebraic problems, that PyTorch can \n", " solve very effectively, and we will use them many times later.\n", "

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Reference Links

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Official documentation for torch.Tensor \n", " \n", " https://pytorch.org/docs/stable/tensors.html

\n", "

Official documentation for torch.Autograd \n", " \n", " https://pytorch.org/docs/stable/autograd.html

" ] }, { "cell_type": "code", "execution_count": 175, "metadata": { "collapsed": true }, "outputs": [ { "data": { "text/html": [ "\n" ], "text/plain": [ "" ] }, "execution_count": 175, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.core.display import HTML\n", "import urllib.request\n", "response = urllib.request.urlopen('https://raw.githubusercontent.com/wesszabo/Pytorch-basics/master/CSS/pytorch_basics.css')\n", "HTML(response.read().decode(\"utf-8\"))" ] } ], "metadata": { "hide_input": false, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.7" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": true, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }