├── README.md └── Tutorials ├── NumpyTutorial.ipynb ├── Torch Tutorial.ipynb ├── autograd_tutorial.ipynb └── neural_networks_tutorial.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # fastaipart1v2 2 | Files for Fast.ai part 2 3 | -------------------------------------------------------------------------------- /Tutorials/NumpyTutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Numpy Tutorial\n", 8 | "Originally from DataLore Lab\n", 9 | "https://www.datalorelabs.com" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 39, 15 | "metadata": { 16 | "collapsed": true 17 | }, 18 | "outputs": [], 19 | "source": [ 20 | "import numpy as np" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "metadata": {}, 26 | "source": [ 27 | "### Arrays\n", 28 | "A numpy array is a analogous to python list but the elements of the array should be of same type." 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "execution_count": 40, 34 | "metadata": {}, 35 | "outputs": [ 36 | { 37 | "data": { 38 | "text/plain": [ 39 | "array([1, 2, 3])" 40 | ] 41 | }, 42 | "execution_count": 40, 43 | "metadata": {}, 44 | "output_type": "execute_result" 45 | } 46 | ], 47 | "source": [ 48 | "a = np.array([1, 2, 3])\n", 49 | "a" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "The type of the array is called numpy.ndarray . ( numpy n-dimensional array)" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 41, 62 | "metadata": {}, 63 | "outputs": [ 64 | { 65 | "data": { 66 | "text/plain": [ 67 | "numpy.ndarray" 68 | ] 69 | }, 70 | "execution_count": 41, 71 | "metadata": {}, 72 | "output_type": "execute_result" 73 | } 74 | ], 75 | "source": [ 76 | "type(a)" 77 | ] 78 | }, 79 | { 80 | "cell_type": "markdown", 81 | "metadata": {}, 82 | "source": [ 83 | "We associate numpy arrays with two properties shape and rank, which describe the array about the\n", 84 | "dimension and shape it is of." 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "a is one dimensional array with 3 elements" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 42, 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "Rank of a: 1\n", 104 | "Shape of a: (3,)\n", 105 | "Total number of elements in the array: 3\n", 106 | "Data type of the elements of a: int64\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "print(\"Rank of a: \", a.ndim)\n", 112 | "print(\"Shape of a: \", a.shape)\n", 113 | "print(\"Total number of elements in the array: \", a.size)\n", 114 | "print(\"Data type of the elements of a:\", a.dtype)" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 43, 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "name": "stdout", 124 | "output_type": "stream", 125 | "text": [ 126 | "1 2 3\n", 127 | "[5 2 3]\n" 128 | ] 129 | } 130 | ], 131 | "source": [ 132 | "print(a[0], a[1], a[2]) # Prints \"1 2 3\"\n", 133 | "a[0] = 5 # Change an element of the array\n", 134 | "print(a)" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 44, 140 | "metadata": { 141 | "collapsed": true 142 | }, 143 | "outputs": [], 144 | "source": [ 145 | "b=np.array([[1., 2., 3.], [ 4., 5., 6.]])" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 45, 151 | "metadata": {}, 152 | "outputs": [ 153 | { 154 | "name": "stdout", 155 | "output_type": "stream", 156 | "text": [ 157 | "Rank of b: 2\n", 158 | "Shape of b: (2, 3)\n", 159 | "Total number of elements in b: 6\n", 160 | "Data type of the elements of b: float64\n" 161 | ] 162 | } 163 | ], 164 | "source": [ 165 | "print(\"Rank of b: \", b.ndim)\n", 166 | "print(\"Shape of b: \", b.shape)\n", 167 | "print(\"Total number of elements in b: \", b.size)\n", 168 | "print(\"Data type of the elements of b:\", b.dtype)" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": 46, 174 | "metadata": {}, 175 | "outputs": [ 176 | { 177 | "data": { 178 | "text/plain": [ 179 | "array([[ 1., 2., 3.],\n", 180 | " [ 4., 5., 6.]])" 181 | ] 182 | }, 183 | "execution_count": 46, 184 | "metadata": {}, 185 | "output_type": "execute_result" 186 | } 187 | ], 188 | "source": [ 189 | "b" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 47, 195 | "metadata": {}, 196 | "outputs": [ 197 | { 198 | "name": "stdout", 199 | "output_type": "stream", 200 | "text": [ 201 | "1.0 2.0 4.0\n" 202 | ] 203 | } 204 | ], 205 | "source": [ 206 | "# accessing elements\n", 207 | "print(b[0, 0], b[0, 1], b[1, 0]) # Prints \"1 2 4\"" 208 | ] 209 | }, 210 | { 211 | "cell_type": "markdown", 212 | "metadata": {}, 213 | "source": [ 214 | "### Array Creation\n", 215 | "Numpy provides lots of ways to create a numpy array." 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "execution_count": 48, 221 | "metadata": {}, 222 | "outputs": [ 223 | { 224 | "name": "stdout", 225 | "output_type": "stream", 226 | "text": [ 227 | "[[ 0. 0. 0.]\n", 228 | " [ 0. 0. 0.]\n", 229 | " [ 0. 0. 0.]]\n" 230 | ] 231 | } 232 | ], 233 | "source": [ 234 | "a = np.zeros((3,3)) # Create an array of all zeros\n", 235 | "print(a) " 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": 49, 241 | "metadata": {}, 242 | "outputs": [ 243 | { 244 | "name": "stdout", 245 | "output_type": "stream", 246 | "text": [ 247 | "[[ 1. 1. 1. 1. 1.]\n", 248 | " [ 1. 1. 1. 1. 1.]]\n" 249 | ] 250 | } 251 | ], 252 | "source": [ 253 | "a = np.ones((2,5)) # Create an array of all ones\n", 254 | "print(a) " 255 | ] 256 | }, 257 | { 258 | "cell_type": "code", 259 | "execution_count": 50, 260 | "metadata": {}, 261 | "outputs": [ 262 | { 263 | "name": "stdout", 264 | "output_type": "stream", 265 | "text": [ 266 | "[[ 0. 0. 0. 0. 0.]\n", 267 | " [ 0. 0. 0. 0. 0.]]\n" 268 | ] 269 | } 270 | ], 271 | "source": [ 272 | "c = np.zeros_like(a) # Create an array of all zeros like a's shape\n", 273 | "print(c)" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": 51, 279 | "metadata": {}, 280 | "outputs": [ 281 | { 282 | "name": "stdout", 283 | "output_type": "stream", 284 | "text": [ 285 | "[[ 1. 1. 1.]\n", 286 | " [ 1. 1. 1.]]\n" 287 | ] 288 | } 289 | ], 290 | "source": [ 291 | "c = np.ones_like(b) # Create an array of all ones like b's shape\n", 292 | "print(c)" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": 52, 298 | "metadata": {}, 299 | "outputs": [ 300 | { 301 | "name": "stdout", 302 | "output_type": "stream", 303 | "text": [ 304 | "[[10 10]\n", 305 | " [10 10]]\n" 306 | ] 307 | } 308 | ], 309 | "source": [ 310 | "#numpy full\n", 311 | "d = np.full((2,2), 10) # Create a constant array\n", 312 | "print(d) # Prints \"[[ 7. 7.]\n", 313 | " # [ 7. 7.]]" 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": 53, 319 | "metadata": {}, 320 | "outputs": [ 321 | { 322 | "name": "stdout", 323 | "output_type": "stream", 324 | "text": [ 325 | "[[ 1. 0. 0.]\n", 326 | " [ 0. 1. 0.]\n", 327 | " [ 0. 0. 1.]]\n" 328 | ] 329 | } 330 | ], 331 | "source": [ 332 | "#numpy eye (Identity)\n", 333 | "e = np.eye(3) # Create a 3x3 identity matrix\n", 334 | "print(e) " 335 | ] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "execution_count": 54, 340 | "metadata": {}, 341 | "outputs": [ 342 | { 343 | "name": "stdout", 344 | "output_type": "stream", 345 | "text": [ 346 | "[[ 0.27096078 0.43627003]\n", 347 | " [ 0.17311355 0.74167019]]\n" 348 | ] 349 | } 350 | ], 351 | "source": [ 352 | "#numpy random\n", 353 | "f = np.random.random((2,2)) # Create an array filled with random values\n", 354 | "print(f)" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": 55, 360 | "metadata": {}, 361 | "outputs": [ 362 | { 363 | "data": { 364 | "text/plain": [ 365 | "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" 366 | ] 367 | }, 368 | "execution_count": 55, 369 | "metadata": {}, 370 | "output_type": "execute_result" 371 | } 372 | ], 373 | "source": [ 374 | "#numpy arange\n", 375 | "np.arange(0, 10, 1) # arguments: start, stop, step" 376 | ] 377 | }, 378 | { 379 | "cell_type": "code", 380 | "execution_count": 56, 381 | "metadata": {}, 382 | "outputs": [ 383 | { 384 | "data": { 385 | "text/plain": [ 386 | "array([ 0. , 2.5, 5. , 7.5, 10. ])" 387 | ] 388 | }, 389 | "execution_count": 56, 390 | "metadata": {}, 391 | "output_type": "execute_result" 392 | } 393 | ], 394 | "source": [ 395 | "#numpy linspace\n", 396 | "np.linspace(0, 10, 5)" 397 | ] 398 | }, 399 | { 400 | "cell_type": "code", 401 | "execution_count": 58, 402 | "metadata": {}, 403 | "outputs": [ 404 | { 405 | "ename": "SyntaxError", 406 | "evalue": "invalid syntax (, line 3)", 407 | "output_type": "error", 408 | "traceback": [ 409 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m %%file mydata.dat\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" 410 | ] 411 | } 412 | ], 413 | "source": [ 414 | "#loading from file Doesnt work\n", 415 | "#need to make file\n", 416 | "%%file mydata.dat" 417 | ] 418 | }, 419 | { 420 | "cell_type": "markdown", 421 | "metadata": {}, 422 | "source": [ 423 | "Writing mydata.dat" 424 | ] 425 | }, 426 | { 427 | "cell_type": "code", 428 | "execution_count": 62, 429 | "metadata": {}, 430 | "outputs": [ 431 | { 432 | "data": { 433 | "text/plain": [ 434 | "array([[ 1., 11.],\n", 435 | " [ 2., 92.],\n", 436 | " [ 3., 81.],\n", 437 | " [ 4., 52.],\n", 438 | " [ 5., 14.],\n", 439 | " [ 6., 23.],\n", 440 | " [ 7., 22.],\n", 441 | " [ 8., 11.],\n", 442 | " [ 9., 0.],\n", 443 | " [ 10., 1.]])" 444 | ] 445 | }, 446 | "execution_count": 62, 447 | "metadata": {}, 448 | "output_type": "execute_result" 449 | } 450 | ], 451 | "source": [ 452 | "np.genfromtxt(\"mydata.dat\",)" 453 | ] 454 | }, 455 | { 456 | "cell_type": "code", 457 | "execution_count": 63, 458 | "metadata": {}, 459 | "outputs": [ 460 | { 461 | "data": { 462 | "text/plain": [ 463 | "array([[ 1., 11.],\n", 464 | " [ 2., 92.],\n", 465 | " [ 3., 81.],\n", 466 | " [ 4., 52.],\n", 467 | " [ 5., 14.],\n", 468 | " [ 6., 23.],\n", 469 | " [ 7., 22.],\n", 470 | " [ 8., 11.],\n", 471 | " [ 9., 0.],\n", 472 | " [ 10., 1.]])" 473 | ] 474 | }, 475 | "execution_count": 63, 476 | "metadata": {}, 477 | "output_type": "execute_result" 478 | } 479 | ], 480 | "source": [ 481 | "np.genfromtxt(\"mydata.dat\",)" 482 | ] 483 | }, 484 | { 485 | "cell_type": "code", 486 | "execution_count": 64, 487 | "metadata": { 488 | "collapsed": true 489 | }, 490 | "outputs": [], 491 | "source": [ 492 | "# Find more\n", 493 | "?np.genfromtxt" 494 | ] 495 | }, 496 | { 497 | "cell_type": "markdown", 498 | "metadata": {}, 499 | "source": [ 500 | "You can read more about array creation in the documentation\n", 501 | "(http://docs.scipy.org/doc/numpy/user/basics.creation.html#arrays-creation)." 502 | ] 503 | }, 504 | { 505 | "cell_type": "markdown", 506 | "metadata": {}, 507 | "source": [ 508 | "## Array Indexing\n", 509 | "Numpy offers several ways to index into arrays.\n", 510 | "Slicing\n", 511 | "One-dimensional arrays can be indexed, sliced and iterated over, much like lists and other Python\n", 512 | "sequences." 513 | ] 514 | }, 515 | { 516 | "cell_type": "code", 517 | "execution_count": 66, 518 | "metadata": {}, 519 | "outputs": [ 520 | { 521 | "name": "stdout", 522 | "output_type": "stream", 523 | "text": [ 524 | "[ 0. 100. 200. 300. 400. 500.]\n" 525 | ] 526 | } 527 | ], 528 | "source": [ 529 | "a = np.linspace(0, 500, 6)\n", 530 | "print(a)" 531 | ] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "execution_count": 67, 536 | "metadata": {}, 537 | "outputs": [ 538 | { 539 | "data": { 540 | "text/plain": [ 541 | "array([ 200., 300.])" 542 | ] 543 | }, 544 | "execution_count": 67, 545 | "metadata": {}, 546 | "output_type": "execute_result" 547 | } 548 | ], 549 | "source": [ 550 | "# Elements at the middle of the array\n", 551 | "a[2:4]" 552 | ] 553 | }, 554 | { 555 | "cell_type": "code", 556 | "execution_count": 68, 557 | "metadata": {}, 558 | "outputs": [ 559 | { 560 | "data": { 561 | "text/plain": [ 562 | "500.0" 563 | ] 564 | }, 565 | "execution_count": 68, 566 | "metadata": {}, 567 | "output_type": "execute_result" 568 | } 569 | ], 570 | "source": [ 571 | "# Last element\n", 572 | "a[-1]" 573 | ] 574 | }, 575 | { 576 | "cell_type": "code", 577 | "execution_count": 69, 578 | "metadata": {}, 579 | "outputs": [ 580 | { 581 | "data": { 582 | "text/plain": [ 583 | "array([ 400., 500.])" 584 | ] 585 | }, 586 | "execution_count": 69, 587 | "metadata": {}, 588 | "output_type": "execute_result" 589 | } 590 | ], 591 | "source": [ 592 | "# Last two elements\n", 593 | "a[-2:]" 594 | ] 595 | }, 596 | { 597 | "cell_type": "markdown", 598 | "metadata": {}, 599 | "source": [ 600 | "Multidimensional arrays can have one index per axis. These indices are given in a tuple separated by\n", 601 | "commas. When accessing multidimensional arrays, we must specify a slice for each dimension of the array" 602 | ] 603 | }, 604 | { 605 | "cell_type": "code", 606 | "execution_count": 70, 607 | "metadata": {}, 608 | "outputs": [ 609 | { 610 | "name": "stdout", 611 | "output_type": "stream", 612 | "text": [ 613 | "[[ 1. 2. 3.]\n", 614 | " [ 4. 5. 6.]\n", 615 | " [ 7. 8. 9.]]\n" 616 | ] 617 | } 618 | ], 619 | "source": [ 620 | "a =np.array([\n", 621 | " np.linspace(1, 3, 3),\n", 622 | " np.linspace(4, 6, 3),\n", 623 | " np.linspace(7, 9, 3)\n", 624 | "])\n", 625 | "print(a)" 626 | ] 627 | }, 628 | { 629 | "cell_type": "code", 630 | "execution_count": 71, 631 | "metadata": {}, 632 | "outputs": [ 633 | { 634 | "name": "stdout", 635 | "output_type": "stream", 636 | "text": [ 637 | "First element: 1.0\n" 638 | ] 639 | } 640 | ], 641 | "source": [ 642 | "print(\"First element: \", a[0,0])" 643 | ] 644 | }, 645 | { 646 | "cell_type": "markdown", 647 | "metadata": {}, 648 | "source": [ 649 | "When fewer indices are provided than the number of axes, the missing indices are considered complete\n", 650 | "slices" 651 | ] 652 | }, 653 | { 654 | "cell_type": "code", 655 | "execution_count": 72, 656 | "metadata": {}, 657 | "outputs": [ 658 | { 659 | "name": "stdout", 660 | "output_type": "stream", 661 | "text": [ 662 | "First row:\n", 663 | " [ 1. 2. 3.]\n" 664 | ] 665 | } 666 | ], 667 | "source": [ 668 | "print(\"First row:\\n\", a[0])" 669 | ] 670 | }, 671 | { 672 | "cell_type": "code", 673 | "execution_count": 73, 674 | "metadata": {}, 675 | "outputs": [ 676 | { 677 | "name": "stdout", 678 | "output_type": "stream", 679 | "text": [ 680 | "Upper Right 2x2 matrix:\n", 681 | " [[ 2. 3.]\n", 682 | " [ 5. 6.]]\n", 683 | "Lower Right 2x2 matrix:\n", 684 | " [[ 5. 6.]\n", 685 | " [ 8. 9.]]\n" 686 | ] 687 | } 688 | ], 689 | "source": [ 690 | "print(\"Upper Right 2x2 matrix:\\n\", a[0:2, 1:])\n", 691 | "print(\"Lower Right 2x2 matrix:\\n\", a[1:, 1:])" 692 | ] 693 | }, 694 | { 695 | "cell_type": "code", 696 | "execution_count": 74, 697 | "metadata": {}, 698 | "outputs": [ 699 | { 700 | "name": "stdout", 701 | "output_type": "stream", 702 | "text": [ 703 | "Upper Left 2x2 matrix:\n", 704 | " [[ 1. 2.]\n", 705 | " [ 4. 5.]]\n", 706 | "Lower Left 2x2 matrix:\n", 707 | " [[ 4. 5.]\n", 708 | " [ 7. 8.]]\n" 709 | ] 710 | } 711 | ], 712 | "source": [ 713 | "print(\"Upper Left 2x2 matrix:\\n\", a[:2, :2])\n", 714 | "print(\"Lower Left 2x2 matrix:\\n\", a[1:, :2])" 715 | ] 716 | }, 717 | { 718 | "cell_type": "code", 719 | "execution_count": 75, 720 | "metadata": {}, 721 | "outputs": [ 722 | { 723 | "name": "stdout", 724 | "output_type": "stream", 725 | "text": [ 726 | "[[False False]\n", 727 | " [ True True]\n", 728 | " [ True True]]\n" 729 | ] 730 | } 731 | ], 732 | "source": [ 733 | "## Boolean array indexing\n", 734 | "a = np.array([[1,2], [3, 4], [5, 6]])\n", 735 | "bool_idx = (a > 2)\n", 736 | "print(bool_idx)" 737 | ] 738 | }, 739 | { 740 | "cell_type": "code", 741 | "execution_count": 76, 742 | "metadata": {}, 743 | "outputs": [ 744 | { 745 | "name": "stdout", 746 | "output_type": "stream", 747 | "text": [ 748 | "[3 4 5 6]\n" 749 | ] 750 | } 751 | ], 752 | "source": [ 753 | "print(a[bool_idx])" 754 | ] 755 | }, 756 | { 757 | "cell_type": "code", 758 | "execution_count": 77, 759 | "metadata": {}, 760 | "outputs": [ 761 | { 762 | "name": "stdout", 763 | "output_type": "stream", 764 | "text": [ 765 | "[3 4 5 6]\n" 766 | ] 767 | } 768 | ], 769 | "source": [ 770 | "# We can do the above operation in single line:\n", 771 | "print(a[a > 2])" 772 | ] 773 | }, 774 | { 775 | "cell_type": "markdown", 776 | "metadata": {}, 777 | "source": [ 778 | "Read More on indexing here (https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html)" 779 | ] 780 | }, 781 | { 782 | "cell_type": "markdown", 783 | "metadata": {}, 784 | "source": [ 785 | "## Other function to subset an array\n", 786 | "### where\n", 787 | "Convert conditional indices to position index using the where function" 788 | ] 789 | }, 790 | { 791 | "cell_type": "code", 792 | "execution_count": 78, 793 | "metadata": {}, 794 | "outputs": [ 795 | { 796 | "name": "stdout", 797 | "output_type": "stream", 798 | "text": [ 799 | "[[1 2]\n", 800 | " [3 4]\n", 801 | " [5 6]] \n", 802 | "\n", 803 | "Axis-0: [1 1 2 2]\n", 804 | "Axis-1: [0 1 0 1]\n" 805 | ] 806 | } 807 | ], 808 | "source": [ 809 | "print(a, \"\\n\")\n", 810 | "m, n = np.where(a > 2)\n", 811 | "print(\"Axis-0: \", m)\n", 812 | "print(\"Axis-1: \",n)" 813 | ] 814 | }, 815 | { 816 | "cell_type": "code", 817 | "execution_count": 79, 818 | "metadata": {}, 819 | "outputs": [ 820 | { 821 | "data": { 822 | "text/plain": [ 823 | "array([3, 4, 5, 6])" 824 | ] 825 | }, 826 | "execution_count": 79, 827 | "metadata": {}, 828 | "output_type": "execute_result" 829 | } 830 | ], 831 | "source": [ 832 | "a[m,n]" 833 | ] 834 | }, 835 | { 836 | "cell_type": "code", 837 | "execution_count": 80, 838 | "metadata": {}, 839 | "outputs": [ 840 | { 841 | "name": "stdout", 842 | "output_type": "stream", 843 | "text": [ 844 | "A:\n", 845 | " [[ 0 1 2 3 4]\n", 846 | " [10 11 12 13 14]\n", 847 | " [20 21 22 23 24]\n", 848 | " [30 31 32 33 34]\n", 849 | " [40 41 42 43 44]]\n" 850 | ] 851 | }, 852 | { 853 | "data": { 854 | "text/plain": [ 855 | "array([ 0, 11, 22, 33, 44])" 856 | ] 857 | }, 858 | "execution_count": 80, 859 | "metadata": {}, 860 | "output_type": "execute_result" 861 | } 862 | ], 863 | "source": [ 864 | "# diag\n", 865 | "A = np.array([[n+m*10 for n in range(5)] for m in range(5)])\n", 866 | "print(\"A:\\n\", A)\n", 867 | "np.diag(A)" 868 | ] 869 | }, 870 | { 871 | "cell_type": "code", 872 | "execution_count": 81, 873 | "metadata": {}, 874 | "outputs": [ 875 | { 876 | "data": { 877 | "text/plain": [ 878 | "array([[ 4, 3, 2, 1, 0],\n", 879 | " [14, 13, 12, 11, 10],\n", 880 | " [24, 23, 22, 21, 20],\n", 881 | " [34, 33, 32, 31, 30],\n", 882 | " [44, 43, 42, 41, 40]])" 883 | ] 884 | }, 885 | "execution_count": 81, 886 | "metadata": {}, 887 | "output_type": "execute_result" 888 | } 889 | ], 890 | "source": [ 891 | "# reverse diagonal\n", 892 | "A[:, ::-1]" 893 | ] 894 | }, 895 | { 896 | "cell_type": "code", 897 | "execution_count": 82, 898 | "metadata": {}, 899 | "outputs": [ 900 | { 901 | "data": { 902 | "text/plain": [ 903 | "array([ 4, 13, 22, 31, 40])" 904 | ] 905 | }, 906 | "execution_count": 82, 907 | "metadata": {}, 908 | "output_type": "execute_result" 909 | } 910 | ], 911 | "source": [ 912 | "np.diag(A[:, ::-1])" 913 | ] 914 | }, 915 | { 916 | "cell_type": "markdown", 917 | "metadata": {}, 918 | "source": [ 919 | "### Take" 920 | ] 921 | }, 922 | { 923 | "cell_type": "code", 924 | "execution_count": 83, 925 | "metadata": {}, 926 | "outputs": [ 927 | { 928 | "data": { 929 | "text/plain": [ 930 | "array([-3, -2, -1, 0, 1, 2])" 931 | ] 932 | }, 933 | "execution_count": 83, 934 | "metadata": {}, 935 | "output_type": "execute_result" 936 | } 937 | ], 938 | "source": [ 939 | "v = np.arange(-3,3)\n", 940 | "v" 941 | ] 942 | }, 943 | { 944 | "cell_type": "markdown", 945 | "metadata": {}, 946 | "source": [ 947 | "### indexing via a list" 948 | ] 949 | }, 950 | { 951 | "cell_type": "code", 952 | "execution_count": 84, 953 | "metadata": {}, 954 | "outputs": [ 955 | { 956 | "data": { 957 | "text/plain": [ 958 | "array([-2, 0, 2])" 959 | ] 960 | }, 961 | "execution_count": 84, 962 | "metadata": {}, 963 | "output_type": "execute_result" 964 | } 965 | ], 966 | "source": [ 967 | "row_indices = [1, 3, 5]\n", 968 | "v[row_indices]" 969 | ] 970 | }, 971 | { 972 | "cell_type": "markdown", 973 | "metadata": {}, 974 | "source": [ 975 | "### Doesn’t work with List" 976 | ] 977 | }, 978 | { 979 | "cell_type": "code", 980 | "execution_count": 85, 981 | "metadata": {}, 982 | "outputs": [ 983 | { 984 | "ename": "TypeError", 985 | "evalue": "list indices must be integers or slices, not list", 986 | "output_type": "error", 987 | "traceback": [ 988 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 989 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 990 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\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;34m[\u001b[0m\u001b[0mrow_indices\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 991 | "\u001b[0;31mTypeError\u001b[0m: list indices must be integers or slices, not list" 992 | ] 993 | } 994 | ], 995 | "source": [ 996 | "[-3, -2, -1, 0, 1, 2][row_indices]" 997 | ] 998 | }, 999 | { 1000 | "cell_type": "markdown", 1001 | "metadata": {}, 1002 | "source": [ 1003 | "Works like a charm!" 1004 | ] 1005 | }, 1006 | { 1007 | "cell_type": "code", 1008 | "execution_count": 87, 1009 | "metadata": {}, 1010 | "outputs": [ 1011 | { 1012 | "data": { 1013 | "text/plain": [ 1014 | "array([-2, 0, 2])" 1015 | ] 1016 | }, 1017 | "execution_count": 87, 1018 | "metadata": {}, 1019 | "output_type": "execute_result" 1020 | } 1021 | ], 1022 | "source": [ 1023 | "np.take([-3, -2, -1, 0, 1, 2], row_indices)" 1024 | ] 1025 | }, 1026 | { 1027 | "cell_type": "markdown", 1028 | "metadata": {}, 1029 | "source": [ 1030 | "## Linear Algebra\n", 1031 | "### Elementwise-array operations\n", 1032 | "Arithmetic operators on arrays apply elementwise. A new array is created and filled with the result.\n", 1033 | "Elementwise addition; both produce the array" 1034 | ] 1035 | }, 1036 | { 1037 | "cell_type": "code", 1038 | "execution_count": 88, 1039 | "metadata": {}, 1040 | "outputs": [ 1041 | { 1042 | "name": "stdout", 1043 | "output_type": "stream", 1044 | "text": [ 1045 | "[[ 6. 8.]\n", 1046 | " [ 10. 12.]] \n", 1047 | "\n", 1048 | "[[ 6. 8.]\n", 1049 | " [ 10. 12.]]\n" 1050 | ] 1051 | } 1052 | ], 1053 | "source": [ 1054 | "x = np.array([[1,2],[3,4]], dtype=np.float64)\n", 1055 | "y = np.array([[5,6],[7,8]], dtype=np.float64)\n", 1056 | "print(x + y, \"\\n\")\n", 1057 | "print(np.add(x, y))" 1058 | ] 1059 | }, 1060 | { 1061 | "cell_type": "code", 1062 | "execution_count": 89, 1063 | "metadata": {}, 1064 | "outputs": [ 1065 | { 1066 | "name": "stdout", 1067 | "output_type": "stream", 1068 | "text": [ 1069 | "[[-4. -4.]\n", 1070 | " [-4. -4.]] \n", 1071 | "\n", 1072 | "[[-4. -4.]\n", 1073 | " [-4. -4.]]\n" 1074 | ] 1075 | } 1076 | ], 1077 | "source": [ 1078 | "#Elementwise difference; both produce the array\n", 1079 | "print(x - y, \"\\n\")\n", 1080 | "print(np.subtract(x, y))" 1081 | ] 1082 | }, 1083 | { 1084 | "cell_type": "code", 1085 | "execution_count": 90, 1086 | "metadata": {}, 1087 | "outputs": [ 1088 | { 1089 | "name": "stdout", 1090 | "output_type": "stream", 1091 | "text": [ 1092 | "[[ 5. 12.]\n", 1093 | " [ 21. 32.]] \n", 1094 | "\n", 1095 | "[[ 5. 12.]\n", 1096 | " [ 21. 32.]]\n" 1097 | ] 1098 | } 1099 | ], 1100 | "source": [ 1101 | "# Elementwise product; both produce the array\n", 1102 | "print(x * y, \"\\n\")\n", 1103 | "print(np.multiply(x, y))" 1104 | ] 1105 | }, 1106 | { 1107 | "cell_type": "code", 1108 | "execution_count": 91, 1109 | "metadata": {}, 1110 | "outputs": [ 1111 | { 1112 | "name": "stdout", 1113 | "output_type": "stream", 1114 | "text": [ 1115 | "[[ 0.2 0.33333333]\n", 1116 | " [ 0.42857143 0.5 ]] \n", 1117 | "\n", 1118 | "[[ 0.2 0.33333333]\n", 1119 | " [ 0.42857143 0.5 ]]\n" 1120 | ] 1121 | } 1122 | ], 1123 | "source": [ 1124 | "#Elementwise division; both produce the array\n", 1125 | "print(x / y, \"\\n\")\n", 1126 | "print(np.divide(x, y))" 1127 | ] 1128 | }, 1129 | { 1130 | "cell_type": "code", 1131 | "execution_count": 92, 1132 | "metadata": {}, 1133 | "outputs": [ 1134 | { 1135 | "name": "stdout", 1136 | "output_type": "stream", 1137 | "text": [ 1138 | "Squaring...\n", 1139 | "\n", 1140 | "a: \n", 1141 | " [[1 2]\n", 1142 | " [3 4]\n", 1143 | " [5 6]]\n", 1144 | "\n", 1145 | "a**2: \n", 1146 | " [[ 1 4]\n", 1147 | " [ 9 16]\n", 1148 | " [25 36]]\n", 1149 | "\n", 1150 | "np.square(a): \n", 1151 | " [[ 1 4]\n", 1152 | " [ 9 16]\n", 1153 | " [25 36]]\n" 1154 | ] 1155 | } 1156 | ], 1157 | "source": [ 1158 | "#Other Useful Elementwise Operations\n", 1159 | "print(\"Squaring...\\n\")\n", 1160 | "print(\"a: \\n\", a)\n", 1161 | "print(\"\\na**2: \\n\", a**2)\n", 1162 | "print(\"\\nnp.square(a): \\n\", np.square(a))" 1163 | ] 1164 | }, 1165 | { 1166 | "cell_type": "markdown", 1167 | "metadata": {}, 1168 | "source": [ 1169 | "Same operation on python list raises an error." 1170 | ] 1171 | }, 1172 | { 1173 | "cell_type": "code", 1174 | "execution_count": 93, 1175 | "metadata": {}, 1176 | "outputs": [ 1177 | { 1178 | "ename": "TypeError", 1179 | "evalue": "unsupported operand type(s) for ** or pow(): 'list' and 'int'", 1180 | "output_type": "error", 1181 | "traceback": [ 1182 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 1183 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 1184 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mlist_a\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m11\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m12\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m13\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m14\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m15\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m16\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m17\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m18\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m19\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mlist_a\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 1185 | "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for ** or pow(): 'list' and 'int'" 1186 | ] 1187 | } 1188 | ], 1189 | "source": [ 1190 | "list_a = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n", 1191 | "list_a**2" 1192 | ] 1193 | }, 1194 | { 1195 | "cell_type": "code", 1196 | "execution_count": null, 1197 | "metadata": {}, 1198 | "outputs": [], 1199 | "source": [ 1200 | "print(\"exp(a):\\n\", np.exp(a))" 1201 | ] 1202 | }, 1203 | { 1204 | "cell_type": "code", 1205 | "execution_count": null, 1206 | "metadata": {}, 1207 | "outputs": [], 1208 | "source": [ 1209 | "print(\"a**1.2:\\n\", np.power(a, 1.2))" 1210 | ] 1211 | }, 1212 | { 1213 | "cell_type": "code", 1214 | "execution_count": null, 1215 | "metadata": {}, 1216 | "outputs": [], 1217 | "source": [ 1218 | "print(\"Natural logarithm: \\n\", np.log(a))\n", 1219 | "print(\"\\nBase10 logarithm: \\n\", np.log10(a))\n", 1220 | "print(\"\\nBase2 logarithm: \\n\", np.log2(a))" 1221 | ] 1222 | }, 1223 | { 1224 | "cell_type": "markdown", 1225 | "metadata": {}, 1226 | "source": [ 1227 | "## Vector Operations\n", 1228 | "We can use the usual arithmetic operators to multiply, add, subtract, and divide vectors with scalar\n", 1229 | "numbers." 1230 | ] 1231 | }, 1232 | { 1233 | "cell_type": "code", 1234 | "execution_count": null, 1235 | "metadata": {}, 1236 | "outputs": [], 1237 | "source": [ 1238 | "v1 = np.arange(0, 5)\n", 1239 | "v1" 1240 | ] 1241 | }, 1242 | { 1243 | "cell_type": "code", 1244 | "execution_count": null, 1245 | "metadata": {}, 1246 | "outputs": [], 1247 | "source": [ 1248 | "print(v1 * 2)\n", 1249 | "print(v1 / 2)\n", 1250 | "print(v1 ** 2)\n", 1251 | "print(v1 * v1)" 1252 | ] 1253 | }, 1254 | { 1255 | "cell_type": "markdown", 1256 | "metadata": {}, 1257 | "source": [ 1258 | "Inner Product" 1259 | ] 1260 | }, 1261 | { 1262 | "cell_type": "code", 1263 | "execution_count": null, 1264 | "metadata": { 1265 | "collapsed": true 1266 | }, 1267 | "outputs": [], 1268 | "source": [ 1269 | "v2 = np.arange(5, 10)\n", 1270 | "v2" 1271 | ] 1272 | }, 1273 | { 1274 | "cell_type": "code", 1275 | "execution_count": null, 1276 | "metadata": {}, 1277 | "outputs": [], 1278 | "source": [ 1279 | "\n", 1280 | "v1 = [0, 1, 2, 3, 4]\n", 1281 | "v2 = [5, 6, 7, 8, 9]\n", 1282 | "#v1 . v2 = 0∗5+1 ∗ 6 + 2 ∗ 7 + 3 ∗ 8 + 4 ∗ 9\n", 1283 | "np.dot(v1, v2)" 1284 | ] 1285 | }, 1286 | { 1287 | "cell_type": "markdown", 1288 | "metadata": {}, 1289 | "source": [ 1290 | "Vector Magnitude (self inner product)" 1291 | ] 1292 | }, 1293 | { 1294 | "cell_type": "code", 1295 | "execution_count": null, 1296 | "metadata": {}, 1297 | "outputs": [], 1298 | "source": [ 1299 | "sum = 0\n", 1300 | "for each element in vector:\n", 1301 | " sum += element * element" 1302 | ] 1303 | }, 1304 | { 1305 | "cell_type": "code", 1306 | "execution_count": null, 1307 | "metadata": {}, 1308 | "outputs": [], 1309 | "source": [ 1310 | "np.sum([element*element for element in v1])" 1311 | ] 1312 | }, 1313 | { 1314 | "cell_type": "code", 1315 | "execution_count": null, 1316 | "metadata": {}, 1317 | "outputs": [], 1318 | "source": [ 1319 | "## should work\n", 1320 | "print(v1 @ v1)" 1321 | ] 1322 | }, 1323 | { 1324 | "cell_type": "markdown", 1325 | "metadata": {}, 1326 | "source": [ 1327 | "## Matrix Algebra" 1328 | ] 1329 | }, 1330 | { 1331 | "cell_type": "code", 1332 | "execution_count": null, 1333 | "metadata": {}, 1334 | "outputs": [], 1335 | "source": [ 1336 | "A = np.array([[n+m*10 for n in range(5)] for m in range(5)])\n", 1337 | "A" 1338 | ] 1339 | }, 1340 | { 1341 | "cell_type": "markdown", 1342 | "metadata": {}, 1343 | "source": [ 1344 | "Transpose" 1345 | ] 1346 | }, 1347 | { 1348 | "cell_type": "code", 1349 | "execution_count": null, 1350 | "metadata": {}, 1351 | "outputs": [], 1352 | "source": [ 1353 | "A.T" 1354 | ] 1355 | }, 1356 | { 1357 | "cell_type": "markdown", 1358 | "metadata": {}, 1359 | "source": [ 1360 | "## Matrix-Vector Multiplication" 1361 | ] 1362 | }, 1363 | { 1364 | "cell_type": "code", 1365 | "execution_count": null, 1366 | "metadata": {}, 1367 | "outputs": [], 1368 | "source": [ 1369 | "v1" 1370 | ] 1371 | }, 1372 | { 1373 | "cell_type": "markdown", 1374 | "metadata": {}, 1375 | "source": [ 1376 | "v1 is multiplied to each row" 1377 | ] 1378 | }, 1379 | { 1380 | "cell_type": "code", 1381 | "execution_count": null, 1382 | "metadata": {}, 1383 | "outputs": [], 1384 | "source": [ 1385 | "A * v1" 1386 | ] 1387 | }, 1388 | { 1389 | "cell_type": "code", 1390 | "execution_count": null, 1391 | "metadata": {}, 1392 | "outputs": [], 1393 | "source": [ 1394 | "# Elementwise Matrix Multiplication\n", 1395 | "A * A" 1396 | ] 1397 | }, 1398 | { 1399 | "cell_type": "code", 1400 | "execution_count": null, 1401 | "metadata": {}, 1402 | "outputs": [], 1403 | "source": [ 1404 | "#Matrix Multiplication\n", 1405 | "A.dot(A)" 1406 | ] 1407 | }, 1408 | { 1409 | "cell_type": "code", 1410 | "execution_count": null, 1411 | "metadata": {}, 1412 | "outputs": [], 1413 | "source": [ 1414 | "A@A" 1415 | ] 1416 | }, 1417 | { 1418 | "cell_type": "markdown", 1419 | "metadata": {}, 1420 | "source": [ 1421 | "Alternatively we can cast the array to Matrix , which enables normal arithmatic opertions to perform\n", 1422 | "matrix algebra." 1423 | ] 1424 | }, 1425 | { 1426 | "cell_type": "code", 1427 | "execution_count": null, 1428 | "metadata": {}, 1429 | "outputs": [], 1430 | "source": [ 1431 | "A_mat = np.matrix(A)\n", 1432 | "v = np.matrix(v1).T # make it a column vector\n", 1433 | "print(\"Matrix A:\\n\", A_mat)\n", 1434 | "print(\"\\nVector v:\\n\",v)" 1435 | ] 1436 | }, 1437 | { 1438 | "cell_type": "code", 1439 | "execution_count": null, 1440 | "metadata": {}, 1441 | "outputs": [], 1442 | "source": [ 1443 | "type(A_mat)" 1444 | ] 1445 | }, 1446 | { 1447 | "cell_type": "code", 1448 | "execution_count": null, 1449 | "metadata": {}, 1450 | "outputs": [], 1451 | "source": [ 1452 | "A_mat * A_mat" 1453 | ] 1454 | }, 1455 | { 1456 | "cell_type": "code", 1457 | "execution_count": null, 1458 | "metadata": {}, 1459 | "outputs": [], 1460 | "source": [ 1461 | "v.T * A_mat" 1462 | ] 1463 | }, 1464 | { 1465 | "cell_type": "code", 1466 | "execution_count": null, 1467 | "metadata": {}, 1468 | "outputs": [], 1469 | "source": [ 1470 | "A_mat * v" 1471 | ] 1472 | }, 1473 | { 1474 | "cell_type": "code", 1475 | "execution_count": null, 1476 | "metadata": {}, 1477 | "outputs": [], 1478 | "source": [ 1479 | "#If we try to add, subtract or multiply objects with incomplatible shapes we get an error:\n", 1480 | "v = np.matrix([1,2,3,4]).T\n", 1481 | "A_mat.shape, v.shape" 1482 | ] 1483 | }, 1484 | { 1485 | "cell_type": "code", 1486 | "execution_count": null, 1487 | "metadata": {}, 1488 | "outputs": [], 1489 | "source": [ 1490 | "## This should pop an error \n", 1491 | "A_mat * v" 1492 | ] 1493 | }, 1494 | { 1495 | "cell_type": "markdown", 1496 | "metadata": {}, 1497 | "source": [ 1498 | "## Other Useful Functions\n", 1499 | "Sum" 1500 | ] 1501 | }, 1502 | { 1503 | "cell_type": "code", 1504 | "execution_count": null, 1505 | "metadata": {}, 1506 | "outputs": [], 1507 | "source": [ 1508 | "A" 1509 | ] 1510 | }, 1511 | { 1512 | "cell_type": "code", 1513 | "execution_count": null, 1514 | "metadata": {}, 1515 | "outputs": [], 1516 | "source": [ 1517 | "A.sum()" 1518 | ] 1519 | }, 1520 | { 1521 | "cell_type": "code", 1522 | "execution_count": null, 1523 | "metadata": {}, 1524 | "outputs": [], 1525 | "source": [ 1526 | "# Column-wise sum or Reduce by row\n", 1527 | "A.sum(axis=0)" 1528 | ] 1529 | }, 1530 | { 1531 | "cell_type": "code", 1532 | "execution_count": null, 1533 | "metadata": {}, 1534 | "outputs": [], 1535 | "source": [ 1536 | "# Row-wise sum or Reduce by column\n", 1537 | "A.sum(axis=1)" 1538 | ] 1539 | }, 1540 | { 1541 | "cell_type": "markdown", 1542 | "metadata": {}, 1543 | "source": [ 1544 | "### Statistics" 1545 | ] 1546 | }, 1547 | { 1548 | "cell_type": "markdown", 1549 | "metadata": {}, 1550 | "source": [ 1551 | "Mean" 1552 | ] 1553 | }, 1554 | { 1555 | "cell_type": "code", 1556 | "execution_count": null, 1557 | "metadata": {}, 1558 | "outputs": [], 1559 | "source": [ 1560 | "print(\"Mean of A: \\n\", A.mean())\n", 1561 | "print(\"Column-wise mean of A: \\n\", A.mean(axis=0))\n", 1562 | "print(\"Row-wise mean of A: \\n\", A.mean(axis=1))" 1563 | ] 1564 | }, 1565 | { 1566 | "cell_type": "code", 1567 | "execution_count": null, 1568 | "metadata": {}, 1569 | "outputs": [], 1570 | "source": [ 1571 | "# Variance\n", 1572 | "print(\"Variance of A: \\n\", A.var())\n", 1573 | "print(\"Column-wise variance of A: \\n\", A.var(axis=0))\n", 1574 | "print(\"Row-wise variance of A: \\n\", A.var(axis=1))" 1575 | ] 1576 | }, 1577 | { 1578 | "cell_type": "code", 1579 | "execution_count": null, 1580 | "metadata": {}, 1581 | "outputs": [], 1582 | "source": [ 1583 | "# Standard deviation\n", 1584 | "print(\"Standard Deviation of A: \\n\", A.std())\n", 1585 | "print(\"Column-wise Standard Deviation of A: \\n\", A.std(axis=0))\n", 1586 | "print(\"Row-wise Standard Deviation of A: \\n\", A.std(axis=1))" 1587 | ] 1588 | }, 1589 | { 1590 | "cell_type": "code", 1591 | "execution_count": null, 1592 | "metadata": {}, 1593 | "outputs": [], 1594 | "source": [ 1595 | "#Min and Max\n", 1596 | "print(\"Minimum of A: \\n\", A.min())\n", 1597 | "print(\"Column-wise Minimum of A: \\n\", A.min(axis=0))\n", 1598 | "print(\"Row-wise Minimum of A: \\n\", A.min(axis=1))" 1599 | ] 1600 | }, 1601 | { 1602 | "cell_type": "code", 1603 | "execution_count": null, 1604 | "metadata": {}, 1605 | "outputs": [], 1606 | "source": [ 1607 | "print(\"Maximum of A: \\n\", A.max())\n", 1608 | "print(\"Column-wise Maximum of A: \\n\", A.max(axis=0))\n", 1609 | "print(\"Row-wise Maximum of A: \\n\", A.max(axis=1))" 1610 | ] 1611 | }, 1612 | { 1613 | "cell_type": "markdown", 1614 | "metadata": {}, 1615 | "source": [ 1616 | "## Broadcasting\n", 1617 | "source: Justin Johnson (http://cs.stanford.edu/people/jcjohns/)\n", 1618 | "\n", 1619 | "Broadcasting is a powerful mechanism that allows numpy to work with arrays of different shapes when\n", 1620 | "performing arithmetic operations.\n", 1621 | "For example, suppose that we want to add a constant vector to each row of a matrix. We could do it like\n", 1622 | "this:" 1623 | ] 1624 | }, 1625 | { 1626 | "cell_type": "code", 1627 | "execution_count": 94, 1628 | "metadata": {}, 1629 | "outputs": [ 1630 | { 1631 | "name": "stdout", 1632 | "output_type": "stream", 1633 | "text": [ 1634 | "X: \n", 1635 | " [[1 2]\n", 1636 | " [3 4]\n", 1637 | " [5 6]\n", 1638 | " [7 8]]\n", 1639 | "\n", 1640 | "v:\n", 1641 | " [1 2]\n" 1642 | ] 1643 | } 1644 | ], 1645 | "source": [ 1646 | "X = np.array([[1,2],[3, 4] ,[5,6], [7,8]])\n", 1647 | "v = np.array([1, 2])\n", 1648 | "print(\"X: \\n\", X)\n", 1649 | "print(\"\\nv:\\n\", v)" 1650 | ] 1651 | }, 1652 | { 1653 | "cell_type": "markdown", 1654 | "metadata": {}, 1655 | "source": [ 1656 | "We can add the vector v to each row of the matrix x, storing the result in the matrix y" 1657 | ] 1658 | }, 1659 | { 1660 | "cell_type": "code", 1661 | "execution_count": 95, 1662 | "metadata": {}, 1663 | "outputs": [ 1664 | { 1665 | "name": "stdout", 1666 | "output_type": "stream", 1667 | "text": [ 1668 | "[[ 2 4]\n", 1669 | " [ 4 6]\n", 1670 | " [ 6 8]\n", 1671 | " [ 8 10]]\n" 1672 | ] 1673 | } 1674 | ], 1675 | "source": [ 1676 | "Y = np.zeros_like(X)\n", 1677 | "# Add the vector v to each row of the matrix x with an explicit loop\n", 1678 | "for i in range(4):\n", 1679 | " Y[i, :] = X[i, :] + v\n", 1680 | "print(Y)" 1681 | ] 1682 | }, 1683 | { 1684 | "cell_type": "markdown", 1685 | "metadata": {}, 1686 | "source": [ 1687 | "Adding v to every row of matrix X is equivalent to form a matrix vv by stacking multiple copies of v\n", 1688 | "vertically, then performing elementwise summation of X and vv" 1689 | ] 1690 | }, 1691 | { 1692 | "cell_type": "code", 1693 | "execution_count": 96, 1694 | "metadata": {}, 1695 | "outputs": [ 1696 | { 1697 | "name": "stdout", 1698 | "output_type": "stream", 1699 | "text": [ 1700 | "Stacked vectors: \n", 1701 | " [[1 2]\n", 1702 | " [1 2]\n", 1703 | " [1 2]\n", 1704 | " [1 2]]\n", 1705 | "\n", 1706 | "Result: \n", 1707 | " [[ 2 4]\n", 1708 | " [ 4 6]\n", 1709 | " [ 6 8]\n", 1710 | " [ 8 10]]\n" 1711 | ] 1712 | } 1713 | ], 1714 | "source": [ 1715 | "vv = np.tile(v, (4, 1)) # stack four rows of v\n", 1716 | "print(\"Stacked vectors: \\n\", vv)\n", 1717 | "Y = X + vv # Add x and vv elementwise\n", 1718 | "print(\"\\nResult: \\n\", Y)" 1719 | ] 1720 | }, 1721 | { 1722 | "cell_type": "markdown", 1723 | "metadata": {}, 1724 | "source": [ 1725 | "Numpy broadcasting allows us to perform this computation without actually creating multiple copies of v .\n", 1726 | "Subject to certain constraints, the smaller array is broadcast across the larger array so that they have\n", 1727 | "compatible shapes." 1728 | ] 1729 | }, 1730 | { 1731 | "cell_type": "code", 1732 | "execution_count": 97, 1733 | "metadata": {}, 1734 | "outputs": [ 1735 | { 1736 | "name": "stdout", 1737 | "output_type": "stream", 1738 | "text": [ 1739 | "[[ 2 4]\n", 1740 | " [ 4 6]\n", 1741 | " [ 6 8]\n", 1742 | " [ 8 10]]\n" 1743 | ] 1744 | } 1745 | ], 1746 | "source": [ 1747 | "Y = X + v # Add v to each row of x using broadcasting\n", 1748 | "print(Y)" 1749 | ] 1750 | }, 1751 | { 1752 | "cell_type": "markdown", 1753 | "metadata": {}, 1754 | "source": [ 1755 | "Broadcasting two arrays together follows these rules:\n", 1756 | "1. If the arrays do not have the same rank, prepend the shape of the lower rank array with 1s until both\n", 1757 | "shapes have the same length.\n", 1758 | "2. The two arrays are said to be compatible in a dimension if they have the same size in the dimension,\n", 1759 | "or if one of the arrays has size 1 in that dimension.\n", 1760 | "3. The arrays can be broadcast together if they are compatible in all dimensions.\n", 1761 | "4. After broadcasting, each array behaves as if it had shape equal to the elementwise maximum of\n", 1762 | "shapes of the two input arrays.\n", 1763 | "5. In any dimension where one array had size 1 and the other array had size greater than 1, the first\n", 1764 | "array behaves as if it were copied along that dimension\n", 1765 | "For more on broadcasting please read Eric’s Broadcasting Docs (http://scipy.github.io/oldwiki/pages/EricsBroadcastingDoc)\n", 1766 | "or the documentation\n", 1767 | "(https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html)" 1768 | ] 1769 | }, 1770 | { 1771 | "cell_type": "code", 1772 | "execution_count": 98, 1773 | "metadata": {}, 1774 | "outputs": [ 1775 | { 1776 | "name": "stdout", 1777 | "output_type": "stream", 1778 | "text": [ 1779 | "Rank of v: 1\n" 1780 | ] 1781 | }, 1782 | { 1783 | "data": { 1784 | "text/plain": [ 1785 | "array([[2, 3],\n", 1786 | " [4, 5],\n", 1787 | " [6, 7],\n", 1788 | " [8, 9]])" 1789 | ] 1790 | }, 1791 | "execution_count": 98, 1792 | "metadata": {}, 1793 | "output_type": "execute_result" 1794 | } 1795 | ], 1796 | "source": [ 1797 | "v = np.array([1])\n", 1798 | "print(\"Rank of v: \", v.ndim)\n", 1799 | "X + v" 1800 | ] 1801 | }, 1802 | { 1803 | "cell_type": "markdown", 1804 | "metadata": {}, 1805 | "source": [ 1806 | "Compute outer product of vectors\n", 1807 | "To compute an outer product, we first reshape v to be a column vector of shape (3, 1) . We can then\n", 1808 | "broadcast it against w to yield an output of shape (3, 2) , which is the outer product of v and w :" 1809 | ] 1810 | }, 1811 | { 1812 | "cell_type": "code", 1813 | "execution_count": 99, 1814 | "metadata": {}, 1815 | "outputs": [ 1816 | { 1817 | "name": "stdout", 1818 | "output_type": "stream", 1819 | "text": [ 1820 | "[[ 4 5]\n", 1821 | " [ 8 10]\n", 1822 | " [12 15]]\n" 1823 | ] 1824 | } 1825 | ], 1826 | "source": [ 1827 | "v = np.array([1,2,3]) # v has shape (3,)\n", 1828 | "w = np.array([4,5]) # w has shape (2,)\n", 1829 | "print(np.reshape(v, (3, 1)) * w)" 1830 | ] 1831 | }, 1832 | { 1833 | "cell_type": "markdown", 1834 | "metadata": {}, 1835 | "source": [ 1836 | "## Array Reshape, Concatenation, Stacking and Copy\n", 1837 | "### Reshape" 1838 | ] 1839 | }, 1840 | { 1841 | "cell_type": "code", 1842 | "execution_count": 100, 1843 | "metadata": {}, 1844 | "outputs": [ 1845 | { 1846 | "data": { 1847 | "text/plain": [ 1848 | "array([[ 0, 1, 2, 3, 4],\n", 1849 | " [10, 11, 12, 13, 14],\n", 1850 | " [20, 21, 22, 23, 24],\n", 1851 | " [30, 31, 32, 33, 34],\n", 1852 | " [40, 41, 42, 43, 44]])" 1853 | ] 1854 | }, 1855 | "execution_count": 100, 1856 | "metadata": {}, 1857 | "output_type": "execute_result" 1858 | } 1859 | ], 1860 | "source": [ 1861 | "A = np.array([[n+m*10 for n in range(5)] for m in range(5)])\n", 1862 | "A" 1863 | ] 1864 | }, 1865 | { 1866 | "cell_type": "code", 1867 | "execution_count": 101, 1868 | "metadata": {}, 1869 | "outputs": [ 1870 | { 1871 | "name": "stdout", 1872 | "output_type": "stream", 1873 | "text": [ 1874 | "(1, 25)\n", 1875 | "[[ 0 1 2 3 4 10 11 12 13 14 20 21 22 23 24 30 31 32 33 34 40 41 42 43\n", 1876 | " 44]]\n" 1877 | ] 1878 | } 1879 | ], 1880 | "source": [ 1881 | "m, n = A.shape\n", 1882 | "B = A.reshape((1, m*n))\n", 1883 | "print(B.shape)\n", 1884 | "print(B)" 1885 | ] 1886 | }, 1887 | { 1888 | "cell_type": "code", 1889 | "execution_count": 102, 1890 | "metadata": {}, 1891 | "outputs": [], 1892 | "source": [ 1893 | "B[0, 0:5] = -1" 1894 | ] 1895 | }, 1896 | { 1897 | "cell_type": "code", 1898 | "execution_count": 103, 1899 | "metadata": {}, 1900 | "outputs": [ 1901 | { 1902 | "data": { 1903 | "text/plain": [ 1904 | "array([[-1, -1, -1, -1, -1],\n", 1905 | " [10, 11, 12, 13, 14],\n", 1906 | " [20, 21, 22, 23, 24],\n", 1907 | " [30, 31, 32, 33, 34],\n", 1908 | " [40, 41, 42, 43, 44]])" 1909 | ] 1910 | }, 1911 | "execution_count": 103, 1912 | "metadata": {}, 1913 | "output_type": "execute_result" 1914 | } 1915 | ], 1916 | "source": [ 1917 | "A" 1918 | ] 1919 | }, 1920 | { 1921 | "cell_type": "code", 1922 | "execution_count": 104, 1923 | "metadata": {}, 1924 | "outputs": [ 1925 | { 1926 | "name": "stdout", 1927 | "output_type": "stream", 1928 | "text": [ 1929 | "(25,)\n", 1930 | "[-1 -1 -1 -1 -1 10 11 12 13 14 20 21 22 23 24 30 31 32 33 34 40 41 42 43 44]\n" 1931 | ] 1932 | } 1933 | ], 1934 | "source": [ 1935 | "B = A.flatten()\n", 1936 | "print(B.shape)\n", 1937 | "print(B)" 1938 | ] 1939 | }, 1940 | { 1941 | "cell_type": "code", 1942 | "execution_count": 105, 1943 | "metadata": {}, 1944 | "outputs": [ 1945 | { 1946 | "data": { 1947 | "text/plain": [ 1948 | "array([10, 10, 10, 10, 10, 10, 11, 12, 13, 14, 20, 21, 22, 23, 24, 30, 31,\n", 1949 | " 32, 33, 34, 40, 41, 42, 43, 44])" 1950 | ] 1951 | }, 1952 | "execution_count": 105, 1953 | "metadata": {}, 1954 | "output_type": "execute_result" 1955 | } 1956 | ], 1957 | "source": [ 1958 | "B[0:5] = 10\n", 1959 | "B" 1960 | ] 1961 | }, 1962 | { 1963 | "cell_type": "code", 1964 | "execution_count": 106, 1965 | "metadata": {}, 1966 | "outputs": [ 1967 | { 1968 | "data": { 1969 | "text/plain": [ 1970 | "array([[-1, -1, -1, -1, -1],\n", 1971 | " [10, 11, 12, 13, 14],\n", 1972 | " [20, 21, 22, 23, 24],\n", 1973 | " [30, 31, 32, 33, 34],\n", 1974 | " [40, 41, 42, 43, 44]])" 1975 | ] 1976 | }, 1977 | "execution_count": 106, 1978 | "metadata": {}, 1979 | "output_type": "execute_result" 1980 | } 1981 | ], 1982 | "source": [ 1983 | "A" 1984 | ] 1985 | }, 1986 | { 1987 | "cell_type": "markdown", 1988 | "metadata": {}, 1989 | "source": [ 1990 | "### Concatenation and Stacking\n", 1991 | "Join a sequence of arrays along an existing axis." 1992 | ] 1993 | }, 1994 | { 1995 | "cell_type": "code", 1996 | "execution_count": 107, 1997 | "metadata": {}, 1998 | "outputs": [ 1999 | { 2000 | "data": { 2001 | "text/plain": [ 2002 | "array([[1, 2],\n", 2003 | " [3, 4],\n", 2004 | " [5, 6]])" 2005 | ] 2006 | }, 2007 | "execution_count": 107, 2008 | "metadata": {}, 2009 | "output_type": "execute_result" 2010 | } 2011 | ], 2012 | "source": [ 2013 | "a = np.array([[1, 2], [3, 4]])\n", 2014 | "b = np.array([[5, 6]])\n", 2015 | "np.concatenate((a, b), axis=0)" 2016 | ] 2017 | }, 2018 | { 2019 | "cell_type": "code", 2020 | "execution_count": 108, 2021 | "metadata": {}, 2022 | "outputs": [ 2023 | { 2024 | "data": { 2025 | "text/plain": [ 2026 | "array([[1, 2, 5],\n", 2027 | " [3, 4, 6]])" 2028 | ] 2029 | }, 2030 | "execution_count": 108, 2031 | "metadata": {}, 2032 | "output_type": "execute_result" 2033 | } 2034 | ], 2035 | "source": [ 2036 | "np.concatenate((a, b.T), axis=1)" 2037 | ] 2038 | }, 2039 | { 2040 | "cell_type": "code", 2041 | "execution_count": 109, 2042 | "metadata": {}, 2043 | "outputs": [ 2044 | { 2045 | "data": { 2046 | "text/plain": [ 2047 | "array([[1, 2],\n", 2048 | " [3, 4],\n", 2049 | " [5, 6]])" 2050 | ] 2051 | }, 2052 | "execution_count": 109, 2053 | "metadata": {}, 2054 | "output_type": "execute_result" 2055 | } 2056 | ], 2057 | "source": [ 2058 | "np.vstack((a,b))" 2059 | ] 2060 | }, 2061 | { 2062 | "cell_type": "code", 2063 | "execution_count": 110, 2064 | "metadata": {}, 2065 | "outputs": [ 2066 | { 2067 | "data": { 2068 | "text/plain": [ 2069 | "array([[1, 2, 5],\n", 2070 | " [3, 4, 6]])" 2071 | ] 2072 | }, 2073 | "execution_count": 110, 2074 | "metadata": {}, 2075 | "output_type": "execute_result" 2076 | } 2077 | ], 2078 | "source": [ 2079 | "np.hstack((a,b.T))" 2080 | ] 2081 | }, 2082 | { 2083 | "cell_type": "markdown", 2084 | "metadata": {}, 2085 | "source": [ 2086 | "### Copy\n", 2087 | "To achieve high performance, assignments in Python usually do not copy the underlaying objects. This is\n", 2088 | "important for example when objects are passed between functions, to avoid an excessive amount of\n", 2089 | "memory copying when it is not necessary (technical term: pass by reference)." 2090 | ] 2091 | }, 2092 | { 2093 | "cell_type": "code", 2094 | "execution_count": 111, 2095 | "metadata": {}, 2096 | "outputs": [ 2097 | { 2098 | "data": { 2099 | "text/plain": [ 2100 | "array([[1, 2],\n", 2101 | " [3, 4]])" 2102 | ] 2103 | }, 2104 | "execution_count": 111, 2105 | "metadata": {}, 2106 | "output_type": "execute_result" 2107 | } 2108 | ], 2109 | "source": [ 2110 | "A = np.array([[1, 2], [3, 4]])\n", 2111 | "A" 2112 | ] 2113 | }, 2114 | { 2115 | "cell_type": "code", 2116 | "execution_count": 112, 2117 | "metadata": {}, 2118 | "outputs": [ 2119 | { 2120 | "data": { 2121 | "text/plain": [ 2122 | "array([[10, 2],\n", 2123 | " [ 3, 4]])" 2124 | ] 2125 | }, 2126 | "execution_count": 112, 2127 | "metadata": {}, 2128 | "output_type": "execute_result" 2129 | } 2130 | ], 2131 | "source": [ 2132 | "# now B is referring to the same array data as A\n", 2133 | "B = A\n", 2134 | "# changing B affects A\n", 2135 | "B[0,0] = 10\n", 2136 | "B" 2137 | ] 2138 | }, 2139 | { 2140 | "cell_type": "code", 2141 | "execution_count": 113, 2142 | "metadata": {}, 2143 | "outputs": [ 2144 | { 2145 | "data": { 2146 | "text/plain": [ 2147 | "array([[10, 2],\n", 2148 | " [ 3, 4]])" 2149 | ] 2150 | }, 2151 | "execution_count": 113, 2152 | "metadata": {}, 2153 | "output_type": "execute_result" 2154 | } 2155 | ], 2156 | "source": [ 2157 | "A" 2158 | ] 2159 | }, 2160 | { 2161 | "cell_type": "markdown", 2162 | "metadata": {}, 2163 | "source": [ 2164 | "If we want to avoid this behavior, so that when we get a new completely independent object B copied\n", 2165 | "from A , then we need to do a so-called “deep copy” using the function copy :" 2166 | ] 2167 | }, 2168 | { 2169 | "cell_type": "code", 2170 | "execution_count": 114, 2171 | "metadata": {}, 2172 | "outputs": [ 2173 | { 2174 | "data": { 2175 | "text/plain": [ 2176 | "array([[-5, 2],\n", 2177 | " [ 3, 4]])" 2178 | ] 2179 | }, 2180 | "execution_count": 114, 2181 | "metadata": {}, 2182 | "output_type": "execute_result" 2183 | } 2184 | ], 2185 | "source": [ 2186 | "B = A.copy()\n", 2187 | "# now, if we modify B, A is not affected\n", 2188 | "B[0,0] = -5\n", 2189 | "B" 2190 | ] 2191 | }, 2192 | { 2193 | "cell_type": "code", 2194 | "execution_count": 115, 2195 | "metadata": {}, 2196 | "outputs": [ 2197 | { 2198 | "data": { 2199 | "text/plain": [ 2200 | "array([[10, 2],\n", 2201 | " [ 3, 4]])" 2202 | ] 2203 | }, 2204 | "execution_count": 115, 2205 | "metadata": {}, 2206 | "output_type": "execute_result" 2207 | } 2208 | ], 2209 | "source": [ 2210 | "A" 2211 | ] 2212 | }, 2213 | { 2214 | "cell_type": "markdown", 2215 | "metadata": {}, 2216 | "source": [ 2217 | "Further Reading\n", 2218 | "- http://numpy.scipy.org (http://numpy.scipy.org)\n", 2219 | "- http://scipy.org/Tentative_NumPy_Tutorial (http://scipy.org/Tentative_NumPy_Tutorial)\n", 2220 | "- http://scipy.org/NumPy_for_Matlab_Users (http://scipy.org/NumPy_for_Matlab_Users) - A Numpy\n", 2221 | "guide for MATLAB users" 2222 | ] 2223 | } 2224 | ], 2225 | "metadata": { 2226 | "kernelspec": { 2227 | "display_name": "Python [conda root]", 2228 | "language": "python", 2229 | "name": "conda-root-py" 2230 | }, 2231 | "language_info": { 2232 | "codemirror_mode": { 2233 | "name": "ipython", 2234 | "version": 3 2235 | }, 2236 | "file_extension": ".py", 2237 | "mimetype": "text/x-python", 2238 | "name": "python", 2239 | "nbconvert_exporter": "python", 2240 | "pygments_lexer": "ipython3", 2241 | "version": "3.5.3" 2242 | } 2243 | }, 2244 | "nbformat": 4, 2245 | "nbformat_minor": 2 2246 | } 2247 | -------------------------------------------------------------------------------- /Tutorials/Torch Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Pytorch tutorial\n", 8 | "http://pytorch.org/tutorials/beginner/blitz/tensor_tutorial.html#sphx-glr-beginner-blitz-tensor-tutorial-py\n", 9 | "\n", 10 | "To setup an initial understanding." 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 1, 16 | "metadata": { 17 | "collapsed": true 18 | }, 19 | "outputs": [], 20 | "source": [ 21 | "from __future__ import print_function\n", 22 | "import torch" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "Construct a 5x3 matrix, uninitialized:" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 2, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "name": "stdout", 39 | "output_type": "stream", 40 | "text": [ 41 | "\n", 42 | " 0.0000e+00 0.0000e+00 -7.4668e+00\n", 43 | " 4.5673e-41 -5.3806e-02 4.5673e-41\n", 44 | "-2.6815e+06 4.5673e-41 -2.6815e+06\n", 45 | " 4.5673e-41 -3.2452e+06 4.5673e-41\n", 46 | "-3.1552e+06 4.5673e-41 -1.1177e-01\n", 47 | "[torch.FloatTensor of size 5x3]\n", 48 | "\n" 49 | ] 50 | } 51 | ], 52 | "source": [ 53 | "x = torch.Tensor(5, 3)\n", 54 | "print(x)" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "Construct a randomly initialized matrix" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 3, 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "\n", 74 | " 0.8180 0.1831 0.6580\n", 75 | " 0.3410 0.1037 0.8664\n", 76 | " 0.9746 0.2583 0.8823\n", 77 | " 0.7392 0.4272 0.1205\n", 78 | " 0.6209 0.0127 0.6182\n", 79 | "[torch.FloatTensor of size 5x3]\n", 80 | "\n" 81 | ] 82 | } 83 | ], 84 | "source": [ 85 | "x = torch.rand(5, 3)\n", 86 | "print(x)" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "Get its size" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 4, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "torch.Size([5, 3])\n" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "print(x.size())" 111 | ] 112 | }, 113 | { 114 | "cell_type": "markdown", 115 | "metadata": {}, 116 | "source": [ 117 | "### Operations" 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": { 123 | "collapsed": true 124 | }, 125 | "source": [ 126 | "There are multiple syntaxes for operations. Let’s see addition as an example" 127 | ] 128 | }, 129 | { 130 | "cell_type": "markdown", 131 | "metadata": {}, 132 | "source": [ 133 | "Addition: syntax 1" 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 5, 139 | "metadata": {}, 140 | "outputs": [ 141 | { 142 | "name": "stdout", 143 | "output_type": "stream", 144 | "text": [ 145 | "\n", 146 | " 1.2339 0.3642 1.2537\n", 147 | " 0.4079 0.3407 1.8247\n", 148 | " 1.7992 0.4524 1.6935\n", 149 | " 1.0790 0.5248 0.9662\n", 150 | " 1.1204 0.0545 1.3213\n", 151 | "[torch.FloatTensor of size 5x3]\n", 152 | "\n" 153 | ] 154 | } 155 | ], 156 | "source": [ 157 | "y = torch.rand(5, 3)\n", 158 | "print(x + y)" 159 | ] 160 | }, 161 | { 162 | "cell_type": "markdown", 163 | "metadata": {}, 164 | "source": [ 165 | "Addition: syntax 2" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 6, 171 | "metadata": {}, 172 | "outputs": [ 173 | { 174 | "name": "stdout", 175 | "output_type": "stream", 176 | "text": [ 177 | "\n", 178 | " 1.2339 0.3642 1.2537\n", 179 | " 0.4079 0.3407 1.8247\n", 180 | " 1.7992 0.4524 1.6935\n", 181 | " 1.0790 0.5248 0.9662\n", 182 | " 1.1204 0.0545 1.3213\n", 183 | "[torch.FloatTensor of size 5x3]\n", 184 | "\n" 185 | ] 186 | } 187 | ], 188 | "source": [ 189 | "print(torch.add(x, y))" 190 | ] 191 | }, 192 | { 193 | "cell_type": "markdown", 194 | "metadata": {}, 195 | "source": [ 196 | "Addition: giving an output tensor" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 7, 202 | "metadata": {}, 203 | "outputs": [ 204 | { 205 | "name": "stdout", 206 | "output_type": "stream", 207 | "text": [ 208 | "\n", 209 | " 1.2339 0.3642 1.2537\n", 210 | " 0.4079 0.3407 1.8247\n", 211 | " 1.7992 0.4524 1.6935\n", 212 | " 1.0790 0.5248 0.9662\n", 213 | " 1.1204 0.0545 1.3213\n", 214 | "[torch.FloatTensor of size 5x3]\n", 215 | "\n" 216 | ] 217 | } 218 | ], 219 | "source": [ 220 | "result = torch.Tensor(5, 3)\n", 221 | "## watch the out for result\n", 222 | "torch.add(x, y, out=result)\n", 223 | "print(result)" 224 | ] 225 | }, 226 | { 227 | "cell_type": "markdown", 228 | "metadata": {}, 229 | "source": [ 230 | "Addition: in-place" 231 | ] 232 | }, 233 | { 234 | "cell_type": "code", 235 | "execution_count": 8, 236 | "metadata": {}, 237 | "outputs": [ 238 | { 239 | "name": "stdout", 240 | "output_type": "stream", 241 | "text": [ 242 | "\n", 243 | " 1.2339 0.3642 1.2537\n", 244 | " 0.4079 0.3407 1.8247\n", 245 | " 1.7992 0.4524 1.6935\n", 246 | " 1.0790 0.5248 0.9662\n", 247 | " 1.1204 0.0545 1.3213\n", 248 | "[torch.FloatTensor of size 5x3]\n", 249 | "\n" 250 | ] 251 | } 252 | ], 253 | "source": [ 254 | "# adds x to y\n", 255 | "y.add_(x)\n", 256 | "print(y)" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": 15, 262 | "metadata": {}, 263 | "outputs": [ 264 | { 265 | "name": "stdout", 266 | "output_type": "stream", 267 | "text": [ 268 | "\n", 269 | " 0.8180 0.1831 0.6580\n", 270 | " 0.3410 0.1037 0.8664\n", 271 | " 0.9746 0.2583 0.8823\n", 272 | " 0.7392 0.4272 0.1205\n", 273 | " 0.6209 0.0127 0.6182\n", 274 | "[torch.FloatTensor of size 5x3]\n", 275 | "\n", 276 | "\n", 277 | " 0.1831\n", 278 | " 0.1037\n", 279 | " 0.2583\n", 280 | " 0.4272\n", 281 | " 0.0127\n", 282 | "[torch.FloatTensor of size 5]\n", 283 | "\n", 284 | "\n", 285 | " 0.3410\n", 286 | " 0.1037\n", 287 | " 0.8664\n", 288 | "[torch.FloatTensor of size 3]\n", 289 | "\n", 290 | "\n", 291 | " 0.3410\n", 292 | "[torch.FloatTensor of size 1]\n", 293 | "\n" 294 | ] 295 | } 296 | ], 297 | "source": [ 298 | "print(x)\n", 299 | "### prints [every row , 2nd column]\n", 300 | "print(x[:, 1])\n", 301 | "### prints [2nd row , all columns]\n", 302 | "print(x[1, :])\n", 303 | "### prints [2nd row , all columns]\n", 304 | "print(x[1, :1])" 305 | ] 306 | }, 307 | { 308 | "cell_type": "code", 309 | "execution_count": null, 310 | "metadata": { 311 | "collapsed": true 312 | }, 313 | "outputs": [], 314 | "source": [] 315 | }, 316 | { 317 | "cell_type": "markdown", 318 | "metadata": {}, 319 | "source": [ 320 | "## Converting torch Tensor to numpy Array" 321 | ] 322 | }, 323 | { 324 | "cell_type": "code", 325 | "execution_count": 14, 326 | "metadata": {}, 327 | "outputs": [ 328 | { 329 | "name": "stdout", 330 | "output_type": "stream", 331 | "text": [ 332 | "\n", 333 | " 1\n", 334 | " 1\n", 335 | " 1\n", 336 | " 1\n", 337 | " 1\n", 338 | "[torch.FloatTensor of size 5]\n", 339 | "\n" 340 | ] 341 | } 342 | ], 343 | "source": [ 344 | "a = torch.ones(5)\n", 345 | "print(a)" 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": 15, 351 | "metadata": {}, 352 | "outputs": [ 353 | { 354 | "name": "stdout", 355 | "output_type": "stream", 356 | "text": [ 357 | "[ 1. 1. 1. 1. 1.]\n" 358 | ] 359 | } 360 | ], 361 | "source": [ 362 | "b = a.numpy()\n", 363 | "print(b)" 364 | ] 365 | }, 366 | { 367 | "cell_type": "markdown", 368 | "metadata": {}, 369 | "source": [ 370 | "See how the numpy array changed in value." 371 | ] 372 | }, 373 | { 374 | "cell_type": "code", 375 | "execution_count": 16, 376 | "metadata": {}, 377 | "outputs": [ 378 | { 379 | "name": "stdout", 380 | "output_type": "stream", 381 | "text": [ 382 | "\n", 383 | " 2\n", 384 | " 2\n", 385 | " 2\n", 386 | " 2\n", 387 | " 2\n", 388 | "[torch.FloatTensor of size 5]\n", 389 | "\n", 390 | "[ 2. 2. 2. 2. 2.]\n" 391 | ] 392 | } 393 | ], 394 | "source": [ 395 | "a.add_(1)\n", 396 | "print(a)\n", 397 | "print(b)" 398 | ] 399 | }, 400 | { 401 | "cell_type": "markdown", 402 | "metadata": {}, 403 | "source": [ 404 | "## Converting numpy Array to torch Tensor\n", 405 | "See how changing the np array changed the torch Tensor automatically" 406 | ] 407 | }, 408 | { 409 | "cell_type": "code", 410 | "execution_count": 17, 411 | "metadata": {}, 412 | "outputs": [ 413 | { 414 | "name": "stdout", 415 | "output_type": "stream", 416 | "text": [ 417 | "[ 2. 2. 2. 2. 2.]\n", 418 | "\n", 419 | " 2\n", 420 | " 2\n", 421 | " 2\n", 422 | " 2\n", 423 | " 2\n", 424 | "[torch.DoubleTensor of size 5]\n", 425 | "\n" 426 | ] 427 | } 428 | ], 429 | "source": [ 430 | "import numpy as np\n", 431 | "a = np.ones(5)\n", 432 | "b = torch.from_numpy(a)\n", 433 | "np.add(a, 1, out=a)\n", 434 | "print(a)\n", 435 | "print(b)" 436 | ] 437 | }, 438 | { 439 | "cell_type": "markdown", 440 | "metadata": {}, 441 | "source": [ 442 | "All the Tensors on the CPU except a CharTensor support converting to NumPy and back." 443 | ] 444 | }, 445 | { 446 | "cell_type": "markdown", 447 | "metadata": {}, 448 | "source": [ 449 | "## CUDA Tensors\n", 450 | "Tensors can be moved onto GPU using the .cuda function." 451 | ] 452 | }, 453 | { 454 | "cell_type": "code", 455 | "execution_count": 22, 456 | "metadata": {}, 457 | "outputs": [ 458 | { 459 | "name": "stdout", 460 | "output_type": "stream", 461 | "text": [ 462 | "\n", 463 | " 0.2582 0.4529 0.1641\n", 464 | " 0.5981 0.5377 0.0012\n", 465 | " 0.4543 0.0688 0.0840\n", 466 | " 0.7876 0.0048 0.8325\n", 467 | " 0.8351 0.6137 0.0908\n", 468 | "[torch.cuda.FloatTensor of size 5x3 (GPU 0)]\n", 469 | "\n" 470 | ] 471 | } 472 | ], 473 | "source": [ 474 | "# let us run this cell only if CUDA is available\n", 475 | "if torch.cuda.is_available():\n", 476 | " x = x.cuda()\n", 477 | " y = y.cuda()\n", 478 | " x + y\n", 479 | "\n", 480 | "print(x)" 481 | ] 482 | }, 483 | { 484 | "cell_type": "markdown", 485 | "metadata": {}, 486 | "source": [ 487 | "# Part 2" 488 | ] 489 | }, 490 | { 491 | "cell_type": "code", 492 | "execution_count": null, 493 | "metadata": { 494 | "collapsed": true 495 | }, 496 | "outputs": [], 497 | "source": [] 498 | } 499 | ], 500 | "metadata": { 501 | "kernelspec": { 502 | "display_name": "Python [conda root]", 503 | "language": "python", 504 | "name": "conda-root-py" 505 | }, 506 | "language_info": { 507 | "codemirror_mode": { 508 | "name": "ipython", 509 | "version": 3 510 | }, 511 | "file_extension": ".py", 512 | "mimetype": "text/x-python", 513 | "name": "python", 514 | "nbconvert_exporter": "python", 515 | "pygments_lexer": "ipython3", 516 | "version": "3.5.3" 517 | } 518 | }, 519 | "nbformat": 4, 520 | "nbformat_minor": 2 521 | } 522 | -------------------------------------------------------------------------------- /Tutorials/autograd_tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 27, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "%matplotlib inline" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "\n", 17 | "Autograd: automatic differentiation\n", 18 | "===================================\n", 19 | "\n", 20 | "Central to all neural networks in PyTorch is the ``autograd`` package.\n", 21 | "Let’s first briefly visit this, and we will then go to training our\n", 22 | "first neural network.\n", 23 | "\n", 24 | "\n", 25 | "The ``autograd`` package provides automatic differentiation for all operations\n", 26 | "on Tensors. It is a define-by-run framework, which means that your backprop is\n", 27 | "defined by how your code is run, and that every single iteration can be\n", 28 | "different.\n", 29 | "\n", 30 | "Let us see this in more simple terms with some examples.\n", 31 | "\n", 32 | "Variable\n", 33 | "--------\n", 34 | "\n", 35 | "``autograd.Variable`` is the central class of the package. It wraps a\n", 36 | "Tensor, and supports nearly all of operations defined on it. Once you\n", 37 | "finish your computation you can call ``.backward()`` and have all the\n", 38 | "gradients computed automatically.\n", 39 | "\n", 40 | "You can access the raw tensor through the ``.data`` attribute, while the\n", 41 | "gradient w.r.t. this variable is accumulated into ``.grad``.\n", 42 | "\n", 43 | ".. figure:: /_static/img/Variable.png\n", 44 | " :alt: Variable\n", 45 | "\n", 46 | " Variable\n", 47 | "\n", 48 | "There’s one more class which is very important for autograd\n", 49 | "implementation - a ``Function``.\n", 50 | "\n", 51 | "``Variable`` and ``Function`` are interconnected and build up an acyclic\n", 52 | "graph, that encodes a complete history of computation. Each variable has\n", 53 | "a ``.grad_fn`` attribute that references a ``Function`` that has created\n", 54 | "the ``Variable`` (except for Variables created by the user - their\n", 55 | "``grad_fn is None``).\n", 56 | "\n", 57 | "If you want to compute the derivatives, you can call ``.backward()`` on\n", 58 | "a ``Variable``. If ``Variable`` is a scalar (i.e. it holds a one element\n", 59 | "data), you don’t need to specify any arguments to ``backward()``,\n", 60 | "however if it has more elements, you need to specify a ``grad_output``\n", 61 | "argument that is a tensor of matching shape.\n", 62 | "\n" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 28, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [ 71 | "import torch\n", 72 | "from torch.autograd import Variable" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "Create a variable:\n", 80 | "\n" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 29, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "name": "stdout", 90 | "output_type": "stream", 91 | "text": [ 92 | "Variable containing:\n", 93 | " 1 1\n", 94 | " 1 1\n", 95 | "[torch.FloatTensor of size 2x2]\n", 96 | "\n" 97 | ] 98 | } 99 | ], 100 | "source": [ 101 | "x = Variable(torch.ones(2, 2), requires_grad=True)\n", 102 | "print(x)" 103 | ] 104 | }, 105 | { 106 | "cell_type": "markdown", 107 | "metadata": {}, 108 | "source": [ 109 | "Do an operation of variable:\n", 110 | "\n" 111 | ] 112 | }, 113 | { 114 | "cell_type": "code", 115 | "execution_count": 30, 116 | "metadata": {}, 117 | "outputs": [ 118 | { 119 | "name": "stdout", 120 | "output_type": "stream", 121 | "text": [ 122 | "Variable containing:\n", 123 | " 3 3\n", 124 | " 3 3\n", 125 | "[torch.FloatTensor of size 2x2]\n", 126 | "\n" 127 | ] 128 | } 129 | ], 130 | "source": [ 131 | "y = x + 2\n", 132 | "print(y)" 133 | ] 134 | }, 135 | { 136 | "cell_type": "markdown", 137 | "metadata": {}, 138 | "source": [ 139 | "``y`` was created as a result of an operation, so it has a ``grad_fn``.\n", 140 | "\n" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 31, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "name": "stdout", 150 | "output_type": "stream", 151 | "text": [ 152 | "\n" 153 | ] 154 | } 155 | ], 156 | "source": [ 157 | "print(y.grad_fn)" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "Do more operations on y\n", 165 | "\n" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 32, 171 | "metadata": {}, 172 | "outputs": [ 173 | { 174 | "name": "stdout", 175 | "output_type": "stream", 176 | "text": [ 177 | "Variable containing:\n", 178 | " 45 45\n", 179 | " 45 45\n", 180 | "[torch.FloatTensor of size 2x2]\n", 181 | " Variable containing:\n", 182 | " 45\n", 183 | "[torch.FloatTensor of size 1]\n", 184 | "\n" 185 | ] 186 | } 187 | ], 188 | "source": [ 189 | "z = y * y * 5\n", 190 | "out = z.mean()\n", 191 | "\n", 192 | "print(z, out)" 193 | ] 194 | }, 195 | { 196 | "cell_type": "markdown", 197 | "metadata": {}, 198 | "source": [ 199 | "Gradients\n", 200 | "---------\n", 201 | "let's backprop now\n", 202 | "``out.backward()`` is equivalent to doing ``out.backward(torch.Tensor([1.0]))``\n", 203 | "\n" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 33, 209 | "metadata": {}, 210 | "outputs": [], 211 | "source": [ 212 | "out.backward()" 213 | ] 214 | }, 215 | { 216 | "cell_type": "markdown", 217 | "metadata": {}, 218 | "source": [ 219 | "print gradients d(out)/dx\n", 220 | "\n", 221 | "\n" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": 34, 227 | "metadata": {}, 228 | "outputs": [ 229 | { 230 | "name": "stdout", 231 | "output_type": "stream", 232 | "text": [ 233 | "Variable containing:\n", 234 | " 7.5000 7.5000\n", 235 | " 7.5000 7.5000\n", 236 | "[torch.FloatTensor of size 2x2]\n", 237 | "\n" 238 | ] 239 | } 240 | ], 241 | "source": [ 242 | "print(x.grad)" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": {}, 248 | "source": [ 249 | "You should have got a matrix of ``4.5``. Let’s call the ``out``\n", 250 | "*Variable* “$o$”.\n", 251 | "We have that $o = \\frac{1}{4}\\sum_i z_i$,\n", 252 | "$z_i = 3(x_i+2)^2$ and $z_i\\bigr\\rvert_{x_i=1} = 27$.\n", 253 | "Therefore,\n", 254 | "$\\frac{\\partial o}{\\partial x_i} = \\frac{3}{2}(x_i+2)$, hence\n", 255 | "$\\frac{\\partial o}{\\partial x_i}\\bigr\\rvert_{x_i=1} = \\frac{9}{2} = 4.5$.\n", 256 | "\n" 257 | ] 258 | }, 259 | { 260 | "cell_type": "markdown", 261 | "metadata": {}, 262 | "source": [ 263 | "You can do many crazy things with autograd!\n", 264 | "\n" 265 | ] 266 | }, 267 | { 268 | "cell_type": "code", 269 | "execution_count": 35, 270 | "metadata": {}, 271 | "outputs": [ 272 | { 273 | "name": "stdout", 274 | "output_type": "stream", 275 | "text": [ 276 | "\n", 277 | " 0.1434\n", 278 | " 2.4304\n", 279 | " 0.4210\n", 280 | "[torch.FloatTensor of size 3]\n", 281 | "\n" 282 | ] 283 | } 284 | ], 285 | "source": [ 286 | "x = torch.randn(3)\n", 287 | "print(x)" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": 36, 293 | "metadata": {}, 294 | "outputs": [ 295 | { 296 | "name": "stdout", 297 | "output_type": "stream", 298 | "text": [ 299 | "Variable containing:\n", 300 | " 0.1434\n", 301 | " 2.4304\n", 302 | " 0.4210\n", 303 | "[torch.FloatTensor of size 3]\n", 304 | " Variable containing:\n", 305 | " 0.2869\n", 306 | " 4.8608\n", 307 | " 0.8421\n", 308 | "[torch.FloatTensor of size 3]\n", 309 | "\n" 310 | ] 311 | } 312 | ], 313 | "source": [ 314 | "x = Variable(x, requires_grad=True)\n", 315 | "\n", 316 | "y = x * 2\n", 317 | "print( x,y)" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": 49, 323 | "metadata": {}, 324 | "outputs": [ 325 | { 326 | "name": "stdout", 327 | "output_type": "stream", 328 | "text": [ 329 | "Variable containing:\n", 330 | " 4700.1108\n", 331 | " 79639.4141\n", 332 | " 13796.4980\n", 333 | "[torch.FloatTensor of size 3]\n", 334 | "\n", 335 | "80962.1558043174\n" 336 | ] 337 | } 338 | ], 339 | "source": [ 340 | "y = y * 2\n", 341 | "print(y)\n", 342 | "print(y.data.norm())" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": 50, 348 | "metadata": {}, 349 | "outputs": [ 350 | { 351 | "name": "stdout", 352 | "output_type": "stream", 353 | "text": [ 354 | "Variable containing:\n", 355 | " 4700.1108\n", 356 | " 79639.4141\n", 357 | " 13796.4980\n", 358 | "[torch.FloatTensor of size 3]\n", 359 | "\n" 360 | ] 361 | } 362 | ], 363 | "source": [ 364 | "while y.data.norm() < 1000:\n", 365 | " print(y.data.norm())\n", 366 | " y = y * 2\n", 367 | "\n", 368 | "print(y)" 369 | ] 370 | }, 371 | { 372 | "cell_type": "code", 373 | "execution_count": 51, 374 | "metadata": {}, 375 | "outputs": [ 376 | { 377 | "name": "stdout", 378 | "output_type": "stream", 379 | "text": [ 380 | "Variable containing:\n", 381 | " 3276.8000\n", 382 | " 32768.0000\n", 383 | " 3.2768\n", 384 | "[torch.FloatTensor of size 3]\n", 385 | "\n" 386 | ] 387 | } 388 | ], 389 | "source": [ 390 | "gradients = torch.FloatTensor([0.1, 1.0, 0.0001])\n", 391 | "y.backward(gradients)\n", 392 | "\n", 393 | "print(x.grad)" 394 | ] 395 | }, 396 | { 397 | "cell_type": "markdown", 398 | "metadata": {}, 399 | "source": [ 400 | "**Read Later:**\n", 401 | "\n", 402 | "Documentation of ``Variable`` and ``Function`` is at\n", 403 | "http://pytorch.org/docs/autograd\n", 404 | "\n" 405 | ] 406 | } 407 | ], 408 | "metadata": { 409 | "kernelspec": { 410 | "display_name": "Python [default]", 411 | "language": "python", 412 | "name": "python3" 413 | }, 414 | "language_info": { 415 | "codemirror_mode": { 416 | "name": "ipython", 417 | "version": 3 418 | }, 419 | "file_extension": ".py", 420 | "mimetype": "text/x-python", 421 | "name": "python", 422 | "nbconvert_exporter": "python", 423 | "pygments_lexer": "ipython3", 424 | "version": "3.5.3" 425 | } 426 | }, 427 | "nbformat": 4, 428 | "nbformat_minor": 1 429 | } 430 | -------------------------------------------------------------------------------- /Tutorials/neural_networks_tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "%matplotlib inline" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "\n", 17 | "Neural Networks\n", 18 | "===============\n", 19 | "\n", 20 | "Neural networks can be constructed using the ``torch.nn`` package.\n", 21 | "\n", 22 | "Now that you had a glimpse of ``autograd``, ``nn`` depends on\n", 23 | "``autograd`` to define models and differentiate them.\n", 24 | "An ``nn.Module`` contains layers, and a method ``forward(input)``\\ that\n", 25 | "returns the ``output``.\n", 26 | "\n", 27 | "For example, look at this network that classfies digit images:\n", 28 | "\n", 29 | ".. figure:: /_static/img/mnist.png\n", 30 | " :alt: convnet\n", 31 | "\n", 32 | " convnet\n", 33 | "\n", 34 | "It is a simple feed-forward network. It takes the input, feeds it\n", 35 | "through several layers one after the other, and then finally gives the\n", 36 | "output.\n", 37 | "\n", 38 | "A typical training procedure for a neural network is as follows:\n", 39 | "\n", 40 | "- Define the neural network that has some learnable parameters (or\n", 41 | " weights)\n", 42 | "- Iterate over a dataset of inputs\n", 43 | "- Process input through the network\n", 44 | "- Compute the loss (how far is the output from being correct)\n", 45 | "- Propagate gradients back into the network’s parameters\n", 46 | "- Update the weights of the network, typically using a simple update rule:\n", 47 | " ``weight = weight - learning_rate * gradient``\n", 48 | "\n", 49 | "Define the network\n", 50 | "------------------\n", 51 | "\n", 52 | "Let’s define this network:\n", 53 | "\n" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 2, 59 | "metadata": { 60 | "collapsed": true 61 | }, 62 | "outputs": [], 63 | "source": [ 64 | "import torch\n", 65 | "from torch.autograd import Variable\n", 66 | "import torch.nn as nn\n", 67 | "import torch.nn.functional as F" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 3, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [ 76 | "class Net(nn.Module):\n", 77 | " def __init__(self):\n", 78 | " super(Net, self).__init__()\n", 79 | " # 1 input image channel, 6 output channels, 5x5 square convolution\n", 80 | " # kernel\n", 81 | " self.conv1 = nn.Conv2d(1, 6, 5)\n", 82 | " self.conv2 = nn.Conv2d(6, 16, 5)\n", 83 | " # an affine operation: y = Wx + b\n", 84 | " self.fc1 = nn.Linear(16 * 5 * 5, 120)\n", 85 | " self.fc2 = nn.Linear(120, 84)\n", 86 | " self.fc3 = nn.Linear(84, 10)\n", 87 | "\n", 88 | " def forward(self, x):\n", 89 | " # Max pooling over a (2, 2) window\n", 90 | " x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))\n", 91 | " # If the size is a square you can only specify a single number\n", 92 | " x = F.max_pool2d(F.relu(self.conv2(x)), 2)\n", 93 | " x = x.view(-1, self.num_flat_features(x))\n", 94 | " x = F.relu(self.fc1(x))\n", 95 | " x = F.relu(self.fc2(x))\n", 96 | " x = self.fc3(x)\n", 97 | " return x\n", 98 | "\n", 99 | " def num_flat_features(self, x):\n", 100 | " size = x.size()[1:] # all dimensions except the batch dimension\n", 101 | " num_features = 1\n", 102 | " for s in size:\n", 103 | " num_features *= s\n", 104 | " return num_features" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 4, 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "name": "stdout", 114 | "output_type": "stream", 115 | "text": [ 116 | "Net (\n", 117 | " (conv1): Conv2d(1, 6, kernel_size=(5, 5), stride=(1, 1))\n", 118 | " (conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1))\n", 119 | " (fc1): Linear (400 -> 120)\n", 120 | " (fc2): Linear (120 -> 84)\n", 121 | " (fc3): Linear (84 -> 10)\n", 122 | ")\n" 123 | ] 124 | } 125 | ], 126 | "source": [ 127 | "net = Net()\n", 128 | "print(net)" 129 | ] 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "metadata": {}, 134 | "source": [ 135 | "You just have to define the ``forward`` function, and the ``backward``\n", 136 | "function (where gradients are computed) is automatically defined for you\n", 137 | "using ``autograd``.\n", 138 | "You can use any of the Tensor operations in the ``forward`` function.\n", 139 | "\n", 140 | "The learnable parameters of a model are returned by ``net.parameters()``\n", 141 | "\n" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 5, 147 | "metadata": {}, 148 | "outputs": [ 149 | { 150 | "name": "stdout", 151 | "output_type": "stream", 152 | "text": [ 153 | "10\n", 154 | "torch.Size([6, 1, 5, 5])\n" 155 | ] 156 | } 157 | ], 158 | "source": [ 159 | "params = list(net.parameters())\n", 160 | "print(len(params))\n", 161 | "print(params[0].size()) # conv1's .weight" 162 | ] 163 | }, 164 | { 165 | "cell_type": "markdown", 166 | "metadata": {}, 167 | "source": [ 168 | "The input to the forward is an ``autograd.Variable``, and so is the output.\n", 169 | "Note: Expected input size to this net(LeNet) is 32x32. To use this net on\n", 170 | "MNIST dataset,please resize the images from the dataset to 32x32.\n", 171 | "\n" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 6, 177 | "metadata": {}, 178 | "outputs": [ 179 | { 180 | "name": "stdout", 181 | "output_type": "stream", 182 | "text": [ 183 | "Variable containing:\n", 184 | "-0.0311 0.1132 0.0138 0.0526 0.0108 0.0591 0.0444 -0.0924 0.0409 -0.1083\n", 185 | "[torch.FloatTensor of size 1x10]\n", 186 | "\n" 187 | ] 188 | } 189 | ], 190 | "source": [ 191 | "input = Variable(torch.randn(1, 1, 32, 32))\n", 192 | "out = net(input)\n", 193 | "print(out)" 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": {}, 199 | "source": [ 200 | "Zero the gradient buffers of all parameters and backprops with random\n", 201 | "gradients:\n", 202 | "\n" 203 | ] 204 | }, 205 | { 206 | "cell_type": "code", 207 | "execution_count": 7, 208 | "metadata": {}, 209 | "outputs": [], 210 | "source": [ 211 | "net.zero_grad()\n", 212 | "out.backward(torch.randn(1, 10))" 213 | ] 214 | }, 215 | { 216 | "cell_type": "markdown", 217 | "metadata": {}, 218 | "source": [ 219 | "

