|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# 201 Torch and Numpy\n", |
| 8 | + "\n", |
| 9 | + "View more, visit my tutorial page: https://morvanzhou.github.io/tutorials/\n", |
| 10 | + "My Youtube Channel: https://www.youtube.com/user/MorvanZhou\n", |
| 11 | + "\n", |
| 12 | + "Dependencies:\n", |
| 13 | + "* torch: 0.1.11\n", |
| 14 | + "* numpy\n", |
| 15 | + "\n", |
| 16 | + "Details about math operation in torch can be found in: http://pytorch.org/docs/torch.html#math-operations\n" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "code", |
| 21 | + "execution_count": 1, |
| 22 | + "metadata": { |
| 23 | + "collapsed": true |
| 24 | + }, |
| 25 | + "outputs": [], |
| 26 | + "source": [ |
| 27 | + "import torch\n", |
| 28 | + "import numpy as np" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": 2, |
| 34 | + "metadata": {}, |
| 35 | + "outputs": [ |
| 36 | + { |
| 37 | + "name": "stdout", |
| 38 | + "output_type": "stream", |
| 39 | + "text": [ |
| 40 | + "\n", |
| 41 | + "numpy array: [[0 1 2]\n", |
| 42 | + " [3 4 5]] \n", |
| 43 | + "torch tensor: \n", |
| 44 | + " 0 1 2\n", |
| 45 | + " 3 4 5\n", |
| 46 | + "[torch.LongTensor of size 2x3]\n", |
| 47 | + " \n", |
| 48 | + "tensor to array: [[0 1 2]\n", |
| 49 | + " [3 4 5]]\n" |
| 50 | + ] |
| 51 | + } |
| 52 | + ], |
| 53 | + "source": [ |
| 54 | + "# convert numpy to tensor or vise versa\n", |
| 55 | + "np_data = np.arange(6).reshape((2, 3))\n", |
| 56 | + "torch_data = torch.from_numpy(np_data)\n", |
| 57 | + "tensor2array = torch_data.numpy()\n", |
| 58 | + "print(\n", |
| 59 | + " '\\nnumpy array:', np_data, # [[0 1 2], [3 4 5]]\n", |
| 60 | + " '\\ntorch tensor:', torch_data, # 0 1 2 \\n 3 4 5 [torch.LongTensor of size 2x3]\n", |
| 61 | + " '\\ntensor to array:', tensor2array, # [[0 1 2], [3 4 5]]\n", |
| 62 | + ")" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "code", |
| 67 | + "execution_count": 3, |
| 68 | + "metadata": {}, |
| 69 | + "outputs": [ |
| 70 | + { |
| 71 | + "name": "stdout", |
| 72 | + "output_type": "stream", |
| 73 | + "text": [ |
| 74 | + "\n", |
| 75 | + "abs \n", |
| 76 | + "numpy: [1 2 1 2] \n", |
| 77 | + "torch: \n", |
| 78 | + " 1\n", |
| 79 | + " 2\n", |
| 80 | + " 1\n", |
| 81 | + " 2\n", |
| 82 | + "[torch.FloatTensor of size 4]\n", |
| 83 | + "\n" |
| 84 | + ] |
| 85 | + } |
| 86 | + ], |
| 87 | + "source": [ |
| 88 | + "# abs\n", |
| 89 | + "data = [-1, -2, 1, 2]\n", |
| 90 | + "tensor = torch.FloatTensor(data) # 32-bit floating point\n", |
| 91 | + "print(\n", |
| 92 | + " '\\nabs',\n", |
| 93 | + " '\\nnumpy: ', np.abs(data), # [1 2 1 2]\n", |
| 94 | + " '\\ntorch: ', torch.abs(tensor) # [1 2 1 2]\n", |
| 95 | + ")" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": 4, |
| 101 | + "metadata": {}, |
| 102 | + "outputs": [ |
| 103 | + { |
| 104 | + "data": { |
| 105 | + "text/plain": [ |
| 106 | + "\n", |
| 107 | + " 1\n", |
| 108 | + " 2\n", |
| 109 | + " 1\n", |
| 110 | + " 2\n", |
| 111 | + "[torch.FloatTensor of size 4]" |
| 112 | + ] |
| 113 | + }, |
| 114 | + "execution_count": 4, |
| 115 | + "metadata": {}, |
| 116 | + "output_type": "execute_result" |
| 117 | + } |
| 118 | + ], |
| 119 | + "source": [ |
| 120 | + "tensor.abs()" |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "cell_type": "code", |
| 125 | + "execution_count": 5, |
| 126 | + "metadata": {}, |
| 127 | + "outputs": [ |
| 128 | + { |
| 129 | + "name": "stdout", |
| 130 | + "output_type": "stream", |
| 131 | + "text": [ |
| 132 | + "\n", |
| 133 | + "sin \n", |
| 134 | + "numpy: [-0.84147098 -0.90929743 0.84147098 0.90929743] \n", |
| 135 | + "torch: \n", |
| 136 | + "-0.8415\n", |
| 137 | + "-0.9093\n", |
| 138 | + " 0.8415\n", |
| 139 | + " 0.9093\n", |
| 140 | + "[torch.FloatTensor of size 4]\n", |
| 141 | + "\n" |
| 142 | + ] |
| 143 | + } |
| 144 | + ], |
| 145 | + "source": [ |
| 146 | + "# sin\n", |
| 147 | + "print(\n", |
| 148 | + " '\\nsin',\n", |
| 149 | + " '\\nnumpy: ', np.sin(data), # [-0.84147098 -0.90929743 0.84147098 0.90929743]\n", |
| 150 | + " '\\ntorch: ', torch.sin(tensor) # [-0.8415 -0.9093 0.8415 0.9093]\n", |
| 151 | + ")" |
| 152 | + ] |
| 153 | + }, |
| 154 | + { |
| 155 | + "cell_type": "code", |
| 156 | + "execution_count": 6, |
| 157 | + "metadata": {}, |
| 158 | + "outputs": [ |
| 159 | + { |
| 160 | + "data": { |
| 161 | + "text/plain": [ |
| 162 | + "\n", |
| 163 | + " 0.2689\n", |
| 164 | + " 0.1192\n", |
| 165 | + " 0.7311\n", |
| 166 | + " 0.8808\n", |
| 167 | + "[torch.FloatTensor of size 4]" |
| 168 | + ] |
| 169 | + }, |
| 170 | + "execution_count": 6, |
| 171 | + "metadata": {}, |
| 172 | + "output_type": "execute_result" |
| 173 | + } |
| 174 | + ], |
| 175 | + "source": [ |
| 176 | + "tensor.sigmoid()" |
| 177 | + ] |
| 178 | + }, |
| 179 | + { |
| 180 | + "cell_type": "code", |
| 181 | + "execution_count": 7, |
| 182 | + "metadata": {}, |
| 183 | + "outputs": [ |
| 184 | + { |
| 185 | + "data": { |
| 186 | + "text/plain": [ |
| 187 | + "\n", |
| 188 | + " 0.3679\n", |
| 189 | + " 0.1353\n", |
| 190 | + " 2.7183\n", |
| 191 | + " 7.3891\n", |
| 192 | + "[torch.FloatTensor of size 4]" |
| 193 | + ] |
| 194 | + }, |
| 195 | + "execution_count": 7, |
| 196 | + "metadata": {}, |
| 197 | + "output_type": "execute_result" |
| 198 | + } |
| 199 | + ], |
| 200 | + "source": [ |
| 201 | + "tensor.exp()" |
| 202 | + ] |
| 203 | + }, |
| 204 | + { |
| 205 | + "cell_type": "code", |
| 206 | + "execution_count": 8, |
| 207 | + "metadata": {}, |
| 208 | + "outputs": [ |
| 209 | + { |
| 210 | + "name": "stdout", |
| 211 | + "output_type": "stream", |
| 212 | + "text": [ |
| 213 | + "\n", |
| 214 | + "mean \n", |
| 215 | + "numpy: 0.0 \n", |
| 216 | + "torch: 0.0\n" |
| 217 | + ] |
| 218 | + } |
| 219 | + ], |
| 220 | + "source": [ |
| 221 | + "# mean\n", |
| 222 | + "print(\n", |
| 223 | + " '\\nmean',\n", |
| 224 | + " '\\nnumpy: ', np.mean(data), # 0.0\n", |
| 225 | + " '\\ntorch: ', torch.mean(tensor) # 0.0\n", |
| 226 | + ")" |
| 227 | + ] |
| 228 | + }, |
| 229 | + { |
| 230 | + "cell_type": "code", |
| 231 | + "execution_count": 9, |
| 232 | + "metadata": {}, |
| 233 | + "outputs": [ |
| 234 | + { |
| 235 | + "name": "stdout", |
| 236 | + "output_type": "stream", |
| 237 | + "text": [ |
| 238 | + "\n", |
| 239 | + "matrix multiplication (matmul) \n", |
| 240 | + "numpy: [[ 7 10]\n", |
| 241 | + " [15 22]] \n", |
| 242 | + "torch: \n", |
| 243 | + " 7 10\n", |
| 244 | + " 15 22\n", |
| 245 | + "[torch.FloatTensor of size 2x2]\n", |
| 246 | + "\n" |
| 247 | + ] |
| 248 | + } |
| 249 | + ], |
| 250 | + "source": [ |
| 251 | + "# matrix multiplication\n", |
| 252 | + "data = [[1,2], [3,4]]\n", |
| 253 | + "tensor = torch.FloatTensor(data) # 32-bit floating point\n", |
| 254 | + "# correct method\n", |
| 255 | + "print(\n", |
| 256 | + " '\\nmatrix multiplication (matmul)',\n", |
| 257 | + " '\\nnumpy: ', np.matmul(data, data), # [[7, 10], [15, 22]]\n", |
| 258 | + " '\\ntorch: ', torch.mm(tensor, tensor) # [[7, 10], [15, 22]]\n", |
| 259 | + ")" |
| 260 | + ] |
| 261 | + }, |
| 262 | + { |
| 263 | + "cell_type": "code", |
| 264 | + "execution_count": 14, |
| 265 | + "metadata": {}, |
| 266 | + "outputs": [ |
| 267 | + { |
| 268 | + "name": "stdout", |
| 269 | + "output_type": "stream", |
| 270 | + "text": [ |
| 271 | + "\n", |
| 272 | + "matrix multiplication (dot) \n", |
| 273 | + "numpy: [[ 7 10]\n", |
| 274 | + " [15 22]] \n", |
| 275 | + "torch: 30.0\n" |
| 276 | + ] |
| 277 | + } |
| 278 | + ], |
| 279 | + "source": [ |
| 280 | + "# incorrect method\n", |
| 281 | + "data = np.array(data)\n", |
| 282 | + "print(\n", |
| 283 | + " '\\nmatrix multiplication (dot)',\n", |
| 284 | + " '\\nnumpy: ', data.dot(data), # [[7, 10], [15, 22]]\n", |
| 285 | + " '\\ntorch: ', tensor.dot(tensor) # this will convert tensor to [1,2,3,4], you'll get 30.0\n", |
| 286 | + ")" |
| 287 | + ] |
| 288 | + }, |
| 289 | + { |
| 290 | + "cell_type": "markdown", |
| 291 | + "metadata": {}, |
| 292 | + "source": [ |
| 293 | + "Note that:\n", |
| 294 | + "\n", |
| 295 | + "torch.dot(tensor1, tensor2) → float\n", |
| 296 | + "\n", |
| 297 | + "Computes the dot product (inner product) of two tensors. Both tensors are treated as 1-D vectors." |
| 298 | + ] |
| 299 | + }, |
| 300 | + { |
| 301 | + "cell_type": "code", |
| 302 | + "execution_count": 11, |
| 303 | + "metadata": {}, |
| 304 | + "outputs": [ |
| 305 | + { |
| 306 | + "data": { |
| 307 | + "text/plain": [ |
| 308 | + "\n", |
| 309 | + " 7 10\n", |
| 310 | + " 15 22\n", |
| 311 | + "[torch.FloatTensor of size 2x2]" |
| 312 | + ] |
| 313 | + }, |
| 314 | + "execution_count": 11, |
| 315 | + "metadata": {}, |
| 316 | + "output_type": "execute_result" |
| 317 | + } |
| 318 | + ], |
| 319 | + "source": [ |
| 320 | + "tensor.mm(tensor)" |
| 321 | + ] |
| 322 | + }, |
| 323 | + { |
| 324 | + "cell_type": "code", |
| 325 | + "execution_count": 12, |
| 326 | + "metadata": {}, |
| 327 | + "outputs": [ |
| 328 | + { |
| 329 | + "data": { |
| 330 | + "text/plain": [ |
| 331 | + "\n", |
| 332 | + " 1 4\n", |
| 333 | + " 9 16\n", |
| 334 | + "[torch.FloatTensor of size 2x2]" |
| 335 | + ] |
| 336 | + }, |
| 337 | + "execution_count": 12, |
| 338 | + "metadata": {}, |
| 339 | + "output_type": "execute_result" |
| 340 | + } |
| 341 | + ], |
| 342 | + "source": [ |
| 343 | + "tensor * tensor" |
| 344 | + ] |
| 345 | + }, |
| 346 | + { |
| 347 | + "cell_type": "code", |
| 348 | + "execution_count": 13, |
| 349 | + "metadata": {}, |
| 350 | + "outputs": [ |
| 351 | + { |
| 352 | + "data": { |
| 353 | + "text/plain": [ |
| 354 | + "30.0" |
| 355 | + ] |
| 356 | + }, |
| 357 | + "execution_count": 13, |
| 358 | + "metadata": {}, |
| 359 | + "output_type": "execute_result" |
| 360 | + } |
| 361 | + ], |
| 362 | + "source": [ |
| 363 | + "tensor.dot(tensor)" |
| 364 | + ] |
| 365 | + }, |
| 366 | + { |
| 367 | + "cell_type": "code", |
| 368 | + "execution_count": null, |
| 369 | + "metadata": { |
| 370 | + "collapsed": true |
| 371 | + }, |
| 372 | + "outputs": [], |
| 373 | + "source": [] |
| 374 | + } |
| 375 | + ], |
| 376 | + "metadata": { |
| 377 | + "kernelspec": { |
| 378 | + "display_name": "Python 3", |
| 379 | + "language": "python", |
| 380 | + "name": "python3" |
| 381 | + }, |
| 382 | + "language_info": { |
| 383 | + "codemirror_mode": { |
| 384 | + "name": "ipython", |
| 385 | + "version": 3 |
| 386 | + }, |
| 387 | + "file_extension": ".py", |
| 388 | + "mimetype": "text/x-python", |
| 389 | + "name": "python", |
| 390 | + "nbconvert_exporter": "python", |
| 391 | + "pygments_lexer": "ipython3", |
| 392 | + "version": "3.5.2" |
| 393 | + } |
| 394 | + }, |
| 395 | + "nbformat": 4, |
| 396 | + "nbformat_minor": 2 |
| 397 | +} |
0 commit comments