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

clearState preserves structure in tables with Tensors #703

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
22 changes: 12 additions & 10 deletions Container.lua
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,21 @@ end

function Container:clearState()
-- don't call set because it might reset referenced tensors
local function clear(f)
if self[f] then
if torch.isTensor(self[f]) then
self[f] = self[f].new()
elseif type(self[f]) == 'table' then
self[f] = {}
else
self[f] = nil
local function clear(t)
if torch.isTensor(t) then
return t.new()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be t:set()? t.new() creates a new tensor.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nevermind, i read the comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that if #673 gets merged, we will be able to just call :set() on the tensors and keep all the references to the network tensors after clearState.

elseif type(t) == 'table' then
local cleared = {}
for k,v in pairs(t) do
cleared[k] = clear(v)
end
return cleared
else
return nil
end
end
clear('output')
clear('gradInput')
if self.output then self.output = clear(self.output) end
if self.gradInput then self.gradInput = clear(self.gradInput) end
if self.modules then
for i,module in pairs(self.modules) do
module:clearState()
Expand Down
22 changes: 12 additions & 10 deletions Identity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ end

function Identity:clearState()
-- don't call set because it might reset referenced tensors
local function clear(f)
if self[f] then
if torch.isTensor(self[f]) then
self[f] = self[f].new()
elseif type(self[f]) == 'table' then
self[f] = {}
else
self[f] = nil
local function clear(t)
if torch.isTensor(t) then
return t.new()
elseif type(t) == 'table' then
local cleared = {}
for k,v in pairs(t) do
cleared[k] = clear(v)
end
return cleared
else
return nil
end
end
clear('output')
clear('gradInput')
if self.output then self.output = clear(self.output) end
if self.gradInput then self.gradInput = clear(self.gradInput) end
return self
end
20 changes: 11 additions & 9 deletions utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,20 @@ function nn.utils.clear(self, ...)
if #arg > 0 and type(arg[1]) == 'table' then
arg = arg[1]
end
local function clear(f)
if self[f] then
if torch.isTensor(self[f]) then
self[f]:set()
elseif type(self[f]) == 'table' then
self[f] = {}
else
self[f] = nil
local function clear(t)
if torch.isTensor(t) then
return t:set()
elseif type(t) == 'table' then
local cleared = {}
for k,v in pairs(t) do
cleared[k] = clear(v)
end
return cleared
else
return nil
end
end
for i,v in ipairs(arg) do clear(v) end
for i,v in ipairs(arg) do if self[v] then self[v] = clear(self[v]) end end
return self
end

Expand Down