├── assets └── images │ ├── pp.png │ ├── xb.png │ └── ntcd.png ├── week1 ├── NumPy-intro.ipynb ├── PyTorch-intro.ipynb ├── PytorchTut.ipynb ├── Google_Colab_Tutorial.ipynb └── Python_Tutorial_The_byte.ipynb └── README.md /assets/images/pp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ntu-nail/CE7455/HEAD/assets/images/pp.png -------------------------------------------------------------------------------- /assets/images/xb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ntu-nail/CE7455/HEAD/assets/images/xb.png -------------------------------------------------------------------------------- /assets/images/ntcd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ntu-nail/CE7455/HEAD/assets/images/ntcd.png -------------------------------------------------------------------------------- /week1/NumPy-intro.ipynb: -------------------------------------------------------------------------------- 1 | {"nbformat":4,"nbformat_minor":0,"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.7.3"},"colab":{"name":"NumPy-intro.ipynb","provenance":[]}},"cells":[{"cell_type":"markdown","metadata":{"id":"hFPxN_O-8MKZ","colab_type":"text"},"source":["# Introduction to NumPy\n","\n","\n","Before reading this introduction you should know a bit of:\n","1. Python - look at [official tutorial](https://docs.python.org/3/tutorial/)\n","2. Linear Algebra and Matrices - look at [Coursera tutorial](https://www.coursera.org/learn/linear-algebra-machine-learning) and/or book [Introduction to Applied Linear Algebra](http://vmls-book.stanford.edu/vmls.pdf)\n","\n","
\n","\n","From offical NumPy page we could read that \n","```\n","NumPy is the fundamental package for scientific computing with Python. It contains among other things:\n"," * a powerful N-dimensional array object\n"," * useful linear algebra, Fourier transform, and random number capabilities\n"," * sophisticated (broadcasting) functions\n"," * tools for integrating C/C++ and Fortran code \n","```\n","\n","All of that functionalities are very useful if you want to do Machine Learning using **CPU**. To install NumPy package, go to [link](https://scipy.org/install.html). There are also very good tutorials:\n","* [Official NumPy tutorial](https://numpy.org/devdocs/user/quickstart.html)\n","* [From Python to Numpy](https://www.labri.fr/perso/nrougier/from-python-to-numpy/) \n","\n","Here we want give you a quick crash course of using NumPy library."]},{"cell_type":"markdown","metadata":{"ExecuteTime":{"end_time":"2019-10-13T12:02:31.745617Z","start_time":"2019-10-13T12:02:31.737921Z"},"id":"TbFCBdTY8MKc","colab_type":"text"},"source":["## Basics: creating a numpy array \n","\n","Important notes:\n","* all items in NumPy array (a.k.a. `ndarray`) cantain only one data type e.g. `int8`, `float32`, ... ([all datatypes](https://numpy.org/devdocs/user/basics.types.html))\n","* you cannot modify NumPy array"]},{"cell_type":"code","metadata":{"ExecuteTime":{"end_time":"2019-10-13T18:13:31.979567Z","start_time":"2019-10-13T18:13:31.900397Z"},"id":"_4RAm4b98MKd","colab_type":"code","outputId":"e4d6bb8e-f754-469c-ef5f-d503a28d77d5","colab":{}},"source":["import numpy as np # there is a convention to always import numpy as `np`\n","\n","\n","print(\"1d NumPy array from Python list (with `int32` type)\")\n","list1d = [0, 1, 2, 3, 4, 5, 6, 7]\n","ndarray1d = np.array(list1d, dtype='int32') \n","print(ndarray1d) # print ndarray\n","print(ndarray1d.shape) # print ndarray shape\n","print()\n","\n","print(\"2d NumPy array from Python list of lists (with `float32` type)\")\n","list2d = [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10, 11], [12, 13], [14, 15]]\n","ndarray2d = np.array(list2d, dtype='float32')\n","print(ndarray2d) # print ndarray\n","print(ndarray2d.shape) # print ndarray shape\n","print()\n","\n","print(\"1d random array (with `float32` type)\")\n","ndarray1d_random = np.random.rand(8, 2).astype('float32')\n","print(ndarray1d_random) # print ndarray\n","print(ndarray1d_random.shape) # print ndarray shape\n","print()\n","\n","print(\"1d array with uniform distribution\")\n","ndarray1d_uniform = np.random.uniform(-10, 10, 16)\n","print(ndarray1d_uniform) # print ndarray\n","print(ndarray1d_uniform.shape) # print ndarray shape\n","print()\n","\n","print(\"1d array based on linearly spaced vector\")\n","ndarray1d_linspace = np.linspace(0, 7, num=8, dtype='int8')\n","print(ndarray1d_linspace) # print ndarray\n","print(ndarray1d_linspace.shape) # print ndarray shape\n","print()\n","\n","print(\"1d array based on `arange` mechanism\")\n","ndarray1d_arange = np.arange(0, 10, 3)\n","print(ndarray1d_arange) # print ndarray\n","print(ndarray1d_arange.shape) # print ndarray shape\n","print()\n","\n","print(\"2d zeros array\")\n","ndarray2d_zeros = np.zeros([2, 4])\n","print(ndarray2d_zeros) # print ndarray\n","print(ndarray2d_zeros.shape) # print ndarray shape\n","print()\n","\n","print(\"2d ones array\")\n","ndarray2d_ones = np.ones([2, 4])\n","print(ndarray2d_ones) # print ndarray\n","print(ndarray2d_ones.shape) # print ndarray shape\n","print()"],"execution_count":0,"outputs":[{"output_type":"stream","text":["1d NumPy array from Python list (with `int32` type)\n","[0 1 2 3 4 5 6 7]\n","(8,)\n","\n","2d NumPy array from Python list of lists (with `float32` type)\n","[[ 0. 1.]\n"," [ 2. 3.]\n"," [ 4. 5.]\n"," [ 6. 7.]\n"," [ 8. 9.]\n"," [10. 11.]\n"," [12. 13.]\n"," [14. 15.]]\n","(8, 2)\n","\n","1d random array (with `float32` type)\n","[[0.07929848 0.04646103]\n"," [0.13551946 0.09415994]\n"," [0.31865713 0.7731082 ]\n"," [0.17045486 0.6933958 ]\n"," [0.09345574 0.99480355]\n"," [0.6637033 0.32966194]\n"," [0.72250724 0.711149 ]\n"," [0.9396972 0.8823931 ]]\n","(8, 2)\n","\n","1d array with uniform distribution\n","[-7.02870666 7.86882401 9.65007187 -7.83166989 -2.72224666 5.57936585\n"," -5.10631165 -9.66892718 3.56747523 -7.68795799 -1.05204412 6.419049\n"," -5.60641786 -2.30801985 -9.21079302 -4.18085661]\n","(16,)\n","\n","1d array based on linearly spaced vector\n","[0 1 2 3 4 5 6 7]\n","(8,)\n","\n","1d array based on `arange` mechanism\n","[0 3 6 9]\n","(4,)\n","\n","2d zeros array\n","[[0. 0. 0. 0.]\n"," [0. 0. 0. 0.]]\n","(2, 4)\n","\n","2d ones array\n","[[1. 1. 1. 1.]\n"," [1. 1. 1. 1.]]\n","(2, 4)\n","\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"ExecuteTime":{"end_time":"2019-10-13T13:12:01.187358Z","start_time":"2019-10-13T13:12:01.183488Z"},"id":"N0Vf9Z9J8MKh","colab_type":"text"},"source":["## Basics: extracting specific values from ndarrays\n","\n","Important notes:\n","* ndarrays can be indexed using the standard Python x[obj] syntax, wherea x is the array and obj the selection, see more at [NumPy indexing](https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html)"]},{"cell_type":"code","metadata":{"ExecuteTime":{"end_time":"2019-10-13T18:13:31.990206Z","start_time":"2019-10-13T18:13:31.982027Z"},"id":"kmjqaArx8MKi","colab_type":"code","outputId":"48ce7c6c-8189-47a0-dabe-1a6c63f72830","colab":{}},"source":["print(\"Get specific element\")\n","print(ndarray2d[1,1])\n","print()\n","\n","print(\"The basic slice syntax is i:j:k where i is the starting index, j is the stopping index, and k is the step\")\n","print(ndarray1d[0:6:2])\n","print()\n","\n","print(\"Extract only one dimension from multidimensional ndarray\") \n","print(ndarray2d[:, 0])\n","print()\n","\n","print(\"Boolean array indexing\") \n","print(ndarray1d[([True, False, True, False, True, False, True, False])])\n","print()\n","\n","print(\"Using condition statement for indexing array\") \n","print(ndarray1d[(ndarray1d % 2 == 0)]) \n","print()"],"execution_count":0,"outputs":[{"output_type":"stream","text":["Get specific element\n","3.0\n","\n","The basic slice syntax is i:j:k where i is the starting index, j is the stopping index, and k is the step\n","[0 2 4]\n","\n","Extract only one dimension from multidimensional ndarray\n","[ 0. 2. 4. 6. 8. 10. 12. 14.]\n","\n","Boolean array indexing\n","[0 2 4 6]\n","\n","Using condition statement for indexing array\n","[0 2 4 6]\n","\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"ExecuteTime":{"end_time":"2019-10-13T13:42:45.916975Z","start_time":"2019-10-13T13:42:45.911823Z"},"id":"YRlYpv_t8MKk","colab_type":"text"},"source":["## Basics: sum, min, max, mean, reshape \n"]},{"cell_type":"code","metadata":{"ExecuteTime":{"end_time":"2019-10-13T18:13:32.002505Z","start_time":"2019-10-13T18:13:31.992418Z"},"id":"w3ugauV88MKl","colab_type":"code","outputId":"45421697-3e69-4093-ffd3-260ab0d6c72d","colab":{}},"source":["print(\"represent `not a number value`\")\n","print(np.nan)\n","print()\n","\n","print(\"represent `infinite`\")\n","print(np.inf)\n","print()\n","\n","print(\"calculate mean, max and min in ndarray\")\n","print(\"sum \", ndarray2d.sum())\n","print(\"max \", ndarray2d.max())\n","print(\"min \", ndarray2d.min())\n","print(\"mean \", ndarray2d.mean())\n","print()\n","\n","print(\"calculate max on different axis\")\n","print(\"column max: \", np.max(ndarray2d, axis=0))\n","print(\"row max: \", np.max(ndarray2d, axis=1))\n","print()\n","\n","print(\"reshape 2d ndarray\")\n","print(ndarray2d.reshape(4, 4))\n","print(ndarray2d.shape, ndarray2d.reshape(4, 4).shape)\n","print()"],"execution_count":0,"outputs":[{"output_type":"stream","text":["represent `not a number value`\n","nan\n","\n","represent `infinite`\n","inf\n","\n","calculate mean, max and min in ndarray\n","sum 120.0\n","max 15.0\n","min 0.0\n","mean 7.5\n","\n","calculate max on different axis\n","column max: [14. 15.]\n","row max: [ 1. 3. 5. 7. 9. 11. 13. 15.]\n","\n","reshape 2d ndarray\n","[[ 0. 1. 2. 3.]\n"," [ 4. 5. 6. 7.]\n"," [ 8. 9. 10. 11.]\n"," [12. 13. 14. 15.]]\n","(8, 2) (4, 4)\n","\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"RzdLXvi_8MKn","colab_type":"text"},"source":["## Basics: array math\n"]},{"cell_type":"code","metadata":{"ExecuteTime":{"end_time":"2019-10-13T18:13:32.057925Z","start_time":"2019-10-13T18:13:32.005089Z"},"id":"1V26m-448MKo","colab_type":"code","outputId":"8cb35ac9-7a2b-4ee2-b11d-6f5bc4f81a8f","colab":{}},"source":["print(\"Initialize NumPy array, x1\")\n","x1 = np.ones([3, 3], dtype='float32')\n","x1[:, 0] = [2, 3, 4]\n","print(x1)\n","print()\n","\n","print(\"Transpose x1 array\")\n","print(x1.T)\n","print()\n","\n","print(\"Multiply by scalar, x2=x1*3\")\n","x2 = x1*3.\n","print(x2)\n","print()\n","\n","print(\"Element-wise sum, x1+x2\")\n","print(x1+x2)\n","print()\n","\n","print(\"Element-wise subtract, x1-x2\")\n","print(x1-x2)\n","print()\n","\n","print(\"Element-wise product, x1*x2\")\n","print(x1*x2)\n","print()\n","\n","print(\"Element-wise divide, x1/x2\")\n","print(x1/x2)\n","print()\n","\n","print(\"Element-wise power, x2^2\")\n","print(np.power(x2, 2))\n","print()\n","\n","print(\"Element-wise square root, sqrt(x2)\")\n","print(np.sqrt(x2))\n","print()\n","\n","print(\"Matrix multiplication, x1*x2\")\n","print(x1.dot(x2))\n","print()\n","\n","print(\"Vector multiplication, x1*x2[0]\")\n","print(x1.dot(x2[0]))\n","print()"],"execution_count":0,"outputs":[{"output_type":"stream","text":["Initialize NumPy array, x1\n","[[2. 1. 1.]\n"," [3. 1. 1.]\n"," [4. 1. 1.]]\n","\n","Transpose x1 array\n","[[2. 3. 4.]\n"," [1. 1. 1.]\n"," [1. 1. 1.]]\n","\n","Multiply by scalar, x2=x1*3\n","[[ 6. 3. 3.]\n"," [ 9. 3. 3.]\n"," [12. 3. 3.]]\n","\n","Element-wise sum, x1+x2\n","[[ 8. 4. 4.]\n"," [12. 4. 4.]\n"," [16. 4. 4.]]\n","\n","Element-wise subtract, x1-x2\n","[[-4. -2. -2.]\n"," [-6. -2. -2.]\n"," [-8. -2. -2.]]\n","\n","Element-wise product, x1*x2\n","[[12. 3. 3.]\n"," [27. 3. 3.]\n"," [48. 3. 3.]]\n","\n","Element-wise divide, x1/x2\n","[[0.33333334 0.33333334 0.33333334]\n"," [0.33333334 0.33333334 0.33333334]\n"," [0.33333334 0.33333334 0.33333334]]\n","\n","Element-wise power, x2^2\n","[[ 36. 9. 9.]\n"," [ 81. 9. 9.]\n"," [144. 9. 9.]]\n","\n","Element-wise square root, sqrt(x2)\n","[[2.4494898 1.7320508 1.7320508]\n"," [3. 1.7320508 1.7320508]\n"," [3.4641016 1.7320508 1.7320508]]\n","\n","Matrix multiplication, x1*x2\n","[[33. 12. 12.]\n"," [39. 15. 15.]\n"," [45. 18. 18.]]\n","\n","Vector multiplication, x1*x2[0]\n","[18. 24. 30.]\n","\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"ExecuteTime":{"end_time":"2019-10-13T15:46:53.797483Z","start_time":"2019-10-13T15:46:53.793264Z"},"id":"K-VvAo_y8MKr","colab_type":"text"},"source":["## Advanced: broadcasting\n","\n","Broadcasting allows universal functions to deal in a meaningful way with inputs that do not have exactly the same shape (see more at [this link](https://numpy.org/deavdocs/user/basics.broadcasting.html))"]},{"cell_type":"code","metadata":{"ExecuteTime":{"end_time":"2019-10-13T18:13:32.065016Z","start_time":"2019-10-13T18:13:32.059750Z"},"id":"hdhOb-_Z8MKr","colab_type":"code","outputId":"c114b5e2-eec8-4f4e-8f7f-6d67503c5d43","colab":{}},"source":["print(\"Add vector x2 to each row of matrix x1 using broadcasting mechanism\")\n","x1 = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10, 11], [12, 13], [14, 15]])\n","x2 = np.array([1, 3])\n","print(x1+x2)"],"execution_count":0,"outputs":[{"output_type":"stream","text":["Add vector x2 to each row of matrix x1 using broadcasting mechanism\n","[[ 1 4]\n"," [ 3 6]\n"," [ 5 8]\n"," [ 7 10]\n"," [ 9 12]\n"," [11 14]\n"," [13 16]\n"," [15 18]]\n"],"name":"stdout"}]}]} -------------------------------------------------------------------------------- /week1/PyTorch-intro.ipynb: -------------------------------------------------------------------------------- 1 | {"nbformat":4,"nbformat_minor":0,"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.7.3"},"colab":{"name":"PyTorch-intro.ipynb","provenance":[]}},"cells":[{"cell_type":"markdown","metadata":{"id":"1aG6DTtWokLY","colab_type":"text"},"source":["# Introduction to PyTorch\n","\n","Before reading this introduction you should know a bit of:\n","1. Python - look at [official tutorial](https://docs.python.org/3/tutorial/)\n","2. Linear Algebra and Matrices - look at [Coursera tutorial](https://www.coursera.org/learn/linear-algebra-machine-learning) and/or book [Introduction to Applied Linear Algebra](http://vmls-book.stanford.edu/vmls.pdf)\n","\n","\n","\n","
\n","\n","\n","From official NumPy page we could read that\n","\n","```\n","PyTorch is a Python-based scientific computing package targeted at two sets of audiences:\n","\n","* A replacement for NumPy to use the power of GPUs\n","* a deep learning research platform that provides maximum flexibility and speed\n","```\n","\n","Contrary to NumPy, PyTorch was designed mostly to work on **GPU**. PyTorch represents n-dimensional array object as `Tensor`. To install PyTorch library, go to [link](https://pytorch.org/get-started/locally/). There are also very good tutorials:\n","* [Official PyTorch tutorials](https://pytorch.org/tutorials/)\n","* [Deep Learning for Natural Language Processing with Pytorch](https://github.com/rguthrie3/DeepLearningForNLPInPytorch/blob/master/Deep%20Learning%20for%20Natural%20Language%20Processing%20with%20Pytorch.ipynb) \n","\n","Here we want give you a quick crash course of using PyTorch library, especially Tensor object. "]},{"cell_type":"markdown","metadata":{"id":"wk2YydsWokLb","colab_type":"text"},"source":["## Basics: creating a PyTorch tensor\n","\n","Important notes:\n","* all items in PyTorch array (a.k.a. `Tensor`) cantain only one data type e.g. `int8`, `float32`, ... ([all datatypes](https://pytorch.org/docs/stable/tensors.html))"]},{"cell_type":"code","metadata":{"ExecuteTime":{"end_time":"2019-10-13T18:13:36.933429Z","start_time":"2019-10-13T18:13:36.703426Z"},"id":"ZeKrtCJiokLc","colab_type":"code","outputId":"fb7ffe8d-ea99-4e85-a3d8-1d2bdada17d8","colab":{}},"source":["import torch \n","\n","print(\"1d Tensor from Python list (with `int32` type)\")\n","list1d = [0, 1, 2, 3, 4, 5, 6, 7]\n","tensor1d = torch.tensor(list1d, dtype=torch.int32) \n","print(tensor1d) # print tensor\n","print(tensor1d.size()) # print tensor shape\n","print()\n","\n","print(\"2d Tensor from Python list of lists (with `float32` type)\")\n","list2d = [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10, 11], [12, 13], [14, 15]]\n","tensor2d = torch.tensor(list2d, dtype=torch.float32)\n","print(tensor2d) # print tensor\n","print(tensor2d.size()) # print tensor shape\n","print()\n","\n","print(\"1d random tensor (with `float32` type)\")\n","tensor1d_random = torch.rand(8, 2, dtype=torch.float32)\n","print(tensor1d_random) # print tensor\n","print(tensor1d_random.size()) # print tensor shape\n","print()\n","\n","print(\"1d tensor with uniform distribution\")\n","tensor1d_uniform = torch.FloatTensor(16, 1).uniform_(-10, 10)\n","print(tensor1d_uniform) # print tensor\n","print(tensor1d_uniform.size()) # print tensor shape\n","print()\n","\n","print(\"1d tensor based on linearly spaced vector\")\n","tensor1d_linspace = torch.linspace(0, 7, steps=8, dtype=torch.float32)\n","print(tensor1d_linspace) # print tensor\n","print(tensor1d_linspace.size()) # print tensor shape\n","print()\n","\n","print(\"1d tensor based on `arange` mechanism\")\n","tensor1d_arange = torch.arange(0, 10, 3)\n","print(tensor1d_arange) # print tensor\n","print(tensor1d_arange.size()) # print tensor shape\n","print()\n","\n","print(\"2d zeros tensor\")\n","torch2d_zeros = torch.zeros([2, 4])\n","print(torch2d_zeros) # print tensor\n","print(torch2d_zeros.size()) # print tensor shape\n","print()\n","\n","print(\"2d ones tensor\")\n","torch2d_ones = torch.ones([2, 4])\n","print(torch2d_ones) # print tensor\n","print(torch2d_ones.size()) # print tensor shape\n","print()"],"execution_count":0,"outputs":[{"output_type":"stream","text":["1d Tensor from Python list (with `int32` type)\n","tensor([0, 1, 2, 3, 4, 5, 6, 7], dtype=torch.int32)\n","torch.Size([8])\n","\n","2d Tensor from Python list of lists (with `float32` type)\n","tensor([[ 0., 1.],\n"," [ 2., 3.],\n"," [ 4., 5.],\n"," [ 6., 7.],\n"," [ 8., 9.],\n"," [10., 11.],\n"," [12., 13.],\n"," [14., 15.]])\n","torch.Size([8, 2])\n","\n","1d random tensor (with `float32` type)\n","tensor([[0.2339, 0.1720],\n"," [0.7893, 0.9454],\n"," [0.3148, 0.3166],\n"," [0.7262, 0.8609],\n"," [0.0912, 0.0767],\n"," [0.5113, 0.8339],\n"," [0.7812, 0.9421],\n"," [0.1299, 0.1272]])\n","torch.Size([8, 2])\n","\n","1d tensor with uniform distribution\n","tensor([[-6.8103],\n"," [ 0.8485],\n"," [ 6.5259],\n"," [ 2.5182],\n"," [ 0.6380],\n"," [-3.1469],\n"," [-3.4691],\n"," [ 4.6706],\n"," [ 2.0206],\n"," [-8.8526],\n"," [-3.8986],\n"," [ 0.9371],\n"," [-9.4456],\n"," [-8.4750],\n"," [-1.1130],\n"," [ 3.2142]])\n","torch.Size([16, 1])\n","\n","1d tensor based on linearly spaced vector\n","tensor([0., 1., 2., 3., 4., 5., 6., 7.])\n","torch.Size([8])\n","\n","1d tensor based on `arange` mechanism\n","tensor([0, 3, 6, 9])\n","torch.Size([4])\n","\n","2d zeros tensor\n","tensor([[0., 0., 0., 0.],\n"," [0., 0., 0., 0.]])\n","torch.Size([2, 4])\n","\n","2d ones tensor\n","tensor([[1., 1., 1., 1.],\n"," [1., 1., 1., 1.]])\n","torch.Size([2, 4])\n","\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"r0xD9G7TokLh","colab_type":"text"},"source":["## Basics: extracting specific values from tensors\n","\n","Important notes:\n","* tensor can be indexed using the standard Python x[obj] syntax, wherea x is the array and obj the selection"]},{"cell_type":"code","metadata":{"ExecuteTime":{"end_time":"2019-10-13T18:13:36.942523Z","start_time":"2019-10-13T18:13:36.935148Z"},"id":"SLhk6f7uokLi","colab_type":"code","outputId":"d828dc61-1e0d-4568-dc1d-dbf7976325e8","colab":{}},"source":["print(\"Get specific element\")\n","print(tensor2d[1,1])\n","print()\n","\n","print(\"The basic slice syntax is i:j:k where i is the starting index, j is the stopping index, and k is the step\")\n","print(tensor1d[0:6:2])\n","print()\n","\n","print(\"Extract only one dimension from multidimensional ndarray\") \n","print(tensor2d[:, 0])\n","print()\n","\n","print(\"Boolean array indexing\") \n","print(tensor1d[([True, False, True, False, True, False, True, False])])\n","print()\n","\n","print(\"Using condition statement for indexing array\") \n","print(tensor1d[(tensor1d % 2 == 0)]) \n","print()"],"execution_count":0,"outputs":[{"output_type":"stream","text":["Get specific element\n","tensor(3.)\n","\n","The basic slice syntax is i:j:k where i is the starting index, j is the stopping index, and k is the step\n","tensor([0, 2, 4], dtype=torch.int32)\n","\n","Extract only one dimension from multidimensional ndarray\n","tensor([ 0., 2., 4., 6., 8., 10., 12., 14.])\n","\n","Boolean array indexing\n","tensor([0, 2, 4, 6], dtype=torch.int32)\n","\n","Using condition statement for indexing array\n","tensor([0, 2, 4, 6], dtype=torch.int32)\n","\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"LxsaRlGkokLk","colab_type":"text"},"source":["## Basics: sum, min, max, mean, reshape "]},{"cell_type":"code","metadata":{"ExecuteTime":{"end_time":"2019-10-13T18:13:36.959263Z","start_time":"2019-10-13T18:13:36.944221Z"},"id":"fKwA8BISokLl","colab_type":"code","outputId":"216dcc03-23a8-4371-e58b-e943d167cacf","colab":{}},"source":["print(\"represent `not a number value`\")\n","print(torch.tensor(float('nan')))\n","print()\n","\n","print(\"represent `infinite`\")\n","print(torch.tensor(float('Inf')))\n","print()\n","\n","print(\"calculate mean, max and min in tensor\")\n","print(\"sum \", tensor2d.sum())\n","print(\"max \", tensor2d.max())\n","print(\"min \", tensor2d.min())\n","print(\"mean \", tensor2d.mean())\n","print()\n","\n","print(\"calculate max on different axis\")\n","print(\"column max: \", tensor2d.max(dim=0)[0])\n","print(\"row max: \", tensor2d.max(dim=1)[0])\n","print()\n","\n","print(\"reshape 2d tensor\")\n","print(tensor2d.view(4, 4))\n","print(tensor2d.size(), tensor2d.view(4, 4).size())\n","print()\n","\n","print(\"reshape 2d tensor (second way)\")\n","print(tensor2d.view(4, -1)) # the second dimention is adjusted to size of the matrix\n","print(tensor2d.size(), tensor2d.view(4, -1).size())\n","print()"],"execution_count":0,"outputs":[{"output_type":"stream","text":["represent `not a number value`\n","tensor(nan)\n","\n","represent `infinite`\n","tensor(inf)\n","\n","calculate mean, max and min in tensor\n","sum tensor(120.)\n","max tensor(15.)\n","min tensor(0.)\n","mean tensor(7.5000)\n","\n","calculate max on different axis\n","column max: tensor([14., 15.])\n","row max: tensor([ 1., 3., 5., 7., 9., 11., 13., 15.])\n","\n","reshape 2d tensor\n","tensor([[ 0., 1., 2., 3.],\n"," [ 4., 5., 6., 7.],\n"," [ 8., 9., 10., 11.],\n"," [12., 13., 14., 15.]])\n","torch.Size([8, 2]) torch.Size([4, 4])\n","\n","reshape 2d tensor (second way)\n","tensor([[ 0., 1., 2., 3.],\n"," [ 4., 5., 6., 7.],\n"," [ 8., 9., 10., 11.],\n"," [12., 13., 14., 15.]])\n","torch.Size([8, 2]) torch.Size([4, 4])\n","\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"MfFeOqbwokLn","colab_type":"text"},"source":["## Basics: tensor math"]},{"cell_type":"code","metadata":{"ExecuteTime":{"end_time":"2019-10-13T18:13:36.979309Z","start_time":"2019-10-13T18:13:36.961184Z"},"id":"u9qNagd4okLo","colab_type":"code","outputId":"af6f17cc-b86a-47f6-ca9e-ffadf4a005dc","colab":{}},"source":["print(\"Initialize tensor, x1\")\n","x1 = torch.ones([3, 3], dtype=torch.float32)\n","x1[:, 0] = torch.tensor([2., 3., 4.])\n","print(x1)\n","print()\n","\n","print(\"Transpose x1 tensor\")\n","print(x1.transpose(0, 1))\n","print()\n","\n","print(\"Multiply by scalar, x2=x1*3\")\n","x2 = x1*3.\n","print(x2)\n","print()\n","\n","print(\"Element-wise sum, x1+x2\")\n","print(x1+x2)\n","print()\n","\n","print(\"Element-wise subtract, x1-x2\")\n","print(x1-x2)\n","print()\n","\n","print(\"Element-wise product, x1*x2\")\n","print(x1*x2)\n","print()\n","\n","print(\"Element-wise divide, x1/x2\")\n","print(x1/x2)\n","print()\n","\n","print(\"Element-wise power, x2^2\")\n","print(torch.pow(x2, 2))\n","print()\n","\n","print(\"Element-wise square root, sqrt(x2)\")\n","print(torch.sqrt(x2))\n","print()\n","\n","print(\"Matrix multiplication, x1*x2\")\n","print(x1.mm(x2))\n","print()\n","\n","print(\"Vector multiplication, x1*x2[0]\")\n","print(x1.mm(x2[0].view([-1, 1])).view(-1))\n","print()"],"execution_count":0,"outputs":[{"output_type":"stream","text":["Initialize tensor, x1\n","tensor([[2., 1., 1.],\n"," [3., 1., 1.],\n"," [4., 1., 1.]])\n","\n","Transpose x1 tensor\n","tensor([[2., 3., 4.],\n"," [1., 1., 1.],\n"," [1., 1., 1.]])\n","\n","Multiply by scalar, x2=x1*3\n","tensor([[ 6., 3., 3.],\n"," [ 9., 3., 3.],\n"," [12., 3., 3.]])\n","\n","Element-wise sum, x1+x2\n","tensor([[ 8., 4., 4.],\n"," [12., 4., 4.],\n"," [16., 4., 4.]])\n","\n","Element-wise subtract, x1-x2\n","tensor([[-4., -2., -2.],\n"," [-6., -2., -2.],\n"," [-8., -2., -2.]])\n","\n","Element-wise product, x1*x2\n","tensor([[12., 3., 3.],\n"," [27., 3., 3.],\n"," [48., 3., 3.]])\n","\n","Element-wise divide, x1/x2\n","tensor([[0.3333, 0.3333, 0.3333],\n"," [0.3333, 0.3333, 0.3333],\n"," [0.3333, 0.3333, 0.3333]])\n","\n","Element-wise power, x2^2\n","tensor([[ 36., 9., 9.],\n"," [ 81., 9., 9.],\n"," [144., 9., 9.]])\n","\n","Element-wise square root, sqrt(x2)\n","tensor([[2.4495, 1.7321, 1.7321],\n"," [3.0000, 1.7321, 1.7321],\n"," [3.4641, 1.7321, 1.7321]])\n","\n","Matrix multiplication, x1*x2\n","tensor([[33., 12., 12.],\n"," [39., 15., 15.],\n"," [45., 18., 18.]])\n","\n","Vector multiplication, x1*x2[0]\n","tensor([18., 24., 30.])\n","\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"ExecuteTime":{"end_time":"2019-10-13T18:08:48.844141Z","start_time":"2019-10-13T18:08:48.839846Z"},"id":"m-TIC6l7okLq","colab_type":"text"},"source":["## Advanced: broadcasting\n","\n","In short, if a PyTorch operation supports broadcast, then its Tensor arguments can be automatically expanded to be of equal sizes (see more at [this link](https://pytorch.org/docs/stable/notes/broadcasting.html))"]},{"cell_type":"code","metadata":{"ExecuteTime":{"end_time":"2019-10-13T18:13:36.990697Z","start_time":"2019-10-13T18:13:36.981482Z"},"id":"0Ox25KQSokLr","colab_type":"code","outputId":"838b2457-843e-45b7-efa5-a109eef4aadc","colab":{}},"source":["print(\"Add vector x2 to each row of matrix x1 using broadcasting mechanism\")\n","x1 = torch.tensor([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10, 11], [12, 13], [14, 15]])\n","x2 = torch.tensor([1, 3])\n","print(x1+x2)"],"execution_count":0,"outputs":[{"output_type":"stream","text":["Add vector x2 to each row of matrix x1 using broadcasting mechanism\n","tensor([[ 1, 4],\n"," [ 3, 6],\n"," [ 5, 8],\n"," [ 7, 10],\n"," [ 9, 12],\n"," [11, 14],\n"," [13, 16],\n"," [15, 18]])\n"],"name":"stdout"}]}]} -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CE7455: Deep Learning For Natural Language Processing 2 | 3 | # Course Objectives 4 | 5 | Natural language processing (NLP) is one of the most important fields in artificial intelligence (AI). It has become very crucial in the information age because most of the information is in the form of unstructured text. NLP technologies are applied everywhere as people communicate mostly in language: language translation, web search, customer support, emails, forums, advertisement, radiology reports, to name a few. 6 | 7 | There are several core NLP tasks and machine learning models behind NLP applications. Deep learning, a sub-field of machine learning, has recently brought a paradigm shift from traditional task-specific feature engineering to end-to-end systems and has obtained high performance across many different NLP tasks and downstream applications. Tech companies like Google, Baidu, Alibaba, Apple, Amazon, Facebook, Tencent, and Microsoft are now actively working on deep learning methods to improve their products. For example, Google recently replaced its traditional statistical machine translation and speech-recognition systems with systems based on deep learning methods. 8 | 9 | **Optional Textbooks** 10 | 11 | - Deep Learning by Goodfellow, Bengio, and Courville [free online](http://www.deeplearningbook.org/) 12 | - Machine Learning — A Probabilistic Perspective by Kevin Murphy [online](https://doc.lagout.org/science/Artificial%20Intelligence/Machine%20learning/Machine%20Learning_%20A%20Probabilistic%20Perspective%20%5BMurphy%202012-08-24%5D.pdf) 13 | - Natural Language Processing by Jacob Eisenstein [free online](https://github.com/jacobeisenstein/gt-nlp-class/blob/master/notes/eisenstein-nlp-notes.pdf) 14 | - Speech and Language Processing by Dan Jurafsky and James H. Martin [(3rd ed. draft)](https://web.stanford.edu/~jurafsky/slp3/) 15 | 16 | # Intended Learning Outcomes 17 | 18 | In this course, students will learn state-of-the-art deep learning methods for NLP. Through lectures and practical assignments, students will learn the necessary tricks for making their models work on practical problems. They will learn to implement, and possibly invent their deep learning models using available deep learning libraries like [Pytorch](https://pytorch.org/). 19 | 20 | **Our Approach** 21 | 22 | - Thorough and Detailed: How to write from scratch, debug, and train deep neural models 23 | 24 | - State of the art: Most lecture materials are new from the research world in the past 1-5 years. 25 | 26 | - Practical: Focus on practical techniques for training the models on GPUs. 27 | 28 | - Fun: Cover exciting new advancements in NLP (e.g., Transformer, BERT). 29 | 30 | # Assessment Approach 31 | 32 | **Weekly Workload** 33 | 34 | - Lecture and practical problems implemented in PyTorch. 35 | - There will be NO office hours. 36 | 37 | **Assignments (individually graded)** 38 | 39 | - Two (2) assignments will contribute to ***2 * 25% = 50%*** of the total assessment. 40 | - Students will be graded individually on the assignments. They will be allowed to discuss with each other on the homework assignments, but they are required to submit individual write-ups and coding exercises. 41 | 42 | **Final Project (Group work but individually graded)** 43 | 44 | - There will be a final project contributing to the remaining 50% of the total coursework assessment. 45 | - ***3–5*** students per group 46 | - Project proposal: ***5%***, presentation: ***15%***, report: ***30%*** 47 | - The project will be a group work. Students will be graded individually, depending on their contribution to the group. The final project presentation will ensure the student’s understanding of the project. 48 | 49 | # Course Prerequisites 50 | 51 | - Proficiency in Python (using Numpy and PyTorch). There is a lecture for those who are not familiar with Python. 52 | - Linear Algebra, basic Probability and Statistics 53 | - Machine Learning basics 54 | 55 | # Teaching 56 | 57 | ## Instructors 58 | 59 |

Wang Wenya (Part 1)

60 | 61 |

Luu Anh Tuan (Part 2)

62 | 63 | ## Teaching Assistants 64 | 65 |

He Qiyuan (Part 1)

66 |

qiyuan001@e.ntu.edu.sg

67 | 68 |

Nguyen Tran Cong Duy (Part 2)

69 |

nguyentr003@e.ntu.edu.sg

70 | 71 | # Schedule & Course Content 72 | 73 | ## Week 1: Introduction 74 | 75 | [Lecture Slide](https://drive.google.com/file/d/1cVAxrntk1Q4R_LC1zFQufkfyakG21E-y/view?usp=sharing) 76 | 77 | ### Lecture Content 78 | 79 | - What is Natural Language Processing? 80 | - Why is language understanding difficult? 81 | - What is Deep Learning? 82 | - Deep learning vs. other machine learning methods? 83 | - Why deep learning for NLP? 84 | - Applications of deep learning to NLP 85 | - Knowing the target group (background, field of study, programming experience) 86 | - Expectation from the course 87 | 88 | ### Python & PyTorch Basics 89 | 90 | - Programming in Python 91 | 92 | - Jupiter Notebook and [Google Colab](https://colab.research.google.com/drive/16pBJQePbqkz3QFV54L4NIkOn1kwpuRrj) 93 | - [Introduction to Python](https://colab.research.google.com/drive/1bQG32CFoMZ-jBk02uaFon60tER3yFx4c) 94 | - Deep Learning Frameworks 95 | - Why Pytorch? 96 | - [Deep learning with PyTorch](https://drive.google.com/file/d/1c33y8bkdr7SJ_I8-wmqTAhld-y7KcspA/view?usp=sharing) 97 | - [Supplementary] 98 | - Numerical programming with Numpy/Scipy - [Numpy intro](https://drive.google.com/file/d/1cUzRzQGURrCKes8XynvTTA4Zvl_gUJdc/view?usp=sharing) 99 | - Numerical programming with Pytorch - [Pytorch intro](https://drive.google.com/file/d/18cgPOj2QKQN0WR9_vXoz6BoravvS9mTm/view?usp=sharing) 100 | 101 | 102 | ## Week 2: Machine Learning Basics 103 | 104 | [Lecture Slide](https://docs.google.com/presentation/d/1oRnv9yGMOm2vbznzS7ii4TB7PCxVD1bawtAGBAj0kwU/edit?usp=sharing) 105 | 106 | ### Lecture Content 107 | 108 | - What is Machine Learning? 109 | - Machine learning for text classification 110 | - Naive Bayes 111 | - Logistic Regression 112 | - Multi-class classification 113 | - Gradient-based optimization 114 | 115 | ### Practical exercise with Pytorch 116 | 117 | - [Deep learning with PyTorch](https://colab.research.google.com/drive/1aZVfsPUko-ugt1TVCmRwqGJXlxEJVaTq?usp=sharing) 118 | - [Linear Regression](https://colab.research.google.com/drive/12QpBf7x_Jt6-zypN4OrUFFHXz1u6CmYe?usp=sharing) 119 | - [Logistic Regression](https://colab.research.google.com/drive/1nTrYW5dUu6WO9cx7SGEvP9oX7qRbsGJk?usp=sharing) 120 | - [Supplementary] 121 | - Numerical programming with Pytorch - [Pytorch intro](https://drive.google.com/file/d/18cgPOj2QKQN0WR9_vXoz6BoravvS9mTm/view?usp=sharing) 122 | 123 | 124 | 125 | ## Week 3: Neural Networks & Optimization Basics 126 | 127 | [Lecture Slide](https://drive.google.com/file/d/1jc9T1-6WJ6GizPyRXKq7boLl0G9DgH9D/view?usp=sharing) 128 | 129 | ### Lecture Content 130 | 131 | - From Logistic Regression to Feed-forward NN 132 | - Activation functions 133 | - SGD with Backpropagation 134 | - Adaptive SGD (adagrad, adam, RMSProp) 135 | - Regularization (Dropout, Batch normalization, L1/L2 norm, Gradient clipping) 136 | 137 | ### Practical exercise with Pytorch 138 | 139 | - [Deep learning with PyTorch](https://colab.research.google.com/drive/1aZVfsPUko-ugt1TVCmRwqGJXlxEJVaTq?usp=sharing) 140 | - [Numpy notebook](https://colab.research.google.com/drive/1IAonxZnZjJb0_xUVWHt5atIxaI5GTJQ2) [Pytorch notebook](https://colab.research.google.com/drive/1YzZrMAmJ3hjvJfNIdGxae9kxGABG6yaT) 141 | - Backpropagation 142 | - Dropout 143 | - Batch normalization 144 | - Initialization 145 | - Gradient clipping 146 | 147 | 148 | ## Week 4: Word Vectors 149 | 150 | [Lecture Slide](https://drive.google.com/file/d/1uLkxAm1vDEPBvYn5lOkMHTtPxSiMOyU5/view?usp=sharing) 151 | 152 | 153 | [Project Proposal Instruction](https://docs.google.com/document/d/1i6QlzNX-HDjkW5HwRTLX8lYC6F4tCwhO40oqUItMP_s/edit?usp=sharing) 154 | 155 | [Group Allocation (form your own group)](https://docs.google.com/spreadsheets/d/13zTwv7U5AFvr7SfBUFFT2W7tWx8PCJm1kVUqpVPFqWg/edit?usp=sharing) 156 | 157 | 158 | ### Lecture Content 159 | 160 | - Word meaning 161 | - Distributed representation of words 162 | - Word2Vec models (Skip-gram, CBOW) 163 | - Negative sampling 164 | - Glove 165 | - FastText 166 | - Evaluating word vectors 167 | - Intrinsic evaluation 168 | - Extrinsic evaluation 169 | - Cross-lingual word embeddings 170 | 171 | ### Practical exercise with Pytorch 172 | 173 | [Skip-gram training](https://colab.research.google.com/drive/164dB-Vemzwavf1ffqDDVNtx7Y5VtcmQh) 174 | 175 | ### Suggested Readings 176 | 177 | - Word2Vec Tutorial - The Skip-Gram Model, [blog](http://mccormickml.com/2016/04/19/word2vec-tutorial-the-skip-gram-model/) 178 | - [Efficient Estimation of Word Representations in Vector Space](https://arxiv.org/abs/1301.3781) - Original word2vec paper 179 | - [Distributed Representations of Words and Phrases and their Compositionality](https://papers.nips.cc/paper/5021-distributed-representations-of-words-and-phrases-and-their-compositionality.pdf) - negative sampling paper 180 | - [GloVe: Global Vectors for Word Representation](https://nlp.stanford.edu/pubs/glove.pdf) 181 | - [FastText: Enriching Word Vectors with Subword Information](https://www.mitpressjournals.org/doi/abs/10.1162/tacl_a_00051?mobileUi=0) 182 | - [Linguistic Regularities in Sparse and Explicit Word Representations.](https://levyomer.files.wordpress.com/2014/04/linguistic-regularities-in-sparse-and-explicit-word-representations-conll-2014.pdf) 183 | - [Neural Word Embeddings as Implicit Matrix Factorization.](https://arxiv.org/abs/1702.02098) 184 | - [Survey on Cross-lingual embedding methods](https://arxiv.org/abs/1706.04902) 185 | - [Slides on Cross-lingual embedding](https://www.dropbox.com/s/3eq5apr75yrz9ix/Cross-lingual%20word%20embeddings%20and%20beyond.pdf?dl=0) 186 | - [Adversarial autoencoder for unsupervised word translation](https://arxiv.org/abs/1904.04116) 187 | - [Evaluating Cross-Lingual Word Embeddings](https://www.aclweb.org/anthology/P19-1070) 188 | 189 | 190 | ## Week 5: Window-based Approach and Convolutional Nets 191 | 192 | [Lecture Slide](https://drive.google.com/file/d/1MpVGB3UBvPhUdcuj0r_ui_WkoMJ5IBUE/view?usp=sharing) 193 | 194 | ### Lecture Content 195 | 196 | - Classification tasks in NLP 197 | - Window-based Approach for language modeling 198 | - Window-based Approach for NER, POS tagging, and Chunking 199 | - Convolutional Neural Net for NLP 200 | - Loss Functions 201 | 202 | ### Suggested Readings 203 | 204 | - [Linear Algebraic Structure of Word Senses, with Applications to Polysemy](https://transacl.org/ojs/index.php/tacl/article/viewFile/1346/320) 205 | - [Improving Distributional Similarity with Lessons Learned from Word Embeddings](https://www.aclweb.org/anthology/Q15-1016/) 206 | - [Natural Language Processing (Almost) from Scratch](http://www.jmlr.org/papers/volume12/collobert11a/collobert11a.pdf) 207 | - [Convolutional Neural Networks for Sentence Classification](https://arxiv.org/abs/1408.5882) 208 | - [Fast and Accurate Entity Recognition with Iterated Dilated Convolutions](https://arxiv.org/abs/1702.02098) 209 | 210 | ### Practical exercise with Pytorch 211 | 212 | [Named Entity Recognition](https://colab.research.google.com/drive/1aDFQtSCYBpgRwkj9eq8Rao9eq5_68C31) 213 | 214 | [CNN for text classification](https://colab.research.google.com/drive/1r3qUTV0AQBV9cUAF0GML6Yu3aMfPdBGQ#scrollTo=mc6ZP_xosSil) 215 | 216 | 217 | ## Week 6: Recurrent Neural Nets 218 | 219 | [Lecture Slide](https://drive.google.com/file/d/1jGUvuEp1rhNXsXK-BBpnlmmucqGt4uLc/view?usp=sharing) 220 | 221 | 222 | Assignment 1 is out [here](https://docs.google.com/document/d/1oVHB2kby-G9ikIdS6dxnIMiDz1gPbQw4dd7_QghXt7M/edit?usp=sharing). **Deadline: 23 March 2025**. 223 | 224 | ### Lecture Content 225 | 226 | 227 | - Language modeling with RNNs 228 | - Backpropagation through time 229 | - Text generation with RNN LM 230 | - Sequence labeling with RNNs 231 | - Sequence classification with RNNs 232 | - Issues with Vanilla RNNs 233 | - Gated Recurrent Units (GRUs) and LSTMs 234 | - Bidirectional RNNs 235 | - Multi-layer RNNs 236 | - Recursive Neural Nets 237 | 238 | ### Practical exercise with Pytorch (RNN for POS Tagging) 239 | 240 | - [POS Tagging](https://colab.research.google.com/drive/1a_0JGhg-N9pItzUDwfULtuxdbnh1m1y8) 241 | 242 | ### Suggested Readings 243 | 244 | - [N-gram Language Models](https://web.stanford.edu/~jurafsky/slp3/3.pdf) 245 | - [Karpathy’s nice blog on Recurrent Neural Networks](http://karpathy.github.io/2015/05/21/rnn-effectiveness/) 246 | - [Building an Efficient Neural Language Model](https://research.fb.com/building-an-efficient-neural-language-model-over-a-billion-words/) 247 | - [On the difficulty of training recurrent neural networks](http://proceedings.mlr.press/v28/pascanu13.pdf) 248 | - [Colah’s blog on LSTMs/GRUs](http://colah.github.io/posts/2015-08-Understanding-LSTMs/) 249 | - [Neural Architectures for Named Entity Recognition](https://www.aclweb.org/anthology/N16-1030/) 250 | - [Fine-grained Opinion Mining with Recurrent Neural Networks and Word Embeddings](https://www.aclweb.org/anthology/D15-1168/) 251 | 252 | 253 | 254 | ## Week 7: Machine translation and Seq2Seq Models 255 | 256 | [Lecture Slide](https://drive.google.com/file/d/1BxNxakiFSN9FYEbA2VKSuHQSmWA1sXyz/view?usp=sharing) 257 | 258 | ### Lecture Content 259 | 260 | - Machine translation 261 | - Early days (1950s) 262 | - Statistical machine translation or SMT (1990-2010) 263 | - Alignment in SMT 264 | - Neural machine translation or NMT (2014 - ) 265 | - Encoder-decoder model for NMT 266 | - Advantages and disadvantages of NMT 267 | - Decoding strategies such as beam search and sampling 268 | - MT evaluation 269 | - Other evaluation methods for language generation 270 | - Review 271 | 272 | ### Suggested Readings 273 | 274 | - [Statistical Machine Translation slides, CS224n 2015 (lectures 2/3/4)](https://web.stanford.edu/class/archive/cs/cs224n/cs224n.1162/syllabus.shtml) 275 | - [Sequence to Sequence Learning with Neural Networks (original seq2seq NMT paper)](https://arxiv.org/pdf/1409.3215.pdf) 276 | - [Statistical Machine Translation (book by Philipp Koehn)](https://www.cambridge.org/core/books/statistical-machine-translation/94EADF9F680558E13BE759997553CDE5) 277 | - [A Neural Conversational Model](https://arxiv.org/abs/1506.05869) 278 | - [BLEU (original paper)](https://www.aclweb.org/anthology/P02-1040.pdf) 279 | 280 | ### Practical exercise with Pytorch 281 | 282 | - [Neural Machine Translation](https://colab.research.google.com/drive/1htCUnTIFY9NAhbANuuZEeHGxvfPGHBdK) 283 | 284 | 285 | ## Week 8: Seq2Seg Models, Attentions, Subwords 286 | 287 | [Lecture Slide](https://drive.google.com/file/d/14stMogPaHp6-s2bQqo-eR1FvzJ5Dt_T1/view?usp=share_link) 288 | 289 | ### Lecture Content 290 | 291 | - Information bottleneck issue with vanilla Seq2Seq 292 | - Attention to the rescue 293 | - Details of attention mechanism 294 | - Sub-word models 295 | - Byte-pair encoding 296 | - Hybrid models 297 | 298 | 299 | ### Practical exercise with Pytorch 300 | 301 | - [Neural machine translation tutorial in pytorch](https://colab.research.google.com/drive/1cYyBxmdjFjKls0CEsPc8WIHfxPDy4eSq) 302 | 303 | 304 | ### Suggested Readings 305 | 306 | - [Neural Machine Translation by Jointly Learning to Align and Translate (original seq2seq+attention paper)](https://arxiv.org/pdf/1409.0473.pdf) 307 | - [Effective Approaches to Attention-based Neural Machine Translation](https://nlp.stanford.edu/~lmthang/data/papers/emnlp15_attn.pdf) 308 | - [Achieving Open Vocabulary Neural Machine Translation with Hybrid Word-Character Models](https://arxiv.org/abs/1604.00788) 309 | 310 | 311 | 312 | ## Week 9: Seq2Seq Variants and Transformer 313 | 314 | [Lecture Slide](https://drive.google.com/file/d/1wWxMg_tIVdShRccSyH-v38g-LKmOcO3L/view?usp=sharing) 315 | 316 | ### Lecture Content 317 | 318 | - Seq2Seq Variants (Pointer nets, Pointer Generator Nets) 319 | - Machine Translation 320 | - Summarization 321 | - Transformer architecture 322 | - Self-attention 323 | - Positional encoding 324 | - Multi-head attention 325 | 326 | [The Annotated Transformer](https://nlp.seas.harvard.edu/annotated-transformer/) 327 | 328 | ### Suggested Readings 329 | 330 | - [Get To The Point: Summarization with Pointer-Generator Networks](https://arxiv.org/abs/1704.04368) 331 | - [Pointer Networks](https://papers.nips.cc/paper/5866-pointer-networks) 332 | - [Stack-Pointer Networks for Dependency Parsing](https://www.aclweb.org/anthology/P18-1130.pdf) 333 | - [A Unified Linear-Time Framework for Sentence-Level Discourse Parsing](https://arxiv.org/abs/1905.05682) 334 | - [Attention Is All You Need](https://arxiv.org/abs/1706.03762) 335 | - [The Illustrated Transformer](https://jalammar.github.io/illustrated-transformer/) 336 | - [Resurrecting Submodularity in Neural Abstractive Summarization](https://arxiv.org/abs/1911.03014) 337 | 338 | 339 | 340 | ## Week 10: Contextual embeddings and self-supervised learning 341 | 342 | [Lecture Slide](https://drive.google.com/file/d/18upNeGM8YdUMl9dn-FqfVrIeFVfb3mtb/view?usp=share_link) 343 | 344 | 345 | 346 | ### Lecture Content 347 | 348 | - Why semi-supervsied? 349 | - Semisupervised learning dimensions 350 | - Pre-training and fine-tuning methods 351 | 352 | - CoVe 353 | - TagLM 354 | - ELMo 355 | - GPT 356 | - ULMfit 357 | - BERT 358 | - BART 359 | - Evaluation benchmarks 360 | 361 | - GLUE 362 | - SQuAD 363 | - NER 364 | - SuperGLUE 365 | - XNLI 366 | 367 | [Pre-train Fine-tune with HF](https://colab.research.google.com/drive/1L_hwnQISoIBrH7W_r83I62hJ4FBlfNsz?usp=sharing) 368 | 369 | ### Suggested Readings 370 | 371 | - [Cove Paper](https://arxiv.org/pdf/1708.00107.pdf) 372 | - [ULMFit paper](https://arxiv.org/abs/1801.06146) 373 | - [BERT Paper](https://arxiv.org/abs/1810.04805) 374 | - [ELMo paper](https://arxiv.org/abs/1802.05365) 375 | - [BART paper](https://arxiv.org/abs/1910.13461) 376 | 377 | 378 | ## Week 11: Large Language Models & Multilingual NLP 379 | 380 | 381 | Assignment 2 is out [here](https://docs.google.com/document/d/1AonpEdaFc3Tgu0RdEux-ZMaJ0-RHOrPp/edit?usp=sharing&ouid=118265590333925180950&rtpof=true&sd=true). **Deadline: 23 Apr 2025, 11:59pm**. 382 | 383 | [Final project report instruction](https://drive.google.com/file/d/1zV7cV4hOMWaq2NcvoHumkx6U16HDATeL/view?usp=sharing) 384 | 385 | [Lecture Slide](https://drive.google.com/file/d/1L5H0_e7tVQw-lBELcejppVs2DTY6hb2b/view?usp=share_link) 386 | 387 | ### Lecture Content 388 | 389 | - Large Language Models 390 | - Examples of Large Language Models 391 | - Multilingual NLP 392 | - Why do we need Multilingual NLP? 393 | - Low-resource NLP 394 | - Cross-lingual models 395 | - Multilingual models 396 | 397 | ### Suggested Readings 398 | 399 | - [XLM paper](https://arxiv.org/abs/1901.07291) 400 | - [Transformer XL paper](https://arxiv.org/pdf/1901.02860.pdf) 401 | - [XLNet paper](https://arxiv.org/abs/1906.08237) 402 | - [mBART paper](https://arxiv.org/abs/2001.08210) 403 | 404 | ## Week 12: LLM Prompting 405 | 406 | [Lecture Slide](https://drive.google.com/file/d/1bH3rNigKxn3mhJ8KKUeaTi6mMLiRL0SH/view?usp=sharing) 407 | 408 | ### Lecture Content 409 | 410 | - Chain-of-Thought Prompting 411 | - Self-Consistency Improves Chain of Thought Reasoning in Language Models 412 | - Program of Thoughts Prompting 413 | - Least-to-Most Prompting Enables Complex Reasoning in Large Language Models 414 | - Measuring and Narrowing the Compositionality Gap in Language Models 415 | 416 | 437 | -------------------------------------------------------------------------------- /week1/PytorchTut.ipynb: -------------------------------------------------------------------------------- 1 | {"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"PytorchTut.ipynb","provenance":[]},"kernelspec":{"name":"python3","display_name":"Python 3"}},"cells":[{"cell_type":"markdown","metadata":{"id":"q-nLVTY_LwXh"},"source":["# Pytorch Basics\n","\n","\n","#### Table of contents\n","0. Pytorch tensors\n","\n","1. Basic autograd example 1\n","\n","2. Basic autograd example 2\n","\n","3. Loading data from numpy \n","\n","4. Input pipeline \n","\n","5. Input pipeline for custom dataset \n","\n","6. Pretrained model \n","\n","7. Save and load model\n","\n","8. Train a simple MNIST Neural nets\n","\n","\n"]},{"cell_type":"code","metadata":{"id":"LeO-UXC-OZ_A"},"source":["import torch \n","import torchvision\n","import torch.nn as nn\n","import numpy as np\n","import torchvision.transforms as transforms\n","import matplotlib.pyplot as plt\n"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"bwEVQF5xagPM"},"source":["##0. Pytorch tensors"]},{"cell_type":"code","metadata":{"id":"e4YZOt2tajAB"},"source":["# Very similar to numpy\n","x = torch.tensor(1.)\n","print(x)\n","\n","x = torch.tensor([1,2,3,4,5]).float()\n","print(x)\n","print(x.size())\n","\n","ran = torch.Tensor(2,5,5).uniform_()"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"rQjMkMYia60M"},"source":["# min, max, mean, reshaping\n","ran = torch.Tensor(2,5,5).uniform_()\n","print(ran)\n","print(ran.size())\n","print(ran.min())\n","print(ran.max())\n","print(ran.mean())\n","rsan = ran.view(5,2,5)\n","print(rsan)\n","print(rsan.size())"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"hZthx9bibTog"},"source":["# Tensor math\n","x = torch.Tensor(2, 4, 5).uniform_()\n","y = torch.Tensor(2, 4, 5).uniform_()\n","print(x.size())\n","print(y.size())\n","print(x)\n","print(y)\n","a = x + y\n","m = x * y\n","# transpose of y (the last 2 dimension)\n","yt = y.transpose(1, 2)\n","print(yt.size())\n","matmul = torch.matmul(x, yt)\n","print(matmul.size())\n","print(matmul)"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"NvmBBRDnPjIH"},"source":["##1. Basic autograd example 1"]},{"cell_type":"code","metadata":{"id":"5iHZVz5GOdbb"},"source":["# Create tensors.\n","x = torch.tensor(1., requires_grad=True)\n","w = torch.tensor(2., requires_grad=True)\n","b = torch.tensor(3., requires_grad=True)\n","\n","# Build a computational graph.\n","y = w * x + b # y = 2 * x + 3\n","\n","# Compute gradients.\n","y.backward()\n","\n","# Print out the gradients.\n","print(x.grad) # x.grad = 2 \n","print(w.grad) # w.grad = 1 \n","print(b.grad) # b.grad = 1 \n"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"9nMe0u_oPyD-"},"source":["##1. Basic autograd example 1"]},{"cell_type":"code","metadata":{"id":"oUvKhddOPcBM","colab":{"base_uri":"https://localhost:8080/","height":187},"executionInfo":{"status":"ok","timestamp":1573203612612,"user_tz":-480,"elapsed":2969,"user":{"displayName":"Xuan Phi Nguyen","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mBMe29t94knDbWFUTdAsYnOA_RwiofW5MdraN0v1TU=s64","userId":"15291677822697214066"}},"outputId":"4e684706-ef6c-453d-e5af-cb895b7a4856"},"source":["# Create tensors of shape (10, 3) and (10, 2).\n","x = torch.randn(10, 3)\n","y = torch.randn(10, 2)\n","\n","# Build a fully connected layer.\n","linear = nn.Linear(3, 2)\n","print ('w: ', linear.weight)\n","print ('b: ', linear.bias)\n","\n","# Build loss function and optimizer.\n","criterion = nn.MSELoss()\n","optimizer = torch.optim.SGD(linear.parameters(), lr=0.01)\n","\n","# Forward pass.\n","pred = linear(x)\n","\n","# Compute loss.\n","loss = criterion(pred, y)\n","print('loss: ', loss.item())\n","\n","# Backward pass.\n","loss.backward()\n","\n","# Print out the gradients.\n","print ('dL/dw: ', linear.weight.grad) \n","print ('dL/db: ', linear.bias.grad)\n","\n","# 1-step gradient descent.\n","optimizer.step()\n","\n","# You can also perform gradient descent at the low level.\n","# linear.weight.data.sub_(0.01 * linear.weight.grad.data)\n","# linear.bias.data.sub_(0.01 * linear.bias.grad.data)\n","\n","# Print out the loss after 1-step gradient descent.\n","pred = linear(x)\n","loss = criterion(pred, y)\n","print('loss after 1 step optimization: ', loss.item())\n","\n"],"execution_count":null,"outputs":[{"output_type":"stream","text":["w: Parameter containing:\n","tensor([[-0.0474, 0.0154, 0.1638],\n"," [-0.1966, -0.2304, 0.4822]], requires_grad=True)\n","b: Parameter containing:\n","tensor([-0.2659, 0.1617], requires_grad=True)\n","loss: 0.8229060769081116\n","dL/dw: tensor([[ 0.3962, 0.3727, 0.3502],\n"," [ 0.1079, -0.2590, 0.1530]])\n","dL/db: tensor([-0.5923, -0.2813])\n","loss after 1 step optimization: 0.8134800791740417\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"KiJypsSQPzUC"},"source":["##3. Loading data from numpy"]},{"cell_type":"code","metadata":{"id":"RpFqOS4GQMeC"},"source":["# Create a numpy array.\n","x = np.array([[1, 2], [3, 4]])\n","\n","# Convert the numpy array to a torch tensor.\n","y = torch.from_numpy(x)\n","\n","# Convert the torch tensor to a numpy array.\n","z = y.numpy()\n"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"Y8BGz1VxP2JY"},"source":["##4. Input pipeline"]},{"cell_type":"code","metadata":{"id":"hqXhTlCxQPwy"},"source":["# Download and construct CIFAR-10 dataset.\n","train_dataset = torchvision.datasets.CIFAR10(\n"," root='.',\n"," train=True, \n"," transform=transforms.ToTensor(),\n"," download=True)\n","\n","# Fetch one data pair (read data from disk).\n","image, label = train_dataset[0]\n","print (image.size())\n","print (label)\n","\n","# Data loader (this provides queues and threads in a very simple way).\n","train_loader = torch.utils.data.DataLoader(\n"," dataset=train_dataset,\n"," batch_size=64, \n"," shuffle=True)\n","\n","# When iteration starts, queue and thread start to load data from files.\n","data_iter = iter(train_loader)\n","\n","# Mini-batch images and labels.\n","images, labels = data_iter.next()\n","\n","# Actual usage of the data loader is as below.\n","for images, labels in train_loader:\n"," # Training code should be written here.\n"," pass\n","\n"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"22zI4Mi-P3nk"},"source":["##5. Input pipeline for custom dataset"]},{"cell_type":"code","metadata":{"id":"km9Ma1TTQcZB"},"source":["# You should build your custom dataset as below.\n","class CustomDataset(torch.utils.data.Dataset):\n"," def __init__(self):\n"," # TODO\n"," # 1. Initialize file paths or a list of file names. \n"," pass\n"," def __getitem__(self, index):\n"," # TODO\n"," # 1. Read one data from file (e.g. using numpy.fromfile, PIL.Image.open).\n"," # 2. Preprocess the data (e.g. torchvision.Transform).\n"," # 3. Return a data pair (e.g. image and label).\n"," pass\n"," def __len__(self):\n"," # You should change 0 to the total size of your dataset.\n"," return 0 \n","\n","# You can then use the prebuilt data loader. \n","custom_dataset = CustomDataset()\n","train_loader = torch.utils.data.DataLoader(\n"," dataset=custom_dataset,\n"," batch_size=64, \n"," shuffle=True)\n"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"nyDUflRGP3v_"},"source":["##6. Pretrained model"]},{"cell_type":"code","metadata":{"id":"ez0wfFfTQg06"},"source":["# Download and load the pretrained ResNet-18.\n","resnet = torchvision.models.resnet18(pretrained=True)\n","\n","# If you want to finetune only the top layer of the model, set as below.\n","for param in resnet.parameters():\n"," param.requires_grad = False\n","\n","# Replace the top layer for finetuning.\n","resnet.fc = nn.Linear(resnet.fc.in_features, 100) # 100 is an example.\n","\n","# Forward pass.\n","images = torch.randn(64, 3, 224, 224)\n","outputs = resnet(images)\n","print (outputs.size()) # (64, 100)\n"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"oX-SQZO0P36F"},"source":["##7. Save and load model"]},{"cell_type":"code","metadata":{"id":"zDpSgZKFQjKJ"},"source":["# Save and load the entire model.\n","torch.save(resnet, 'model.ckpt')\n","model = torch.load('model.ckpt')\n","\n","# Save and load only the model parameters (recommended).\n","torch.save(resnet.state_dict(), 'params.ckpt')\n","resnet.load_state_dict(torch.load('params.ckpt'))"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"iK1YQYVgUurI"},"source":["##8. Train a simple MNIST Neural nets\n","Let's train a simple neural network to classify MNIST hand-written digit\n"]},{"cell_type":"markdown","metadata":{"id":"8WyC0tZ2U-rX"},"source":["####1. Download the dataset"]},{"cell_type":"code","metadata":{"id":"1TRyf_4lUwqC"},"source":["import torch\n","import torch.nn as nn\n","import torchvision\n","import torchvision.transforms as transforms\n","import matplotlib.pyplot as plt\n","\n","\n","# Device configuration\n","device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n","\n","# Hyper-parameters \n","input_size = 784\n","hidden_size = 500\n","num_classes = 10\n","num_epochs = 5\n","batch_size = 100\n","learning_rate = 0.001\n","\n","# MNIST dataset \n","train_dataset = torchvision.datasets.MNIST(root='.', \n"," train=True, \n"," transform=transforms.ToTensor(), \n"," download=True)\n","\n","test_dataset = torchvision.datasets.MNIST(root='.', \n"," train=False, \n"," transform=transforms.ToTensor())\n","\n","# Data loader\n","train_loader = torch.utils.data.DataLoader(dataset=train_dataset, \n"," batch_size=batch_size, \n"," shuffle=True)\n","\n","test_loader = torch.utils.data.DataLoader(dataset=test_dataset, \n"," batch_size=batch_size, \n"," shuffle=False)\n"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"7BEDqrM6VGNd","colab":{"base_uri":"https://localhost:8080/","height":299},"executionInfo":{"status":"ok","timestamp":1573205236173,"user_tz":-480,"elapsed":3175,"user":{"displayName":"Xuan Phi Nguyen","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mBMe29t94knDbWFUTdAsYnOA_RwiofW5MdraN0v1TU=s64","userId":"15291677822697214066"}},"outputId":"79f68291-d9f5-4836-91c2-cd49e455e530"},"source":["image, label = test_dataset[0]\n","# reduce batch=1 to no batch\n","image = image[0]\n","print(f'{image.size()} , Label: {label}')\n","plt.imshow(image)"],"execution_count":null,"outputs":[{"output_type":"stream","text":["torch.Size([28, 28]) , Label: 7\n"],"name":"stdout"},{"output_type":"execute_result","data":{"text/plain":[""]},"metadata":{"tags":[]},"execution_count":13},{"output_type":"display_data","data":{"image/png":"iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0\ndHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAANiklEQVR4nO3df4wc9XnH8c8n/kV8QGtDcF3j4ISQ\nqE4aSHWBRNDKESUFImSiJBRLtVyJ5lALElRRW0QVBalVSlEIok0aySluHESgaQBhJTSNa6W1UKlj\ng4yxgdaEmsau8QFOaxPAP/DTP24cHXD7vWNndmft5/2SVrs7z87Oo/F9PLMzO/t1RAjA8e9tbTcA\noD8IO5AEYQeSIOxAEoQdSGJ6Pxc207PiBA31c5FAKq/qZzoYBzxRrVbYbV8s6XZJ0yT9bUTcXHr9\nCRrSeb6wziIBFGyIdR1rXe/G254m6auSLpG0WNIy24u7fT8AvVXnM/u5kp6OiGci4qCkeyQtbaYt\nAE2rE/YFkn4y7vnOatrr2B6xvcn2pkM6UGNxAOro+dH4iFgZEcMRMTxDs3q9OAAd1An7LkkLxz0/\nvZoGYADVCftGSWfZfpftmZKulLSmmbYANK3rU28Rcdj2tZL+SWOn3lZFxLbGOgPQqFrn2SPiQUkP\nNtQLgB7i67JAEoQdSIKwA0kQdiAJwg4kQdiBJAg7kARhB5Ig7EAShB1IgrADSRB2IAnCDiRB2IEk\nCDuQBGEHkiDsQBKEHUiCsANJEHYgCcIOJEHYgSQIO5AEYQeSIOxAEoQdSIKwA0kQdiAJwg4kQdiB\nJGoN2Wx7h6T9kl6TdDgihptoCkDzaoW98rGIeKGB9wHQQ+zGA0nUDXtI+oHtR2yPTPQC2yO2N9ne\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b9H7aftn1DGz10YnuH7cdtb7a9qeVeVtke\ntb113LS5ttfa3l7dTzjGXku93WR7V7XuNtu+tKXeFtr+oe0nbG+zfV01vdV1V+irL+ut75/ZbU+T\n9J+SLpK0U9JGScsi4om+NtKB7R2ShiOi9S9g2P4NSS9J+mZEfKCadoukvRFxc/Uf5ZyI+JMB6e0m\nSS+1PYx3NVrR/PHDjEu6XNLvqsV1V+jrCvVhvbWxZT9X0tMR8UxEHJR0j6SlLfQx8CJivaS9b5i8\nVNLq6vFqjf2x9F2H3gZCROyOiEerx/slHR1mvNV1V+irL9oI+wJJPxn3fKcGa7z3kPQD24/YHmm7\nmQnMi4jd1ePnJM1rs5kJTDqMdz+9YZjxgVl33Qx/XhcH6N7sgoj4NUmXSLqm2l0dSDH2GWyQzp1O\naRjvfplgmPGfa3PddTv8eV1thH2XpIXjnp9eTRsIEbGruh+VdL8GbyjqPUdH0K3uR1vu5+cGaRjv\niYYZ1wCsuzaHP28j7BslnWX7XbZnSrpS0poW+ngT20PVgRPZHpL0cQ3eUNRrJK2oHq+Q9ECLvbzO\noAzj3WmYcbW87lof/jwi+n6TdKnGjsj/WNKfttFDh77eLemx6rat7d4k3a2x3bpDGju2cZWkUySt\nk7Rd0j9LmjtAvd0p6XFJWzQWrPkt9XaBxnbRt0jaXN0ubXvdFfrqy3rj67JAEhygA5Ig7EAShB1I\ngrADSRB2IAnCDiRB2IEk/h9BCfQTVPflJQAAAABJRU5ErkJggg==\n","text/plain":["
"]},"metadata":{"tags":[]}}]},{"cell_type":"markdown","metadata":{"id":"CDYcpmKeVx3S"},"source":["####2. Initiate the Neural Network (multi-layer perceptron)\n","The network has 2 layers, with ReLu activation in between\n","\n","![](https://www.researchgate.net/profile/Mohamed_Zahran6/publication/303875065/figure/fig4/AS:371118507610123@1465492955561/A-hypothetical-example-of-Multilayer-Perceptron-Network.png)\n"]},{"cell_type":"code","metadata":{"id":"LGCTJ2CBVVmL"},"source":["# Device configuration\n","device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n","\n","# Hyper-parameters \n","input_size = 784\n","hidden_size = 500\n","num_classes = 10\n","num_epochs = 5\n","batch_size = 100\n","learning_rate = 0.001\n","\n","\n","# Fully connected neural network with one hidden layer\n","class NeuralNet(nn.Module):\n"," def __init__(self, input_size, hidden_size, num_classes):\n"," super(NeuralNet, self).__init__()\n"," self.fc1 = nn.Linear(input_size, hidden_size) \n"," self.relu = nn.ReLU()\n"," self.fc2 = nn.Linear(hidden_size, num_classes) \n"," \n"," def forward(self, x):\n"," out = self.fc1(x)\n"," out = self.relu(out)\n"," out = self.fc2(out)\n"," return out\n","\n","model = NeuralNet(input_size, hidden_size, num_classes).to(device)\n"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"Ihi_EnNxWETI"},"source":["####3. Train the network"]},{"cell_type":"code","metadata":{"id":"QLTt3Un5WCpo","colab":{"base_uri":"https://localhost:8080/","height":527},"executionInfo":{"status":"ok","timestamp":1573205407448,"user_tz":-480,"elapsed":49262,"user":{"displayName":"Xuan Phi Nguyen","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mBMe29t94knDbWFUTdAsYnOA_RwiofW5MdraN0v1TU=s64","userId":"15291677822697214066"}},"outputId":"5c8b26bd-d9ff-4878-b4f6-a5cc5408e2eb"},"source":["# Loss and optimizer\n","criterion = nn.CrossEntropyLoss()\n","optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) \n","\n","# Train the model\n","total_step = len(train_loader)\n","for epoch in range(num_epochs):\n"," for i, (images, labels) in enumerate(train_loader): \n"," # Move tensors to the configured device\n"," images = images.reshape(-1, 28*28).to(device)\n"," labels = labels.to(device)\n"," \n"," # Forward pass\n"," outputs = model(images)\n"," loss = criterion(outputs, labels)\n"," \n"," # Backward and optimize\n"," optimizer.zero_grad()\n"," loss.backward()\n"," optimizer.step()\n"," \n"," if (i+1) % 100 == 0:\n"," print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}' \n"," .format(epoch+1, num_epochs, i+1, total_step, loss.item()))\n"],"execution_count":null,"outputs":[{"output_type":"stream","text":["Epoch [1/5], Step [100/600], Loss: 0.2173\n","Epoch [1/5], Step [200/600], Loss: 0.3335\n","Epoch [1/5], Step [300/600], Loss: 0.1924\n","Epoch [1/5], Step [400/600], Loss: 0.1521\n","Epoch [1/5], Step [500/600], Loss: 0.2531\n","Epoch [1/5], Step [600/600], Loss: 0.1566\n","Epoch [2/5], Step [100/600], Loss: 0.1063\n","Epoch [2/5], Step [200/600], Loss: 0.1457\n","Epoch [2/5], Step [300/600], Loss: 0.1089\n","Epoch [2/5], Step [400/600], Loss: 0.0812\n","Epoch [2/5], Step [500/600], Loss: 0.2208\n","Epoch [2/5], Step [600/600], Loss: 0.1829\n","Epoch [3/5], Step [100/600], Loss: 0.0711\n","Epoch [3/5], Step [200/600], Loss: 0.0966\n","Epoch [3/5], Step [300/600], Loss: 0.1316\n","Epoch [3/5], Step [400/600], Loss: 0.1719\n","Epoch [3/5], Step [500/600], Loss: 0.0641\n","Epoch [3/5], Step [600/600], Loss: 0.0860\n","Epoch [4/5], Step [100/600], Loss: 0.0132\n","Epoch [4/5], Step [200/600], Loss: 0.0497\n","Epoch [4/5], Step [300/600], Loss: 0.0247\n","Epoch [4/5], Step [400/600], Loss: 0.1300\n","Epoch [4/5], Step [500/600], Loss: 0.0533\n","Epoch [4/5], Step [600/600], Loss: 0.0640\n","Epoch [5/5], Step [100/600], Loss: 0.0239\n","Epoch [5/5], Step [200/600], Loss: 0.0781\n","Epoch [5/5], Step [300/600], Loss: 0.0574\n","Epoch [5/5], Step [400/600], Loss: 0.0903\n","Epoch [5/5], Step [500/600], Loss: 0.0463\n","Epoch [5/5], Step [600/600], Loss: 0.0289\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"SlOaxGskWMFo"},"source":["####4. Test the network"]},{"cell_type":"code","metadata":{"id":"6VKlDUX8WLNj","colab":{"base_uri":"https://localhost:8080/","height":34},"executionInfo":{"status":"ok","timestamp":1573205417197,"user_tz":-480,"elapsed":2726,"user":{"displayName":"Xuan Phi Nguyen","photoUrl":"https://lh3.googleusercontent.com/a-/AAuE7mBMe29t94knDbWFUTdAsYnOA_RwiofW5MdraN0v1TU=s64","userId":"15291677822697214066"}},"outputId":"96cb3f78-cf73-4c1b-c572-9a415cd28746"},"source":["# Test the model\n","# In test phase, we don't need to compute gradients (for memory efficiency)\n","with torch.no_grad():\n"," correct = 0\n"," total = 0\n"," for images, labels in test_loader:\n"," images = images.reshape(-1, 28*28).to(device)\n"," labels = labels.to(device)\n"," outputs = model(images)\n"," _, predicted = torch.max(outputs.data, 1)\n"," total += labels.size(0)\n"," correct += (predicted == labels).sum().item()\n","\n"," print('Accuracy of the network on the 10000 test images: {} %'.format(100 * correct / total))\n","\n","# Save the model checkpoint\n","torch.save(model.state_dict(), 'model.pt')"],"execution_count":null,"outputs":[{"output_type":"stream","text":["Accuracy of the network on the 10000 test images: 97.96 %\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"id":"lEidschCWZH3"},"source":[""],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"mFcrwxgYW9BE"},"source":["#### Credits: https://github.com/keineahnung2345/pytorch-tutorial-jupyter-notebooks"]}]} -------------------------------------------------------------------------------- /week1/Google_Colab_Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "collapsed_sections": [], 8 | "toc_visible": true 9 | }, 10 | "kernelspec": { 11 | "name": "python3", 12 | "display_name": "Python 3" 13 | }, 14 | "accelerator": "GPU" 15 | }, 16 | "cells": [ 17 | { 18 | "cell_type": "markdown", 19 | "metadata": { 20 | "id": "BCmeo64HcLfs" 21 | }, 22 | "source": [ 23 | "# Note\n", 24 | "\n", 25 | "The following tutorial is accumulated from google colab's official tutorial. \n", 26 | "Possible additional comments, tips and tricks are added here and there." 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "metadata": { 32 | "id": "FueL0AgGl8ol" 33 | }, 34 | "source": [], 35 | "execution_count": null, 36 | "outputs": [] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": { 41 | "id": "1YKJmOTK0hdr" 42 | }, 43 | "source": [ 44 | "Important concepts.\n", 45 | "\n", 46 | "1. Connect \n", 47 | "2. Code Cell \n", 48 | "3. Text Cell\n", 49 | "4. Runtime \n" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "metadata": { 55 | "id": "cLkX9vxT2VBR", 56 | "outputId": "f90d8acb-92cb-4c29-c908-b3e98263afb5", 57 | "colab": { 58 | "base_uri": "https://localhost:8080/", 59 | "height": 35 60 | } 61 | }, 62 | "source": [ 63 | "print(\"Hello world\")" 64 | ], 65 | "execution_count": null, 66 | "outputs": [ 67 | { 68 | "output_type": "stream", 69 | "text": [ 70 | "Hello world\n" 71 | ], 72 | "name": "stdout" 73 | } 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": { 79 | "id": "DO6Z47v1bO-w" 80 | }, 81 | "source": [ 82 | "

\"Colaboratory

\n", 83 | "\n", 84 | "

Google Colaboratory!

\n", 85 | "\n", 86 | "\n", 87 | "Colaboratory is a free Jupyter notebook environment that requires no setup and runs entirely in the cloud.\n", 88 | "\n", 89 | "With Colaboratory you can write and execute code, save and share your analyses, and access powerful computing resources, all for free from your browser." 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "metadata": { 95 | "id": "oHTd7xQrt-BW", 96 | "outputId": "1d01d732-05fe-43cb-9ac0-1b045911546c", 97 | "colab": { 98 | "base_uri": "https://localhost:8080/", 99 | "height": 421 100 | } 101 | }, 102 | "source": [ 103 | "#@title Introducing Colaboratory { display-mode: \"form\" }\n", 104 | "#@markdown This 3-minute video gives an overview of the key features of Colaboratory:\n", 105 | "from IPython.display import YouTubeVideo\n", 106 | "YouTubeVideo('inN8seMm7UI', width=600, height=400)" 107 | ], 108 | "execution_count": null, 109 | "outputs": [ 110 | { 111 | "output_type": "execute_result", 112 | "data": { 113 | "text/html": [ 114 | "\n", 115 | " \n", 122 | " " 123 | ], 124 | "text/plain": [ 125 | "" 126 | ], 127 | "image/jpeg": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAUDBAYIBgcGBwYGBgYHBwUGBgcGBgYHBwYGBgUGBgYG\nBQcHChwXCAgaCQYFGCEYGh0dHx8fBwsiJCIeJBweHxIBBQUFCAcICAgICBIICAgSEhISEhISEh4S\nEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhISEhIeEhISEv/AABEIAWgB4AMBIgACEQED\nEQH/xAAdAAEAAQUBAQEAAAAAAAAAAAAABgMEBQcIAgEJ/8QAYBAAAQIEAgQGCwoICgcHBQAAAgAD\nAQQFEgYREyEiMgcUMUJSYRhBUVVicXKBgpTVCCMzNXSRkqGz8BVTorGywdHUJCU0Q1RztMLS4hYm\nY4Ph8fI2RHWEhZPTFzdkZaP/xAAbAQEAAgMBAQAAAAAAAAAAAAAAAQIDBAUGB//EADERAQACAgAE\nBAQFAwUAAAAAAAABAgMRBBIhMQUTQVEiRXGCFDKBg5EVQsEjQ2Fiof/aAAwDAQACEQMRAD8A4yRE\nQEREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBE\nRAREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBERAREQE\nREBERAREQEREBERAREQEREBERAREQEREBERAREQEREBERARdJdhpjXvphf12rez07DTGvfTC/rtW\n9noObUXSXYaY176YX9dq3s9Ow0xr30wv67VvZ6Dm1F0l2GmNe+mF/Xat7PTsNMa99ML+u1b2eg5t\nRdJdhpjXvphf12rez07DTGvfTC/rtW9noObUXSXYaY176YX9dq3s9Ow0xr30wv67VvZ6Dm1F0l2G\nmNe+mF/Xat7PTsNMa99ML+u1b2eg5tRdJdhpjXvphf12rez07DTGvfTC/rtW9noObUXSXYaY176Y\nX9dq3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l2d4Sqm7G4mJAY5COy1Mdrynlm+DfhurtCn3KjJStKefdlzlCGcYnHGxa\nN1p0iAWZkYwLNoO3ly6lS14mNQO9eFfDQHKPVSWaEJxkRdmrB/lUuA5GR91wR1wjyxgEYa9WWm5M\nyKfbLoslteES1Y/7sXGRtk0VKwtEHBMDHiNW2hOFpQ+MO5FQKV4cK81bbK0qMRGAbTE5yQhlryme\nVRS8RHUdaBdb0l4OO1b5K5ba90JiMeSTovq07+9L0PuhsR3QPiVFKI69cvP/AL2reZBpp1ERYEiI\niAiIgIiICIiAiIgIiICIiAiIgIiICIiAiIgIiICIiAiIgIiICIiAiIgIiICIiAiIgIiICIiAiIgI\niICIiAiIgIiICIiAiIgIiICIiAiIgIiICIiAiIgIiICIiAiIgIiICIiAiIgIiICIiAiIgIiICIiA\niIgIiICIiAiIgIiICIiAiIgIiICIiAiIgIiICIiAiIgIiICIiAiIgIiICIiAiIgIiICIiAiIgIiI\nCIiAiIgIiICIiAiIgIiICIiAiIgIiICIiAiIgIiICIiAiIgIiICIiAiIgIiICIiAiIg//9k=\n" 128 | }, 129 | "metadata": { 130 | "tags": [] 131 | }, 132 | "execution_count": 5 133 | } 134 | ] 135 | }, 136 | { 137 | "cell_type": "markdown", 138 | "metadata": { 139 | "id": "5FmXWIMVcfUt" 140 | }, 141 | "source": [ 142 | "# Virtual Machine \n", 143 | "The most powerful feature of google colab is the ability to use cloud GPU for free. Like the other desktop environment you can also access most of the bash command with a `!` added in the front of the command.\n", 144 | "\n", 145 | "At first turn on the GPU from `Runtime`->`Change Runtime Type`->`Hardware Acceleration`\n", 146 | "\n", 147 | "The entire colab runs in a cloud VM. Let's investigate the VM. You will see that the current colab notebook is running on top of `Ubuntu 18.04.3 LTS` (at the time of this writing.)" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "metadata": { 153 | "id": "PDqnY7fNbzbN", 154 | "outputId": "2c51ee3a-4ac4-49ad-98c4-6ce2fdc5cf5a", 155 | "colab": { 156 | "base_uri": "https://localhost:8080/", 157 | "height": 89 158 | } 159 | }, 160 | "source": [ 161 | "!ls -l\n", 162 | "!pwd" 163 | ], 164 | "execution_count": null, 165 | "outputs": [ 166 | { 167 | "output_type": "stream", 168 | "text": [ 169 | "total 8\n", 170 | "drwx------ 3 root root 4096 Nov 9 04:17 drive\n", 171 | "drwxr-xr-x 1 root root 4096 Nov 6 16:17 sample_data\n", 172 | "/content\n" 173 | ], 174 | "name": "stdout" 175 | } 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "metadata": { 181 | "id": "rythfaRYdyFy", 182 | "outputId": "76d4357e-46bd-475b-9270-21a5b6c6725c", 183 | "colab": { 184 | "base_uri": "https://localhost:8080/", 185 | "height": 485 186 | } 187 | }, 188 | "source": [ 189 | "!ls -l /\n" 190 | ], 191 | "execution_count": null, 192 | "outputs": [ 193 | { 194 | "output_type": "stream", 195 | "text": [ 196 | "total 104\n", 197 | "drwxr-xr-x 1 root root 4096 Oct 25 16:51 bin\n", 198 | "drwxr-xr-x 2 root root 4096 Apr 24 2018 boot\n", 199 | "drwxr-xr-x 1 root root 4096 Nov 9 04:17 content\n", 200 | "drwxr-xr-x 1 root root 4096 Nov 6 16:52 datalab\n", 201 | "drwxr-xr-x 5 root root 440 Nov 9 03:56 dev\n", 202 | "drwxr-xr-x 1 root root 4096 Nov 9 03:56 etc\n", 203 | "drwxr-xr-x 2 root root 4096 Apr 24 2018 home\n", 204 | "drwxr-xr-x 1 root root 4096 Oct 25 16:53 lib\n", 205 | "drwxr-xr-x 2 root root 4096 Oct 25 16:44 lib32\n", 206 | "drwxr-xr-x 2 root root 4096 Aug 7 13:03 lib64\n", 207 | "drwxr-xr-x 2 root root 4096 Aug 7 13:02 media\n", 208 | "drwxr-xr-x 2 root root 4096 Aug 7 13:02 mnt\n", 209 | "drwxr-xr-x 1 root root 4096 Nov 9 03:56 opt\n", 210 | "dr-xr-xr-x 124 root root 0 Nov 9 03:56 proc\n", 211 | "drwx------ 1 root root 4096 Nov 9 04:17 root\n", 212 | "drwxr-xr-x 1 root root 4096 Oct 25 16:51 run\n", 213 | "drwxr-xr-x 1 root root 4096 Oct 25 16:52 sbin\n", 214 | "drwxr-xr-x 2 root root 4096 Aug 7 13:02 srv\n", 215 | "drwxr-xr-x 4 root root 4096 Nov 6 16:51 swift\n", 216 | "dr-xr-xr-x 12 root root 0 Nov 9 04:14 sys\n", 217 | "drwxr-xr-x 1 root root 4096 Nov 6 16:47 tensorflow-2.0.0\n", 218 | "drwxrwxrwt 1 root root 4096 Nov 9 04:17 tmp\n", 219 | "drwxr-xr-x 1 root root 4096 Nov 6 16:52 tools\n", 220 | "drwxr-xr-x 1 root root 4096 Nov 9 03:56 usr\n", 221 | "drwxr-xr-x 1 root root 4096 Nov 9 03:56 var\n" 222 | ], 223 | "name": "stdout" 224 | } 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "metadata": { 230 | "id": "zydmUtFqd1-t", 231 | "outputId": "434b094a-0d54-47a9-d8e0-2e37db494ab9", 232 | "colab": { 233 | "base_uri": "https://localhost:8080/", 234 | "height": 305 235 | } 236 | }, 237 | "source": [ 238 | "!cat /etc/*release " 239 | ], 240 | "execution_count": null, 241 | "outputs": [ 242 | { 243 | "output_type": "stream", 244 | "text": [ 245 | "DISTRIB_ID=Ubuntu\n", 246 | "DISTRIB_RELEASE=18.04\n", 247 | "DISTRIB_CODENAME=bionic\n", 248 | "DISTRIB_DESCRIPTION=\"Ubuntu 18.04.3 LTS\"\n", 249 | "NAME=\"Ubuntu\"\n", 250 | "VERSION=\"18.04.3 LTS (Bionic Beaver)\"\n", 251 | "ID=ubuntu\n", 252 | "ID_LIKE=debian\n", 253 | "PRETTY_NAME=\"Ubuntu 18.04.3 LTS\"\n", 254 | "VERSION_ID=\"18.04\"\n", 255 | "HOME_URL=\"https://www.ubuntu.com/\"\n", 256 | "SUPPORT_URL=\"https://help.ubuntu.com/\"\n", 257 | "BUG_REPORT_URL=\"https://bugs.launchpad.net/ubuntu/\"\n", 258 | "PRIVACY_POLICY_URL=\"https://www.ubuntu.com/legal/terms-and-policies/privacy-policy\"\n", 259 | "VERSION_CODENAME=bionic\n", 260 | "UBUNTU_CODENAME=bionic\n" 261 | ], 262 | "name": "stdout" 263 | } 264 | ] 265 | }, 266 | { 267 | "cell_type": "markdown", 268 | "metadata": { 269 | "id": "XKhoD7iT5z8J" 270 | }, 271 | "source": [ 272 | "# GPU Details\n", 273 | "\n", 274 | "The GPU details can be accessed by `!nvidia-smi`." 275 | ] 276 | }, 277 | { 278 | "cell_type": "code", 279 | "metadata": { 280 | "id": "rZIfeoVWgLjc", 281 | "outputId": "adb25fa1-09a2-4cd5-bc22-f08c3e564d44", 282 | "colab": { 283 | "base_uri": "https://localhost:8080/", 284 | "height": 323 285 | } 286 | }, 287 | "source": [ 288 | "!nvidia-smi" 289 | ], 290 | "execution_count": null, 291 | "outputs": [ 292 | { 293 | "output_type": "stream", 294 | "text": [ 295 | "Sat Nov 9 05:25:53 2019 \n", 296 | "+-----------------------------------------------------------------------------+\n", 297 | "| NVIDIA-SMI 430.50 Driver Version: 418.67 CUDA Version: 10.1 |\n", 298 | "|-------------------------------+----------------------+----------------------+\n", 299 | "| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |\n", 300 | "| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |\n", 301 | "|===============================+======================+======================|\n", 302 | "| 0 Tesla T4 Off | 00000000:00:04.0 Off | 0 |\n", 303 | "| N/A 37C P8 9W / 70W | 0MiB / 15079MiB | 0% Default |\n", 304 | "+-------------------------------+----------------------+----------------------+\n", 305 | " \n", 306 | "+-----------------------------------------------------------------------------+\n", 307 | "| Processes: GPU Memory |\n", 308 | "| GPU PID Type Process name Usage |\n", 309 | "|=============================================================================|\n", 310 | "| No running processes found |\n", 311 | "+-----------------------------------------------------------------------------+\n" 312 | ], 313 | "name": "stdout" 314 | } 315 | ] 316 | }, 317 | { 318 | "cell_type": "markdown", 319 | "metadata": { 320 | "id": "z5FZ_n_ieQfV" 321 | }, 322 | "source": [ 323 | "# Ubuntu 18.04.3 LTS and drivers\n", 324 | "\n", 325 | "Now that, as you are using ubuntu, you can use any command that you use from GNU terminal. This makes life very simple.\n", 326 | "\n", 327 | "However, for your project you have have to find a way to transfer/download your data files here in this VM. The easiest way is to mound your google drive here in this VM and use git for version control of your codes. \n", 328 | "\n", 329 | "Another easiest way is to get your file is to use `wget` from a file server or dropbox. \n", 330 | "\n" 331 | ] 332 | }, 333 | { 334 | "cell_type": "markdown", 335 | "metadata": { 336 | "id": "l37pADFurT60" 337 | }, 338 | "source": [ 339 | "To install a python package with pip use following. \n", 340 | "You can also use `apt-get` to install any package in ubuntu." 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "metadata": { 346 | "id": "_BVV-cvYfTi8", 347 | "outputId": "7e31c241-4bd7-47b6-e0f6-aded20587ade", 348 | "colab": { 349 | "base_uri": "https://localhost:8080/", 350 | "height": 415 351 | } 352 | }, 353 | "source": [ 354 | "!pip install gpustat\n", 355 | "!pip install fairseq\n", 356 | "!apt-get install build-essential" 357 | ], 358 | "execution_count": null, 359 | "outputs": [ 360 | { 361 | "output_type": "stream", 362 | "text": [ 363 | "Requirement already satisfied: gpustat in /usr/local/lib/python3.6/dist-packages (0.6.0)\n", 364 | "Requirement already satisfied: six>=1.7 in /usr/local/lib/python3.6/dist-packages (from gpustat) (1.12.0)\n", 365 | "Requirement already satisfied: psutil in /usr/local/lib/python3.6/dist-packages (from gpustat) (5.4.8)\n", 366 | "Requirement already satisfied: nvidia-ml-py3>=7.352.0 in /usr/local/lib/python3.6/dist-packages (from gpustat) (7.352.0)\n", 367 | "Requirement already satisfied: blessings>=1.6 in /usr/local/lib/python3.6/dist-packages (from gpustat) (1.7)\n", 368 | "Requirement already satisfied: fairseq in /usr/local/lib/python3.6/dist-packages (0.8.0)\n", 369 | "Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from fairseq) (1.17.3)\n", 370 | "Requirement already satisfied: cffi in /usr/local/lib/python3.6/dist-packages (from fairseq) (1.13.1)\n", 371 | "Requirement already satisfied: sacrebleu in /usr/local/lib/python3.6/dist-packages (from fairseq) (1.4.2)\n", 372 | "Requirement already satisfied: fastBPE in /usr/local/lib/python3.6/dist-packages (from fairseq) (0.1.0)\n", 373 | "Requirement already satisfied: torch in /usr/local/lib/python3.6/dist-packages (from fairseq) (1.3.0+cu100)\n", 374 | "Requirement already satisfied: regex in /usr/local/lib/python3.6/dist-packages (from fairseq) (2019.11.1)\n", 375 | "Requirement already satisfied: tqdm in /usr/local/lib/python3.6/dist-packages (from fairseq) (4.28.1)\n", 376 | "Requirement already satisfied: pycparser in /usr/local/lib/python3.6/dist-packages (from cffi->fairseq) (2.19)\n", 377 | "Requirement already satisfied: portalocker in /usr/local/lib/python3.6/dist-packages (from sacrebleu->fairseq) (1.5.1)\n", 378 | "Requirement already satisfied: typing in /usr/local/lib/python3.6/dist-packages (from sacrebleu->fairseq) (3.6.6)\n", 379 | "Reading package lists... Done\n", 380 | "Building dependency tree \n", 381 | "Reading state information... Done\n", 382 | "build-essential is already the newest version (12.4ubuntu1).\n", 383 | "0 upgraded, 0 newly installed, 0 to remove and 35 not upgraded.\n" 384 | ], 385 | "name": "stdout" 386 | } 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "metadata": { 392 | "id": "eOjNVCFy6F6T", 393 | "outputId": "1cd1645d-5f5f-46bc-d76c-f69cdfe2c569", 394 | "colab": { 395 | "base_uri": "https://localhost:8080/", 396 | "height": 53 397 | } 398 | }, 399 | "source": [ 400 | "!gpustat" 401 | ], 402 | "execution_count": null, 403 | "outputs": [ 404 | { 405 | "output_type": "stream", 406 | "text": [ 407 | "\u001b[1m\u001b[37m25193fa42234 \u001b[m Sat Nov 9 05:27:08 2019 \u001b[1m\u001b[30m418.67\u001b[m\n", 408 | "\u001b[36m[0]\u001b[m \u001b[34mTesla T4 \u001b[m |\u001b[31m 36'C\u001b[m, \u001b[32m 0 %\u001b[m | \u001b[36m\u001b[1m\u001b[33m 0\u001b[m / \u001b[33m15079\u001b[m MB |\n" 409 | ], 410 | "name": "stdout" 411 | } 412 | ] 413 | }, 414 | { 415 | "cell_type": "markdown", 416 | "metadata": { 417 | "id": "rL3vV06CsTbU" 418 | }, 419 | "source": [ 420 | "\n", 421 | "Please check the nvidia driver version (see the output of `!nvidia-smi` above), cuda version in current OS. Because not all version support the latest cuda, cudnn etc. Deep learning libraries are changing at a rapid pace. So make sure that you can install your preferred deep learning library with the current nvidia-driver, cuda and cudnn." 422 | ] 423 | }, 424 | { 425 | "cell_type": "code", 426 | "metadata": { 427 | "id": "BRqElCWbsSVj", 428 | "outputId": "8a675e0b-f420-451e-83e1-1739cb92de1d", 429 | "colab": { 430 | "base_uri": "https://localhost:8080/", 431 | "height": 71 432 | } 433 | }, 434 | "source": [ 435 | "from platform import python_version\n", 436 | "import torch\n", 437 | "print(\"Python version\", python_version())\n", 438 | "print(\"Pytorch - version\", torch.__version__)\n", 439 | "print(\"Pytorch - cuDNN version :\", torch.backends.cudnn.version())" 440 | ], 441 | "execution_count": null, 442 | "outputs": [ 443 | { 444 | "output_type": "stream", 445 | "text": [ 446 | "Python version 3.6.8\n", 447 | "Pytorch - version 1.3.0+cu100\n", 448 | "Pytorch - cuDNN version : 7603\n" 449 | ], 450 | "name": "stdout" 451 | } 452 | ] 453 | }, 454 | { 455 | "cell_type": "markdown", 456 | "metadata": { 457 | "id": "rhqwIRcxu2qC" 458 | }, 459 | "source": [ 460 | "# Data uploading to VM\n", 461 | "1. From Machine -- explore the built in left panel of google colab\n", 462 | "2. From google drive -- explore the built in left panel of google colab\n", 463 | "3. with wget \n" 464 | ] 465 | }, 466 | { 467 | "cell_type": "code", 468 | "metadata": { 469 | "id": "WqwWJnUOuhRU", 470 | "outputId": "25568036-72c7-47b5-853c-70dacaae98a1", 471 | "colab": { 472 | "base_uri": "https://localhost:8080/", 473 | "height": 55 474 | } 475 | }, 476 | "source": [ 477 | "from google.colab import drive\n", 478 | "drive.mount('/content/drive')" 479 | ], 480 | "execution_count": null, 481 | "outputs": [ 482 | { 483 | "output_type": "stream", 484 | "text": [ 485 | "Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n" 486 | ], 487 | "name": "stdout" 488 | } 489 | ] 490 | }, 491 | { 492 | "cell_type": "code", 493 | "metadata": { 494 | "id": "4TNUjRALvP7B", 495 | "outputId": "3592b553-e2a7-4ade-c5d6-e5f717ae350d", 496 | "colab": { 497 | "base_uri": "https://localhost:8080/", 498 | "height": 35 499 | } 500 | }, 501 | "source": [ 502 | "!pwd" 503 | ], 504 | "execution_count": null, 505 | "outputs": [ 506 | { 507 | "output_type": "stream", 508 | "text": [ 509 | "/content\n" 510 | ], 511 | "name": "stdout" 512 | } 513 | ] 514 | }, 515 | { 516 | "cell_type": "code", 517 | "metadata": { 518 | "id": "ubh7nOJevUgp", 519 | "outputId": "3fef4871-1376-4835-db0c-46fb2195c060", 520 | "colab": { 521 | "base_uri": "https://localhost:8080/", 522 | "height": 235 523 | } 524 | }, 525 | "source": [ 526 | "!wget https://dl.fbaipublicfiles.com/XLM/codes_xnli_100" 527 | ], 528 | "execution_count": null, 529 | "outputs": [ 530 | { 531 | "output_type": "stream", 532 | "text": [ 533 | "--2019-11-09 05:28:50-- https://dl.fbaipublicfiles.com/XLM/codes_xnli_100\n", 534 | "Resolving dl.fbaipublicfiles.com (dl.fbaipublicfiles.com)... 104.20.6.166, 104.20.22.166, 2606:4700:10::6814:6a6, ...\n", 535 | "Connecting to dl.fbaipublicfiles.com (dl.fbaipublicfiles.com)|104.20.6.166|:443... connected.\n", 536 | "HTTP request sent, awaiting response... 200 OK\n", 537 | "Length: 2973279 (2.8M) [text/plain]\n", 538 | "Saving to: ‘codes_xnli_100.1’\n", 539 | "\n", 540 | "codes_xnli_100.1 100%[===================>] 2.83M 2.74MB/s in 1.0s \n", 541 | "\n", 542 | "2019-11-09 05:28:52 (2.74 MB/s) - ‘codes_xnli_100.1’ saved [2973279/2973279]\n", 543 | "\n" 544 | ], 545 | "name": "stdout" 546 | } 547 | ] 548 | }, 549 | { 550 | "cell_type": "code", 551 | "metadata": { 552 | "id": "Z0AJKQOiyKnJ", 553 | "outputId": "f0587ff5-c35a-45f5-dbde-702fd2e858ee", 554 | "colab": { 555 | "base_uri": "https://localhost:8080/", 556 | "height": 107 557 | } 558 | }, 559 | "source": [ 560 | "!ls -l" 561 | ], 562 | "execution_count": null, 563 | "outputs": [ 564 | { 565 | "output_type": "stream", 566 | "text": [ 567 | "total 5816\n", 568 | "-rw-r--r-- 1 root root 2973279 Aug 18 02:44 codes_xnli_100\n", 569 | "-rw-r--r-- 1 root root 2973279 Aug 18 02:44 codes_xnli_100.1\n", 570 | "drwx------ 3 root root 4096 Nov 9 04:17 drive\n", 571 | "drwxr-xr-x 1 root root 4096 Nov 6 16:17 sample_data\n" 572 | ], 573 | "name": "stdout" 574 | } 575 | ] 576 | }, 577 | { 578 | "cell_type": "code", 579 | "metadata": { 580 | "id": "VGMuEvUqyQeX", 581 | "outputId": "0cb98d97-a7d6-4894-c811-165bcf26d903", 582 | "colab": { 583 | "base_uri": "https://localhost:8080/", 584 | "height": 197 585 | } 586 | }, 587 | "source": [ 588 | "!head -n 10 codes_xnli_100" 589 | ], 590 | "execution_count": null, 591 | "outputs": [ 592 | { 593 | "output_type": "stream", 594 | "text": [ 595 | "a n 58300410\n", 596 | "e r 55874871\n", 597 | "e n 48332651\n", 598 | "i n 46367809\n", 599 | "a r 42047014\n", 600 | "s t 41963553\n", 601 | "a l 34495577\n", 602 | "e n 33743928\n", 603 | "o n 30604783\n", 604 | "o r 30086868\n" 605 | ], 606 | "name": "stdout" 607 | } 608 | ] 609 | }, 610 | { 611 | "cell_type": "code", 612 | "metadata": { 613 | "id": "F_cW4rmg20Wu", 614 | "outputId": "d6c15761-1f45-439b-defe-7f073dca0031", 615 | "colab": { 616 | "base_uri": "https://localhost:8080/", 617 | "height": 35 618 | } 619 | }, 620 | "source": [ 621 | "!git clone https://github.com/ntunlp/MH6812-SPMS-2019" 622 | ], 623 | "execution_count": null, 624 | "outputs": [ 625 | { 626 | "output_type": "stream", 627 | "text": [ 628 | "fatal: destination path 'MH6812-SPMS-2019' already exists and is not an empty directory.\n" 629 | ], 630 | "name": "stdout" 631 | } 632 | ] 633 | }, 634 | { 635 | "cell_type": "code", 636 | "metadata": { 637 | "id": "7ZEIBbCj3Gqe", 638 | "outputId": "42cff5ec-c3ef-42a3-c3fb-5acb898aad49", 639 | "colab": { 640 | "base_uri": "https://localhost:8080/", 641 | "height": 53 642 | } 643 | }, 644 | "source": [ 645 | "!ls -l CE-7455-SPMS-2019" 646 | ], 647 | "execution_count": null, 648 | "outputs": [ 649 | { 650 | "output_type": "stream", 651 | "text": [ 652 | "total 4\n", 653 | "-rw-r--r-- 1 root root 42 Nov 5 18:55 README.md\n" 654 | ], 655 | "name": "stdout" 656 | } 657 | ] 658 | }, 659 | { 660 | "cell_type": "markdown", 661 | "metadata": { 662 | "id": "4pA2fSmgwMxr" 663 | }, 664 | "source": [ 665 | "## Getting Started\n", 666 | "\n", 667 | "The document you are reading is a [Jupyter notebook](https://jupyter.org/), hosted in Colaboratory. It is not a static page, but an interactive environment that lets you write and execute code in Python and other languages.\n", 668 | "\n", 669 | "For example, here is a **code cell** with a short Python script that computes a value, stores it in a variable, and prints the result:" 670 | ] 671 | }, 672 | { 673 | "cell_type": "code", 674 | "metadata": { 675 | "id": "zB0Ue-JuwPIM", 676 | "outputId": "696c4bb8-02fa-4e45-e68d-2f0c274181ec", 677 | "colab": { 678 | "base_uri": "https://localhost:8080/", 679 | "height": 35 680 | } 681 | }, 682 | "source": [ 683 | "seconds_in_a_day = 24 * 60 * 60\n", 684 | "seconds_in_a_day" 685 | ], 686 | "execution_count": null, 687 | "outputs": [ 688 | { 689 | "output_type": "execute_result", 690 | "data": { 691 | "text/plain": [ 692 | "86400" 693 | ] 694 | }, 695 | "metadata": { 696 | "tags": [] 697 | }, 698 | "execution_count": 96 699 | } 700 | ] 701 | }, 702 | { 703 | "cell_type": "code", 704 | "metadata": { 705 | "id": "MDfdEdl7Ylg6", 706 | "colab": { 707 | "base_uri": "https://localhost:8080/", 708 | "height": 35 709 | }, 710 | "outputId": "cbf06a07-6faa-4366-ef56-004c79c6a7e3" 711 | }, 712 | "source": [ 713 | "var = 1\n", 714 | "name = 'NTU'\n", 715 | "print(name)" 716 | ], 717 | "execution_count": null, 718 | "outputs": [ 719 | { 720 | "output_type": "stream", 721 | "text": [ 722 | "NTU\n" 723 | ], 724 | "name": "stdout" 725 | } 726 | ] 727 | }, 728 | { 729 | "cell_type": "markdown", 730 | "metadata": { 731 | "id": "SsH659SayfJ4" 732 | }, 733 | "source": [ 734 | "To execute the code in the above cell, select it with a click and then either press the play button to the left of the code, or use the keyboard shortcut \"Command/Ctrl+Enter\".\n", 735 | "\n", 736 | "All cells modify the same global state, so variables that you define by executing a cell can be used in other cells:" 737 | ] 738 | }, 739 | { 740 | "cell_type": "code", 741 | "metadata": { 742 | "id": "bviL6WmpwUk6", 743 | "outputId": "e18e51db-aaf1-4695-f380-5057bd39e00d", 744 | "colab": { 745 | "base_uri": "https://localhost:8080/", 746 | "height": 35 747 | } 748 | }, 749 | "source": [ 750 | "seconds_in_a_week = 7 * seconds_in_a_day\n", 751 | "seconds_in_a_week" 752 | ], 753 | "execution_count": null, 754 | "outputs": [ 755 | { 756 | "output_type": "execute_result", 757 | "data": { 758 | "text/plain": [ 759 | "604800" 760 | ] 761 | }, 762 | "metadata": { 763 | "tags": [] 764 | }, 765 | "execution_count": 102 766 | } 767 | ] 768 | }, 769 | { 770 | "cell_type": "markdown", 771 | "metadata": { 772 | "id": "7sC1fIAAyisY" 773 | }, 774 | "source": [ 775 | "For more information about working with Colaboratory notebooks, see [Overview of Colaboratory](/notebooks/basic_features_overview.ipynb).\n" 776 | ] 777 | }, 778 | { 779 | "cell_type": "markdown", 780 | "metadata": { 781 | "id": "u1wdmFKqylSI" 782 | }, 783 | "source": [ 784 | "# Important Resources\n", 785 | "\n", 786 | "Learn how to make the most of Python, Jupyter, Colaboratory, and related tools with these resources:\n", 787 | "\n", 788 | "### Working with Notebooks in Colaboratory\n", 789 | "- [Overview of Colaboratory](/notebooks/basic_features_overview.ipynb) \n", 790 | "- [Guide to Markdown](/notebooks/markdown_guide.ipynb)\n", 791 | "- [Importing libraries and installing dependencies](/notebooks/snippets/importing_libraries.ipynb)\n", 792 | "- [Saving and loading notebooks in GitHub](https://colab.research.google.com/github/googlecolab/colabtools/blob/master/notebooks/colab-github-demo.ipynb)\n" 793 | ] 794 | }, 795 | { 796 | "cell_type": "markdown", 797 | "metadata": { 798 | "id": "y--_94WozIka" 799 | }, 800 | "source": [ 801 | "# Additional Content\n", 802 | "- [Interactive forms](/notebooks/forms.ipynb)\n", 803 | "- [Interactive widgets](/notebooks/widgets.ipynb)\n", 804 | "- \"New\"\n", 805 | " [TensorFlow 2 in Colab](/notebooks/tensorflow_version.ipynb)\n", 806 | "\n", 807 | "### Working with Data\n", 808 | "- [Loading data: Drive, Sheets, and Google Cloud Storage](/notebooks/io.ipynb) \n", 809 | "- [Charts: visualizing data](/notebooks/charts.ipynb)\n", 810 | "- [Getting started with BigQuery](/notebooks/bigquery.ipynb)\n", 811 | "\n", 812 | "### Machine Learning Crash Course\n", 813 | "These are a few of the notebooks from Google's online Machine Learning course. See the [full course website](https://developers.google.com/machine-learning/crash-course/) for more.\n", 814 | "- [Intro to Pandas](/notebooks/mlcc/intro_to_pandas.ipynb)\n", 815 | "- [Tensorflow concepts](/notebooks/mlcc/tensorflow_programming_concepts.ipynb)\n", 816 | "- [First steps with TensorFlow](/notebooks/mlcc/first_steps_with_tensor_flow.ipynb)\n", 817 | "- [Intro to neural nets](/notebooks/mlcc/intro_to_neural_nets.ipynb)\n", 818 | "- [Intro to sparse data and embeddings](/notebooks/mlcc/intro_to_sparse_data_and_embeddings.ipynb)\n", 819 | "\n", 820 | "### Using Accelerated Hardware\n", 821 | "- [TensorFlow with GPUs](/notebooks/gpu.ipynb)\n", 822 | "- [TensorFlow with TPUs](/notebooks/tpu.ipynb)" 823 | ] 824 | }, 825 | { 826 | "cell_type": "code", 827 | "metadata": { 828 | "id": "7aH1A-IwzLyX" 829 | }, 830 | "source": [], 831 | "execution_count": null, 832 | "outputs": [] 833 | } 834 | ] 835 | } -------------------------------------------------------------------------------- /week1/Python_Tutorial_The_byte.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "provenance": [], 7 | "collapsed_sections": [] 8 | }, 9 | "kernelspec": { 10 | "name": "python3", 11 | "display_name": "Python 3" 12 | } 13 | }, 14 | "cells": [ 15 | { 16 | "cell_type": "markdown", 17 | "metadata": { 18 | "id": "Iofgm5TrY8Dd" 19 | }, 20 | "source": [ 21 | "# Python Tutorial - The byte\n", 22 | "-- $M \\ Saiful \\ Bari$\n", 23 | "\n", 24 | "The official python tutorial, https://docs.python.org/3.6/tutorial/\n", 25 | "\n", 26 | "The language is named after the BBC show **Monty Python’s Flying Circus** and has nothing to do with **reptiles**.\n", 27 | "\n", 28 | "**[talk]** Python's motivation and journey. [oxford union talk](https://www.youtube.com/watch?v=7kn7NtlV6g0)\n", 29 | "\n", 30 | "**Prerequisite :** Knowledge of any programming language, some small concepts of OOP.\n", 31 | "\n" 32 | ] 33 | }, 34 | { 35 | "cell_type": "markdown", 36 | "metadata": { 37 | "id": "WFmLsCjpZfrf" 38 | }, 39 | "source": [ 40 | "# Table of Content\n", 41 | "0. Introduction\n", 42 | "1. Interpreter\n", 43 | "2. Variables & Built in Data-structures\n", 44 | "3. Conditional\n", 45 | "4. Function\n", 46 | "5. while loop\n", 47 | "6. for loop\n", 48 | "7. Random number\n", 49 | "8. Generator Function\n", 50 | "9. Class, Inheritance\n", 51 | "10. Asserts, exceptions\n", 52 | "11. Input Output (I/O)\n", 53 | "12. The Pythonic Way\n", 54 | "13. Brief tour to Standard Library\n", 55 | "14. Package Manager\n", 56 | "15. The complete language reference\n" 57 | ] 58 | }, 59 | { 60 | "cell_type": "markdown", 61 | "metadata": { 62 | "id": "qbBpD7dvcJbM" 63 | }, 64 | "source": [ 65 | "# Learn ideas and Practice Syntax" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "metadata": { 71 | "id": "LTBnlzhUcxRz" 72 | }, 73 | "source": [ 74 | "# 0. Introduction\n", 75 | "\n", 76 | "Theoretically, Any turing complete language can mimic each other's performance. A programming language should have the following feature to be Turing-complete.\n", 77 | "0. Conditional repetition or conditional jump (while, for, if and goto)\n", 78 | "1. Read and Write mechanism (variables)\n", 79 | "\n", 80 | "**Example :** C, C++, C#, Java, Lua, Python\n" 81 | ] 82 | }, 83 | { 84 | "cell_type": "markdown", 85 | "metadata": { 86 | "id": "w4LpBYrEbUpp" 87 | }, 88 | "source": [ 89 | "## Why choose python for Deep Learning Research?\n", 90 | "Early Deep learning era was built on top of Lua ([torch](https://github.com/torch)) and python ([Theano](https://github.com/Theano/Theano)). But somehow python stands out in the follwoing points. \n", 91 | "\n", 92 | "0. Shorter codes.\n", 93 | "1. Momentum, rapid prototyping and rapid application development.\n", 94 | "2. Vibrant Comminity, and Community support.\n", 95 | "3. Excellent eco-system of library and package managers.\n", 96 | "4. Good Debugging tools.\n", 97 | "5. Better OS intrigration.\n", 98 | "\n", 99 | "Later google built [tensorflow](https://github.com/tensorflow/tensorflow) with full python api support and facebook adopted Soumit Chintala's [pytroch](https://github.com/pytorch/pytorch) and start supporting it. Both of them are now excellent framework for doing Deep Learning Research and Deployment.\n", 100 | "\n", 101 | "Now **Python has become the Mother-tongue of scientific Programming.**" 102 | ] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "metadata": { 107 | "id": "ozzlBP6unuhe" 108 | }, 109 | "source": [ 110 | "# 1. Interpreter\n", 111 | "\n", 112 | "One of the major difference between compiler and interpreter is **when** and **how** the compilation and execution is done? \n", 113 | "\n", 114 | "In Compiler based language, Compilation is done before the execution.\n", 115 | "In Interpreter based language, the Compilation and Execution take place simultaneously. It enables us to create **this** type of notebook where we can run each cell, see the output and again run a set of new command. This is simply,\n", 116 | "\n", 117 | "$$GREAT \\ \\ FOR \\ \\ DEBUGGING$$\n", 118 | "\n", 119 | "Also it helps us to take **educative decisions** while inspecting any phenomena inside the **data** or **model**.\n", 120 | "\n" 121 | ] 122 | }, 123 | { 124 | "cell_type": "markdown", 125 | "metadata": { 126 | "id": "UWTF-tkMzfnR" 127 | }, 128 | "source": [ 129 | "##Code running\n", 130 | "\n", 131 | "3 different way.\n", 132 | "\n", 133 | "1. **From shell :** from the terminal write `python -c \"python code\"`\n", 134 | "2. **From a *.py file :** Write python code in a text file\n", 135 | "3. **Interactive mode :** In the shell write `python` and an interactive python shell will start where you can run python code line by line. Same feature is used for creating **this** types of **notebook**." 136 | ] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "metadata": { 141 | "id": "rqXwepIQzN2p" 142 | }, 143 | "source": [ 144 | "### From Shell" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "metadata": { 150 | "id": "qoG4aaQ18ST_", 151 | "outputId": "b7e6f2ed-5b68-4997-e5bc-683def60c1eb", 152 | "colab": { 153 | "base_uri": "https://localhost:8080/", 154 | "height": 52 155 | } 156 | }, 157 | "source": [ 158 | "!python -c 'import torch; print(\"Hello Word\"); print(torch.__version__)'" 159 | ], 160 | "execution_count": null, 161 | "outputs": [ 162 | { 163 | "output_type": "stream", 164 | "text": [ 165 | "Hello Word\n", 166 | "1.3.1\n" 167 | ], 168 | "name": "stdout" 169 | } 170 | ] 171 | }, 172 | { 173 | "cell_type": "markdown", 174 | "metadata": { 175 | "id": "lFIDknzPzSop" 176 | }, 177 | "source": [ 178 | "### From a *.py file\n", 179 | "\n", 180 | "Let's create the python file first. and then run the python file from terminal. In real life you may use an IDE or editor to create the python code and save it as `*.py` extension." 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "metadata": { 186 | "id": "fxRSmOMTwcvI", 187 | "outputId": "58e26d7a-ae4a-48aa-ce57-7c3255096ccf", 188 | "colab": { 189 | "base_uri": "https://localhost:8080/", 190 | "height": 125 191 | } 192 | }, 193 | "source": [ 194 | "!rm hello_word.py # delete if any hello_world.py is there\n", 195 | "!touch hello_word.py # create an empty hello_world.oy text file\n", 196 | "!echo -e \"import torch\" >> hello_word.py #write on hello_world.py file\n", 197 | "!echo -e \"print(\\\"Hello Word\\\")\" >> hello_word.py #write on hello_world.py file\n", 198 | "!echo -e \"print(torch.__version__)\" >> hello_word.py #write on hello_world.py file\n", 199 | "!cat hello_word.py # View helloworld.py\n", 200 | "!python hello_word.py # run hello_world.py\n", 201 | "arr = [0,0,0,0,0]" 202 | ], 203 | "execution_count": null, 204 | "outputs": [ 205 | { 206 | "output_type": "stream", 207 | "text": [ 208 | "rm: cannot remove 'hello_word.py': No such file or directory\n", 209 | "import torch\n", 210 | "print(\"Hello Word\")\n", 211 | "print(torch.__version__)\n", 212 | "Hello Word\n", 213 | "1.3.1\n" 214 | ], 215 | "name": "stdout" 216 | } 217 | ] 218 | }, 219 | { 220 | "cell_type": "markdown", 221 | "metadata": { 222 | "id": "CbDMa6H7101u" 223 | }, 224 | "source": [ 225 | "### Interactive mode\n" 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "metadata": { 231 | "id": "Ske0iL2F0FYi", 232 | "outputId": "3008da88-9182-4733-f303-bb3fb7f7c955", 233 | "colab": { 234 | "base_uri": "https://localhost:8080/", 235 | "height": 89 236 | } 237 | }, 238 | "source": [ 239 | "import torch\n", 240 | "print(\"Hello Word\")\n", 241 | "print(torch.__version__)\n", 242 | "print(var0)\n", 243 | "arr[0] = arr[0]+1\n", 244 | "print(arr)" 245 | ], 246 | "execution_count": null, 247 | "outputs": [ 248 | { 249 | "output_type": "stream", 250 | "text": [ 251 | "Hello Word\n", 252 | "1.3.1\n", 253 | "5\n", 254 | "[2, 2, 3, 4]\n" 255 | ], 256 | "name": "stdout" 257 | } 258 | ] 259 | }, 260 | { 261 | "cell_type": "markdown", 262 | "metadata": { 263 | "id": "5uP1wc3a4-CB" 264 | }, 265 | "source": [ 266 | "# 2. Variables & Built in Data-structures" 267 | ] 268 | }, 269 | { 270 | "cell_type": "markdown", 271 | "metadata": { 272 | "id": "5Eljaojy6xcr" 273 | }, 274 | "source": [ 275 | "## Variable Declaration\n", 276 | "- Declare a variable with an `=` sign and assign a value to it.\n", 277 | "- to see the data type use `type()` function\n", 278 | "- make sure you know about the automatic type conversion. mainly for `floating` point numbers.\n", 279 | "- run simple operations with operator `+,-,*,/, %, **`. `%` is modulus operation and `**` power operation. `a**c` is equivalent to $a^c$ \n", 280 | "- import python libraries by `import library_name`.\n", 281 | "- In python each of the variable is a class." 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "metadata": { 287 | "id": "bSwZNf_D0T2K", 288 | "outputId": "16212b2f-e6e9-4aff-94df-fb3f0390ab95", 289 | "colab": { 290 | "base_uri": "https://localhost:8080/", 291 | "height": 73 292 | } 293 | }, 294 | "source": [ 295 | "var0 = 5\n", 296 | "var1 = 5.0\n", 297 | "var2 = 'The quick brown for runs over the lazy dog'\n", 298 | "var3 = 1e-3 # 0.001\n", 299 | "var4 = 1e3 # 0.001\n", 300 | "var5 = True\n", 301 | "var6 = False\n", 302 | "arr = [1,2,3,4]\n", 303 | "print(var0, var1, var2, var3, var4, var5, var6)\n", 304 | "print(type(var0), type(var1), type(var2), type(var3), type(var4), type(var5), type(var6))" 305 | ], 306 | "execution_count": null, 307 | "outputs": [ 308 | { 309 | "output_type": "stream", 310 | "text": [ 311 | "5 5.0 The quick brown for runs over the lazy dog 0.001 1000.0 True False\n", 312 | " \n" 313 | ], 314 | "name": "stdout" 315 | } 316 | ] 317 | }, 318 | { 319 | "cell_type": "code", 320 | "metadata": { 321 | "id": "W_vWQ17M3Hxu", 322 | "outputId": "414e8122-dd8b-460a-d4a2-f4e4f960df31", 323 | "colab": { 324 | "base_uri": "https://localhost:8080/", 325 | "height": 116 326 | } 327 | }, 328 | "source": [ 329 | "a = 5/3\n", 330 | "print(\"a = 5/3 =\", a, type(a))\n", 331 | "a = 5.0/3\n", 332 | "print(\"a = 5.0/3 =\", a, type(a))\n", 333 | "a = 5/3.0\n", 334 | "print(\"a = 5/3.0 =\", a, type(a))\n", 335 | "a = 5//3\n", 336 | "print(\"a = 5//3 =\", a, type(a))\n", 337 | "a = 5.0//3\n", 338 | "print(\"a = 5.0//3 =\", a, type(a)) \n", 339 | "a = 5//3.0\n", 340 | "print(\"a = 5//3.0 =\", a, type(a))" 341 | ], 342 | "execution_count": null, 343 | "outputs": [ 344 | { 345 | "output_type": "stream", 346 | "text": [ 347 | "a = 5/3 = 1.6666666666666667 \n", 348 | "a = 5.0/3 = 1.6666666666666667 \n", 349 | "a = 5/3.0 = 1.6666666666666667 \n", 350 | "a = 5//3 = 1 \n", 351 | "a = 5.0//3 = 1.0 \n", 352 | "a = 5//3.0 = 1.0 \n" 353 | ], 354 | "name": "stdout" 355 | } 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "metadata": { 361 | "id": "ufvSarOZ3H0i" 362 | }, 363 | "source": [ 364 | "a = 5\n", 365 | "b = a+1\n", 366 | "b += 1 # b = b+1" 367 | ], 368 | "execution_count": null, 369 | "outputs": [] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "metadata": { 374 | "id": "un44_9T1JrRO", 375 | "outputId": "a0739c5b-d0e7-45d0-e82c-8f20d9d106b3", 376 | "colab": { 377 | "base_uri": "https://localhost:8080/", 378 | "height": 100 379 | } 380 | }, 381 | "source": [ 382 | "a = '1'\n", 383 | "print(type(a), a)\n", 384 | "b = int(a)\n", 385 | "print(type(b), b)\n", 386 | "c = 1\n", 387 | "print(type(c), c)\n", 388 | "d = str(c)\n", 389 | "print(type(d), d)\n", 390 | "k = 3.14159265\n", 391 | "print(k, int(k), float(int(k)))" 392 | ], 393 | "execution_count": null, 394 | "outputs": [ 395 | { 396 | "output_type": "stream", 397 | "text": [ 398 | " 1\n", 399 | " 1\n", 400 | " 1\n", 401 | " 1\n", 402 | "3.14159265 3 3.0\n" 403 | ], 404 | "name": "stdout" 405 | } 406 | ] 407 | }, 408 | { 409 | "cell_type": "markdown", 410 | "metadata": { 411 | "id": "5YBtEAVx76HA" 412 | }, 413 | "source": [ 414 | "**Note:** unlike C/C++ code, `++` is invalid in python.\n" 415 | ] 416 | }, 417 | { 418 | "cell_type": "code", 419 | "metadata": { 420 | "id": "baEZfKbw3H2d", 421 | "outputId": "71420409-93e4-4313-9abc-da584e11a04c", 422 | "colab": { 423 | "base_uri": "https://localhost:8080/", 424 | "height": 130 425 | } 426 | }, 427 | "source": [ 428 | "b++ # This line won't run" 429 | ], 430 | "execution_count": null, 431 | "outputs": [ 432 | { 433 | "output_type": "error", 434 | "ename": "SyntaxError", 435 | "evalue": "ignored", 436 | "traceback": [ 437 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m b++ # This line won't run\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" 438 | ] 439 | } 440 | ] 441 | }, 442 | { 443 | "cell_type": "code", 444 | "metadata": { 445 | "id": "XsvbvEvX3H5W", 446 | "outputId": "6b9a727c-44a7-47c4-bc6f-200ace95aa0f", 447 | "colab": { 448 | "base_uri": "https://localhost:8080/", 449 | "height": 33 450 | } 451 | }, 452 | "source": [ 453 | "a = 500\n", 454 | "b = a % 3\n", 455 | "c = a ** b\n", 456 | "print(c)" 457 | ], 458 | "execution_count": null, 459 | "outputs": [ 460 | { 461 | "output_type": "stream", 462 | "text": [ 463 | "250000\n" 464 | ], 465 | "name": "stdout" 466 | } 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "metadata": { 472 | "id": "HWXvaeqv9K5J", 473 | "outputId": "d61b1b20-500b-40d5-d524-bb681332199e", 474 | "colab": { 475 | "base_uri": "https://localhost:8080/", 476 | "height": 33 477 | } 478 | }, 479 | "source": [ 480 | "import math\n", 481 | "print(math.sqrt(5))" 482 | ], 483 | "execution_count": null, 484 | "outputs": [ 485 | { 486 | "output_type": "stream", 487 | "text": [ 488 | "2.23606797749979\n" 489 | ], 490 | "name": "stdout" 491 | } 492 | ] 493 | }, 494 | { 495 | "cell_type": "markdown", 496 | "metadata": { 497 | "id": "8Js7QmNeCGPm" 498 | }, 499 | "source": [ 500 | "## List" 501 | ] 502 | }, 503 | { 504 | "cell_type": "code", 505 | "metadata": { 506 | "id": "MzWtW1N6CHM1" 507 | }, 508 | "source": [ 509 | "a = [1, 2, 3, 4, 5]\n", 510 | "b = [1, \"hello world\", 3.0, {\"prof\":\"Dr. Shafiq Joty\", \"Course\" : \"MH6812\" }]\n", 511 | "print(a)\n", 512 | "print(b)\n", 513 | "\n", 514 | "a.append(100)\n", 515 | "print(a)\n" 516 | ], 517 | "execution_count": null, 518 | "outputs": [] 519 | }, 520 | { 521 | "cell_type": "markdown", 522 | "metadata": { 523 | "id": "_ByvnIPmCXaI" 524 | }, 525 | "source": [ 526 | "Note that, in python list works as a call by reference. " 527 | ] 528 | }, 529 | { 530 | "cell_type": "code", 531 | "metadata": { 532 | "id": "ZtAM83zECPNK" 533 | }, 534 | "source": [ 535 | "a = [0,1,2,3,4,5,6,7]\n", 536 | "b = a\n", 537 | "b[4] = 100\n", 538 | "print(a)\n", 539 | "print(b)\n", 540 | "print(len(a))\n", 541 | "print(len(b))" 542 | ], 543 | "execution_count": null, 544 | "outputs": [] 545 | }, 546 | { 547 | "cell_type": "code", 548 | "metadata": { 549 | "id": "GqCbD331X9eo" 550 | }, 551 | "source": [ 552 | "import copy\n", 553 | "a = [0,1,2,3,4,5,6,7]\n", 554 | "b = copy.deepcopy(a)\n", 555 | "b[4] = 100\n", 556 | "print(a)\n", 557 | "print(b)" 558 | ], 559 | "execution_count": null, 560 | "outputs": [] 561 | }, 562 | { 563 | "cell_type": "markdown", 564 | "metadata": { 565 | "id": "G3Cyjnwv8_ZT" 566 | }, 567 | "source": [ 568 | "## String" 569 | ] 570 | }, 571 | { 572 | "cell_type": "code", 573 | "metadata": { 574 | "id": "fKKN9Pit3H8E" 575 | }, 576 | "source": [ 577 | "a = 5\n", 578 | "b = 10\n", 579 | "s = 'The quick brown for runs over the lazy dog'\n", 580 | "f = 'a = {}, b = {}'.format(a, b) #string formating\n", 581 | "print(f)" 582 | ], 583 | "execution_count": null, 584 | "outputs": [] 585 | }, 586 | { 587 | "cell_type": "markdown", 588 | "metadata": { 589 | "id": "fH_knxMeA132" 590 | }, 591 | "source": [ 592 | "Note that sting is not mutable. You can't do random access to the position of the string. you have to call library function for that." 593 | ] 594 | }, 595 | { 596 | "cell_type": "code", 597 | "metadata": { 598 | "id": "XuWnE4CRBRP-" 599 | }, 600 | "source": [ 601 | "a = 'The quick brown for runs over the lazy dog'\n", 602 | "a[0] = 'x' # This line won't run" 603 | ], 604 | "execution_count": null, 605 | "outputs": [] 606 | }, 607 | { 608 | "cell_type": "code", 609 | "metadata": { 610 | "id": "lIOWKQaHBgqh" 611 | }, 612 | "source": [ 613 | "a = 'The quick brown for runs over the lazy dog'\n", 614 | "a = list(a)\n", 615 | "print(a)\n", 616 | "a[0] = 'x'\n", 617 | "a = \"\".join(a)\n", 618 | "print(a)" 619 | ], 620 | "execution_count": null, 621 | "outputs": [] 622 | }, 623 | { 624 | "cell_type": "markdown", 625 | "metadata": { 626 | "id": "z3_aiQHI_UbL" 627 | }, 628 | "source": [ 629 | "## Dictionary\n", 630 | "\n", 631 | "- Dictionaries are set of `key` and `value`. \n", 632 | "- Keys are unique. \n", 633 | "- regular dictionary doesn't ensure the order information of data. \n", 634 | "- Ordered dictionary contains the order information of data but you have to import it from `collections` library.\n" 635 | ] 636 | }, 637 | { 638 | "cell_type": "code", 639 | "metadata": { 640 | "id": "0vR1pYH4AB6v", 641 | "outputId": "522f4694-56e6-4f59-ffb4-a327c37d7523", 642 | "colab": { 643 | "base_uri": "https://localhost:8080/", 644 | "height": 35 645 | } 646 | }, 647 | "source": [ 648 | "a = { 0 : \"Mr. a\", 1 : \"Mr. b\", 2 : \"Mr. c\" }\n", 649 | "print(a)" 650 | ], 651 | "execution_count": null, 652 | "outputs": [ 653 | { 654 | "output_type": "stream", 655 | "text": [ 656 | "{0: 'Mr. a', 1: 'Mr. b', 2: 'Mr. c'}\n" 657 | ], 658 | "name": "stdout" 659 | } 660 | ] 661 | }, 662 | { 663 | "cell_type": "code", 664 | "metadata": { 665 | "id": "Trl9reiHAXyr" 666 | }, 667 | "source": [ 668 | "a = { 0 : \"Mr. a\", 0 : \"Mr. b\", 2 : \"Mr. b\", 1 : \"Mr. c\", 0 : \"Mr. c\" }\n", 669 | "print(a)" 670 | ], 671 | "execution_count": null, 672 | "outputs": [] 673 | }, 674 | { 675 | "cell_type": "code", 676 | "metadata": { 677 | "id": "NdgsL5iwArgW" 678 | }, 679 | "source": [ 680 | "from collections import OrderedDict\n", 681 | "b = OrderedDict([('b', 2), ('a', 1)])\n", 682 | "print(b)\n", 683 | "\n", 684 | "b = OrderedDict({ 0 : \"Mr. a\", 1 : \"Mr. b\", 2 : \"Mr. b\" })\n", 685 | "print(b)\n", 686 | "\n", 687 | "b = OrderedDict({ 0 : \"Mr. a\", 0 : \"Mr. b\", 2 : \"Mr. b\", 1 : \"Mr. c\", 0 : \"Mr. c\" })\n", 688 | "print(b)\n" 689 | ], 690 | "execution_count": null, 691 | "outputs": [] 692 | }, 693 | { 694 | "cell_type": "markdown", 695 | "metadata": { 696 | "id": "UhRdt-LI-sKc" 697 | }, 698 | "source": [ 699 | "## Slicing\n", 700 | "\n", 701 | "A very inportant concept. How to access data in python.\n", 702 | "\n", 703 | "\n", 704 | "\n", 705 | "```\n", 706 | " +---+---+---+---+---+---+\n", 707 | " | P | y | t | h | o | n |\n", 708 | " +---+---+---+---+---+---+\n", 709 | " 0 1 2 3 4 5 \n", 710 | " -6 -5 -4 -3 -2 -1\n", 711 | "```\n", 712 | "\n" 713 | ] 714 | }, 715 | { 716 | "cell_type": "code", 717 | "metadata": { 718 | "id": "22CoIsKX3IGM", 719 | "outputId": "866d9a3b-0c68-4a13-bb3e-66dd4e6ac0cb", 720 | "colab": { 721 | "base_uri": "https://localhost:8080/", 722 | "height": 105 723 | } 724 | }, 725 | "source": [ 726 | "letters = ['p', 'y', 't', 'h', 'o', 'n']\n", 727 | "print(\"0\", letters[0])\n", 728 | "print(\"1\", letters[1])\n", 729 | "print(\"2\", letters[2])\n", 730 | "print(\"-1\", letters[-1])\n", 731 | "print(\"-2\", letters[-2])" 732 | ], 733 | "execution_count": null, 734 | "outputs": [ 735 | { 736 | "output_type": "stream", 737 | "text": [ 738 | "0 p\n", 739 | "1 y\n", 740 | "2 t\n", 741 | "-1 n\n", 742 | "-2 o\n" 743 | ], 744 | "name": "stdout" 745 | } 746 | ] 747 | }, 748 | { 749 | "cell_type": "code", 750 | "metadata": { 751 | "id": "nAx_XPV43II5", 752 | "outputId": "7fee34c8-1c0d-4b1b-80c1-4066789f2c9a", 753 | "colab": { 754 | "base_uri": "https://localhost:8080/", 755 | "height": 227 756 | } 757 | }, 758 | "source": [ 759 | "print(\":\", letters[:])\n", 760 | "print(\"0:\", letters[0:])\n", 761 | "print(\":-1\", letters[:-1])\n", 762 | "print(\"0:5\", letters[0:5])\n", 763 | "print(\":2\", letters[:2])\n", 764 | "print(\"3:5\", letters[3:5])\n", 765 | "print(\"0:-1\", letters[0:-1])\n", 766 | "print(\"0:-2\", letters[0:-2])\n", 767 | "print(\"-1:0\", letters[-1:0])\n", 768 | "print(\"-5:-2\", letters[-5:-2])\n", 769 | "print(letters[-1:0:-2])\n", 770 | "print(letters[-1:-7:-1])" 771 | ], 772 | "execution_count": null, 773 | "outputs": [ 774 | { 775 | "output_type": "stream", 776 | "text": [ 777 | ": ['p', 'y', 't', 'h', 'o', 'n']\n", 778 | "0: ['p', 'y', 't', 'h', 'o', 'n']\n", 779 | ":-1 ['p', 'y', 't', 'h', 'o']\n", 780 | "0:5 ['p', 'y', 't', 'h', 'o']\n", 781 | ":2 ['p', 'y']\n", 782 | "3:5 ['h', 'o']\n", 783 | "0:-1 ['p', 'y', 't', 'h', 'o']\n", 784 | "0:-2 ['p', 'y', 't', 'h']\n", 785 | "-1:0 []\n", 786 | "-5:-2 ['y', 't', 'h']\n", 787 | "['n', 'o', 'h', 't', 'y']\n", 788 | "['n', 'o', 'h', 't', 'y', 'p']\n" 789 | ], 790 | "name": "stdout" 791 | } 792 | ] 793 | }, 794 | { 795 | "cell_type": "markdown", 796 | "metadata": { 797 | "id": "HqoojIttFfsc" 798 | }, 799 | "source": [ 800 | "## Practice\n", 801 | "Go to the follwoing sites and write the code segment in your preffered notebook and try to understand the output. \n", 802 | "- [An Informal Introduction to Python](https://docs.python.org/3.6/tutorial/introduction.html) -> section 3.1\n" 803 | ] 804 | }, 805 | { 806 | "cell_type": "markdown", 807 | "metadata": { 808 | "id": "FIzSmW0-Lgoz" 809 | }, 810 | "source": [ 811 | "# 3. Conditional\n", 812 | "\n", 813 | "Note that in python, indentation is **must**. Without proper indentation code will not execute.\n", 814 | "```\n", 815 | "if condition:\n", 816 | " statement\n", 817 | "elif condition:\n", 818 | " statement\n", 819 | "...\n", 820 | "...\n", 821 | "else:\n", 822 | " statement\n", 823 | "```\n", 824 | "\n", 825 | "\n", 826 | "\n" 827 | ] 828 | }, 829 | { 830 | "cell_type": "code", 831 | "metadata": { 832 | "id": "ZjdJZ3PSNtv4" 833 | }, 834 | "source": [ 835 | "year = 2000\n", 836 | "if year % 400 == 0 :\n", 837 | " print(True)\n", 838 | "elif year % 4 == 0 and year % 100 != 0:\n", 839 | " print(True)\n", 840 | "else:\n", 841 | " print(False)" 842 | ], 843 | "execution_count": null, 844 | "outputs": [] 845 | }, 846 | { 847 | "cell_type": "code", 848 | "metadata": { 849 | "id": "XUbO5YX3LrPj", 850 | "outputId": "56c13aa0-dff7-4181-c600-a2a2e07eb32a", 851 | "colab": { 852 | "base_uri": "https://localhost:8080/", 853 | "height": 52 854 | } 855 | }, 856 | "source": [ 857 | "score = int(input(\"Score :\"))\n", 858 | "if score >= 80:\n", 859 | " print(\"A\")\n", 860 | "elif score >= 75:\n", 861 | " print(\"A-\")\n", 862 | "elif score >= 70:\n", 863 | " print(\"B\")\n", 864 | "else:\n", 865 | " print(\"Fail\")" 866 | ], 867 | "execution_count": null, 868 | "outputs": [ 869 | { 870 | "output_type": "stream", 871 | "text": [ 872 | "Score :50\n", 873 | "Fail\n" 874 | ], 875 | "name": "stdout" 876 | } 877 | ] 878 | }, 879 | { 880 | "cell_type": "markdown", 881 | "metadata": { 882 | "id": "xPosaAhJtdZS" 883 | }, 884 | "source": [ 885 | "# 4. Function\n", 886 | "\n", 887 | "Function definition is given below. Note that argument with default values will always be followed by regular argument.\n", 888 | "\n", 889 | "```\n", 890 | "def function_name( arg1, args2, args2, .. argsn):\n", 891 | " ...\n", 892 | " ...\n", 893 | " statememt\n", 894 | " ...\n", 895 | " ...\n", 896 | " return ret_obj\n", 897 | "```\n", 898 | "\n" 899 | ] 900 | }, 901 | { 902 | "cell_type": "code", 903 | "metadata": { 904 | "id": "xLs_wk0pPbDE" 905 | }, 906 | "source": [ 907 | "def isLeapYear(year):\n", 908 | " if year % 400 == 0 :\n", 909 | " return True\n", 910 | " if year % 4 == 0 and year % 100 != 0:\n", 911 | " return True\n", 912 | " return False\n", 913 | "print(isLeapYear(2000))\n", 914 | "print(isLeapYear(2019))" 915 | ], 916 | "execution_count": null, 917 | "outputs": [] 918 | }, 919 | { 920 | "cell_type": "code", 921 | "metadata": { 922 | "id": "70SkB1U7LuYb" 923 | }, 924 | "source": [ 925 | "def grader(score):\n", 926 | " if score >= 80:\n", 927 | " return \"A\"\n", 928 | " elif score >= 75:\n", 929 | " return \"A-\"\n", 930 | " elif score >= 70:\n", 931 | " return \"B\"\n", 932 | " else:\n", 933 | " return \"Fail\"" 934 | ], 935 | "execution_count": null, 936 | "outputs": [] 937 | }, 938 | { 939 | "cell_type": "code", 940 | "metadata": { 941 | "id": "yCboTT8ANiNi" 942 | }, 943 | "source": [ 944 | "grader(100)" 945 | ], 946 | "execution_count": null, 947 | "outputs": [] 948 | }, 949 | { 950 | "cell_type": "code", 951 | "metadata": { 952 | "id": "ZLYGLX2dtcgA" 953 | }, 954 | "source": [ 955 | "def fibonacci(n):\n", 956 | " a, b = 0, 1\n", 957 | " ret = []\n", 958 | " while a < n:\n", 959 | " ret.append(a)\n", 960 | " a, b = b, a+b\n", 961 | " return ret" 962 | ], 963 | "execution_count": null, 964 | "outputs": [] 965 | }, 966 | { 967 | "cell_type": "code", 968 | "metadata": { 969 | "id": "Ec9xvC-vwXJO" 970 | }, 971 | "source": [ 972 | "fibonacci(5)" 973 | ], 974 | "execution_count": null, 975 | "outputs": [] 976 | }, 977 | { 978 | "cell_type": "code", 979 | "metadata": { 980 | "id": "wxdR6_jcwimG" 981 | }, 982 | "source": [ 983 | "def fibonacci(n=10000):\n", 984 | " a, b = 0, 1\n", 985 | " ret = []\n", 986 | " while a < n:\n", 987 | " ret.append(a)\n", 988 | " a, b = b, a+b\n", 989 | " return ret" 990 | ], 991 | "execution_count": null, 992 | "outputs": [] 993 | }, 994 | { 995 | "cell_type": "markdown", 996 | "metadata": { 997 | "id": "9jufSACyK7aM" 998 | }, 999 | "source": [] 1000 | }, 1001 | { 1002 | "cell_type": "code", 1003 | "metadata": { 1004 | "id": "FfOtK3uCwluu" 1005 | }, 1006 | "source": [ 1007 | "fibonacci2()" 1008 | ], 1009 | "execution_count": null, 1010 | "outputs": [] 1011 | }, 1012 | { 1013 | "cell_type": "markdown", 1014 | "metadata": { 1015 | "id": "Nt-vhDRjK8mT" 1016 | }, 1017 | "source": [ 1018 | "Let's have some fun with recursive function." 1019 | ] 1020 | }, 1021 | { 1022 | "cell_type": "code", 1023 | "metadata": { 1024 | "id": "Ug-IWHyvHCPT" 1025 | }, 1026 | "source": [ 1027 | "def nCr(n, r, MOD=10000000+7):\n", 1028 | " \n", 1029 | " if n == r or r == 0:\n", 1030 | " return 1\n", 1031 | " if r==1:\n", 1032 | " return n\n", 1033 | " ret = (nCr(n-1,r)%MOD + nCr(n-1,r-1)%MOD)%MOD\n", 1034 | " return ret" 1035 | ], 1036 | "execution_count": null, 1037 | "outputs": [] 1038 | }, 1039 | { 1040 | "cell_type": "markdown", 1041 | "metadata": { 1042 | "id": "_rpo2zmOORxj" 1043 | }, 1044 | "source": [ 1045 | "# 5. While loop\n", 1046 | "\n", 1047 | "```\n", 1048 | "while condition:\n", 1049 | " statement\n", 1050 | " statement\n", 1051 | " statement\n", 1052 | " ...\n", 1053 | " ...\n", 1054 | " ...\n", 1055 | " \n", 1056 | "```\n" 1057 | ] 1058 | }, 1059 | { 1060 | "cell_type": "code", 1061 | "metadata": { 1062 | "id": "jcViHk_NORNP" 1063 | }, 1064 | "source": [ 1065 | "idx = 10\n", 1066 | "while idx < 100:\n", 1067 | " if idx % 29 == 0:\n", 1068 | " break\n", 1069 | " if idx % 2 == 0 :\n", 1070 | " print(idx, idx**2)\n", 1071 | " elif idx % 5 == 0 :\n", 1072 | " idx += 1 ### What happend if we comment this line\n", 1073 | " continue\n", 1074 | " elif idx % 3 == 0 :\n", 1075 | " print(\"An odd number\")\n", 1076 | " idx += 1" 1077 | ], 1078 | "execution_count": null, 1079 | "outputs": [] 1080 | }, 1081 | { 1082 | "cell_type": "code", 1083 | "metadata": { 1084 | "id": "tPpSK1WFIz2Z" 1085 | }, 1086 | "source": [ 1087 | "row = 0\n", 1088 | "base = 10\n", 1089 | "while row < base:\n", 1090 | " col = 0\n", 1091 | " while col <= row:\n", 1092 | " print(nCr(row, col), \" \", end=\"\")\n", 1093 | " col += 1\n", 1094 | " print()\n", 1095 | " row += 1" 1096 | ], 1097 | "execution_count": null, 1098 | "outputs": [] 1099 | }, 1100 | { 1101 | "cell_type": "markdown", 1102 | "metadata": { 1103 | "id": "cc4DBisrKSOf" 1104 | }, 1105 | "source": [ 1106 | "let's implement the above cell as a function" 1107 | ] 1108 | }, 1109 | { 1110 | "cell_type": "code", 1111 | "metadata": { 1112 | "id": "y1dUPCACKXum" 1113 | }, 1114 | "source": [ 1115 | "def pascal_triangle(num_of_row=5):\n", 1116 | " row = 0\n", 1117 | " base = num_of_row\n", 1118 | " while row < base:\n", 1119 | " col = 0\n", 1120 | " while col <= row:\n", 1121 | " print(nCr(row, col), \" \", end=\"\")\n", 1122 | " col += 1\n", 1123 | " print()\n", 1124 | " row += 1" 1125 | ], 1126 | "execution_count": null, 1127 | "outputs": [] 1128 | }, 1129 | { 1130 | "cell_type": "code", 1131 | "metadata": { 1132 | "id": "j3oiu-2hKqTY" 1133 | }, 1134 | "source": [ 1135 | "pascal_triangle(num_of_row=10)" 1136 | ], 1137 | "execution_count": null, 1138 | "outputs": [] 1139 | }, 1140 | { 1141 | "cell_type": "markdown", 1142 | "metadata": { 1143 | "id": "dfisL7hZLBXK" 1144 | }, 1145 | "source": [ 1146 | "**Note** : We can make this function **really fast** with Dynamic Programmming/memoization technique. But that's not in the scope of this tutorial.\n", 1147 | "\n", 1148 | "But instead of printing the values let's save then in a list." 1149 | ] 1150 | }, 1151 | { 1152 | "cell_type": "code", 1153 | "metadata": { 1154 | "id": "gcVEDahQReFp" 1155 | }, 1156 | "source": [ 1157 | "def pascal_triangle(num_of_row=5):\n", 1158 | " row = 0\n", 1159 | " base = num_of_row\n", 1160 | " ret = []\n", 1161 | " while row < base:\n", 1162 | " col = 0\n", 1163 | " ret_row = []\n", 1164 | " while col <= row:\n", 1165 | " nCr_val = nCr(row, col)\n", 1166 | " ret_row.append(nCr_val)\n", 1167 | " col += 1\n", 1168 | " row += 1\n", 1169 | " ret.append(ret_row)\n", 1170 | " return ret" 1171 | ], 1172 | "execution_count": null, 1173 | "outputs": [] 1174 | }, 1175 | { 1176 | "cell_type": "code", 1177 | "metadata": { 1178 | "id": "E8-4Y1R9RxFF" 1179 | }, 1180 | "source": [ 1181 | "arr = pascal_triangle(num_of_row=5)" 1182 | ], 1183 | "execution_count": null, 1184 | "outputs": [] 1185 | }, 1186 | { 1187 | "cell_type": "code", 1188 | "metadata": { 1189 | "id": "v9IDZFbrSg-M" 1190 | }, 1191 | "source": [ 1192 | "idx = 0\n", 1193 | "tot_row = len(arr)\n", 1194 | "while idx < tot_row:\n", 1195 | " print(arr[idx])\n", 1196 | " idx += 1" 1197 | ], 1198 | "execution_count": null, 1199 | "outputs": [] 1200 | }, 1201 | { 1202 | "cell_type": "markdown", 1203 | "metadata": { 1204 | "id": "YnlBwnd4SLC1" 1205 | }, 1206 | "source": [ 1207 | "Let's convert the above cell into a new function called, `print2Dmat()`." 1208 | ] 1209 | }, 1210 | { 1211 | "cell_type": "code", 1212 | "metadata": { 1213 | "id": "eCLZAfWiSrxU" 1214 | }, 1215 | "source": [ 1216 | "def print2Dmat(arr):\n", 1217 | " idx = 0\n", 1218 | " tot_row = len(arr)\n", 1219 | " while idx < tot_row:\n", 1220 | " print(arr[idx])\n", 1221 | " idx += 1\n", 1222 | "\n", 1223 | "print2Dmat(pascal_triangle(15))" 1224 | ], 1225 | "execution_count": null, 1226 | "outputs": [] 1227 | }, 1228 | { 1229 | "cell_type": "code", 1230 | "metadata": { 1231 | "id": "J4V-yRedUB2w", 1232 | "outputId": "12f21bf8-1b2b-4774-b465-6b5c228957e3", 1233 | "colab": { 1234 | "base_uri": "https://localhost:8080/", 1235 | "height": 50 1236 | } 1237 | }, 1238 | "source": [ 1239 | "def ask_ok(prompt, retries=4, reminder='Please try again!'):\n", 1240 | " \"\"\"The basic function to ask for yes or no question.\n", 1241 | " return type: True or False\n", 1242 | " \"\"\"\n", 1243 | " while True:\n", 1244 | " ok = input(prompt)\n", 1245 | " if ok in ('y', 'ye', 'yes'):\n", 1246 | " return True\n", 1247 | " if ok in ('n', 'no', 'nop', 'nope'):\n", 1248 | " return False\n", 1249 | " retries = retries - 1\n", 1250 | " if retries < 0:\n", 1251 | " raise ValueError('invalid user response') # We will discuss this in a bit\n", 1252 | " print(reminder)\n", 1253 | "\n", 1254 | "ask_ok(\"Agree? : \", retries=4, reminder='Please try again!')" 1255 | ], 1256 | "execution_count": null, 1257 | "outputs": [ 1258 | { 1259 | "output_type": "stream", 1260 | "text": [ 1261 | "Agree? : y\n" 1262 | ], 1263 | "name": "stdout" 1264 | }, 1265 | { 1266 | "output_type": "execute_result", 1267 | "data": { 1268 | "text/plain": [ 1269 | "True" 1270 | ] 1271 | }, 1272 | "metadata": { 1273 | "tags": [] 1274 | }, 1275 | "execution_count": 12 1276 | } 1277 | ] 1278 | }, 1279 | { 1280 | "cell_type": "code", 1281 | "metadata": { 1282 | "id": "ZvQ_7e3mY54S", 1283 | "outputId": "f4ec0d15-1dfb-4e6a-a8e2-8adba529d367", 1284 | "colab": { 1285 | "base_uri": "https://localhost:8080/", 1286 | "height": 66 1287 | } 1288 | }, 1289 | "source": [ 1290 | "print(ask_ok.__doc__)\n" 1291 | ], 1292 | "execution_count": null, 1293 | "outputs": [ 1294 | { 1295 | "output_type": "stream", 1296 | "text": [ 1297 | "The basic function to ask for yes or no question.\n", 1298 | " return type: True or False\n", 1299 | " \n" 1300 | ], 1301 | "name": "stdout" 1302 | } 1303 | ] 1304 | }, 1305 | { 1306 | "cell_type": "markdown", 1307 | "metadata": { 1308 | "id": "UB4b67ztY1Xf" 1309 | }, 1310 | "source": [ 1311 | "# 6. for loop\n", 1312 | "\n", 1313 | "```\n", 1314 | "for variable in iterable_object:\n", 1315 | " statement\n", 1316 | "```" 1317 | ] 1318 | }, 1319 | { 1320 | "cell_type": "code", 1321 | "metadata": { 1322 | "id": "hmMRuCAjZ397", 1323 | "outputId": "d1ecbd34-89f4-4697-db48-4e6b1e65b9af", 1324 | "colab": { 1325 | "base_uri": "https://localhost:8080/", 1326 | "height": 33 1327 | } 1328 | }, 1329 | "source": [ 1330 | "range(10) # returns [0-10)\n", 1331 | "range(100, 120) # returns [100-200)\n", 1332 | "range(100, 120, 3) # returns every 3 member of [100-200)" 1333 | ], 1334 | "execution_count": null, 1335 | "outputs": [ 1336 | { 1337 | "output_type": "execute_result", 1338 | "data": { 1339 | "text/plain": [ 1340 | "range(100, 120, 3)" 1341 | ] 1342 | }, 1343 | "metadata": { 1344 | "tags": [] 1345 | }, 1346 | "execution_count": 42 1347 | } 1348 | ] 1349 | }, 1350 | { 1351 | "cell_type": "code", 1352 | "metadata": { 1353 | "id": "VG8GPpO4Y0yc", 1354 | "outputId": "d79f5d58-b51e-433b-cec4-a91e4ab5e639", 1355 | "colab": { 1356 | "base_uri": "https://localhost:8080/", 1357 | "height": 66 1358 | } 1359 | }, 1360 | "source": [ 1361 | "for i in range(10):\n", 1362 | " print(i, \" \", end=\"\")\n", 1363 | "print()\n", 1364 | "for i in range(100, 120):\n", 1365 | " print(i, \" \", end=\"\")\n", 1366 | "print()\n", 1367 | "for i in range(100, 120, 3):\n", 1368 | " print(i, \" \", end=\"\")" 1369 | ], 1370 | "execution_count": null, 1371 | "outputs": [ 1372 | { 1373 | "output_type": "stream", 1374 | "text": [ 1375 | "0 1 2 3 4 5 6 7 8 9 \n", 1376 | "100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 \n", 1377 | "100 103 106 109 112 115 118 " 1378 | ], 1379 | "name": "stdout" 1380 | } 1381 | ] 1382 | }, 1383 | { 1384 | "cell_type": "markdown", 1385 | "metadata": { 1386 | "id": "sX4IlC6bagIu" 1387 | }, 1388 | "source": [ 1389 | "In general, every data structure/container in python should have a traversing procedure written inside of it's internal class." 1390 | ] 1391 | }, 1392 | { 1393 | "cell_type": "markdown", 1394 | "metadata": { 1395 | "id": "zov8BwuFa0Re" 1396 | }, 1397 | "source": [ 1398 | "## Iteration through list" 1399 | ] 1400 | }, 1401 | { 1402 | "cell_type": "code", 1403 | "metadata": { 1404 | "id": "zVFTOsRUVeZ9", 1405 | "outputId": "be659c20-97e6-4eb0-9838-720e5060b703", 1406 | "colab": { 1407 | "base_uri": "https://localhost:8080/", 1408 | "height": 100 1409 | } 1410 | }, 1411 | "source": [ 1412 | "for i in [1, 2, 3, 4, 6]:\n", 1413 | " print(i)" 1414 | ], 1415 | "execution_count": null, 1416 | "outputs": [ 1417 | { 1418 | "output_type": "stream", 1419 | "text": [ 1420 | "1\n", 1421 | "2\n", 1422 | "3\n", 1423 | "4\n", 1424 | "6\n" 1425 | ], 1426 | "name": "stdout" 1427 | } 1428 | ] 1429 | }, 1430 | { 1431 | "cell_type": "code", 1432 | "metadata": { 1433 | "id": "FhKbQi9Sa53Z" 1434 | }, 1435 | "source": [ 1436 | "## Iteration through dictionary" 1437 | ], 1438 | "execution_count": null, 1439 | "outputs": [] 1440 | }, 1441 | { 1442 | "cell_type": "code", 1443 | "metadata": { 1444 | "id": "YAczrjUmaxQa", 1445 | "outputId": "daeaa0a7-e8c9-4953-ac34-0acc06c9674e", 1446 | "colab": { 1447 | "base_uri": "https://localhost:8080/", 1448 | "height": 566 1449 | } 1450 | }, 1451 | "source": [ 1452 | "my_dictionary = {}\n", 1453 | "for i in range(10):\n", 1454 | " my_dictionary[i] = i*i*i\n", 1455 | "print(\"Keys\")\n", 1456 | "for i in my_dictionary.keys():\n", 1457 | " print(i)\n", 1458 | "print(\"values\")\n", 1459 | "for i in my_dictionary.values():\n", 1460 | " print(i)\n", 1461 | "print(\"key value together\")\n", 1462 | "for k, v in my_dictionary.items():\n", 1463 | " print(k, v)" 1464 | ], 1465 | "execution_count": null, 1466 | "outputs": [ 1467 | { 1468 | "output_type": "stream", 1469 | "text": [ 1470 | "Keys\n", 1471 | "0\n", 1472 | "1\n", 1473 | "2\n", 1474 | "3\n", 1475 | "4\n", 1476 | "5\n", 1477 | "6\n", 1478 | "7\n", 1479 | "8\n", 1480 | "9\n", 1481 | "values\n", 1482 | "0\n", 1483 | "1\n", 1484 | "8\n", 1485 | "27\n", 1486 | "64\n", 1487 | "125\n", 1488 | "216\n", 1489 | "343\n", 1490 | "512\n", 1491 | "729\n", 1492 | "key value together\n", 1493 | "0 0\n", 1494 | "1 1\n", 1495 | "2 8\n", 1496 | "3 27\n", 1497 | "4 64\n", 1498 | "5 125\n", 1499 | "6 216\n", 1500 | "7 343\n", 1501 | "8 512\n", 1502 | "9 729\n" 1503 | ], 1504 | "name": "stdout" 1505 | } 1506 | ] 1507 | }, 1508 | { 1509 | "cell_type": "code", 1510 | "metadata": { 1511 | "id": "Ax9nS7erblKE", 1512 | "outputId": "d927380c-c14c-4f60-f416-22a28b6a627a", 1513 | "colab": { 1514 | "base_uri": "https://localhost:8080/", 1515 | "height": 100 1516 | } 1517 | }, 1518 | "source": [ 1519 | "a = [1, 2, 3, 4, 5]\n", 1520 | "b = [100, 99, 98, 96, 96, 95]\n", 1521 | "for i, j in zip(a,b):\n", 1522 | " print(i, j)" 1523 | ], 1524 | "execution_count": null, 1525 | "outputs": [ 1526 | { 1527 | "output_type": "stream", 1528 | "text": [ 1529 | "1 100\n", 1530 | "2 99\n", 1531 | "3 98\n", 1532 | "4 96\n", 1533 | "5 96\n" 1534 | ], 1535 | "name": "stdout" 1536 | } 1537 | ] 1538 | }, 1539 | { 1540 | "cell_type": "markdown", 1541 | "metadata": { 1542 | "id": "T9WAvYYxu91G" 1543 | }, 1544 | "source": [ 1545 | "# 7. Random number\n", 1546 | "\n", 1547 | "Deep leaning algorithms are widely stocastic and [Embarrassingly parallel](https://en.wikipedia.org/wiki/Embarrassingly_parallel). Use of random number/distribution is a big part of it. \n", 1548 | "Let's see some feature of python's random number.\n", 1549 | "Also random number is very important for code reproduciblity.\n", 1550 | "Reference: https://docs.python.org/3/library/random.html\n", 1551 | "\n", 1552 | "An experiment is robust in deep learning when,\n", 1553 | "\n", 1554 | "[standard deviation](https://en.wikipedia.org/wiki/Standard_deviation) is small for experiments with,\n", 1555 | "1. Different seed\n", 1556 | "2. Same seed" 1557 | ] 1558 | }, 1559 | { 1560 | "cell_type": "code", 1561 | "metadata": { 1562 | "id": "JnNzgfTovBFl", 1563 | "outputId": "af790acd-6ada-4c2e-9f12-8d557f444a26", 1564 | "colab": { 1565 | "base_uri": "https://localhost:8080/", 1566 | "height": 107 1567 | } 1568 | }, 1569 | "source": [ 1570 | "import random\n", 1571 | "random.seed(1234) # ensures code reproduciblity.\n", 1572 | "a = random.random() # Return the next random floating point number in the range [0.0, 1.0).\n", 1573 | "print(a)\n", 1574 | "a = random.uniform(0, 10)\n", 1575 | "print(a) # Return a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a.\n", 1576 | "a = random.gauss(.1, 1)\n", 1577 | "print(a)\n", 1578 | "a = random.randint(100, 200)\n", 1579 | "print(a)\n", 1580 | "a = [1,2,3,4,5,6]\n", 1581 | "random.shuffle(a)\n", 1582 | "print(a)" 1583 | ], 1584 | "execution_count": null, 1585 | "outputs": [ 1586 | { 1587 | "output_type": "stream", 1588 | "text": [ 1589 | "0.9664535356921388\n", 1590 | "4.407325991753527\n", 1591 | "2.2970405483761804\n", 1592 | "174\n", 1593 | "[3, 4, 2, 5, 6, 1]\n" 1594 | ], 1595 | "name": "stdout" 1596 | } 1597 | ] 1598 | }, 1599 | { 1600 | "cell_type": "markdown", 1601 | "metadata": { 1602 | "id": "vffAfz8gvMPN" 1603 | }, 1604 | "source": [ 1605 | "# 8. Generator Function\n", 1606 | "\n", 1607 | "Usually we use generator when we need to explore data one by one. In this case rather than processing whole data together, we can process one by one.\n", 1608 | "\n", 1609 | "Generator let us implement this feature. It doesn't process the whole code segment together. It process per `yield` basis.\n", 1610 | "\n" 1611 | ] 1612 | }, 1613 | { 1614 | "cell_type": "code", 1615 | "metadata": { 1616 | "id": "RX8nurpTvzod", 1617 | "outputId": "53a3d45f-f93c-4c10-828e-ca2617bb61e6", 1618 | "colab": { 1619 | "base_uri": "https://localhost:8080/", 1620 | "height": 53 1621 | } 1622 | }, 1623 | "source": [ 1624 | "data = [100, 122, 15, 19, 100, 500, 900, 100, 100, 122, 15, 66, 100, 500, 88, 100, 100, 122, 20, 19, 100, 500, 77, 100, 10]\n", 1625 | "print(data)\n", 1626 | "print(len(data))" 1627 | ], 1628 | "execution_count": null, 1629 | "outputs": [ 1630 | { 1631 | "output_type": "stream", 1632 | "text": [ 1633 | "[100, 122, 15, 19, 100, 500, 900, 100, 100, 122, 15, 66, 100, 500, 88, 100, 100, 122, 20, 19, 100, 500, 77, 100, 10]\n", 1634 | "25\n" 1635 | ], 1636 | "name": "stdout" 1637 | } 1638 | ] 1639 | }, 1640 | { 1641 | "cell_type": "code", 1642 | "metadata": { 1643 | "id": "TSX3Rw_W1T9B" 1644 | }, 1645 | "source": [ 1646 | "# need to get 3 random positioned numbers at a time\n", 1647 | "def data_iterator(data, is_stop = True, is_shuffle = True, batch_size=3):\n", 1648 | " n = len(data)\n", 1649 | " idx = 0\n", 1650 | " index_list = []\n", 1651 | " while idx < n:\n", 1652 | " index_list.append(idx)\n", 1653 | " idx += 1\n", 1654 | " if is_shuffle:\n", 1655 | " random.shuffle(index_list)\n", 1656 | " ret = []\n", 1657 | " i = 0\n", 1658 | " while True:\n", 1659 | " # print(i)\n", 1660 | " if i == n:\n", 1661 | " yield ret\n", 1662 | " if is_stop:\n", 1663 | " break\n", 1664 | " i = 0\n", 1665 | " ret = []\n", 1666 | " random.shuffle(index_list)\n", 1667 | " ret.append( data[ index_list[i] ] )\n", 1668 | " if len(ret) == batch_size:\n", 1669 | " yield ret\n", 1670 | " ret = []\n", 1671 | " i = i + 1\n" 1672 | ], 1673 | "execution_count": null, 1674 | "outputs": [] 1675 | }, 1676 | { 1677 | "cell_type": "code", 1678 | "metadata": { 1679 | "id": "QTi4C59WxT6h", 1680 | "outputId": "88de9942-afc6-4f82-8ec8-290ba91462d0", 1681 | "colab": { 1682 | "base_uri": "https://localhost:8080/", 1683 | "height": 89 1684 | } 1685 | }, 1686 | "source": [ 1687 | "a = data_iterator(data, is_stop = True, is_shuffle = True, batch_size=3)\n", 1688 | "print(a.__next__())\n", 1689 | "print(a.__next__())\n", 1690 | "print(a.__next__())\n", 1691 | "print(a.__next__())" 1692 | ], 1693 | "execution_count": null, 1694 | "outputs": [ 1695 | { 1696 | "output_type": "stream", 1697 | "text": [ 1698 | "[122, 900, 122]\n", 1699 | "[500, 15, 100]\n", 1700 | "[500, 100, 19]\n", 1701 | "[100, 100, 77]\n" 1702 | ], 1703 | "name": "stdout" 1704 | } 1705 | ] 1706 | }, 1707 | { 1708 | "cell_type": "markdown", 1709 | "metadata": { 1710 | "id": "SxjDZs1lI_cD" 1711 | }, 1712 | "source": [ 1713 | "# 9. Scope, Class, Inheritance\n", 1714 | "\n", 1715 | "Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state.\n", 1716 | "\n" 1717 | ] 1718 | }, 1719 | { 1720 | "cell_type": "code", 1721 | "metadata": { 1722 | "id": "g0r49AB1G48J", 1723 | "outputId": "3d0d47a7-57e7-4d80-deac-094541682673", 1724 | "colab": { 1725 | "base_uri": "https://localhost:8080/", 1726 | "height": 83 1727 | } 1728 | }, 1729 | "source": [ 1730 | "def scope_test():\n", 1731 | " def do_local():\n", 1732 | " spam = \"local spam\"\n", 1733 | "\n", 1734 | " def do_nonlocal():\n", 1735 | " nonlocal spam\n", 1736 | " spam = \"nonlocal spam\"\n", 1737 | "\n", 1738 | " def do_global():\n", 1739 | " global spam\n", 1740 | " spam = \"global spam\"\n", 1741 | "\n", 1742 | " spam = \"test spam\"\n", 1743 | " do_local()\n", 1744 | " print(\"After local assignment:\", spam)\n", 1745 | " do_nonlocal()\n", 1746 | " print(\"After nonlocal assignment:\", spam)\n", 1747 | " do_global()\n", 1748 | " print(\"After global assignment:\", spam)\n", 1749 | "\n", 1750 | "scope_test()\n", 1751 | "print(\"In global scope:\", spam)" 1752 | ], 1753 | "execution_count": null, 1754 | "outputs": [ 1755 | { 1756 | "output_type": "stream", 1757 | "text": [ 1758 | "After local assignment: test spam\n", 1759 | "After nonlocal assignment: nonlocal spam\n", 1760 | "After global assignment: nonlocal spam\n", 1761 | "In global scope: global spam\n" 1762 | ], 1763 | "name": "stdout" 1764 | } 1765 | ] 1766 | }, 1767 | { 1768 | "cell_type": "markdown", 1769 | "metadata": { 1770 | "id": "SqhRmdlWUJ9F" 1771 | }, 1772 | "source": [ 1773 | "See the difference between global and nonlocal variables. [link](https://www.python-course.eu/python3_global_vs_local_variables.php)" 1774 | ] 1775 | }, 1776 | { 1777 | "cell_type": "markdown", 1778 | "metadata": { 1779 | "id": "38Y5wqUtKo9M" 1780 | }, 1781 | "source": [ 1782 | "## How to design beautiful Classes\n", 1783 | "0. Creativity\n", 1784 | "1. Try to generalize the code so that it becomes reusable. [example](https://github.com/huggingface/transformers/blob/dfe012ad9d6b6f0c9d30bc508b9f1e4c42280c07/src/transformers/modeling_roberta.py#L173)\n", 1785 | "2. Read codes from Popular opensource libraries.\n" 1786 | ] 1787 | }, 1788 | { 1789 | "cell_type": "markdown", 1790 | "metadata": { 1791 | "id": "y43ZgrSRG-TM" 1792 | }, 1793 | "source": [ 1794 | "## Class definition\n", 1795 | "\n", 1796 | "\n", 1797 | "```\n", 1798 | "class ClassName:\n", 1799 | " \n", 1800 | " .\n", 1801 | " .\n", 1802 | " .\n", 1803 | " \n", 1804 | "```\n", 1805 | "\n" 1806 | ] 1807 | }, 1808 | { 1809 | "cell_type": "code", 1810 | "metadata": { 1811 | "id": "WxmQyDes3ILX" 1812 | }, 1813 | "source": [ 1814 | "class MyClass:\n", 1815 | " \"\"\"A simple example class\"\"\"\n", 1816 | " i = 12345\n", 1817 | "\n", 1818 | " def f(self):\n", 1819 | " return 'hello world'" 1820 | ], 1821 | "execution_count": null, 1822 | "outputs": [] 1823 | }, 1824 | { 1825 | "cell_type": "code", 1826 | "metadata": { 1827 | "id": "ANp2iTymsvyu", 1828 | "outputId": "3a83fc2e-f865-4a31-8f3f-2d64f5e8910a", 1829 | "colab": { 1830 | "base_uri": "https://localhost:8080/", 1831 | "height": 33 1832 | } 1833 | }, 1834 | "source": [ 1835 | "x = MyClass()\n", 1836 | "x.f()" 1837 | ], 1838 | "execution_count": null, 1839 | "outputs": [ 1840 | { 1841 | "output_type": "execute_result", 1842 | "data": { 1843 | "text/plain": [ 1844 | "'hello world'" 1845 | ] 1846 | }, 1847 | "metadata": { 1848 | "tags": [] 1849 | }, 1850 | "execution_count": 19 1851 | } 1852 | ] 1853 | }, 1854 | { 1855 | "cell_type": "code", 1856 | "metadata": { 1857 | "id": "3tovWLwkr73Q" 1858 | }, 1859 | "source": [ 1860 | "class Complex:\n", 1861 | " def __init__(self, realpart, imagpart):\n", 1862 | " self.r = realpart\n", 1863 | " self.i = imagpart" 1864 | ], 1865 | "execution_count": null, 1866 | "outputs": [] 1867 | }, 1868 | { 1869 | "cell_type": "code", 1870 | "metadata": { 1871 | "id": "osv6mqqJIfk5", 1872 | "outputId": "e14d5130-47c3-425b-82f2-a760a7afdfe0", 1873 | "colab": { 1874 | "base_uri": "https://localhost:8080/", 1875 | "height": 33 1876 | } 1877 | }, 1878 | "source": [ 1879 | "x = Complex(3.0, -4.5)\n", 1880 | "print(x.r, x.i)" 1881 | ], 1882 | "execution_count": null, 1883 | "outputs": [ 1884 | { 1885 | "output_type": "stream", 1886 | "text": [ 1887 | "3.0 -4.5\n" 1888 | ], 1889 | "name": "stdout" 1890 | } 1891 | ] 1892 | }, 1893 | { 1894 | "cell_type": "markdown", 1895 | "metadata": { 1896 | "id": "7eLHqf4vIUUR" 1897 | }, 1898 | "source": [ 1899 | "self represents the instance of the class. By using the “self” keyword we can access the attributes and methods of the class in python.\n", 1900 | "\n", 1901 | "**Self is a convention and not a real python keyword**" 1902 | ] 1903 | }, 1904 | { 1905 | "cell_type": "code", 1906 | "metadata": { 1907 | "id": "hqKyn29VH4zb", 1908 | "outputId": "4dc6c14e-3c1e-4f68-c546-6d94e3b175d7", 1909 | "colab": { 1910 | "base_uri": "https://localhost:8080/", 1911 | "height": 33 1912 | } 1913 | }, 1914 | "source": [ 1915 | "class Complex:\n", 1916 | " def __init__(new_self, realpart, imagpart):\n", 1917 | " new_self.r = realpart\n", 1918 | " new_self.i = imagpart\n", 1919 | "\n", 1920 | "y = Complex(3.0, -4.5)\n", 1921 | "print(y.r, y.i)" 1922 | ], 1923 | "execution_count": null, 1924 | "outputs": [ 1925 | { 1926 | "output_type": "stream", 1927 | "text": [ 1928 | "3.0 -4.5\n" 1929 | ], 1930 | "name": "stdout" 1931 | } 1932 | ] 1933 | }, 1934 | { 1935 | "cell_type": "markdown", 1936 | "metadata": { 1937 | "id": "sqGrjcTtJZ48" 1938 | }, 1939 | "source": [ 1940 | "Note that here `__init__()` is the class constructor. The class is constructed by calling this method." 1941 | ] 1942 | }, 1943 | { 1944 | "cell_type": "code", 1945 | "metadata": { 1946 | "id": "CI19p2L7I06F" 1947 | }, 1948 | "source": [ 1949 | "class Bag:\n", 1950 | " def __init__(self):\n", 1951 | " self.data = []\n", 1952 | "\n", 1953 | " def add(self, x):\n", 1954 | " self.data.append(x)\n", 1955 | "\n", 1956 | " def addtwice(self, x):\n", 1957 | " self.add(x)\n", 1958 | " self.add(x)" 1959 | ], 1960 | "execution_count": null, 1961 | "outputs": [] 1962 | }, 1963 | { 1964 | "cell_type": "code", 1965 | "metadata": { 1966 | "id": "6GOgcpxiKgV5", 1967 | "outputId": "1a141802-e9ac-43da-e861-bc70eff5b407", 1968 | "colab": { 1969 | "base_uri": "https://localhost:8080/", 1970 | "height": 33 1971 | } 1972 | }, 1973 | "source": [ 1974 | "a = Bag()\n", 1975 | "a.add(5)\n", 1976 | "a.addtwice(10)\n", 1977 | "print(a.data)" 1978 | ], 1979 | "execution_count": null, 1980 | "outputs": [ 1981 | { 1982 | "output_type": "stream", 1983 | "text": [ 1984 | "[5, 10, 10]\n" 1985 | ], 1986 | "name": "stdout" 1987 | } 1988 | ] 1989 | }, 1990 | { 1991 | "cell_type": "code", 1992 | "metadata": { 1993 | "id": "-JEl5FfgKq5n", 1994 | "outputId": "fa285754-1bc3-4146-928c-83d05079fce0", 1995 | "colab": { 1996 | "base_uri": "https://localhost:8080/", 1997 | "height": 50 1998 | } 1999 | }, 2000 | "source": [ 2001 | "class Person(object): \n", 2002 | " \n", 2003 | " # Constructor \n", 2004 | " def __init__(self, name): \n", 2005 | " self.name = name \n", 2006 | " \n", 2007 | " # To get name \n", 2008 | " def getName(self): \n", 2009 | " return self.name \n", 2010 | " \n", 2011 | " # To check if this person is employee \n", 2012 | " def isEmployee(self): \n", 2013 | " return False\n", 2014 | " \n", 2015 | " \n", 2016 | "# Inherited or Sub class (Note Person in bracket) \n", 2017 | "class Employee(Person): \n", 2018 | " \n", 2019 | " # Here we return true \n", 2020 | " def isEmployee(self): \n", 2021 | " return True\n", 2022 | " \n", 2023 | "# Driver code \n", 2024 | "emp = Person(\"Geek1\") # An Object of Person \n", 2025 | "print(emp.getName(), emp.isEmployee()) \n", 2026 | " \n", 2027 | "emp = Employee(\"Geek2\") # An Object of Employee \n", 2028 | "print(emp.getName(), emp.isEmployee()) " 2029 | ], 2030 | "execution_count": null, 2031 | "outputs": [ 2032 | { 2033 | "output_type": "stream", 2034 | "text": [ 2035 | "Geek1 False\n", 2036 | "Geek2 True\n" 2037 | ], 2038 | "name": "stdout" 2039 | } 2040 | ] 2041 | }, 2042 | { 2043 | "cell_type": "code", 2044 | "metadata": { 2045 | "id": "RR0qVBzEO0Pk", 2046 | "outputId": "984dfbb3-15bc-426d-9e96-cf6fbf98819f", 2047 | "colab": { 2048 | "base_uri": "https://localhost:8080/", 2049 | "height": 83 2050 | } 2051 | }, 2052 | "source": [ 2053 | "class Base1(object): \n", 2054 | " def __init__(self): \n", 2055 | " self.str1 = \"Geek1\"\n", 2056 | " print(\"Base1\")\n", 2057 | " \n", 2058 | "class Base2(object): \n", 2059 | " def __init__(self): \n", 2060 | " self.str2 = \"Geek2\" \n", 2061 | " print(\"Base2\")\n", 2062 | " \n", 2063 | "class Derived(Base1, Base2): \n", 2064 | " def __init__(self): \n", 2065 | " \n", 2066 | " # Calling constructors of Base1 \n", 2067 | " # and Base2 classes \n", 2068 | " Base1.__init__(self) \n", 2069 | " Base2.__init__(self) \n", 2070 | " print(\"Derived\")\n", 2071 | " \n", 2072 | " def printStrs(self): \n", 2073 | " print(self.str1, self.str2) \n", 2074 | " \n", 2075 | " \n", 2076 | "ob = Derived() \n", 2077 | "ob.printStrs() " 2078 | ], 2079 | "execution_count": null, 2080 | "outputs": [ 2081 | { 2082 | "output_type": "stream", 2083 | "text": [ 2084 | "Base1\n", 2085 | "Base2\n", 2086 | "Derived\n", 2087 | "Geek1 Geek2\n" 2088 | ], 2089 | "name": "stdout" 2090 | } 2091 | ] 2092 | }, 2093 | { 2094 | "cell_type": "code", 2095 | "metadata": { 2096 | "id": "Dks5b-Z4Pup1", 2097 | "outputId": "d618a323-1353-4428-c085-11a3f137e79c", 2098 | "colab": { 2099 | "base_uri": "https://localhost:8080/", 2100 | "height": 33 2101 | } 2102 | }, 2103 | "source": [ 2104 | "class Base(object): \n", 2105 | " \n", 2106 | " # Constructor \n", 2107 | " def __init__(self, name): \n", 2108 | " self.name = name \n", 2109 | " \n", 2110 | " # To get name \n", 2111 | " def getName(self): \n", 2112 | " return self.name \n", 2113 | " \n", 2114 | " \n", 2115 | "# Inherited or Sub class (Note Person in bracket) \n", 2116 | "class Child(Base): \n", 2117 | " \n", 2118 | " # Constructor \n", 2119 | " def __init__(self, name, age): \n", 2120 | " Base.__init__(self, name) \n", 2121 | " self.age = age \n", 2122 | " \n", 2123 | " # To get name \n", 2124 | " def getAge(self): \n", 2125 | " return self.age \n", 2126 | " \n", 2127 | "# Inherited or Sub class (Note Person in bracket) \n", 2128 | "class GrandChild(Child): \n", 2129 | " \n", 2130 | " # Constructor \n", 2131 | " def __init__(self, name, age, address): \n", 2132 | " Child.__init__(self, name, age) \n", 2133 | " self.address = address \n", 2134 | " \n", 2135 | " # To get address \n", 2136 | " def getAddress(self): \n", 2137 | " return self.address \n", 2138 | " \n", 2139 | "# Driver code \n", 2140 | "g = GrandChild(\"Geek1\", 23, \"Noida\") \n", 2141 | "print(g.getName(), g.getAge(), g.getAddress()) " 2142 | ], 2143 | "execution_count": null, 2144 | "outputs": [ 2145 | { 2146 | "output_type": "stream", 2147 | "text": [ 2148 | "Geek1 23 Noida\n" 2149 | ], 2150 | "name": "stdout" 2151 | } 2152 | ] 2153 | }, 2154 | { 2155 | "cell_type": "code", 2156 | "metadata": { 2157 | "id": "Ebd1xfflQmtd" 2158 | }, 2159 | "source": [ 2160 | "# first parent class \n", 2161 | "class Person(object): \n", 2162 | " def __init__(self, name, idnumber): \n", 2163 | " self.name = name \n", 2164 | " self.idnumber = idnumber \n", 2165 | " \n", 2166 | "# second parent class \n", 2167 | "class Employee(object): \n", 2168 | " def __init__(self, salary, post): \n", 2169 | " self.salary = salary \n", 2170 | " self.post = post \n", 2171 | " \n", 2172 | "# inheritance from both the parent classes \n", 2173 | "class Leader(Person, Employee): \n", 2174 | " def __init__(self, name, idnumber, salary, post, points): \n", 2175 | " self.points = points \n", 2176 | " Person.__init__(self, name, idnumber) \n", 2177 | " Employee.__init__(self, salary, post) \n" 2178 | ], 2179 | "execution_count": null, 2180 | "outputs": [] 2181 | }, 2182 | { 2183 | "cell_type": "code", 2184 | "metadata": { 2185 | "id": "0HcYM0o9RS5h", 2186 | "outputId": "c17f8a4d-d431-4753-9749-73eef47d184c", 2187 | "colab": { 2188 | "base_uri": "https://localhost:8080/", 2189 | "height": 50 2190 | } 2191 | }, 2192 | "source": [ 2193 | "# Base Class \n", 2194 | "class A(object): \n", 2195 | " def __init__(self): \n", 2196 | " constant1 = 1\n", 2197 | " def method1(self): \n", 2198 | " print('method1 of class A') \n", 2199 | " \n", 2200 | "class B(A): \n", 2201 | " def __init__(self): \n", 2202 | " constant2 = 2\n", 2203 | " self.calling1() \n", 2204 | " A.__init__(self) \n", 2205 | " def method1(self): \n", 2206 | " print('method1 of class B') \n", 2207 | " def calling1(self): \n", 2208 | " self.method1() \n", 2209 | " A.method1(self) \n", 2210 | "b = B() " 2211 | ], 2212 | "execution_count": null, 2213 | "outputs": [ 2214 | { 2215 | "output_type": "stream", 2216 | "text": [ 2217 | "method1 of class B\n", 2218 | "method1 of class A\n" 2219 | ], 2220 | "name": "stdout" 2221 | } 2222 | ] 2223 | }, 2224 | { 2225 | "cell_type": "code", 2226 | "metadata": { 2227 | "id": "tDCDhwb0RWQ0", 2228 | "outputId": "93038174-f0ba-4323-d5f2-b6a559ae614e", 2229 | "colab": { 2230 | "base_uri": "https://localhost:8080/", 2231 | "height": 66 2232 | } 2233 | }, 2234 | "source": [ 2235 | "class A(object): \n", 2236 | " def function1(self): \n", 2237 | " print('function of class A')\n", 2238 | "class B(A): \n", 2239 | " def function1(self): \n", 2240 | " print('function of class B')\n", 2241 | " super(B, self).function1() \n", 2242 | "class C(B): \n", 2243 | " def function1(self): \n", 2244 | " print('function of class C')\n", 2245 | " super(C, self).function1() \n", 2246 | "j = C() \n", 2247 | "j.function1() " 2248 | ], 2249 | "execution_count": null, 2250 | "outputs": [ 2251 | { 2252 | "output_type": "stream", 2253 | "text": [ 2254 | "function of class C\n", 2255 | "function of class B\n", 2256 | "function of class A\n" 2257 | ], 2258 | "name": "stdout" 2259 | } 2260 | ] 2261 | }, 2262 | { 2263 | "cell_type": "code", 2264 | "metadata": { 2265 | "id": "RvqXwLlDRk5y" 2266 | }, 2267 | "source": [ 2268 | "import torch.nn as nn\n", 2269 | "import torch.nn.functional as F\n", 2270 | "\n", 2271 | "class Model(nn.Module):\n", 2272 | " def __init__(self):\n", 2273 | " super(Model, self).__init__()\n", 2274 | " self.conv1 = nn.Conv2d(1, 20, 5)\n", 2275 | " self.conv2 = nn.Conv2d(20, 20, 5)\n", 2276 | "\n", 2277 | " def forward(self, x):\n", 2278 | " x = F.relu(self.conv1(x))\n", 2279 | " return F.relu(self.conv2(x))" 2280 | ], 2281 | "execution_count": null, 2282 | "outputs": [] 2283 | }, 2284 | { 2285 | "cell_type": "code", 2286 | "metadata": { 2287 | "id": "TSNtTXXwU1tV", 2288 | "outputId": "87de197c-41bb-4511-eabd-d429d4046413", 2289 | "colab": { 2290 | "base_uri": "https://localhost:8080/", 2291 | "height": 83 2292 | } 2293 | }, 2294 | "source": [ 2295 | "model = Model()\n", 2296 | "print(model)" 2297 | ], 2298 | "execution_count": null, 2299 | "outputs": [ 2300 | { 2301 | "output_type": "stream", 2302 | "text": [ 2303 | "Model(\n", 2304 | " (conv1): Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1))\n", 2305 | " (conv2): Conv2d(20, 20, kernel_size=(5, 5), stride=(1, 1))\n", 2306 | ")\n" 2307 | ], 2308 | "name": "stdout" 2309 | } 2310 | ] 2311 | }, 2312 | { 2313 | "cell_type": "code", 2314 | "metadata": { 2315 | "id": "QlYmOgn1no_b", 2316 | "outputId": "e477280e-659b-4313-8b8b-d7466d40549e", 2317 | "colab": { 2318 | "base_uri": "https://localhost:8080/", 2319 | "height": 150 2320 | } 2321 | }, 2322 | "source": [ 2323 | "lst = [0,1,2,3,4,5,6]\n", 2324 | "it = iter(lst)\n", 2325 | "print(it.__next__())\n", 2326 | "print(it.__next__())\n", 2327 | "print(it.__next__())\n", 2328 | "print(it.__next__())\n", 2329 | "\n", 2330 | "it = iter(lst)\n", 2331 | "print(it.__next__())\n", 2332 | "print(it.__next__())\n", 2333 | "print(it.__next__())\n", 2334 | "print(it.__next__())" 2335 | ], 2336 | "execution_count": null, 2337 | "outputs": [ 2338 | { 2339 | "output_type": "stream", 2340 | "text": [ 2341 | "0\n", 2342 | "1\n", 2343 | "2\n", 2344 | "3\n", 2345 | "0\n", 2346 | "1\n", 2347 | "2\n", 2348 | "3\n" 2349 | ], 2350 | "name": "stdout" 2351 | } 2352 | ] 2353 | }, 2354 | { 2355 | "cell_type": "markdown", 2356 | "metadata": { 2357 | "id": "wb0hwcqtXjZR" 2358 | }, 2359 | "source": [ 2360 | "### Magic function\n", 2361 | "\n", 2362 | "**__init__()** , **__next__()**, **__repr__()** are called magic function. They have special builtin purpose." 2363 | ] 2364 | }, 2365 | { 2366 | "cell_type": "code", 2367 | "metadata": { 2368 | "id": "dnjHx-vZps6h", 2369 | "outputId": "d72e289a-c346-49a7-e2f3-5ab11851242c", 2370 | "colab": { 2371 | "base_uri": "https://localhost:8080/", 2372 | "height": 175 2373 | } 2374 | }, 2375 | "source": [ 2376 | "class myRange:\n", 2377 | " def __init__(self, start, end, increment=1):\n", 2378 | " self.start = start\n", 2379 | " self.end = end\n", 2380 | " self.curr_num = start\n", 2381 | " self.increment = increment\n", 2382 | "\n", 2383 | " def __iter__(self):\n", 2384 | " self.curr_num = self.start\n", 2385 | " return self\n", 2386 | "\n", 2387 | " def __next__(self):\n", 2388 | " if(self.curr_num >= self.end):\n", 2389 | " raise StopIteration\n", 2390 | " self.curr_num += self.increment\n", 2391 | " return self.curr_num-self.increment\n", 2392 | " \n", 2393 | " def __repr__(self):\n", 2394 | " return \"hello\"\n", 2395 | "\n", 2396 | "range_object = myRange(0, 4)\n", 2397 | "print(range_object)\n", 2398 | "object_iter = iter(range_object)\n", 2399 | "print(next(object_iter))\n", 2400 | "print(next(object_iter))\n", 2401 | "print(next(object_iter))\n", 2402 | "print(next(object_iter))\n", 2403 | "\n", 2404 | "object_iter = iter(range_object)\n", 2405 | "print(next(object_iter))\n", 2406 | "print(next(object_iter))\n", 2407 | "print(next(object_iter))\n", 2408 | "print(next(object_iter))\n" 2409 | ], 2410 | "execution_count": null, 2411 | "outputs": [ 2412 | { 2413 | "output_type": "stream", 2414 | "text": [ 2415 | "hello\n", 2416 | "0\n", 2417 | "1\n", 2418 | "2\n", 2419 | "3\n", 2420 | "0\n", 2421 | "1\n", 2422 | "2\n", 2423 | "3\n" 2424 | ], 2425 | "name": "stdout" 2426 | } 2427 | ] 2428 | }, 2429 | { 2430 | "cell_type": "code", 2431 | "metadata": { 2432 | "id": "DFS6sWIeYEZj", 2433 | "outputId": "679498e9-a464-4fc9-87d5-8bb18ec8c34e", 2434 | "colab": { 2435 | "base_uri": "https://localhost:8080/", 2436 | "height": 66 2437 | } 2438 | }, 2439 | "source": [ 2440 | "class Node:\n", 2441 | " \"\"\" A struct to denote the node of a binary tree.\n", 2442 | " It contains a value and pointers to left and right children.\n", 2443 | " \"\"\"\n", 2444 | " def __init__(self, value, left=None, right=None):\n", 2445 | " self.value = value\n", 2446 | " self.left = left\n", 2447 | " self.right = right\n", 2448 | "\n", 2449 | " def __eq__(self, other):\n", 2450 | " return self.value == other.value\n", 2451 | "\n", 2452 | " def __lt__(self, other):\n", 2453 | " return self.value < other.value\n", 2454 | "\n", 2455 | " def __ge__(self, other):\n", 2456 | " return self.value >= other.value\n", 2457 | "\n", 2458 | "\n", 2459 | "left = Node(4)\n", 2460 | "root = Node(5, left)\n", 2461 | "print(left == root) # False\n", 2462 | "print(left < root) # True\n", 2463 | "print(left >= root)0 # False" 2464 | ], 2465 | "execution_count": null, 2466 | "outputs": [ 2467 | { 2468 | "output_type": "stream", 2469 | "text": [ 2470 | "False\n", 2471 | "True\n", 2472 | "False\n" 2473 | ], 2474 | "name": "stdout" 2475 | } 2476 | ] 2477 | }, 2478 | { 2479 | "cell_type": "code", 2480 | "metadata": { 2481 | "id": "DGYRZ-Wd3IN8", 2482 | "outputId": "02bdeee0-f9ce-4e87-896d-1638f9ef2a1d", 2483 | "colab": { 2484 | "base_uri": "https://localhost:8080/", 2485 | "height": 133 2486 | } 2487 | }, 2488 | "source": [ 2489 | "letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']\n", 2490 | "for letter in letters:\n", 2491 | " print(letter)" 2492 | ], 2493 | "execution_count": null, 2494 | "outputs": [ 2495 | { 2496 | "output_type": "stream", 2497 | "text": [ 2498 | "a\n", 2499 | "b\n", 2500 | "c\n", 2501 | "d\n", 2502 | "e\n", 2503 | "f\n", 2504 | "g\n" 2505 | ], 2506 | "name": "stdout" 2507 | } 2508 | ] 2509 | }, 2510 | { 2511 | "cell_type": "markdown", 2512 | "metadata": { 2513 | "id": "PnGMs3cPixxS" 2514 | }, 2515 | "source": [ 2516 | "# 10. Asserts and exceptions\n", 2517 | "\n", 2518 | "Great for integrating fact check when you are running a big simulation.\n", 2519 | "\n", 2520 | "- get back to `ask_ok()`" 2521 | ] 2522 | }, 2523 | { 2524 | "cell_type": "code", 2525 | "metadata": { 2526 | "id": "k2QhlD_Zzl1s" 2527 | }, 2528 | "source": [ 2529 | "assert fibonacci(3) == [0, 1, 1, 2]\n", 2530 | "assert isLeapYear(2000) == True\n", 2531 | "assert grader(50) == 'Fail'" 2532 | ], 2533 | "execution_count": null, 2534 | "outputs": [] 2535 | }, 2536 | { 2537 | "cell_type": "code", 2538 | "metadata": { 2539 | "id": "A2uPzpG20kjL" 2540 | }, 2541 | "source": [ 2542 | "assert fibonacci(5) == [0, 0, 1, 2, 3] # will raise an error" 2543 | ], 2544 | "execution_count": null, 2545 | "outputs": [] 2546 | }, 2547 | { 2548 | "cell_type": "code", 2549 | "metadata": { 2550 | "id": "z0GruH6r1F6L" 2551 | }, 2552 | "source": [ 2553 | "it = data_iterator(data, is_stop = True, is_shuffle = True, batch_size=3)\n", 2554 | "for i in it:\n", 2555 | " assert len(i)==3" 2556 | ], 2557 | "execution_count": null, 2558 | "outputs": [] 2559 | }, 2560 | { 2561 | "cell_type": "code", 2562 | "metadata": { 2563 | "id": "lFAR3ZM71fZN" 2564 | }, 2565 | "source": [ 2566 | "it = data_iterator(data, is_stop = True, is_shuffle = True, batch_size=3)\n", 2567 | "for i in it:\n", 2568 | " try:\n", 2569 | " assert len(i)==3\n", 2570 | " except:\n", 2571 | " print(\"Warning, there is a problem with length of the generator's output.\")\n", 2572 | " raise" 2573 | ], 2574 | "execution_count": null, 2575 | "outputs": [] 2576 | }, 2577 | { 2578 | "cell_type": "markdown", 2579 | "metadata": { 2580 | "id": "mJlQRQmvgJck" 2581 | }, 2582 | "source": [ 2583 | "# 11. Input Output (I/O)\n", 2584 | "\n", 2585 | "- Take input from keyboard\n", 2586 | "- Read and write from a text file" 2587 | ] 2588 | }, 2589 | { 2590 | "cell_type": "code", 2591 | "metadata": { 2592 | "id": "1eNvo5_L50GC" 2593 | }, 2594 | "source": [ 2595 | "a = input(\"Write something : \")\n", 2596 | "print(a, type(a))\n", 2597 | "b = int(input(\"Write an integer : \"))\n", 2598 | "print(b, type(b))\n", 2599 | "c = float(input(\"Write something : \"))\n", 2600 | "print(c, type(c))" 2601 | ], 2602 | "execution_count": null, 2603 | "outputs": [] 2604 | }, 2605 | { 2606 | "cell_type": "markdown", 2607 | "metadata": { 2608 | "id": "s5MrgCWn630-" 2609 | }, 2610 | "source": [ 2611 | "Let's write some text in a file" 2612 | ] 2613 | }, 2614 | { 2615 | "cell_type": "code", 2616 | "metadata": { 2617 | "id": "LMNdbW_n6lQq" 2618 | }, 2619 | "source": [ 2620 | "filePtr = open(\"text.txt\", \"w\", encoding=\"utf-8\")\n", 2621 | "sentences = [\"Do you know?\", \"why it so popular.\", \"The quick brown fox runs over the lazy dog\"]\n", 2622 | "for sentence in sentences:\n", 2623 | " filePtr.write(sentence)\n", 2624 | "filePtr.close" 2625 | ], 2626 | "execution_count": null, 2627 | "outputs": [] 2628 | }, 2629 | { 2630 | "cell_type": "code", 2631 | "metadata": { 2632 | "id": "89wXMcPB7xf1" 2633 | }, 2634 | "source": [ 2635 | "sentences = [\"Do you know?\", \"why it so popular.\", \"The quick brown fox runs over the lazy dog\"]\n", 2636 | "with open(\"text.txt\", \"w\", encoding=\"utf-8\") as filePtr:\n", 2637 | " for sentence in sentences:\n", 2638 | " filePtr.write(sentence+\"\\n\")" 2639 | ], 2640 | "execution_count": null, 2641 | "outputs": [] 2642 | }, 2643 | { 2644 | "cell_type": "code", 2645 | "metadata": { 2646 | "id": "hPyPwIcL68Bv" 2647 | }, 2648 | "source": [ 2649 | "with open(\"text.txt\", \"r\", encoding=\"utf-8\") as filePtr:\n", 2650 | " for line in filePtr:\n", 2651 | " print(line)" 2652 | ], 2653 | "execution_count": null, 2654 | "outputs": [] 2655 | }, 2656 | { 2657 | "cell_type": "markdown", 2658 | "metadata": { 2659 | "id": "xByFBgyE8C1F" 2660 | }, 2661 | "source": [ 2662 | " For different language text you may have to change the encoding `utf-8` to `latin-1`." 2663 | ] 2664 | }, 2665 | { 2666 | "cell_type": "markdown", 2667 | "metadata": { 2668 | "id": "22wXWh0W7rIE" 2669 | }, 2670 | "source": [ 2671 | "## Python object serialization\n", 2672 | "https://docs.python.org/3/library/pickle.html" 2673 | ] 2674 | }, 2675 | { 2676 | "cell_type": "code", 2677 | "metadata": { 2678 | "id": "NjEL26hq6IN9", 2679 | "outputId": "db01c0d2-ffd3-4774-8a94-ec7a88a6687c", 2680 | "colab": { 2681 | "base_uri": "https://localhost:8080/", 2682 | "height": 70 2683 | } 2684 | }, 2685 | "source": [ 2686 | "import pickle \n", 2687 | "data = [100, 122, 15, 19, 100, 500, 900, 100, 100, 122, 15, 66, 100, 500, 88, 100, 100, 122, 20, 19, 100, 500, 77, 100, 10]\n", 2688 | "pickle.dump( data, open( \"save.pcl\", \"wb\" ) )\n", 2689 | "data1 = pickle.load( open( \"save.pcl\", \"rb\" ) )\n", 2690 | "print(data) \n", 2691 | " \n", 2692 | "class B: \n", 2693 | " def __init__(self): \n", 2694 | " self.x = 0\n", 2695 | " self.y = 0 \n", 2696 | " def add(self, x, y):\n", 2697 | " self.x = self.x + x\n", 2698 | " self.y = self.y + y \n", 2699 | "\n", 2700 | "obj = B()\n", 2701 | "obj.add(5, 5)\n", 2702 | "print(obj.x, obj.y)\n", 2703 | "pickle.dump( obj, open( \"save1.pcl\", \"wb\" ) )\n", 2704 | "obj1 = pickle.load( open( \"save1.pcl\", \"rb\" ) )\n", 2705 | "print(obj1.x, obj1.y)" 2706 | ], 2707 | "execution_count": null, 2708 | "outputs": [ 2709 | { 2710 | "output_type": "stream", 2711 | "text": [ 2712 | "[100, 122, 15, 19, 100, 500, 900, 100, 100, 122, 15, 66, 100, 500, 88, 100, 100, 122, 20, 19, 100, 500, 77, 100, 10]\n", 2713 | "5 5\n", 2714 | "5 5\n" 2715 | ], 2716 | "name": "stdout" 2717 | } 2718 | ] 2719 | }, 2720 | { 2721 | "cell_type": "markdown", 2722 | "metadata": { 2723 | "id": "8FgYrQ1ajKLE" 2724 | }, 2725 | "source": [ 2726 | "# 12. The Pythonic Way\n", 2727 | "\n", 2728 | "Some cool features in python." 2729 | ] 2730 | }, 2731 | { 2732 | "cell_type": "code", 2733 | "metadata": { 2734 | "id": "gNVkCVTi-hde" 2735 | }, 2736 | "source": [ 2737 | "for i in range(10):\n", 2738 | " print(i)" 2739 | ], 2740 | "execution_count": null, 2741 | "outputs": [] 2742 | }, 2743 | { 2744 | "cell_type": "code", 2745 | "metadata": { 2746 | "id": "15PG_22Sk8ou" 2747 | }, 2748 | "source": [ 2749 | "score = 50\n", 2750 | "isFail = True if score > 40 else False " 2751 | ], 2752 | "execution_count": null, 2753 | "outputs": [] 2754 | }, 2755 | { 2756 | "cell_type": "code", 2757 | "metadata": { 2758 | "id": "uLjWC6-U67Vb" 2759 | }, 2760 | "source": [], 2761 | "execution_count": null, 2762 | "outputs": [] 2763 | }, 2764 | { 2765 | "cell_type": "code", 2766 | "metadata": { 2767 | "id": "wdwHYIIzlOFo", 2768 | "outputId": "d0a56e1f-1c41-4521-f89f-4b591a92fcbe", 2769 | "colab": { 2770 | "base_uri": "https://localhost:8080/", 2771 | "height": 35 2772 | } 2773 | }, 2774 | "source": [ 2775 | "lst = [ i*i for i in range(20) ]\n", 2776 | "print(lst)" 2777 | ], 2778 | "execution_count": null, 2779 | "outputs": [ 2780 | { 2781 | "output_type": "stream", 2782 | "text": [ 2783 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361]\n" 2784 | ], 2785 | "name": "stdout" 2786 | } 2787 | ] 2788 | }, 2789 | { 2790 | "cell_type": "code", 2791 | "metadata": { 2792 | "id": "I7GTsfzEmXw2" 2793 | }, 2794 | "source": [ 2795 | "arr = [1, 2, 3, 4, 5, 6]\n", 2796 | "arr = [x * 2 if x % 2 == 0 else x for x in arr]\n", 2797 | "print(arr)" 2798 | ], 2799 | "execution_count": null, 2800 | "outputs": [] 2801 | }, 2802 | { 2803 | "cell_type": "code", 2804 | "metadata": { 2805 | "id": "tuJ-X16flWIu" 2806 | }, 2807 | "source": [ 2808 | "LANGS = [ 'en', 'es', 'de', 'fr', 'ar', 'bg' ]\n", 2809 | "data = {lang: {splt: {} for splt in ['train', 'valid', 'test']} for lang in LANGS}\n", 2810 | "for k,v in data.items():\n", 2811 | " print(k, v)" 2812 | ], 2813 | "execution_count": null, 2814 | "outputs": [] 2815 | }, 2816 | { 2817 | "cell_type": "code", 2818 | "metadata": { 2819 | "id": "KMwa2oiWkpKF", 2820 | "outputId": "7575d012-79fc-4078-a5cd-01aca788c99f", 2821 | "colab": { 2822 | "base_uri": "https://localhost:8080/", 2823 | "height": 385 2824 | } 2825 | }, 2826 | "source": [ 2827 | "import this" 2828 | ], 2829 | "execution_count": null, 2830 | "outputs": [ 2831 | { 2832 | "output_type": "stream", 2833 | "text": [ 2834 | "The Zen of Python, by Tim Peters\n", 2835 | "\n", 2836 | "Beautiful is better than ugly.\n", 2837 | "Explicit is better than implicit.\n", 2838 | "Simple is better than complex.\n", 2839 | "Complex is better than complicated.\n", 2840 | "Flat is better than nested.\n", 2841 | "Sparse is better than dense.\n", 2842 | "Readability counts.\n", 2843 | "Special cases aren't special enough to break the rules.\n", 2844 | "Although practicality beats purity.\n", 2845 | "Errors should never pass silently.\n", 2846 | "Unless explicitly silenced.\n", 2847 | "In the face of ambiguity, refuse the temptation to guess.\n", 2848 | "There should be one-- and preferably only one --obvious way to do it.\n", 2849 | "Although that way may not be obvious at first unless you're Dutch.\n", 2850 | "Now is better than never.\n", 2851 | "Although never is often better than *right* now.\n", 2852 | "If the implementation is hard to explain, it's a bad idea.\n", 2853 | "If the implementation is easy to explain, it may be a good idea.\n", 2854 | "Namespaces are one honking great idea -- let's do more of those!\n" 2855 | ], 2856 | "name": "stdout" 2857 | } 2858 | ] 2859 | }, 2860 | { 2861 | "cell_type": "markdown", 2862 | "metadata": { 2863 | "id": "hp1CTf5CmzZd" 2864 | }, 2865 | "source": [ 2866 | "Once you are confortable with python try to write neat and beautiful code. \n", 2867 | "A useful guide, https://docs.python-guide.org/writing/style/" 2868 | ] 2869 | }, 2870 | { 2871 | "cell_type": "markdown", 2872 | "metadata": { 2873 | "id": "vjSEjVq2jQRG" 2874 | }, 2875 | "source": [ 2876 | "# 13. Brief tour to Some important Library\n", 2877 | "Practice\n", 2878 | "0. https://docs.python.org/3.6/tutorial/stdlib.html\n", 2879 | "1. https://docs.python.org/3.6/tutorial/datastructures.html\n", 2880 | "2. https://stackabuse.com/introduction-to-pythons-collections-module/\n", 2881 | "3. https://www.machinelearningplus.com/python/python-logging-guide/\n", 2882 | "4. https://docs.python.org/2/howto/argparse.html" 2883 | ] 2884 | }, 2885 | { 2886 | "cell_type": "markdown", 2887 | "metadata": { 2888 | "id": "vUXn7fBFjUfb" 2889 | }, 2890 | "source": [ 2891 | "# 14. Package Manager\n", 2892 | "\n", 2893 | "Python has a wide range of library. Sometimes different project use different types of library that may conflict with each other. Meaning we can't use one library while other library is installed. This brings a huge problem.\n", 2894 | "\n", 2895 | "Package manager provides the solution to this problem. Using a package manager we can create different `environment` for different projects and activate those `environment` while we work on each projects. The package remains inside of that environment.\n", 2896 | "\n", 2897 | "As a package manager we will use [Anaconda](https://www.anaconda.com/). You can also use `virtualenv`.\n", 2898 | "\n", 2899 | "Please follow the demo to see how to maintain packages in python by anaconda.\n", 2900 | "\n", 2901 | "To manage anaconda environment follow,\n", 2902 | "https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html" 2903 | ] 2904 | }, 2905 | { 2906 | "cell_type": "markdown", 2907 | "metadata": { 2908 | "id": "Xu0WIRty3jYp" 2909 | }, 2910 | "source": [ 2911 | "# 15. The complete Reference\n", 2912 | "\n", 2913 | "[Python Language reference](https://docs.python.org/3.6/reference/index.html#reference-index)" 2914 | ] 2915 | }, 2916 | { 2917 | "cell_type": "markdown", 2918 | "metadata": { 2919 | "id": "6oWRIWNPWkZa" 2920 | }, 2921 | "source": [ 2922 | "For any question please mail `bari0001@e.ntu.edu.sg`" 2923 | ] 2924 | }, 2925 | { 2926 | "cell_type": "markdown", 2927 | "metadata": { 2928 | "id": "XwPkWz_BQLCy" 2929 | }, 2930 | "source": [ 2931 | "# Credits\n", 2932 | "0. https://docs.python.org/3.6/tutorial/\n", 2933 | "1. www.geeksforgeeks.org" 2934 | ] 2935 | }, 2936 | { 2937 | "cell_type": "code", 2938 | "metadata": { 2939 | "id": "uq6Mf5UKT-vx" 2940 | }, 2941 | "source": [], 2942 | "execution_count": null, 2943 | "outputs": [] 2944 | } 2945 | ] 2946 | } --------------------------------------------------------------------------------