{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# What is PyTorch?\n", "\n", "It’s a Python based scientific computing package targeted at two sets of audiences:\n", "\n", "- Tensorial library that uses the power of GPUs\n", "- A deep learning research platform that provides maximum flexibility and speed\n", "\n", "## Import the library" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import torch # / + " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting help in Jupyter" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "torch.sq # " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# What about all `*Tensor`s?\n", "torch.*Tensor?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "torch.nn.Module() # +" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Annotate your functions / classes!\n", "torch.nn.Module?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "torch.nn.Module??" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dropping to Bash: magic!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "! ls -lh" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "for f in $(ls *.*); do\n", " echo $(wc -l $f)\n", "done" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Help?\n", "%%bash?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Getting some general help\n", "%magic" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Python native data types\n", "\n", "Python has many native datatypes. Here are the important ones:\n", "\n", " - **Booleans** are either `True` or `False`.\n", " - **Numbers** can be integers (1 and 2), floats (1.1 and 1.2), fractions (1/2 and 2/3), or even complex numbers.\n", " - **Strings** are sequences of Unicode characters, e.g. an html document.\n", " - **Lists** are ordered sequences of values.\n", " - **Tuples** are ordered, immutable sequences of values.\n", " - **Sets** are unordered bags of values.\n", " - **Dictionaries** are unordered bags of key-value pairs.\n", " \n", "See [here](http://www.diveintopython3.net/native-datatypes.html) for a complete overview.\n", "\n", "### More resources\n", "\n", " 1. Brief Python introduction [here](https://learnxinyminutes.com/docs/python3/).\n", " 2. Full Python tutorial [here](https://docs.python.org/3/tutorial/).\n", " 3. A Whirlwind Tour of Python [here](https://github.com/jakevdp/WhirlwindTourOfPython).\n", " 4. Python Data Science Handbook [here](https://github.com/jakevdp/PythonDataScienceHandbook)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Torch!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t = torch.Tensor(2, 3, 4)\n", "type(t)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t.size()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# t.size() is a classic tuple =>\n", "print('t size:', ' \\u00D7 '.join(map(str, t.size())))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f'point in a {t.numel()} dimensional space')\n", "print(f'organised in {t.dim()} sub-dimensions')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Mind the underscore!\n", "t.random_(10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "r = torch.Tensor(t)\n", "r.resize_(3, 8)\n", "r" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "r.zero_()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# This *is* important, sigh...\n", "s = r.clone()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s.fill_(1)\n", "s" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "r" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Vectors (1D Tensors)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v = torch.Tensor([1, 2, 3, 4]); v" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f'dim: {v.dim()}, size: {v.size()[0]}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "w = torch.Tensor([1, 0, 2, 0]); w" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Element-wise multiplication\n", "v * w" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Scalar product: 1*1 + 2*0 + 3*2 + 4*0\n", "v @ w" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = torch.Tensor(5).random_(10); x" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f'first: {x[0]}, last: {x[-1]}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Extract sub-Tensor [from:to)\n", "x[1:2 + 1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v = torch.arange(1, 4 + 1); v" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(v.pow(2), v)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(v.pow_(2), v)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matrices (2D Tensors)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m = torch.Tensor([[2, 5, 3, 7],\n", " [4, 2, 1, 9]]); m" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m.dim()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(m.size(0), m.size(1), m.size(), sep=' -- ')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m.numel()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m[0][2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m[0, 2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m[:, 1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m[:, [1]]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m[[0], :]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m[0, :]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v = torch.arange(1, 4 + 1); v" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m @ v" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m[[0], :] @ v" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m[[1], :] @ v" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m + torch.rand(2, 4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m - torch.rand(2, 4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m * torch.rand(2, 4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m / torch.rand(2, 4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m.t()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Same as\n", "m.transpose(0, 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Constructors" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "torch.arange(3, 8 + 1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "torch.arange(5.7, -3, -2.1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "torch.linspace(3, 8, 20).view(1, -1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "torch.zeros(3, 5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "torch.ones(3, 2, 5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "torch.eye(3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Pretty plotting config\n", "%run plot_conf.py" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt_style()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Numpy bridge!\n", "plt.hist(torch.randn(1000).numpy(), 100);" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.hist(torch.randn(10**6).numpy(), 100); # how much does this chart weight?\n", "# use rasterized=True for SVG/EPS/PDF!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.hist(torch.rand(10**6).numpy(), 100);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Casting" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "torch.*Tensor?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m.double()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m.byte()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n", "m.to(device)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m_np = m.numpy(); m_np" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m_np[0, 0] = -1; m_np" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n_np = np.arange(5)\n", "n = torch.from_numpy(n_np)\n", "print(n_np, n)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n.mul_(2)\n", "n_np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More fun" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = torch.Tensor([[1, 2, 3, 4]])\n", "b = torch.Tensor([[5, 6, 7, 8]])\n", "print(a, b)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "torch.cat((a, b), 0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "torch.cat((a, b), 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Much more\n", "\n", "There's definitely much more, but this was the basics about `Tensor`s fun.\n", "\n", "*Torch* full API should be read at least once.\n", "Hence, go [here](http://pytorch.org/docs/0.3.0/torch.html).\n", "You'll find 100+ `Tensor` operations, including transposing, indexing, slicing, mathematical operations, linear algebra, random numbers, etc are described." ] } ], "metadata": { "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.6.5" } }, "nbformat": 4, "nbformat_minor": 1 }