Skip to content

Commit 8596789

Browse files
committed
Build #58 #59
1 parent 6b5ee54 commit 8596789

31 files changed

+602
-581
lines changed

_downloads/dynamic_net.ipynb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
11
{
22
"cells": [
33
{
4-
"cell_type": "code",
54
"source": [
65
"%matplotlib inline"
76
],
7+
"outputs": [],
88
"execution_count": null,
99
"metadata": {
1010
"collapsed": false
1111
},
12-
"outputs": []
12+
"cell_type": "code"
1313
},
1414
{
15-
"cell_type": "markdown",
1615
"source": [
1716
"\nPyTorch: Control Flow + Weight Sharing\n--------------------------------------\n\nTo showcase the power of PyTorch dynamic graphs, we will implement a very strange\nmodel: a fully-connected ReLU network that on each forward pass randomly chooses\na number between 1 and 4 and has that many hidden layers, reusing the same\nweights multiple times to compute the innermost hidden layers.\n\n"
1817
],
19-
"metadata": {}
18+
"metadata": {},
19+
"cell_type": "markdown"
2020
},
2121
{
22-
"cell_type": "code",
2322
"source": [
2423
"import random\nimport torch\nfrom torch.autograd import Variable\n\nclass DynamicNet(torch.nn.Module):\n def __init__(self, D_in, H, D_out):\n \"\"\"\n In the constructor we construct three nn.Linear instances that we will use\n in the forward pass.\n \"\"\"\n super(DynamicNet, self).__init__()\n self.input_linear = torch.nn.Linear(D_in, H)\n self.middle_linear = torch.nn.Linear(H, H)\n self.output_linear = torch.nn.Linear(H, D_out)\n\n def forward(self, x):\n \"\"\"\n For the forward pass of the model, we randomly choose either 0, 1, 2, or 3\n and reuse the middle_linear Module that many times to compute hidden layer\n representations.\n\n Since each forward pass builds a dynamic computation graph, we can use normal\n Python control-flow operators like loops or conditional statements when\n defining the forward pass of the model.\n\n Here we also see that it is perfectly safe to reuse the same Module many\n times when defining a computational graph. This is a big improvement from Lua\n Torch, where each Module could be used only once.\n \"\"\"\n h_relu = self.input_linear(x).clamp(min=0)\n for _ in range(random.randint(0, 3)):\n h_relu = self.middle_linear(h_relu).clamp(min=0)\n y_pred = self.output_linear(h_relu)\n return y_pred\n\n\n# N is batch size; D_in is input dimension;\n# H is hidden dimension; D_out is output dimension.\nN, D_in, H, D_out = 64, 1000, 100, 10\n\n# Create random Tensors to hold inputs and outputs, and wrap them in Variables\nx = Variable(torch.randn(N, D_in))\ny = Variable(torch.randn(N, D_out), requires_grad=False)\n\n# Construct our model by instantiating the class defined above\nmodel = DynamicNet(D_in, H, D_out)\n\n# Construct our loss function and an Optimizer. Training this strange model with\n# vanilla stochastic gradient descent is tough, so we use momentum\ncriterion = torch.nn.MSELoss(size_average=False)\noptimizer = torch.optim.SGD(model.parameters(), lr=1e-4, momentum=0.9)\nfor t in range(500):\n # Forward pass: Compute predicted y by passing x to the model\n y_pred = model(x)\n\n # Compute and print loss\n loss = criterion(y_pred, y)\n print(t, loss.data[0])\n\n # Zero gradients, perform a backward pass, and update the weights.\n optimizer.zero_grad()\n loss.backward()\n optimizer.step()"
2524
],
25+
"outputs": [],
2626
"execution_count": null,
2727
"metadata": {
2828
"collapsed": false
2929
},
30-
"outputs": []
30+
"cell_type": "code"
3131
}
3232
],
3333
"metadata": {
34+
"kernelspec": {
35+
"language": "python",
36+
"name": "python3",
37+
"display_name": "Python 3"
38+
},
3439
"language_info": {
35-
"name": "python",
40+
"nbconvert_exporter": "python",
41+
"mimetype": "text/x-python",
3642
"codemirror_mode": {
3743
"name": "ipython",
3844
"version": 3
3945
},
40-
"mimetype": "text/x-python",
41-
"nbconvert_exporter": "python",
42-
"version": "3.5.2",
46+
"pygments_lexer": "ipython3",
47+
"name": "python",
4348
"file_extension": ".py",
44-
"pygments_lexer": "ipython3"
45-
},
46-
"kernelspec": {
47-
"name": "python3",
48-
"display_name": "Python 3",
49-
"language": "python"
49+
"version": "3.5.2"
5050
}
5151
},
5252
"nbformat_minor": 0,

_downloads/neural_style_tutorial.ipynb

Lines changed: 86 additions & 86 deletions
Large diffs are not rendered by default.

_downloads/neural_style_tutorial.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def __init__(self, target, weight):
286286
self.criterion = nn.MSELoss()
287287

288288
def forward(self, input):
289-
self.loss = self.criterion.forward(input * self.weight, self.target)
289+
self.loss = self.criterion(input * self.weight, self.target)
290290
self.output = input
291291
return self.output
292292

@@ -357,9 +357,9 @@ def __init__(self, target, weight):
357357

358358
def forward(self, input):
359359
self.output = input.clone()
360-
self.G = self.gram.forward(input)
360+
self.G = self.gram(input)
361361
self.G.mul_(self.weight)
362-
self.loss = self.criterion.forward(self.G, self.target)
362+
self.loss = self.criterion(self.G, self.target)
363363
return self.output
364364

365365
def backward(self, retain_variables=True):
@@ -430,15 +430,15 @@ def get_style_model_and_losses(cnn, style_img, content_img,
430430

431431
if name in content_layers:
432432
# add content loss:
433-
target = model.forward(content_img).clone()
433+
target = model(content_img).clone()
434434
content_loss = ContentLoss(target, content_weight)
435435
model.add_module("content_loss_" + str(i), content_loss)
436436
content_losses.append(content_loss)
437437

438438
if name in style_layers:
439439
# add style loss:
440-
target_feature = model.forward(style_img).clone()
441-
target_feature_gram = gram.forward(target_feature)
440+
target_feature = model(style_img).clone()
441+
target_feature_gram = gram(target_feature)
442442
style_loss = StyleLoss(target_feature_gram, style_weight)
443443
model.add_module("style_loss_" + str(i), style_loss)
444444
style_losses.append(style_loss)
@@ -449,15 +449,15 @@ def get_style_model_and_losses(cnn, style_img, content_img,
449449

450450
if name in content_layers:
451451
# add content loss:
452-
target = model.forward(content_img).clone()
452+
target = model(content_img).clone()
453453
content_loss = ContentLoss(target, content_weight)
454454
model.add_module("content_loss_" + str(i), content_loss)
455455
content_losses.append(content_loss)
456456

457457
if name in style_layers:
458458
# add style loss:
459-
target_feature = model.forward(style_img).clone()
460-
target_feature_gram = gram.forward(target_feature)
459+
target_feature = model(style_img).clone()
460+
target_feature_gram = gram(target_feature)
461461
style_loss = StyleLoss(target_feature_gram, style_weight)
462462
model.add_module("style_loss_" + str(i), style_loss)
463463
style_losses.append(style_loss)
@@ -564,7 +564,7 @@ def closure():
564564
input_param.data.clamp_(0, 1)
565565

566566
optimizer.zero_grad()
567-
model.forward(input_param)
567+
model(input_param)
568568
style_score = 0
569569
content_score = 0
570570

0 commit comments

Comments
 (0)