Skip to content

Commit 965dd5d

Browse files
committed
add comments for nn examples
1 parent 1f45934 commit 965dd5d

File tree

9 files changed

+252
-138
lines changed

9 files changed

+252
-138
lines changed

new_function.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

nn/dynamic_net.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import random
2+
import torch
3+
from torch.autograd import Variable
4+
5+
"""
6+
To showcase the power of PyTorch dynamic graphs, we will implement a very strange
7+
model: a fully-connected ReLU network that on each forward pass randomly chooses
8+
a number between 1 and 4 and has that many hidden layers, reusing the same
9+
weights multiple times to compute the innermost hidden layers.
10+
"""
11+
12+
class DynamicNet(torch.nn.Module):
13+
def __init__(self, D_in, H, D_out):
14+
"""
15+
In the constructor we construct three nn.Linear instances that we will use
16+
in the forward pass.
17+
"""
18+
super(DynamicNet, self).__init__()
19+
self.input_linear = torch.nn.Linear(D_in, H)
20+
self.middle_linear = torch.nn.Linear(H, H)
21+
self.output_linear = torch.nn.Linear(H, D_out)
22+
23+
def forward(self, x):
24+
"""
25+
For the forward pass of the model, we randomly choose either 0, 1, 2, or 3
26+
and reuse the middle_linear Module that many times to compute hidden layer
27+
representations.
28+
29+
Since each forward pass builds a dynamic computation graph, we can use normal
30+
Python control-flow operators like loops or conditional statements when
31+
defining the forward pass of the model.
32+
33+
Here we also see that it is perfectly safe to reuse the same Module many
34+
times when defining a computational graph. This is a big improvement from Lua
35+
Torch, where each Module could be used only once.
36+
"""
37+
h_relu = self.input_linear(x).clamp(min=0)
38+
for _ in range(random.randint(0, 3)):
39+
h_relu = self.middle_linear(h_relu).clamp(min=0)
40+
y_pred = self.output_linear(h_relu)
41+
return y_pred
42+
43+
44+
# N is batch size; D_in is input dimension;
45+
# H is hidden dimension; D_out is output dimension.
46+
N, D_in, H, D_out = 64, 1000, 100, 10
47+
48+
# Create random Tensors to hold inputs and outputs, and wrap them in Variables
49+
x = Variable(torch.randn(N, D_in))
50+
y = Variable(torch.randn(N, D_out), requires_grad=False)
51+
52+
# Construct our model by instantiating the class defined above
53+
model = DynamicNet(D_in, H, D_out)
54+
55+
# Construct our loss function and an Optimizer. Training this strange model with
56+
# vanilla stochastic gradient descent is tough, so we use momentum
57+
criterion = torch.nn.MSELoss(size_average=False)
58+
optimizer = torch.optim.SGD(model.parameters(), lr=1e-4, momentum=0.9)
59+
for t in range(500):
60+
# Forward pass: Compute predicted y by passing x to the model
61+
y_pred = model(x)
62+
63+
# Compute and print loss
64+
loss = criterion(y_pred, y)
65+
print(t, loss.data[0])
66+
67+
# Zero gradients, perform a backward pass, and update the weights.
68+
optimizer.zero_grad()
69+
loss.backward()
70+
optimizer.step()
71+

nn/two_layer_net_module.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import torch
2+
from torch.autograd import Variable
3+
4+
"""
5+
A fully-connected ReLU network with one hidden layer, trained to predict y from x
6+
by minimizing squared Euclidean distance.
7+
8+
This implementation defines the model as a custom Module subclass. Whenever you
9+
want a model more complex than a simple sequence of existing Modules you will
10+
need to define your model this way.
11+
"""
12+
13+
class TwoLayerNet(torch.nn.Module):
14+
def __init__(self, D_in, H, D_out):
15+
"""
16+
In the constructor we instantiate two nn.Linear modules and assign them as
17+
member variables.
18+
"""
19+
super(TwoLayerNet, self).__init__()
20+
self.linear1 = torch.nn.Linear(D_in, H)
21+
self.linear2 = torch.nn.Linear(H, D_out)
22+
23+
def forward(self, x):
24+
"""
25+
In the forward function we accept a Variable of input data and we must return
26+
a Variable of output data. We can use Modules defined in the constructor as
27+
well as arbitrary operators on Variables.
28+
"""
29+
h_relu = self.linear1(x).clamp(min=0)
30+
y_pred = self.linear2(h_relu)
31+
return y_pred
32+
33+
34+
# N is batch size; D_in is input dimension;
35+
# H is hidden dimension; D_out is output dimension.
36+
N, D_in, H, D_out = 64, 1000, 100, 10
37+
38+
# Create random Tensors to hold inputs and outputs, and wrap them in Variables
39+
x = Variable(torch.randn(N, D_in))
40+
y = Variable(torch.randn(N, D_out), requires_grad=False)
41+
42+
# Construct our model by instantiating the class defined above
43+
model = TwoLayerNet(D_in, H, D_out)
44+
45+
# Construct our loss function and an Optimizer. The call to model.parameters()
46+
# in the SGD constructor will contain the learnable parameters of the two
47+
# nn.Linear modules which are members of the model.
48+
criterion = torch.nn.MSELoss(size_average=False)
49+
optimizer = torch.optim.SGD(model.parameters(), lr=1e-4)
50+
for t in range(500):
51+
# Forward pass: Compute predicted y by passing x to the model
52+
y_pred = model(x)
53+
54+
# Compute and print loss
55+
loss = criterion(y_pred, y)
56+
print(t, loss.data[0])
57+
58+
# Zero gradients, perform a backward pass, and update the weights.
59+
optimizer.zero_grad()
60+
loss.backward()
61+
optimizer.step()
62+

