Class: DataModeler::Model::FANN

Inherits:
Object
  • Object
show all
Defined in:
lib/data_modeler/model/fann.rb

Overview

Model class based on Fast Artificial Neural Networks (FANN)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(netstruct, algo: nil, actfn: nil) ⇒ FANN



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/data_modeler/model/fann.rb', line 12

def initialize netstruct, algo: nil, actfn: nil
  ninputs, hidden_layers, noutputs = netstruct
  @opts = {
    num_inputs: ninputs,
    hidden_neurons: hidden_layers,
    num_outputs: noutputs
  }
  @algo = algo
  @actfn = actfn
  reset
end

Instance Attribute Details

#actfnObject (readonly)

Returns the value of attribute actfn.



6
7
8
# File 'lib/data_modeler/model/fann.rb', line 6

def actfn
  @actfn
end

#algoObject (readonly)

Returns the value of attribute algo.



6
7
8
# File 'lib/data_modeler/model/fann.rb', line 6

def algo
  @algo
end

#fannObject (readonly)

Returns the value of attribute fann.



6
7
8
# File 'lib/data_modeler/model/fann.rb', line 6

def fann
  @fann
end

#optsObject (readonly)

Returns the value of attribute opts.



6
7
8
# File 'lib/data_modeler/model/fann.rb', line 6

def opts
  @opts
end

Instance Method Details

#resetvoid

This method returns an undefined value.

Resets / initializes the model



26
27
28
29
30
31
32
33
34
# File 'lib/data_modeler/model/fann.rb', line 26

def reset
  @fann = RubyFann::Standard.new opts
  fann.set_training_algorithm(algo) if algo
  if actfn
    fann.set_activation_function_hidden(actfn)
    fann.set_activation_function_output(actfn)
  end
  nil
end

#save(filename) ⇒ void

This method returns an undefined value.

Save the model



59
60
61
62
63
# File 'lib/data_modeler/model/fann.rb', line 59

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
end

#test(inputs) ⇒ Array<Array<outputs>>

Tests the model on inputs.



52
53
54
# File 'lib/data_modeler/model/fann.rb', line 52

def test inputs
  inputs.collect { |i| fann.run i }
end

#train(ngens, trainset) ⇒ void

This method returns an undefined value.

Trains the model for ngens on the trainset



40
41
42
43
44
45
46
47
# File 'lib/data_modeler/model/fann.rb', line 40

def train ngens, trainset
  tset = RubyFann::TrainData.new(
    inputs: trainset[:input], desired_outputs: trainset[:target])
  # fann.init_weights tset # test this weights initialization

  # params: train_data, max_epochs, reports_interval, desired_error
  fann.train_on_data(tset, ngens, 1000, 1e-10)
end