Class: DataModeler::Model::FANN
- Inherits:
-
Object
- Object
- DataModeler::Model::FANN
- Defined in:
- lib/data_modeler/model/fann.rb
Overview
Model the data using an artificial neural network, based on the Fast Artificial Neural Networks (FANN) implementation
Instance Attribute Summary collapse
-
#actfn ⇒ Object
readonly
Returns the value of attribute actfn.
-
#algo ⇒ Object
readonly
Returns the value of attribute algo.
-
#fann ⇒ Object
readonly
Returns the value of attribute fann.
-
#fann_opts ⇒ Object
readonly
Returns the value of attribute fann_opts.
-
#init_weights_range ⇒ Object
readonly
Returns the value of attribute init_weights_range.
-
#ngens ⇒ Object
readonly
Returns the value of attribute ngens.
Instance Method Summary collapse
-
#initialize(ngens:, hidden_layers:, ninputs:, noutputs:, algo: nil, actfn: nil, init_weights_range: nil) ⇒ FANN
constructor
A new instance of FANN.
-
#reset ⇒ void
Resets / initializes the model.
-
#save(filename) ⇒ void
Saves the model.
-
#test(inputs) ⇒ Array<Array<outputs>>
Tests the model on inputs.
-
#train(trainset, ngens = @ngens, report_interval: 1000, desired_error: 1e-10) ⇒ void
Trains the model for ngens on the trainset.
-
#train_rwg(trainset, ngens = @ngens, report_interval: 1000, desired_error: 1e-10) ⇒ void
Trains the model for ngens on the trainset using Random Weight Guessing.
Constructor Details
#initialize(ngens:, hidden_layers:, ninputs:, noutputs:, algo: nil, actfn: nil, init_weights_range: nil) ⇒ FANN
Returns a new instance of FANN.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/data_modeler/model/fann.rb', line 17 def initialize ngens:, hidden_layers:, ninputs:, noutputs:, algo: nil, actfn: nil, init_weights_range: nil @fann_opts = { num_inputs: ninputs, hidden_neurons: hidden_layers, num_outputs: noutputs } @ngens = ngens @algo = algo @actfn = actfn @init_weights_range = init_weights_range reset end |
Instance Attribute Details
#actfn ⇒ Object (readonly)
Returns the value of attribute actfn.
7 8 9 |
# File 'lib/data_modeler/model/fann.rb', line 7 def actfn @actfn end |
#algo ⇒ Object (readonly)
Returns the value of attribute algo.
7 8 9 |
# File 'lib/data_modeler/model/fann.rb', line 7 def algo @algo end |
#fann ⇒ Object (readonly)
Returns the value of attribute fann.
7 8 9 |
# File 'lib/data_modeler/model/fann.rb', line 7 def fann @fann end |
#fann_opts ⇒ Object (readonly)
Returns the value of attribute fann_opts.
7 8 9 |
# File 'lib/data_modeler/model/fann.rb', line 7 def fann_opts @fann_opts end |
#init_weights_range ⇒ Object (readonly)
Returns the value of attribute init_weights_range.
7 8 9 |
# File 'lib/data_modeler/model/fann.rb', line 7 def init_weights_range @init_weights_range end |
#ngens ⇒ Object (readonly)
Returns the value of attribute ngens.
7 8 9 |
# File 'lib/data_modeler/model/fann.rb', line 7 def ngens @ngens end |
Instance Method Details
#reset ⇒ void
This method returns an undefined value.
Resets / initializes the model
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/data_modeler/model/fann.rb', line 32 def reset @fann = RubyFann::Standard.new fann_opts if algo && algo != :rwg fann.set_training_algorithm(algo) end if actfn fann.set_activation_function_hidden(actfn) fann.set_activation_function_output(actfn) end if init_weights_range fann.randomize_weights(*init_weights_range.map(&method(:Float))) end end |
#save(filename) ⇒ void
This method returns an undefined value.
Saves the model
110 111 112 113 114 |
# File 'lib/data_modeler/model/fann.rb', line 110 def save filename # can do filename check here...? # TODO: I'd like to have a kind of `to_s`, and do all the saving in the modeler... fann.save filename.to_s end |
#test(inputs) ⇒ Array<Array<outputs>>
Tests the model on inputs.
103 104 105 |
# File 'lib/data_modeler/model/fann.rb', line 103 def test inputs inputs.collect &fann.method(:run) end |
#train(trainset, ngens = @ngens, report_interval: 1000, desired_error: 1e-10) ⇒ void
This method returns an undefined value.
Trains the model for ngens on the trainset
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/data_modeler/model/fann.rb', line 50 def train trainset, ngens=@ngens, report_interval: 1000, desired_error: 1e-10 # it makes sense to temporarily disable the `report_interval` with `false` or `nil` report_interval ||= 0 # special case: not implemented in FANN if algo == :rwg return train_rwg(trainset, ngens, report_interval: report_interval, desired_error: desired_error) end # TODO: optimize maybe? times, inputs, targets = trainset.values tset = RubyFann::TrainData.new inputs: inputs, desired_outputs: targets # fann.init_weights tset # test this weights initialization # params: train_data, max_epochs, report_interval, desired_error fann.train_on_data(tset, ngens, report_interval, desired_error) end |
#train_rwg(trainset, ngens = @ngens, report_interval: 1000, desired_error: 1e-10) ⇒ void
This method returns an undefined value.
Trains the model for ngens on the trainset using Random Weight Guessing
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/data_modeler/model/fann.rb', line 71 def train_rwg trainset, ngens=@ngens, report_interval: 1000, desired_error: 1e-10 # TODO: use report_interval and desired_error # initialize weight with random values in an interval [min_weight, max_weight] # NOTE: if the RWG training is unsuccessful, this range is the first place to # check to improve performance fann.randomize_weights(*init_weights_range.map(&method(:Float))) # test it on inputs times, inputs, targets = trainset.values outputs = test(inputs) # calculate RMSE rmse_fn = -> (outs) do sq_err = outs.zip(targets).flat_map do |os,ts| os.zip(ts).collect { |o,t| (t-o)**2 } end Math.sqrt(sq_err.reduce(:+) / sq_err.size) end rmse = rmse_fn.call(outputs) # initialize best best = [fann,rmse] # rinse and repeat ngens.times do outputs = test(inputs) rmse = rmse_fn.call(outputs) (best = [fann,rmse]; puts rmse) if rmse < best.last end # expose the best to the interface fann = best.first end |