Class: RubyBrain::WeightContainer
- Inherits:
-
Object
- Object
- RubyBrain::WeightContainer
- Defined in:
- lib/ruby_brain/weights.rb
Instance Method Summary collapse
- #dump_to_yaml(file_name = nil) ⇒ Object
- #each_weights ⇒ Object
- #each_weights_with_index ⇒ Object
- #get_weights_as_array ⇒ Object
-
#initialize(num_units_list) ⇒ WeightContainer
constructor
A new instance of WeightContainer.
- #load_from(weights_set_source) ⇒ Object
- #load_from_yaml_file(yaml_file) ⇒ Object
- #num_sets ⇒ Object
- #overwrite_weights(weights_set_source) ⇒ Object
- #weights_of_order(order_number) ⇒ Object
Constructor Details
#initialize(num_units_list) ⇒ WeightContainer
Returns a new instance of WeightContainer.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/ruby_brain/weights.rb', line 3 def initialize(num_units_list) @w_3d = [] num_units_list.each_cons(2) do |num_units_on_left_layer, num_units_on_right_layer| @w = [] (num_units_on_left_layer + 1).times do |i| @w[i] = [] num_units_on_right_layer.times do |j| @w[i][j] = Random.rand(2.0) - 1.0 end end @w_3d << @w end end |
Instance Method Details
#dump_to_yaml(file_name = nil) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/ruby_brain/weights.rb', line 57 def dump_to_yaml(file_name=nil) if file_name File.open file_name, 'w+' do |f| YAML.dump(@w_3d, f) end end # # @w_3d.to_yaml end |
#each_weights ⇒ Object
70 71 72 73 74 |
# File 'lib/ruby_brain/weights.rb', line 70 def each_weights @w_3d.each do |weights| yield weights end end |
#each_weights_with_index ⇒ Object
76 77 78 79 80 |
# File 'lib/ruby_brain/weights.rb', line 76 def each_weights_with_index @w_3d.each_with_index do |weights, i| yield weights, i end end |
#get_weights_as_array ⇒ Object
22 23 24 |
# File 'lib/ruby_brain/weights.rb', line 22 def get_weights_as_array @w_3d.dup end |
#load_from(weights_set_source) ⇒ Object
18 19 20 |
# File 'lib/ruby_brain/weights.rb', line 18 def load_from(weights_set_source) @w_3d = weights_set_source end |
#load_from_yaml_file(yaml_file) ⇒ Object
66 67 68 |
# File 'lib/ruby_brain/weights.rb', line 66 def load_from_yaml_file(yaml_file) overwrite_weights(YAML.load_file(yaml_file)) end |
#num_sets ⇒ Object
49 50 51 |
# File 'lib/ruby_brain/weights.rb', line 49 def num_sets @w_3d.size end |
#overwrite_weights(weights_set_source) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ruby_brain/weights.rb', line 26 def overwrite_weights(weights_set_source) # weights_set_source.each_with_index do |weights, i| # next if i >= @w_3d.size # weights.each_with_index do |w, j| # next if j >= @w_3d[i].size # w.size.times do |k| # @w_3d[i][j][k] = w[k] unless w[k].nil? # end # end # end @w_3d.zip(weights_set_source).each_with_index do |wl, i| next if wl[1].nil? wl[0].zip(wl[1]).each_with_index do |ww, j| next if ww[1].nil? ww[0].zip(ww[1]).each_with_index do |w, k| @w_3d[i][j][k] = w[1] || w[0] end end end end |
#weights_of_order(order_number) ⇒ Object
53 54 55 |
# File 'lib/ruby_brain/weights.rb', line 53 def weights_of_order(order_number) @w_3d[order_number] end |