Skip to content

Commit a2071de

Browse files
committed
Container -> Module
1 parent b4592e9 commit a2071de

4 files changed

+212
-120
lines changed

Creating Extensions using FFI.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ import torch.nn as nn
9898
from torch.autograd import Variable
9999
from modules.add import MyAddModule
100100

101-
class MyNetwork(nn.Container):
101+
class MyNetwork(nn.Module):
102102
def __init__(self):
103103
super(MyNetwork, self).__init__(
104104
add=MyAddModule(),

Deep Learning with PyTorch.ipynb

+48-28
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
"*This tutorial assumes that you have a basic familiarity of numpy*\n",
1515
"\n",
1616
"\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",
1720
"### What is PyTorch?\n",
1821
"\n",
1922
"It's a Python based scientific computing package targeted at two sets of audiences:\n",
2023
"\n",
2124
"- 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"
2526
]
2627
},
2728
{
@@ -38,7 +39,7 @@
3839
"cell_type": "code",
3940
"execution_count": null,
4041
"metadata": {
41-
"collapsed": true
42+
"collapsed": false
4243
},
4344
"outputs": [],
4445
"source": [
@@ -61,7 +62,7 @@
6162
"cell_type": "code",
6263
"execution_count": null,
6364
"metadata": {
64-
"collapsed": true
65+
"collapsed": false
6566
},
6667
"outputs": [],
6768
"source": [
@@ -402,7 +403,9 @@
402403
"outputs": [],
403404
"source": [
404405
"# 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]))"
406409
]
407410
},
408411
{
@@ -501,7 +504,7 @@
501504
"\n",
502505
"Now that you had a glimpse of `autograd`, `nn` depends on `autograd` to define models and differentiate them.\n",
503506
"\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",
505508
"\n",
506509
"For example, look at this network that classfies digit images:\n",
507510
"\n",
@@ -533,11 +536,8 @@
533536
"source": [
534537
"import torch.nn as nn\n",
535538
"import torch.nn.functional as F\n",
536-
"# Some more python helpers\n",
537-
"import functools\n",
538-
"import operator\n",
539539
"\n",
540-
"class Net(nn.Container):\n",
540+
"class Net(nn.Module):\n",
541541
" def __init__(self):\n",
542542
" super(Net, self).__init__()\n",
543543
" self.conv1 = nn.Conv2d(1, 6, 5) # 1 input image channel, 6 output channels, 5x5 square convolution kernel\n",
@@ -556,7 +556,11 @@
556556
" return x\n",
557557
" \n",
558558
" 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",
560564
"\n",
561565
"net = Net()\n",
562566
"net"
@@ -638,7 +642,6 @@
638642
"* `torch.Tensor` - A **multi-dimensional array**.\n",
639643
"* `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",
640644
"* `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",
642645
"* `nn.Parameter` - A kind of Variable, that is **automatically registered as a parameter when assigned as an attribute to a `Module`**.\n",
643646
"* `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",
644647
"\n",
@@ -659,7 +662,7 @@
659662
"source": [
660663
"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",
661664
"\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",
663666
"\n",
664667
"A simple loss is: `nn.MSELoss` which computes the mean-squared error between the input and the target.\n",
665668
"\n",
@@ -955,7 +958,7 @@
955958
},
956959
"outputs": [],
957960
"source": [
958-
"class Net(nn.Container):\n",
961+
"class Net(nn.Module):\n",
959962
" def __init__(self):\n",
960963
" super(Net, self).__init__()\n",
961964
" self.conv1 = nn.Conv2d(3, 6, 5)\n",
@@ -964,14 +967,13 @@
964967
" self.fc1 = nn.Linear(16*5*5, 120)\n",
965968
" self.fc2 = nn.Linear(120, 84)\n",
966969
" self.fc3 = nn.Linear(84, 10)\n",
967-
" self.relu = nn.ReLU()\n",
968970
"\n",
969971
" 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",
972974
" 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",
975977
" x = self.fc3(x)\n",
976978
" return x\n",
977979
"\n",
@@ -1041,7 +1043,8 @@
10411043
" running_loss += loss.data[0]\n",
10421044
" if i % 2000 == 1999: # print every 2000 mini-batches\n",
10431045
" 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')"
10451048
]
10461049
},
10471050
{
@@ -1186,6 +1189,7 @@
11861189
"source": [
11871190
"#### Training on the GPU\n",
11881191
"Just like how you transfer a Tensor on to the GPU, you transfer the neural net onto the GPU.\n",
1192+
"\n",
11891193
"This will recursively go over all modules and convert their parameters and buffers to CUDA tensors."
11901194
]
11911195
},
@@ -1207,7 +1211,13 @@
12071211
"Why dont I notice MASSIVE speedup compared to CPU? Because your network is realllly small.\n",
12081212
"\n",
12091213
"**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": [
12111221
"\n",
12121222
"#### Goals achieved:\n",
12131223
"\n",
@@ -1221,33 +1231,43 @@
12211231
"source": [
12221232
"## Where do I go next?\n",
12231233
"\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",
12251235
"- [Train a state-of-the-art ResNet network on imagenet](https://github.com/pytorch/examples/tree/master/imagenet)\n",
12261236
"- [Train an face generator using Generative Adversarial Networks](https://github.com/pytorch/examples/tree/master/dcgan)\n",
12271237
"- [Train a word-level language model using Recurrent LSTM networks](https://github.com/pytorch/examples/tree/master/word_language_model)\n",
12281238
"- [More examples](https://github.com/pytorch/examples)\n",
12291239
"- [More tutorials](https://github.com/pytorch/tutorials)\n",
1240+
"- [Discuss PyTorch on the Forums](https://discuss.pytorch.org/)\n",
12301241
"- [Chat with other users on Slack](pytorch.slack.com/messages/beginner/)"
12311242
]
1243+
},
1244+
{
1245+
"cell_type": "code",
1246+
"execution_count": null,
1247+
"metadata": {
1248+
"collapsed": true
1249+
},
1250+
"outputs": [],
1251+
"source": []
12321252
}
12331253
],
12341254
"metadata": {
12351255
"kernelspec": {
1236-
"display_name": "Python 3",
1256+
"display_name": "Python 2",
12371257
"language": "python",
1238-
"name": "python3"
1258+
"name": "python2"
12391259
},
12401260
"language_info": {
12411261
"codemirror_mode": {
12421262
"name": "ipython",
1243-
"version": 3
1263+
"version": 2
12441264
},
12451265
"file_extension": ".py",
12461266
"mimetype": "text/x-python",
12471267
"name": "python",
12481268
"nbconvert_exporter": "python",
1249-
"pygments_lexer": "ipython3",
1250-
"version": "3.5.2"
1269+
"pygments_lexer": "ipython2",
1270+
"version": "2.7.12"
12511271
}
12521272
},
12531273
"nbformat": 4,

0 commit comments

Comments
 (0)