{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Lesson video & note\nhttps://course.fast.ai/videos/?lesson=8             \nhttps://github.com/fastai/course-v3/blob/master/nbs/dl2/01_matmul.ipynb"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"%load_ext autoreload\n%autoreload 2\n%matplotlib inline\nimport numpy as np\nimport pandas as pd\nimport os\nprint(os.listdir(\"../input\"))\nimport operator\n\n#export\nfrom pathlib import Path\nfrom IPython.core.debugger import set_trace\nfrom fastai import datasets\nimport pickle, gzip, math, torch, matplotlib as mpl\nimport matplotlib.pyplot as plt\nfrom torch import tensor\nimport pandas as pd\n\nMNIST_URL='http://deeplearning.net/data/mnist/mnist.pkl'\npath = datasets.download_data(MNIST_URL, ext='.gz');path","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def test(a,b,cmp,cname=None):\n    if cname is None: cname=cmp.__name__\n    assert cmp(a,b), f\"{cname}:\\n{a}\\n{b}\"\ndef test_eq(a,b): test(a,b,operator.eq, '==')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with gzip.open(path, 'rb') as f:\n    ((x_train, y_train), (x_valid, y_valid), _) = pickle.load(f, encoding='latin-1')\n\nx_train, y_train, x_valid, y_valid = map(tensor, (x_train, y_train, x_valid, y_valid))\nn,c = x_train.shape\nplt.imshow(x_train[0].view(28, -1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"w = torch.randn(784, 10)\nb = torch.zeros(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def mm(a,b):\n    ar,ac = a.shape\n    br,bc = b.shape\n    assert ac==br\n    c = torch.zeros(ar,bc)\n    for i in range(ar):\n        for j in range(bc):\n            for k in range(ac):\n                c[i,j] += a[i,k] * b[k,j]\n    return c\n\nm1 = x_valid[:5]\n%time t1 = mm(m1, w); t1.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def mm(a,b):\n    ar,ac = a.shape\n    br,bc = b.shape\n    assert ac==br\n    c = torch.zeros(ar,bc)\n    for i in range(ar):\n        for j in range(bc):\n            c += (a[i,:] * b[:,j]).sum()\n    return c\n\n%time t1 = mm(m1, w)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def near(a,b): return torch.allclose(a,b,1e-3,1e-5)\ndef test_near(a,b): test(a,b,near)\n\ntest_near(t1, mm(m1,w))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def mm(a,b):\n    ar,ac = a.shape\n    br,bc = b.shape\n    assert ac==br\n    c = torch.zeros(ar,bc)\n    for i in range(ar):\n        c[i] = (a[i].unsqueeze(-1) * b).sum()\n    return c\n\n%time t1 = mm(m1, w)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def mm(a,b): return torch.einsum('ik,kj -> ij', a,b)\n%time t1=mm(m1,w)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%time t1=m1@w","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}