|
14 | 14 | "*This tutorial assumes that you have a basic familiarity of numpy*\n",
|
15 | 15 | "\n",
|
16 | 16 | "\n",
|
| 17 | + "**Note:** Make sure you have the [torch](https://github.com/pytorch/pytorch) and [torchvision](https://github.com/pytorch/vision) packages installed.\n", |
| 18 | + "\n", |
| 19 | + "\n", |
17 | 20 | "### What is PyTorch?\n",
|
18 | 21 | "\n",
|
19 | 22 | "It's a Python based scientific computing package targeted at two sets of audiences:\n",
|
20 | 23 | "\n",
|
21 | 24 | "- A replacement for numpy to use the power of GPUs\n",
|
22 |
| - "- a deep learning research platform that provides maximum flexibility and speed\n", |
23 |
| - "\n", |
24 |
| - "**If you want to complete the full tutorial, including training a neural network for image classification, you have to install the `torchvision` package.**" |
| 25 | + "- a deep learning research platform that provides maximum flexibility and speed\n" |
25 | 26 | ]
|
26 | 27 | },
|
27 | 28 | {
|
|
38 | 39 | "cell_type": "code",
|
39 | 40 | "execution_count": null,
|
40 | 41 | "metadata": {
|
41 |
| - "collapsed": true |
| 42 | + "collapsed": false |
42 | 43 | },
|
43 | 44 | "outputs": [],
|
44 | 45 | "source": [
|
|
61 | 62 | "cell_type": "code",
|
62 | 63 | "execution_count": null,
|
63 | 64 | "metadata": {
|
64 |
| - "collapsed": true |
| 65 | + "collapsed": false |
65 | 66 | },
|
66 | 67 | "outputs": [],
|
67 | 68 | "source": [
|
|
402 | 403 | "outputs": [],
|
403 | 404 | "source": [
|
404 | 405 | "# let's backprop now\n",
|
405 |
| - "out.backward()" |
| 406 | + "out.backward()\n", |
| 407 | + "\n", |
| 408 | + "# out.backward() is equivalent to doing out.backward(torch.Tensor([1.0]))" |
406 | 409 | ]
|
407 | 410 | },
|
408 | 411 | {
|
|
501 | 504 | "\n",
|
502 | 505 | "Now that you had a glimpse of `autograd`, `nn` depends on `autograd` to define models and differentiate them.\n",
|
503 | 506 | "\n",
|
504 |
| - "An `nn.Container` contains layers, and a method `forward(input)`that returns the `output`.\n", |
| 507 | + "An `nn.Module` contains layers, and a method `forward(input)`that returns the `output`.\n", |
505 | 508 | "\n",
|
506 | 509 | "For example, look at this network that classfies digit images:\n",
|
507 | 510 | "\n",
|
|
533 | 536 | "source": [
|
534 | 537 | "import torch.nn as nn\n",
|
535 | 538 | "import torch.nn.functional as F\n",
|
536 |
| - "# Some more python helpers\n", |
537 |
| - "import functools\n", |
538 |
| - "import operator\n", |
539 | 539 | "\n",
|
540 |
| - "class Net(nn.Container):\n", |
| 540 | + "class Net(nn.Module):\n", |
541 | 541 | " def __init__(self):\n",
|
542 | 542 | " super(Net, self).__init__()\n",
|
543 | 543 | " self.conv1 = nn.Conv2d(1, 6, 5) # 1 input image channel, 6 output channels, 5x5 square convolution kernel\n",
|
|
556 | 556 | " return x\n",
|
557 | 557 | " \n",
|
558 | 558 | " def num_flat_features(self, x):\n",
|
559 |
| - " return functools.reduce(operator.mul, x.size()[1:])\n", |
| 559 | + " size = x.size()[1:] # all dimensions except the batch dimension\n", |
| 560 | + " num_features = 1\n", |
| 561 | + " for s in size:\n", |
| 562 | + " num_features *= s\n", |
| 563 | + " return num_features\n", |
560 | 564 | "\n",
|
561 | 565 | "net = Net()\n",
|
562 | 566 | "net"
|
|
638 | 642 | "* `torch.Tensor` - A **multi-dimensional array**.\n",
|
639 | 643 | "* `autograd.Variable` - **Wraps a Tensor and records the history of operations** applied to it. Has the same API as a `Tensor`, with some additions like `backward()`. Also **holds the gradient** w.r.t. the tensor.\n",
|
640 | 644 | "* `nn.Module` - Neural network module. **Convenient way of encapsulating parameters**, with helpers for moving them to GPU, exporting, loading, etc.\n",
|
641 |
| - "* `nn.Container` - `Module` that is a **container for other Modules**.\n", |
642 | 645 | "* `nn.Parameter` - A kind of Variable, that is **automatically registered as a parameter when assigned as an attribute to a `Module`**.\n",
|
643 | 646 | "* `autograd.Function` - Implements **forward and backward definitions of an autograd operation**. Every `Variable` operation, creates at least a single `Function` node, that connects to functions that created a `Variable` and **encodes its history**.\n",
|
644 | 647 | "\n",
|
|
659 | 662 | "source": [
|
660 | 663 | "A loss function takes the (output, target) pair of inputs, and computes a value that estimates how far away the output is from the target.\n",
|
661 | 664 | "\n",
|
662 |
| - "There are several different loss functions under the nn package.\n", |
| 665 | + "There are [several different loss functions under the nn package](http://pytorch.org/docs/nn.html#loss-functions).\n", |
663 | 666 | "\n",
|
664 | 667 | "A simple loss is: `nn.MSELoss` which computes the mean-squared error between the input and the target.\n",
|
665 | 668 | "\n",
|
|
955 | 958 | },
|
956 | 959 | "outputs": [],
|
957 | 960 | "source": [
|
958 |
| - "class Net(nn.Container):\n", |
| 961 | + "class Net(nn.Module):\n", |
959 | 962 | " def __init__(self):\n",
|
960 | 963 | " super(Net, self).__init__()\n",
|
961 | 964 | " self.conv1 = nn.Conv2d(3, 6, 5)\n",
|
|
964 | 967 | " self.fc1 = nn.Linear(16*5*5, 120)\n",
|
965 | 968 | " self.fc2 = nn.Linear(120, 84)\n",
|
966 | 969 | " self.fc3 = nn.Linear(84, 10)\n",
|
967 |
| - " self.relu = nn.ReLU()\n", |
968 | 970 | "\n",
|
969 | 971 | " def forward(self, x):\n",
|
970 |
| - " x = self.pool(self.relu(self.conv1(x)))\n", |
971 |
| - " x = self.pool(self.relu(self.conv2(x)))\n", |
| 972 | + " x = self.pool(F.relu(self.conv1(x)))\n", |
| 973 | + " x = self.pool(F.relu(self.conv2(x)))\n", |
972 | 974 | " x = x.view(-1, 16*5*5)\n",
|
973 |
| - " x = self.relu(self.fc1(x))\n", |
974 |
| - " x = self.relu(self.fc2(x))\n", |
| 975 | + " x = F.relu(self.fc1(x))\n", |
| 976 | + " x = F.relu(self.fc2(x))\n", |
975 | 977 | " x = self.fc3(x)\n",
|
976 | 978 | " return x\n",
|
977 | 979 | "\n",
|
|
1041 | 1043 | " running_loss += loss.data[0]\n",
|
1042 | 1044 | " if i % 2000 == 1999: # print every 2000 mini-batches\n",
|
1043 | 1045 | " print('[%d, %5d] loss: %.3f' % (epoch+1, i+1, running_loss / 2000))\n",
|
1044 |
| - " running_loss = 0.0" |
| 1046 | + " running_loss = 0.0\n", |
| 1047 | + "print('Finished Training')" |
1045 | 1048 | ]
|
1046 | 1049 | },
|
1047 | 1050 | {
|
|
1186 | 1189 | "source": [
|
1187 | 1190 | "#### Training on the GPU\n",
|
1188 | 1191 | "Just like how you transfer a Tensor on to the GPU, you transfer the neural net onto the GPU.\n",
|
| 1192 | + "\n", |
1189 | 1193 | "This will recursively go over all modules and convert their parameters and buffers to CUDA tensors."
|
1190 | 1194 | ]
|
1191 | 1195 | },
|
|
1207 | 1211 | "Why dont I notice MASSIVE speedup compared to CPU? Because your network is realllly small.\n",
|
1208 | 1212 | "\n",
|
1209 | 1213 | "**Exercise:** Try increasing the width of your network \n",
|
1210 |
| - "(argument 1 and 2 of `nn.Conv2d`, see what kind of speedup you get.\n", |
| 1214 | + "(argument 1 and 2 of `nn.Conv2d`, see what kind of speedup you get." |
| 1215 | + ] |
| 1216 | + }, |
| 1217 | + { |
| 1218 | + "cell_type": "markdown", |
| 1219 | + "metadata": {}, |
| 1220 | + "source": [ |
1211 | 1221 | "\n",
|
1212 | 1222 | "#### Goals achieved:\n",
|
1213 | 1223 | "\n",
|
|
1221 | 1231 | "source": [
|
1222 | 1232 | "## Where do I go next?\n",
|
1223 | 1233 | "\n",
|
1224 |
| - "- [Train neural nets to play video games](https://github.com/pytorch/tutorials/blob/master/Reinforcement%20(Q-)Learning%20with%20PyTorch.ipynb)\n", |
| 1234 | + "- [Train neural nets to play video games](https://goo.gl/uGOksc)\n", |
1225 | 1235 | "- [Train a state-of-the-art ResNet network on imagenet](https://github.com/pytorch/examples/tree/master/imagenet)\n",
|
1226 | 1236 | "- [Train an face generator using Generative Adversarial Networks](https://github.com/pytorch/examples/tree/master/dcgan)\n",
|
1227 | 1237 | "- [Train a word-level language model using Recurrent LSTM networks](https://github.com/pytorch/examples/tree/master/word_language_model)\n",
|
1228 | 1238 | "- [More examples](https://github.com/pytorch/examples)\n",
|
1229 | 1239 | "- [More tutorials](https://github.com/pytorch/tutorials)\n",
|
| 1240 | + "- [Discuss PyTorch on the Forums](https://discuss.pytorch.org/)\n", |
1230 | 1241 | "- [Chat with other users on Slack](pytorch.slack.com/messages/beginner/)"
|
1231 | 1242 | ]
|
| 1243 | + }, |
| 1244 | + { |
| 1245 | + "cell_type": "code", |
| 1246 | + "execution_count": null, |
| 1247 | + "metadata": { |
| 1248 | + "collapsed": true |
| 1249 | + }, |
| 1250 | + "outputs": [], |
| 1251 | + "source": [] |
1232 | 1252 | }
|
1233 | 1253 | ],
|
1234 | 1254 | "metadata": {
|
1235 | 1255 | "kernelspec": {
|
1236 |
| - "display_name": "Python 3", |
| 1256 | + "display_name": "Python 2", |
1237 | 1257 | "language": "python",
|
1238 |
| - "name": "python3" |
| 1258 | + "name": "python2" |
1239 | 1259 | },
|
1240 | 1260 | "language_info": {
|
1241 | 1261 | "codemirror_mode": {
|
1242 | 1262 | "name": "ipython",
|
1243 |
| - "version": 3 |
| 1263 | + "version": 2 |
1244 | 1264 | },
|
1245 | 1265 | "file_extension": ".py",
|
1246 | 1266 | "mimetype": "text/x-python",
|
1247 | 1267 | "name": "python",
|
1248 | 1268 | "nbconvert_exporter": "python",
|
1249 |
| - "pygments_lexer": "ipython3", |
1250 |
| - "version": "3.5.2" |
| 1269 | + "pygments_lexer": "ipython2", |
| 1270 | + "version": "2.7.12" |
1251 | 1271 | }
|
1252 | 1272 | },
|
1253 | 1273 | "nbformat": 4,
|
|
0 commit comments