Search code examples
classluatorch

passing data to torch.class(es) in lua


I'm trying to use the class function in Torch. however when calling what would be a member function with an argument, the argument is always nil. I know that class is an abstraction to emulate OO programming.

local RpnData, parent = torch.class('nn.RpnData', 'nn.Module')

  function RpnData:__init()
    parent.__init(self)
    local  scale = {8, 16, 32}
    self._feat_stride = 1

    self._allowed_border = 0;
  end

  function RpnData:Foo(input)
    print("This will not work")
    print(input)
  end


local rpnnode = nn.RpnData()
local input = torch.Tensor( 5, 5):zero()
print(input)
rpnnode.Foo(input)

outputs:

Torch 7.0  Copyright (C) 2001-2011 Idiap, NEC Labs, NYU
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0
 0  0  0  0  0
[torch.DoubleTensor of size 5x5]

This will not work  
nil 

Can anyone suggest where i've gone wrong? Are there limitations to the Torch.class system?

Thanks in advance


Solution

  • Use rpnnode.Foo(rpnnode, input) or (better) rpnnode:Foo(input) - the colon is syntactic sugar, see Programming in Lua, 16.1.