nn/two_layer_net_nn.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import torch
2+
from torch.autograd import Variable
3+
4+
5+
"""
6+
A fully-connected ReLU network with one hidden layer, trained to predict y from x
7+
by minimizing squared Euclidean distance.
8+
9+
This implementation uses the nn package from PyTorch to build the network.
10+
PyTorch autograd makes it easy to define computational graphs and take gradients,
11+
but raw autograd can be a bit too low-level for defining complex neural networks;
12+
this is where the nn package can help. The nn package defines a set of Modules,
13+
which you can think of as a neural network layer that has produces output from
14+
input and may have some trainable weights.
15+
"""
16+
17+
# N is batch size; D_in is input dimension;
18+
# H is hidden dimension; D_out is output dimension.
19+
N, D_in, H, D_out = 64, 1000, 100, 10
20+
21+
# Create random Tensors to hold inputs and outputs, and wrap them in Variables.
22+
x = Variable(torch.randn(N, D_in))
23+
y = Variable(torch.randn(N, D_out), requires_grad=False)
24+
25+
# Use the nn package to define our model as a sequence of layers. Each Linear
26+
# module has its own weight and bias.
27+
model = torch.nn.Sequential(
28+
torch.nn.Linear(D_in, H),
29+
torch.nn.ReLU(),
30+
torch.nn.Linear(H, D_out),
31+
)
32+
33+
# The nn package also contains definitions of popular loss functions; in this
34+
# case we will use Mean Squared Error (MSE) as our loss function.
35+
loss_fn = torch.nn.MSELoss(size_average=False)
36+
37+
learning_rate = 1e-4
38+
for t in range(500):
39+
# Forward pass: compute predicted y by passing x to the model. Module objects
40+
# override the __call__ operator so you can call them like functions. When
41+
# doing so you pass a Variable of input data to the Module and it produces
42+
# a Variable of output data.
43+
y_pred = model(x)
44+
45+
# Compute and print loss. We pass Variables containing the predicted and true
46+
# values of y, and the loss function returns a Variable containing the loss.
47+
loss = loss_fn(y_pred, y)
48+
print(t, loss.data[0])
49+
50+
# Zero the gradients before running the backward pass.
51+
model.zero_grad()
52+
53+
# Backward pass: compute gradient of the loss with respect to all the learnable
54+
# parameters of the model. Internally, the parameters of each Module are stored
55+
# in Variables with requires_grad=True, so this call will compute gradients for
56+
# all learnable parameters in the model.
57+
loss.backward()
58+
59+
# Update the weights using gradient descent. Each parameter is a Variable, so
60+
# we can access its data and gradients like we did before.
61+
for param in model.parameters():
62+
param.data -= learning_rate * param.grad.data

nn/two_layer_net_optim.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import torch
2+
from torch.autograd import Variable
3+
4+
5+
"""
6+
A fully-connected ReLU network with one hidden layer, trained to predict y from x
7+
by minimizing squared Euclidean distance.
8+
9+
This implementation uses the nn package from PyTorch to build the network.
10+
11+
Rather than manually updating the weights of the model as we have been doing,
12+
we use the optim package to define an Optimizer that will update the weights
13+
for us. The optim package defines many optimization algorithms that are commonly
14+
used for deep learning, including SGD+momentum, RMSProp, Adam, etc.
15+
"""
16+
17+
# N is batch size; D_in is input dimension;
18+
# H is hidden dimension; D_out is output dimension.
19+
N, D_in, H, D_out = 64, 1000, 100, 10
20+
21+
# Create random Tensors to hold inputs and outputs, and wrap them in Variables.
22+
x = Variable(torch.randn(N, D_in))
23+
y = Variable(torch.randn(N, D_out), requires_grad=False)
24+
25+
# Use the nn package to define our model and loss function.
26+
model = torch.nn.Sequential(
27+
torch.nn.Linear(D_in, H),
28+
torch.nn.ReLU(),
29+
torch.nn.Linear(H, D_out),
30+
)
31+
loss_fn = torch.nn.MSELoss(size_average=False)
32+
33+
# Use the optim package to define an Optimizer that will update the weights of
34+
# the model for us. Here we will use stochastic gradient descent (SGD), but the
35+
# optim package contains many other optimization algoriths. The first argument
36+
# to the SGD constructor tells the optimizer which Variables it should update.
37+
learning_rate = 1e-4
38+
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
39+
for t in range(500):
40+
# Forward pass: compute predicted y by passing x to the model.
41+
y_pred = model(x)
42+
43+
# Compute and print loss.
44+
loss = loss_fn(y_pred, y)
45+
print(t, loss.data[0])
46+
47+
# Before the backward pass, use the optimizer object to zero all of the
48+
# gradients for the variables it will update (which are the learnable weights
49+
# of the model)
50+
optimizer.zero_grad()
51+
52+
# Backward pass: compute gradient of the loss with respect to model parameters
53+
loss.backward()
54+
55+
# Calling the step function on an Optimizer makes an update to its parameters
56+
optimizer.step()
57+

recurrent_net.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

two_layer_net_module.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

two_layer_net_nn.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

two_layer_net_optim.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)