You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Pytorch_Tutorial.ipynb 40 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. {
  2. "nbformat": 4,
  3. "nbformat_minor": 0,
  4. "metadata": {
  5. "colab": {
  6. "name": "Pytorch Tutorial",
  7. "provenance": [],
  8. "collapsed_sections": []
  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": "tHILOGjOQbsQ"
  21. },
  22. "source": [
  23. "# **Pytorch Tutorial**\r\n"
  24. ]
  25. },
  26. {
  27. "cell_type": "code",
  28. "metadata": {
  29. "id": "C1zA7GupxdJv"
  30. },
  31. "source": [
  32. "import torch"
  33. ],
  34. "execution_count": null,
  35. "outputs": []
  36. },
  37. {
  38. "cell_type": "markdown",
  39. "metadata": {
  40. "id": "6Eqj90EkWbWx"
  41. },
  42. "source": [
  43. "**1. Pytorch Documentation Explanation with torch.max**\r\n",
  44. "\r\n"
  45. ]
  46. },
  47. {
  48. "cell_type": "code",
  49. "metadata": {
  50. "id": "JCXOg-iSQuk7"
  51. },
  52. "source": [
  53. "x = torch.randn(4,5)\r\n",
  54. "y = torch.randn(4,5)\r\n",
  55. "z = torch.randn(4,5)\r\n",
  56. "print(x)\r\n",
  57. "print(y)\r\n",
  58. "print(z)"
  59. ],
  60. "execution_count": null,
  61. "outputs": []
  62. },
  63. {
  64. "cell_type": "code",
  65. "metadata": {
  66. "id": "EEqa9GFoWF78"
  67. },
  68. "source": [
  69. "# 1. max of entire tensor (torch.max(input) → Tensor)\r\n",
  70. "m = torch.max(x)\r\n",
  71. "print(m)"
  72. ],
  73. "execution_count": null,
  74. "outputs": []
  75. },
  76. {
  77. "cell_type": "code",
  78. "metadata": {
  79. "id": "wffThGDyWKxJ"
  80. },
  81. "source": [
  82. "# 2. max along a dimension (torch.max(input, dim, keepdim=False, *, out=None) → (Tensor, LongTensor))\r\n",
  83. "m, idx = torch.max(x,0)\r\n",
  84. "print(m)\r\n",
  85. "print(idx)"
  86. ],
  87. "execution_count": null,
  88. "outputs": []
  89. },
  90. {
  91. "cell_type": "code",
  92. "metadata": {
  93. "id": "oKDQW3tIXKg-"
  94. },
  95. "source": [
  96. "# 2-2\r\n",
  97. "m, idx = torch.max(input=x,dim=0)\r\n",
  98. "print(m)\r\n",
  99. "print(idx)"
  100. ],
  101. "execution_count": null,
  102. "outputs": []
  103. },
  104. {
  105. "cell_type": "code",
  106. "metadata": {
  107. "id": "6QZ6WRLyX3De"
  108. },
  109. "source": [
  110. "# 2-3\r\n",
  111. "m, idx = torch.max(x,0,False)\r\n",
  112. "print(m)\r\n",
  113. "print(idx)"
  114. ],
  115. "execution_count": null,
  116. "outputs": []
  117. },
  118. {
  119. "cell_type": "code",
  120. "metadata": {
  121. "id": "nqGuctkKbUEn"
  122. },
  123. "source": [
  124. "# 2-4\r\n",
  125. "m, idx = torch.max(x,dim=0,keepdim=True)\r\n",
  126. "print(m)\r\n",
  127. "print(idx)"
  128. ],
  129. "execution_count": null,
  130. "outputs": []
  131. },
  132. {
  133. "cell_type": "code",
  134. "metadata": {
  135. "id": "9OMzxuMlZPIu"
  136. },
  137. "source": [
  138. "# 2-5\r\n",
  139. "p = (m,idx)\r\n",
  140. "torch.max(x,0,False,out=p)\r\n",
  141. "print(p[0])\r\n",
  142. "print(p[1])\r\n"
  143. ],
  144. "execution_count": null,
  145. "outputs": []
  146. },
  147. {
  148. "cell_type": "code",
  149. "metadata": {
  150. "id": "uhd4TqGTbD2c"
  151. },
  152. "source": [
  153. "# 2-6\r\n",
  154. "p = (m,idx)\r\n",
  155. "torch.max(x,0,False,p)\r\n",
  156. "print(p[0])\r\n",
  157. "print(p[1])"
  158. ],
  159. "execution_count": null,
  160. "outputs": []
  161. },
  162. {
  163. "cell_type": "code",
  164. "metadata": {
  165. "id": "wbxjUSOXxN0n"
  166. },
  167. "source": [
  168. "# 2-7\r\n",
  169. "m, idx = torch.max(x,True)"
  170. ],
  171. "execution_count": null,
  172. "outputs": []
  173. },
  174. {
  175. "cell_type": "code",
  176. "metadata": {
  177. "id": "iMwhGLlGWYaR"
  178. },
  179. "source": [
  180. "# 3. max(choose max) operators on two tensors (torch.max(input, other, *, out=None) → Tensor)\r\n",
  181. "t = torch.max(x,y)\r\n",
  182. "print(t)"
  183. ],
  184. "execution_count": null,
  185. "outputs": []
  186. },
  187. {
  188. "cell_type": "markdown",
  189. "metadata": {
  190. "id": "nFxRKu2Dedwb"
  191. },
  192. "source": [
  193. "**2. Common errors**\r\n",
  194. "\r\n"
  195. ]
  196. },
  197. {
  198. "cell_type": "markdown",
  199. "metadata": {
  200. "id": "KMcRyMxGwhul"
  201. },
  202. "source": [
  203. "The following code blocks show some common errors while using the torch library. First, execute the code with error, and then execute the next code block to fix the error. You need to change the runtime to GPU.\r\n"
  204. ]
  205. },
  206. {
  207. "cell_type": "code",
  208. "metadata": {
  209. "id": "eX-kKdi6ynFf"
  210. },
  211. "source": [
  212. "import torch"
  213. ],
  214. "execution_count": null,
  215. "outputs": []
  216. },
  217. {
  218. "cell_type": "code",
  219. "metadata": {
  220. "id": "-muJ4KKreoP2",
  221. "colab": {
  222. "base_uri": "https://localhost:8080/",
  223. "height": 363
  224. },
  225. "outputId": "c1d5c3a5-9540-4145-d80c-3cbca18a1deb"
  226. },
  227. "source": [
  228. "# 1. different device error\r\n",
  229. "model = torch.nn.Linear(5,1).to(\"cuda:0\")\r\n",
  230. "x = torch.Tensor([1,2,3,4,5]).to(\"cpu\")\r\n",
  231. "y = model(x)"
  232. ],
  233. "execution_count": null,
  234. "outputs": [
  235. {
  236. "output_type": "error",
  237. "ename": "RuntimeError",
  238. "evalue": "ignored",
  239. "traceback": [
  240. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  241. "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
  242. "\u001b[0;32m<ipython-input-2-12e5b7d55705>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mmodel\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mLinear\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mto\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"cuda:0\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTensor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mto\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"cpu\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
  243. "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *input, **kwargs)\u001b[0m\n\u001b[1;32m 725\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_slow_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 726\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 727\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 728\u001b[0m for hook in itertools.chain(\n\u001b[1;32m 729\u001b[0m \u001b[0m_global_forward_hooks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
  244. "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/torch/nn/modules/linear.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input)\u001b[0m\n\u001b[1;32m 91\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 92\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mTensor\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mTensor\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 93\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mF\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlinear\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mweight\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbias\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 94\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 95\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mextra_repr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
  245. "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py\u001b[0m in \u001b[0;36mlinear\u001b[0;34m(input, weight, bias)\u001b[0m\n\u001b[1;32m 1690\u001b[0m \u001b[0mret\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0maddmm\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbias\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mweight\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1691\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1692\u001b[0;31m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmatmul\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mweight\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1693\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mbias\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1694\u001b[0m \u001b[0moutput\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0mbias\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
  246. "\u001b[0;31mRuntimeError\u001b[0m: Tensor for 'out' is on CPU, Tensor for argument #1 'self' is on CPU, but expected them to be on GPU (while checking arguments for addmm)"
  247. ]
  248. }
  249. ]
  250. },
  251. {
  252. "cell_type": "code",
  253. "metadata": {
  254. "id": "a54PqxJLe9-c",
  255. "colab": {
  256. "base_uri": "https://localhost:8080/"
  257. },
  258. "outputId": "909d3693-236f-4419-f269-8fb443ef7534"
  259. },
  260. "source": [
  261. "# 1. different device error (fixed)\r\n",
  262. "x = torch.Tensor([1,2,3,4,5]).to(\"cuda:0\")\r\n",
  263. "y = model(x)\r\n",
  264. "print(y.shape)"
  265. ],
  266. "execution_count": null,
  267. "outputs": [
  268. {
  269. "output_type": "stream",
  270. "text": [
  271. "torch.Size([1])\n"
  272. ],
  273. "name": "stdout"
  274. }
  275. ]
  276. },
  277. {
  278. "cell_type": "code",
  279. "metadata": {
  280. "id": "n7OHtZwbi7Qw",
  281. "colab": {
  282. "base_uri": "https://localhost:8080/",
  283. "height": 201
  284. },
  285. "outputId": "2a7d2dd0-6498-4da0-9591-3554c1739046"
  286. },
  287. "source": [
  288. "# 2. mismatched dimensions error\r\n",
  289. "x = torch.randn(4,5)\r\n",
  290. "y= torch.randn(5,4)\r\n",
  291. "z = x + y"
  292. ],
  293. "execution_count": null,
  294. "outputs": [
  295. {
  296. "output_type": "error",
  297. "ename": "RuntimeError",
  298. "evalue": "ignored",
  299. "traceback": [
  300. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  301. "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
  302. "\u001b[0;32m<ipython-input-4-7fa8b244df3c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mz\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
  303. "\u001b[0;31mRuntimeError\u001b[0m: The size of tensor a (5) must match the size of tensor b (4) at non-singleton dimension 1"
  304. ]
  305. }
  306. ]
  307. },
  308. {
  309. "cell_type": "code",
  310. "metadata": {
  311. "id": "qVynzvrskFCD",
  312. "colab": {
  313. "base_uri": "https://localhost:8080/"
  314. },
  315. "outputId": "926dc01c-be6f-48e1-ad39-a5bcecebc513"
  316. },
  317. "source": [
  318. "# 2. mismatched dimensions error (fixed)\r\n",
  319. "y= y.transpose(0,1)\r\n",
  320. "z = x + y\r\n",
  321. "print(z.shape)"
  322. ],
  323. "execution_count": null,
  324. "outputs": [
  325. {
  326. "output_type": "stream",
  327. "text": [
  328. "torch.Size([4, 5])\n"
  329. ],
  330. "name": "stdout"
  331. }
  332. ]
  333. },
  334. {
  335. "cell_type": "code",
  336. "metadata": {
  337. "id": "Hgzgb9gJANod",
  338. "colab": {
  339. "base_uri": "https://localhost:8080/",
  340. "height": 398
  341. },
  342. "outputId": "21b58850-b3f1-4f2a-db5d-cc45e47ccbea"
  343. },
  344. "source": [
  345. "# 3. cuda out of memory error\n",
  346. "import torch\n",
  347. "import torchvision.models as models\n",
  348. "resnet18 = models.resnet18().to(\"cuda:0\") # Neural Networks for Image Recognition\n",
  349. "data = torch.randn(2048,3,244,244) # Create fake data (512 images)\n",
  350. "out = resnet18(data.to(\"cuda:0\")) # Use Data as Input and Feed to Model\n",
  351. "print(out.shape)\n"
  352. ],
  353. "execution_count": null,
  354. "outputs": [
  355. {
  356. "output_type": "error",
  357. "ename": "RuntimeError",
  358. "evalue": "ignored",
  359. "traceback": [
  360. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  361. "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
  362. "\u001b[0;32m<ipython-input-8-711923c7f347>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mresnet18\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodels\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresnet18\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mto\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"cuda:0\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# Neural Networks for Image Recognition\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mdata\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m2048\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m244\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m244\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# Create fake data (512 images)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mout\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mresnet18\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mto\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"cuda:0\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# Use Data as Input and Feed to Model\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 7\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mout\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
  363. "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *input, **kwargs)\u001b[0m\n\u001b[1;32m 725\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_slow_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 726\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 727\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 728\u001b[0m for hook in itertools.chain(\n\u001b[1;32m 729\u001b[0m \u001b[0m_global_forward_hooks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
  364. "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, x)\u001b[0m\n\u001b[1;32m 218\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 219\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 220\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_forward_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 221\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 222\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
  365. "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/torchvision/models/resnet.py\u001b[0m in \u001b[0;36m_forward_impl\u001b[0;34m(self, x)\u001b[0m\n\u001b[1;32m 202\u001b[0m \u001b[0;31m# See note [TorchScript super()]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 203\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconv1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 204\u001b[0;31m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbn1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 205\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrelu\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 206\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmaxpool\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
  366. "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *input, **kwargs)\u001b[0m\n\u001b[1;32m 725\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_slow_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 726\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 727\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 728\u001b[0m for hook in itertools.chain(\n\u001b[1;32m 729\u001b[0m \u001b[0m_global_forward_hooks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
  367. "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/torch/nn/modules/batchnorm.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input)\u001b[0m\n\u001b[1;32m 134\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrunning_mean\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtraining\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrack_running_stats\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 135\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrunning_var\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtraining\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrack_running_stats\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 136\u001b[0;31m self.weight, self.bias, bn_training, exponential_average_factor, self.eps)\n\u001b[0m\u001b[1;32m 137\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 138\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
  368. "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py\u001b[0m in \u001b[0;36mbatch_norm\u001b[0;34m(input, running_mean, running_var, weight, bias, training, momentum, eps)\u001b[0m\n\u001b[1;32m 2056\u001b[0m return torch.batch_norm(\n\u001b[1;32m 2057\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mweight\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbias\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrunning_mean\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrunning_var\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2058\u001b[0;31m \u001b[0mtraining\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmomentum\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meps\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbackends\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcudnn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0menabled\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2059\u001b[0m )\n\u001b[1;32m 2060\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
  369. "\u001b[0;31mRuntimeError\u001b[0m: CUDA out of memory. Tried to allocate 7.27 GiB (GPU 0; 14.76 GiB total capacity; 8.74 GiB already allocated; 4.42 GiB free; 9.42 GiB reserved in total by PyTorch)"
  370. ]
  371. }
  372. ]
  373. },
  374. {
  375. "cell_type": "code",
  376. "metadata": {
  377. "id": "VPksKnB_w343",
  378. "colab": {
  379. "base_uri": "https://localhost:8080/"
  380. },
  381. "outputId": "fbee46ad-e63e-4bfc-8971-452895dd7a15"
  382. },
  383. "source": [
  384. "# 3. cuda out of memory error (fixed)\n",
  385. "for d in data:\n",
  386. " out = resnet18(d.to(\"cuda:0\").unsqueeze(0))\n",
  387. "print(out.shape)"
  388. ],
  389. "execution_count": null,
  390. "outputs": [
  391. {
  392. "output_type": "stream",
  393. "text": [
  394. "torch.Size([1, 1000])\n"
  395. ],
  396. "name": "stdout"
  397. }
  398. ]
  399. },
  400. {
  401. "cell_type": "code",
  402. "metadata": {
  403. "id": "vqszlxEE0Bk0",
  404. "colab": {
  405. "base_uri": "https://localhost:8080/",
  406. "height": 346
  407. },
  408. "outputId": "a698b34d-00a8-4067-ddc5-180cb4c8eeaa"
  409. },
  410. "source": [
  411. "# 4. mismatched tensor type\n",
  412. "import torch.nn as nn\n",
  413. "L = nn.CrossEntropyLoss()\n",
  414. "outs = torch.randn(5,5)\n",
  415. "labels = torch.Tensor([1,2,3,4,0])\n",
  416. "lossval = L(outs,labels) # Calculate CrossEntropyLoss between outs and labels"
  417. ],
  418. "execution_count": null,
  419. "outputs": [
  420. {
  421. "output_type": "error",
  422. "ename": "RuntimeError",
  423. "evalue": "ignored",
  424. "traceback": [
  425. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  426. "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
  427. "\u001b[0;32m<ipython-input-10-60a5d1aad216>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mouts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mlabels\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTensor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mlossval\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mL\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mouts\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mlabels\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# Calculate CrossEntropyLoss between outs and labels\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
  428. "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[0;34m(self, *input, **kwargs)\u001b[0m\n\u001b[1;32m 725\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_slow_forward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 726\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 727\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 728\u001b[0m for hook in itertools.chain(\n\u001b[1;32m 729\u001b[0m \u001b[0m_global_forward_hooks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
  429. "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/torch/nn/modules/loss.py\u001b[0m in \u001b[0;36mforward\u001b[0;34m(self, input, target)\u001b[0m\n\u001b[1;32m 960\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mforward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mTensor\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtarget\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mTensor\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mTensor\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 961\u001b[0m return F.cross_entropy(input, target, weight=self.weight,\n\u001b[0;32m--> 962\u001b[0;31m ignore_index=self.ignore_index, reduction=self.reduction)\n\u001b[0m\u001b[1;32m 963\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 964\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
  430. "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py\u001b[0m in \u001b[0;36mcross_entropy\u001b[0;34m(input, target, weight, size_average, ignore_index, reduce, reduction)\u001b[0m\n\u001b[1;32m 2466\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0msize_average\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mreduce\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2467\u001b[0m \u001b[0mreduction\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_Reduction\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlegacy_get_string\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msize_average\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreduce\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2468\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mnll_loss\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlog_softmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtarget\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mweight\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mignore_index\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreduction\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2469\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2470\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
  431. "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py\u001b[0m in \u001b[0;36mnll_loss\u001b[0;34m(input, target, weight, size_average, ignore_index, reduce, reduction)\u001b[0m\n\u001b[1;32m 2262\u001b[0m .format(input.size(0), target.size(0)))\n\u001b[1;32m 2263\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mdim\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2264\u001b[0;31m \u001b[0mret\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_C\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_nn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnll_loss\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtarget\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mweight\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_Reduction\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_enum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mreduction\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mignore_index\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2265\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mdim\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2266\u001b[0m \u001b[0mret\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_C\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_nn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnll_loss2d\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtarget\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mweight\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_Reduction\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_enum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mreduction\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mignore_index\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
  432. "\u001b[0;31mRuntimeError\u001b[0m: expected scalar type Long but found Float"
  433. ]
  434. }
  435. ]
  436. },
  437. {
  438. "cell_type": "code",
  439. "metadata": {
  440. "id": "CZwgwup_1dgS",
  441. "colab": {
  442. "base_uri": "https://localhost:8080/"
  443. },
  444. "outputId": "aaf1de76-7ef2-4ca4-b87d-8482a3117249"
  445. },
  446. "source": [
  447. "# 4. mismatched tensor type (fixed)\n",
  448. "labels = labels.long()\n",
  449. "lossval = L(outs,labels)\n",
  450. "print(lossval)"
  451. ],
  452. "execution_count": null,
  453. "outputs": [
  454. {
  455. "output_type": "stream",
  456. "text": [
  457. "tensor(2.6215)\n"
  458. ],
  459. "name": "stdout"
  460. }
  461. ]
  462. },
  463. {
  464. "cell_type": "markdown",
  465. "metadata": {
  466. "id": "dSuNdA8F06dK"
  467. },
  468. "source": [
  469. "**3. More on dataset and dataloader**\r\n"
  470. ]
  471. },
  472. {
  473. "cell_type": "markdown",
  474. "metadata": {
  475. "id": "in84z_xu1rE6"
  476. },
  477. "source": [
  478. "A dataset is a cluster of data in a organized way. A dataloader is a loader which can iterate through the data set."
  479. ]
  480. },
  481. {
  482. "cell_type": "markdown",
  483. "metadata": {
  484. "id": "34zfh-c22Qqs"
  485. },
  486. "source": [
  487. "Let a dataset be the English alphabets \"abcdefghijklmnopqrstuvwxyz\""
  488. ]
  489. },
  490. {
  491. "cell_type": "code",
  492. "metadata": {
  493. "id": "TaiHofty1qKA"
  494. },
  495. "source": [
  496. "dataset = \"abcdefghijklmnopqrstuvwxyz\""
  497. ],
  498. "execution_count": null,
  499. "outputs": []
  500. },
  501. {
  502. "cell_type": "markdown",
  503. "metadata": {
  504. "id": "h0jwhVa12h3a"
  505. },
  506. "source": [
  507. "A simple dataloader could be implemented with the python code \"for\""
  508. ]
  509. },
  510. {
  511. "cell_type": "code",
  512. "metadata": {
  513. "id": "bWC5Wwbv2egy"
  514. },
  515. "source": [
  516. "for datapoint in dataset:\r\n",
  517. " print(datapoint)"
  518. ],
  519. "execution_count": null,
  520. "outputs": []
  521. },
  522. {
  523. "cell_type": "markdown",
  524. "metadata": {
  525. "id": "n33VKzkG2y2U"
  526. },
  527. "source": [
  528. "When using the dataloader, we often like to shuffle the data. This is where torch.utils.data.DataLoader comes in handy. If each data is an index (0,1,2...) from the view of torch.utils.data.DataLoader, shuffling can simply be done by shuffling an index array. \r\n",
  529. "\r\n"
  530. ]
  531. },
  532. {
  533. "cell_type": "markdown",
  534. "metadata": {
  535. "id": "9MXUUKQ65APf"
  536. },
  537. "source": [
  538. "torch.utils.data.DataLoader will need two imformation to fulfill its role. First, it needs to know the length of the data. Second, once torch.utils.data.DataLoader outputs the index of the shuffling results, the dataset needs to return the corresponding data."
  539. ]
  540. },
  541. {
  542. "cell_type": "markdown",
  543. "metadata": {
  544. "id": "BV5txsjK5j4j"
  545. },
  546. "source": [
  547. "Therefore, torch.utils.data.Dataset provides the imformation by two functions, `__len__()` and `__getitem__()` to support torch.utils.data.Dataloader"
  548. ]
  549. },
  550. {
  551. "cell_type": "code",
  552. "metadata": {
  553. "id": "A0IEkemJ5ajD"
  554. },
  555. "source": [
  556. "import torch\r\n",
  557. "import torch.utils.data \r\n",
  558. "class ExampleDataset(torch.utils.data.Dataset):\r\n",
  559. " def __init__(self):\r\n",
  560. " self.data = \"abcdefghijklmnopqrstuvwxyz\"\r\n",
  561. " \r\n",
  562. " def __getitem__(self,idx): # if the index is idx, what will be the data?\r\n",
  563. " return self.data[idx]\r\n",
  564. " \r\n",
  565. " def __len__(self): # What is the length of the dataset\r\n",
  566. " return len(self.data)\r\n",
  567. "\r\n",
  568. "dataset1 = ExampleDataset() # create the dataset\r\n",
  569. "dataloader = torch.utils.data.DataLoader(dataset = dataset1,shuffle = True,batch_size = 1)\r\n",
  570. "for datapoint in dataloader:\r\n",
  571. " print(datapoint)"
  572. ],
  573. "execution_count": null,
  574. "outputs": []
  575. },
  576. {
  577. "cell_type": "markdown",
  578. "metadata": {
  579. "id": "nTt-ZTid9S2n"
  580. },
  581. "source": [
  582. "A simple data augmentation technique can be done by changing the code in `__len__()` and `__getitem__()`. Suppose we want to double the length of the dataset by adding in the uppercase letters, using only the lowercase dataset, you can change the dataset to the following."
  583. ]
  584. },
  585. {
  586. "cell_type": "code",
  587. "metadata": {
  588. "id": "7Wn3BA2j-NXl"
  589. },
  590. "source": [
  591. "import torch.utils.data \r\n",
  592. "class ExampleDataset(torch.utils.data.Dataset):\r\n",
  593. " def __init__(self):\r\n",
  594. " self.data = \"abcdefghijklmnopqrstuvwxyz\"\r\n",
  595. " \r\n",
  596. " def __getitem__(self,idx): # if the index is idx, what will be the data?\r\n",
  597. " if idx >= len(self.data): # if the index >= 26, return upper case letter\r\n",
  598. " return self.data[idx%26].upper()\r\n",
  599. " else: # if the index < 26, return lower case, return lower case letter\r\n",
  600. " return self.data[idx]\r\n",
  601. " \r\n",
  602. " def __len__(self): # What is the length of the dataset\r\n",
  603. " return 2 * len(self.data) # The length is now twice as large\r\n",
  604. "\r\n",
  605. "dataset1 = ExampleDataset() # create the dataset\r\n",
  606. "dataloader = torch.utils.data.DataLoader(dataset = dataset1,shuffle = True,batch_size = 1)\r\n",
  607. "for datapoint in dataloader:\r\n",
  608. " print(datapoint)"
  609. ],
  610. "execution_count": null,
  611. "outputs": []
  612. }
  613. ]
  614. }