Class: DataModeler::Model::FANN

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(ngens:, hidden_layers:, ninputs:, noutputs:, algo: nil, actfn: nil, init_weights_range: nil) ⇒ FANN

Returns a new instance of FANN.

Parameters:

  • ngens (Integer)

    number of generations (repetitions) alloted for training

  • hidden_layers (Array<Integer>)

    list of number of hidden neurons per each hidden layer in the network

  • ninputs (Integer)

    number of inputs in the network

  • noutputs (Integer)

    number of outputs in the network

  • algo (:rprop, :rwg, ...) (defaults to: nil)

    training algorithm

  • actfn (:sigmoid, ...) (defaults to: nil)

    activation function

  • init_weights_range (Array<min_w, max_w>) (defaults to: nil)

    minimum and maximum value for weight initialization range



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

#actfnObject (readonly)

Returns the value of attribute actfn.



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

def actfn
  @actfn
end

#algoObject (readonly)

Returns the value of attribute algo.



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

def algo
  @algo
end

#fannObject (readonly)

Returns the value of attribute fann.



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

def fann
  @fann
end

#fann_optsObject (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_rangeObject (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

#ngensObject (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

#resetvoid

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

Parameters:

  • filename (String/path)

    where to save 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.

Parameters:

  • inputs (Array<Array<inputs>>)

    sequence of inputs for the model

Returns:

  • (Array<Array<outputs>>)

    outputs corresponding to each input



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

Parameters:

  • trainset (Hash<input: Array, target: Array>)

    training set

  • ngens (Integer) (defaults to: @ngens)

    number of training generations



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

Parameters:

  • trainset (Hash-like<input: Array, target: Array>)

    training set

  • ngens (Integer) (defaults to: @ngens)

    number of training generations



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