ankane / torch.rb

Deep learning for Ruby, powered by LibTorch
Other
704 stars 30 forks source link

uninitialized constant Set (NameError) #9

Closed nerdinand closed 4 years ago

nerdinand commented 4 years ago

Hi!

First of all, thanks so much for your work. I think it's fantastic to be able to use torch with Ruby!

I have a problem that might stem from me holding torch.rb wrong. But here's what I'm doing:

# Model is a Torch::NN::Module
@model = Model.new
@target_model = Model.new
@target_model.load_state_dict(@model.state_dict)

Essentially want to copy over the parameters of one Torch::NN::Module to another (of the same structure). I'm doing this in order to reimplement https://pythonprogramming.net/training-deep-q-learning-dqn-reinforcement-learning-python-tutorial/?completed=/deep-q-learning-dqn-reinforcement-learning-python-tutorial/ in Ruby and with Torch. There's this line in the source code (using Tensorflow/Keras):

self.target_model.set_weights(self.model.get_weights())

So that's what I want to achieve.

Now I'm getting this error when doing that:

...
    3: from /Users/ferdi/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/torch-rb-0.3.0/lib/torch/nn/module.rb:119:in `load_state_dict'
    2: from /Users/ferdi/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/torch-rb-0.3.0/lib/torch/nn/module.rb:119:in `each'
    1: from /Users/ferdi/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/torch-rb-0.3.0/lib/torch/nn/module.rb:121:in `block in load_state_dict'
/Users/ferdi/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/torch-rb-0.3.0/lib/torch/nn/module.rb:192:in `named_modules': uninitialized constant Set (NameError)

It's particularly weird, because when I try the same thing in IRB, no exception is thrown.

Can you tell me if this is a bug or if I should be doing this an entirely different way anyway?

Thanks again for your great work!

nerdinand commented 4 years ago

I just experimented a bit and it turns out that this code here fails too:

require 'torch'

module DQN
  class Model < Torch::NN::Module
    def initialize
      @fc = Torch::NN::Linear.new(4, 128)
    end
  end
end

m1 = DQN::Model.new
m2 = DQN::Model.new

m1.load_state_dict(m2.state_dict)

Error is:

Traceback (most recent call last):
    3: from model.rb:14:in `<main>'
    2: from /Users/ferdi/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/torch-rb-0.3.0/lib/torch/nn/module.rb:110:in `state_dict'
    1: from /Users/ferdi/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/torch-rb-0.3.0/lib/torch/nn/module.rb:147:in `named_parameters'
/Users/ferdi/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/torch-rb-0.3.0/lib/torch/nn/module.rb:179:in `named_children': undefined method `each' for nil:NilClass (NoMethodError)
ankane commented 4 years ago

Hey @nerdinand, thanks for reporting! It should be fixed on master.