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

Strange interaction with nn.Container #59

Closed
davidBelanger opened this issue Nov 20, 2015 · 3 comments
Closed

Strange interaction with nn.Container #59

davidBelanger opened this issue Nov 20, 2015 · 3 comments

Comments

@davidBelanger
Copy link

Below is a minimal piece of code demonstrating a strange bug I have experienced.

Here, I have a typical LSTM sequence encoder. I also have a dummy class that inherits from nn.Container. The following code crashes. However, if you move the
parent:__init(self) line to go after mapper:forward(), then everything is ok. It fails in dpnn.Module:

   if moduleClones then
      assert(self.modules == nil)
      self.modules = modules
      clone.modules = moduleClones
   end

Since the container has a member called modules, this code crashes. It seems like the self pointer is wrong here or something. Any ideas about what is going on?

require 'nn'
require 'rnn'

local vocabSize = 25
local embeddingDim = 10
local rnnHidSize = 15

local lstm = nn.Sequencer(nn.LSTM(embeddingDim, rnnHidSize))
local mapper = nn.Sequential():add(nn.LookupTable(vocabSize,embeddingDim)):add(nn.SplitTable(2)):add(lstm):add(nn.SelectTable(-1))

--this is a minibatch of 'sentences'
local data = torch.rand(32,16):mul(vocabSize):ceil()


local NoopContainer, parent = torch.class('nn.NoopContainer', 'nn.Container')

function NoopContainer:__init()
    parent:__init(self)

    local length = 12
    local dd = torch.rand(32,length):mul(vocabSize):ceil()
    mapper:forward(dd)

end


local noop = nn.NoopContainer()
@nicholas-leonard
Copy link
Member

@davidBelanger Wow this is some weird stuff. Looking into it.

@nicholas-leonard
Copy link
Member

@davidBelanger Found the problem :

parent:__init(self)

should be replaced with

parent.__init(self)

Effectively, self was the Module class, thereby setting the modules attribute for all Module subclasses :)

@davidBelanger
Copy link
Author

Awesome! Thank you very much for the help. This fixed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants