Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw error when calling backward more than rho+1 times #401

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions AbstractRecurrent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ end
function AbstractRecurrent:updateGradInput(input, gradOutput)
-- updateGradInput should be called in reverse order of time
self.updateGradInputStep = self.updateGradInputStep or self.step
assert(self.updateGradInputStep >= self.step - self.rho,
string.format('Called backward more than rho+1=%d times', self.rho+1))

-- BPTT for one time-step
self.gradInput = self:_updateGradInput(input, gradOutput)
Expand All @@ -68,6 +70,8 @@ function AbstractRecurrent:accGradParameters(input, gradOutput, scale)
-- accGradParameters should be called in reverse order of time
assert(self.updateGradInputStep < self.step, "Missing updateGradInput")
self.accGradParametersStep = self.accGradParametersStep or self.step
assert(self.accGradParametersStep >= self.step - self.rho,
string.format('Called backward more than rho+1=%d times', self.rho+1))

-- BPTT for one time-step
self:_accGradParameters(input, gradOutput, scale)
Expand Down
11 changes: 11 additions & 0 deletions test/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,17 @@ function rnntest.Recurrent_old()
mytester:assert(err < 0.0001, "Recurrent optim.checkgrad error")
end

function rnntest.RecurrentErrorOnExtraBackward()
local model = nn.Recurrent(
nn.Identity(), nn.Identity(), nil, nil, 1 --[[rho]])
local input = torch.rand(1)
model:training()
for i = 1, 3 do model:forward(input) end
for j = 1, 2 do model:backward(input, input) end
mytester:assertErrorPattern(function() model:backward(input, input) end,
'Called backward more than rho%+1=2 times')
end

function rnntest.Recurrent()
local batchSize = 4
local dictSize = 100
Expand Down