Skip to content

Commit 90c7ea9

Browse files
authored
Merge pull request pytorch#198 from chsasank/new-fixes
Multiple small fixes
2 parents 705a883 + 8e534fc commit 90c7ea9

File tree

8 files changed

+53
-27
lines changed

8 files changed

+53
-27
lines changed

advanced_source/numpy_extensions_tutorial.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,21 @@ def incorrect_fft(input):
8686

8787

8888
class ScipyConv2dFunction(Function):
89-
90-
def forward(self, input, filter):
89+
@staticmethod
90+
def forward(ctx, input, filter):
9191
result = correlate2d(input.numpy(), filter.numpy(), mode='valid')
92-
self.save_for_backward(input, filter)
92+
ctx.save_for_backward(input, filter)
9393
return torch.FloatTensor(result)
9494

95-
def backward(self, grad_output):
96-
input, filter = self.saved_tensors
95+
@staticmethod
96+
def backward(ctx, grad_output):
97+
input, filter = ctx.saved_tensors
98+
grad_output = grad_output.data
9799
grad_input = convolve2d(grad_output.numpy(), filter.t().numpy(), mode='full')
98100
grad_filter = convolve2d(input.numpy(), grad_output.numpy(), mode='valid')
99-
return torch.FloatTensor(grad_input), torch.FloatTensor(grad_filter)
101+
102+
return Variable(torch.FloatTensor(grad_input)), \
103+
Variable(torch.FloatTensor(grad_filter))
100104

101105

102106
class ScipyConv2d(Module):
@@ -106,7 +110,7 @@ def __init__(self, kh, kw):
106110
self.filter = Parameter(torch.randn(kh, kw))
107111

108112
def forward(self, input):
109-
return ScipyConv2dFunction()(input, self.filter)
113+
return ScipyConv2dFunction.apply(input, self.filter)
110114

111115
###############################################################
112116
# **Example usage:**

beginner_source/blitz/neural_networks_tutorial.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,11 @@ def num_flat_features(self, x):
253253
loss = criterion(output, target)
254254
loss.backward()
255255
optimizer.step() # Does the update
256+
257+
258+
###############################################################
259+
# .. Note::
260+
#
261+
# Observe how gradient buffers had to be manually set to zero using
262+
# ``optimizer.zero_grad()``. This is because gradients are accumulated
263+
# as explained in `Backprop`_ section.

beginner_source/blitz/tensor_tutorial.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@
7979

8080
print(x[:, 1])
8181

82+
###############################################################
83+
# Resizing: If you want to resize/reshape tensor, you can use ``torch.view``:
84+
x = torch.randn(4, 4)
85+
y = x.view(16)
86+
z = x.view(-1, 8) # the size -1 is inferred from other dimensions
87+
print(x.size(), y.size(), z.size())
88+
8289
###############################################################
8390
# **Read later:**
8491
#

beginner_source/data_loading_tutorial.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@
6666
landmarks_frame = pd.read_csv('faces/face_landmarks.csv')
6767

6868
n = 65
69-
img_name = landmarks_frame.ix[n, 0]
70-
landmarks = landmarks_frame.ix[n, 1:].as_matrix().astype('float')
71-
landmarks = landmarks.reshape(-1, 2)
69+
img_name = landmarks_frame.iloc[n, 0]
70+
landmarks = landmarks_frame.iloc[n, 1:].as_matrix()
71+
landmarks = landmarks.astype('float').reshape(-1, 2)
7272

7373
print('Image name: {}'.format(img_name))
7474
print('Landmarks shape: {}'.format(landmarks.shape))
@@ -136,10 +136,11 @@ def __len__(self):
136136
return len(self.landmarks_frame)
137137

138138
def __getitem__(self, idx):
139-
img_name = os.path.join(self.root_dir, self.landmarks_frame.ix[idx, 0])
139+
img_name = os.path.join(self.root_dir,
140+
self.landmarks_frame.iloc[idx, 0])
140141
image = io.imread(img_name)
141-
landmarks = self.landmarks_frame.ix[idx, 1:].as_matrix().astype('float')
142-
landmarks = landmarks.reshape(-1, 2)
142+
landmarks = self.landmarks_frame.iloc[idx, 1:].as_matrix()
143+
landmarks = landmarks.astype('float').reshape(-1, 2)
143144
sample = {'image': image, 'landmarks': landmarks}
144145