Note

``torch.nn`` only supports mini-batches The entire ``torch.nn``\n", 220 | " package only supports inputs that are a mini-batch of samples, and not\n", 221 | " a single sample.\n", 222 | "\n", 223 | " For example, ``nn.Conv2d`` will take in a 4D Tensor of\n", 224 | " ``nSamples x nChannels x Height x Width``.\n", 225 | "\n", 226 | " If you have a single sample, just use ``input.unsqueeze(0)`` to add\n", 227 | " a fake batch dimension.

\n", 228 | "\n", 229 | "Before proceeding further, let's recap all the classes you’ve seen so far.\n", 230 | "\n", 231 | "**Recap:**\n", 232 | " - ``torch.Tensor`` - A *multi-dimensional array*.\n", 233 | " - ``autograd.Variable`` - *Wraps a Tensor and records the history of\n", 234 | " operations* applied to it. Has the same API as a ``Tensor``, with\n", 235 | " some additions like ``backward()``. Also *holds the gradient*\n", 236 | " w.r.t. the tensor.\n", 237 | " - ``nn.Module`` - Neural network module. *Convenient way of\n", 238 | " encapsulating parameters*, with helpers for moving them to GPU,\n", 239 | " exporting, loading, etc.\n", 240 | " - ``nn.Parameter`` - A kind of Variable, that is *automatically\n", 241 | " registered as a parameter when assigned as an attribute to a*\n", 242 | " ``Module``.\n", 243 | " - ``autograd.Function`` - Implements *forward and backward definitions\n", 244 | " of an autograd operation*. Every ``Variable`` operation, creates at\n", 245 | " least a single ``Function`` node, that connects to functions that\n", 246 | " created a ``Variable`` and *encodes its history*.\n", 247 | "\n", 248 | "**At this point, we covered:**\n", 249 | " - Defining a neural network\n", 250 | " - Processing inputs and calling backward.\n", 251 | "\n", 252 | "**Still Left:**\n", 253 | " - Computing the loss\n", 254 | " - Updating the weights of the network\n", 255 | "\n", 256 | "Loss Function\n", 257 | "-------------\n", 258 | "A loss function takes the (output, target) pair of inputs, and computes a\n", 259 | "value that estimates how far away the output is from the target.\n", 260 | "\n", 261 | "There are several different\n", 262 | "`loss functions `_ under the\n", 263 | "nn package .\n", 264 | "A simple loss is: ``nn.MSELoss`` which computes the mean-squared error\n", 265 | "between the input and the target.\n", 266 | "\n", 267 | "For example:\n", 268 | "\n" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": 8, 274 | "metadata": {}, 275 | "outputs": [ 276 | { 277 | "name": "stdout", 278 | "output_type": "stream", 279 | "text": [ 280 | "Variable containing:\n", 281 | "-0.0311 0.1132 0.0138 0.0526 0.0108 0.0591 0.0444 -0.0924 0.0409 -0.1083\n", 282 | "[torch.FloatTensor of size 1x10]\n", 283 | "\n" 284 | ] 285 | } 286 | ], 287 | "source": [ 288 | "output = net(input)\n", 289 | "print(output)" 290 | ] 291 | }, 292 | { 293 | "cell_type": "code", 294 | "execution_count": 9, 295 | "metadata": {}, 296 | "outputs": [ 297 | { 298 | "name": "stdout", 299 | "output_type": "stream", 300 | "text": [ 301 | "Variable containing:\n", 302 | " 2\n", 303 | " 3\n", 304 | " 4\n", 305 | " 5\n", 306 | " 6\n", 307 | " 7\n", 308 | " 8\n", 309 | " 9\n", 310 | " 10\n", 311 | " 11\n", 312 | "[torch.FloatTensor of size 10]\n", 313 | "\n", 314 | "Variable containing:\n", 315 | " 50.5410\n", 316 | "[torch.FloatTensor of size 1]\n", 317 | "\n" 318 | ] 319 | } 320 | ], 321 | "source": [ 322 | "target = Variable(torch.arange(2, 12)) # a dummy target, for example\n", 323 | "print (target)\n", 324 | "criterion = nn.MSELoss()\n", 325 | "\n", 326 | "loss = criterion(output, target)\n", 327 | "print(loss)" 328 | ] 329 | }, 330 | { 331 | "cell_type": "markdown", 332 | "metadata": {}, 333 | "source": [ 334 | "Now, if you follow ``loss`` in the backward direction, using it’s\n", 335 | "``.grad_fn`` attribute, you will see a graph of computations that looks\n", 336 | "like this:\n", 337 | "\n", 338 | "::\n", 339 | "\n", 340 | " input -> conv2d -> relu -> maxpool2d -> conv2d -> relu -> maxpool2d\n", 341 | " -> view -> linear -> relu -> linear -> relu -> linear\n", 342 | " -> MSELoss\n", 343 | " -> loss\n", 344 | "\n", 345 | "So, when we call ``loss.backward()``, the whole graph is differentiated\n", 346 | "w.r.t. the loss, and all Variables in the graph will have their\n", 347 | "``.grad`` Variable accumulated with the gradient.\n", 348 | "\n", 349 | "For illustration, let us follow a few steps backward:\n", 350 | "\n" 351 | ] 352 | }, 353 | { 354 | "cell_type": "code", 355 | "execution_count": 10, 356 | "metadata": {}, 357 | "outputs": [ 358 | { 359 | "name": "stdout", 360 | "output_type": "stream", 361 | "text": [ 362 | "\n", 363 | "\n", 364 | "\n" 365 | ] 366 | } 367 | ], 368 | "source": [ 369 | "print(loss.grad_fn) # MSELoss\n", 370 | "print(loss.grad_fn.next_functions[0][0]) # Linear\n", 371 | "print(loss.grad_fn.next_functions[0][0].next_functions[0][0]) # ReLU" 372 | ] 373 | }, 374 | { 375 | "cell_type": "markdown", 376 | "metadata": {}, 377 | "source": [ 378 | "Backprop\n", 379 | "--------\n", 380 | "To backpropagate the error all we have to do is to ``loss.backward()``.\n", 381 | "You need to clear the existing gradients though, else gradients will be\n", 382 | "accumulated to existing gradients\n", 383 | "\n", 384 | "\n", 385 | "Now we shall call ``loss.backward()``, and have a look at conv1's bias\n", 386 | "gradients before and after the backward.\n", 387 | "\n" 388 | ] 389 | }, 390 | { 391 | "cell_type": "code", 392 | "execution_count": 11, 393 | "metadata": {}, 394 | "outputs": [ 395 | { 396 | "name": "stdout", 397 | "output_type": "stream", 398 | "text": [ 399 | "conv1.bias.grad before backward\n", 400 | "Variable containing:\n", 401 | " 0\n", 402 | " 0\n", 403 | " 0\n", 404 | " 0\n", 405 | " 0\n", 406 | " 0\n", 407 | "[torch.FloatTensor of size 6]\n", 408 | "\n", 409 | "conv1.bias.grad after backward\n", 410 | "Variable containing:\n", 411 | " 0.0142\n", 412 | " 0.0234\n", 413 | "-0.2187\n", 414 | "-0.0547\n", 415 | " 0.0425\n", 416 | " 0.0846\n", 417 | "[torch.FloatTensor of size 6]\n", 418 | "\n" 419 | ] 420 | } 421 | ], 422 | "source": [ 423 | "net.zero_grad() # zeroes the gradient buffers of all parameters\n", 424 | "\n", 425 | "print('conv1.bias.grad before backward')\n", 426 | "print(net.conv1.bias.grad)\n", 427 | "\n", 428 | "loss.backward()\n", 429 | "\n", 430 | "print('conv1.bias.grad after backward')\n", 431 | "print(net.conv1.bias.grad)" 432 | ] 433 | }, 434 | { 435 | "cell_type": "markdown", 436 | "metadata": {}, 437 | "source": [ 438 | "Now, we have seen how to use loss functions.\n", 439 | "\n", 440 | "**Read Later:**\n", 441 | "\n", 442 | " The neural network package contains various modules and loss functions\n", 443 | " that form the building blocks of deep neural networks. A full list with\n", 444 | " documentation is `here `_\n", 445 | "\n", 446 | "**The only thing left to learn is:**\n", 447 | "\n", 448 | " - updating the weights of the network\n", 449 | "\n", 450 | "Update the weights\n", 451 | "------------------\n", 452 | "The simplest update rule used in practice is the Stochastic Gradient\n", 453 | "Descent (SGD):\n", 454 | "\n", 455 | " ``weight = weight - learning_rate * gradient``\n", 456 | "\n", 457 | "We can implement this using simple python code:\n", 458 | "\n", 459 | ".. code:: python\n", 460 | "\n", 461 | " learning_rate = 0.01\n", 462 | " for f in net.parameters():\n", 463 | " f.data.sub_(f.grad.data * learning_rate)\n", 464 | "\n", 465 | "However, as you use neural networks, you want to use various different\n", 466 | "update rules such as SGD, Nesterov-SGD, Adam, RMSProp, etc.\n", 467 | "To enable this, we built a small package: ``torch.optim`` that\n", 468 | "implements all these methods. Using it is very simple:\n", 469 | "\n" 470 | ] 471 | }, 472 | { 473 | "cell_type": "code", 474 | "execution_count": 42, 475 | "metadata": {}, 476 | "outputs": [ 477 | { 478 | "ename": "RuntimeError", 479 | "evalue": "bool value of Variable objects containing non-empty torch.ByteTensor is ambiguous", 480 | "output_type": "error", 481 | "traceback": [ 482 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 483 | "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", 484 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mloss\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcriterion\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtarget\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmagic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'pinfo2 loss'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0;32mwhile\u001b[0m \u001b[0;34m(\u001b[0m \u001b[0mloss\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m10\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 12\u001b[0m \u001b[0mloss\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbackward\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0moptimizer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# Does the update\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 485 | "\u001b[0;32m/home/jd/anaconda3/lib/python3.5/site-packages/torch/autograd/variable.py\u001b[0m in \u001b[0;36m__bool__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 121\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;32mFalse\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 122\u001b[0m raise RuntimeError(\"bool value of Variable objects containing non-empty \" +\n\u001b[0;32m--> 123\u001b[0;31m torch.typename(self.data) + \" is ambiguous\")\n\u001b[0m\u001b[1;32m 124\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 125\u001b[0m \u001b[0m__nonzero__\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m__bool__\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 486 | "\u001b[0;31mRuntimeError\u001b[0m: bool value of Variable objects containing non-empty torch.ByteTensor is ambiguous" 487 | ] 488 | } 489 | ], 490 | "source": [ 491 | "import torch.optim as optim\n", 492 | "\n", 493 | "# create your optimizer\n", 494 | "optimizer = optim.SGD(net.parameters(), lr=0.01)\n", 495 | "\n", 496 | "# in your training loop:\n", 497 | "optimizer.zero_grad() # zero the gradient buffers\n", 498 | "output = net(input)\n", 499 | "loss = criterion(output, target)\n", 500 | "??loss\n", 501 | "while ( loss.float() > 10):\n", 502 | " loss.backward()\n", 503 | " optimizer.step() # Does the update\n", 504 | " print (loss)" 505 | ] 506 | }, 507 | { 508 | "cell_type": "code", 509 | "execution_count": 29, 510 | "metadata": {}, 511 | "outputs": [ 512 | { 513 | "name": "stdout", 514 | "output_type": "stream", 515 | "text": [ 516 | "Variable containing:\n", 517 | "(0 ,0 ,.,.) = \n", 518 | " -0.3005 1.0773 1.9537 ... 2.6309 1.7234 -0.0269\n", 519 | " -1.8409 0.0144 1.1780 ... 1.0053 0.5740 0.2438\n", 520 | " -0.6430 -0.4031 0.5979 ... 0.6997 -0.1318 2.1498\n", 521 | " ... ⋱ ... \n", 522 | " 0.3032 1.1982 0.1787 ... 0.7342 0.3553 -0.0828\n", 523 | " 1.2991 -1.5302 -0.7385 ... -1.4729 -0.9230 -0.6216\n", 524 | " 0.1394 -1.6562 0.2930 ... -0.5908 0.1286 -1.3558\n", 525 | "[torch.FloatTensor of size 1x1x32x32]\n", 526 | " Variable containing:\n", 527 | " 0.2604 0.6947 0.0883 0.6302 0.6587 0.8181 0.7697 1.0533 1.2737 0.7723\n", 528 | "[torch.FloatTensor of size 1x10]\n", 529 | "\n", 530 | "Variable containing:\n", 531 | " 40.5664\n", 532 | "[torch.FloatTensor of size 1]\n", 533 | "\n" 534 | ] 535 | } 536 | ], 537 | "source": [] 538 | }, 539 | { 540 | "cell_type": "code", 541 | "execution_count": null, 542 | "metadata": { 543 | "collapsed": true 544 | }, 545 | "outputs": [], 546 | "source": [] 547 | } 548 | ], 549 | "metadata": { 550 | "kernelspec": { 551 | "display_name": "Python [default]", 552 | "language": "python", 553 | "name": "python3" 554 | }, 555 | "language_info": { 556 | "codemirror_mode": { 557 | "name": "ipython", 558 | "version": 3 559 | }, 560 | "file_extension": ".py", 561 | "mimetype": "text/x-python", 562 | "name": "python", 563 | "nbconvert_exporter": "python", 564 | "pygments_lexer": "ipython3", 565 | "version": "3.5.3" 566 | } 567 | }, 568 | "nbformat": 4, 569 | "nbformat_minor": 1 570 | } 571 | --------------------------------------------------------------------------------