Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions advanced_source/numpy_extensions_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ class BadFFTFunction(Function):
def forward(self, input):
numpy_input = input.numpy()
result = abs(rfft2(numpy_input))
return torch.FloatTensor(result)
return input.new(result)

def backward(self, grad_output):
numpy_go = grad_output.numpy()
result = irfft2(numpy_go)
return torch.FloatTensor(result)
return grad_output.new(result)

# since this layer does not have any parameters, we can
# simply declare this as a function, rather than as an nn.Module class
Expand Down Expand Up @@ -90,7 +90,7 @@ class ScipyConv2dFunction(Function):
def forward(ctx, input, filter):
result = correlate2d(input.numpy(), filter.numpy(), mode='valid')
ctx.save_for_backward(input, filter)
return torch.FloatTensor(result)
return input.new(result)

@staticmethod
def backward(ctx, grad_output):
Expand All @@ -99,8 +99,8 @@ def backward(ctx, grad_output):
grad_input = convolve2d(grad_output.numpy(), filter.t().numpy(), mode='full')
grad_filter = convolve2d(input.numpy(), grad_output.numpy(), mode='valid')

return Variable(torch.FloatTensor(grad_input)), \
Variable(torch.FloatTensor(grad_filter))
return Variable(grad_output.new(grad_input)), \
Variable(grad_output.new(grad_filter))


class ScipyConv2d(Module):
Expand Down