145146
if self.transform:

beginner_source/examples_autograd/two_layer_net_custom_function.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,25 @@ class MyReLU(torch.autograd.Function):
2323
which operate on Tensors.
2424
"""
2525

26-
def forward(self, input):
26+
@staticmethod
27+
def forward(ctx, input):
2728
"""
28-
In the forward pass we receive a Tensor containing the input and return a
29-
Tensor containing the output. You can cache arbitrary Tensors for use in the
30-
backward pass using the save_for_backward method.
29+
In the forward pass we receive a Tensor containing the input and return
30+
a Tensor containing the output. ctx is a context object that can be used
31+
to stash information for backward computation. You can cache arbitrary
32+
objects for use in the backward pass using the ctx.save_for_backward method.
3133
"""
32-
self.save_for_backward(input)
34+
ctx.save_for_backward(input)
3335
return input.clamp(min=0)
3436

35-
def backward(self, grad_output):
37+
@staticmethod
38+
def backward(ctx, grad_output):
3639
"""
3740
In the backward pass we receive a Tensor containing the gradient of the loss
3841
with respect to the output, and we need to compute the gradient of the loss
3942
with respect to the input.
4043
"""
41-
input, = self.saved_tensors
44+
input, = ctx.saved_tensors
4245
grad_input = grad_output.clone()
4346
grad_input[input < 0] = 0
4447
return grad_input
@@ -61,8 +64,8 @@ def backward(self, grad_output):
6164

6265
learning_rate = 1e-6
6366
for t in range(500):
64-
# Construct an instance of our MyReLU class to use in our network
65-
relu = MyReLU()
67+
# To apply our Function, we use Function.apply method. We alias this as 'relu'.
68+
relu = MyReLU.apply
6669

6770
# Forward pass: compute predicted y using operations on Variables; we compute
6871
# ReLU using our custom autograd operation.

beginner_source/examples_nn/two_layer_net_optim.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@
4747
print(t, loss.data[0])
4848

4949
# Before the backward pass, use the optimizer object to zero all of the
50-
# gradients for the variables it will update (which are the learnable weights
51-
# of the model)
50+
# gradients for the variables it will update (which are the learnable
51+
# weights of the model). This is because by default, gradients are
52+
# accumulated in buffers( i.e, not overwritten) whenever .backward()
53+
# is called. Checkout docs of torch.autograd.backward for more details.
5254
optimizer.zero_grad()
5355

5456
# Backward pass: compute gradient of the loss with respect to model

beginner_source/nlp/pytorch_tutorial.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@
244244
y = torch.randn((2, 2))
245245
z = x + y # These are Tensor types, and backprop would not be possible
246246

247-
var_x = autograd.Variable(x)
248-
var_y = autograd.Variable(y)
247+
var_x = autograd.Variable(x, requires_grad=True)
248+
var_y = autograd.Variable(y, requires_grad=True)
249249
# var_z contains enough information to compute gradients, as we saw above
250250
var_z = var_x + var_y
251251
print(var_z.grad_fn)

intermediate_source/char_rnn_classification_tutorial.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ def lineToTensor(line):
170170
# as regular feed-forward layers.
171171
#
172172
# This RNN module (mostly copied from `the PyTorch for Torch users
173-
# tutorial <https://github.com/pytorch/tutorials/blob/master/Introduction%20to%20PyTorch%20for%20former%20Torchies.ipynb>`__)
173+
# tutorial <http://pytorch.org/tutorials/beginner/former_torchies/
174+
# nn_tutorial.html#example-2-recurrent-net>`__)
174175
# is just 2 linear layers which operate on an input and hidden state, with
175176
# a LogSoftmax layer after the output.
176177
#

0 commit comments

Comments
 (0)