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(ngens:, hidden_layers:, ninputs:, noutputs:, algo: nil, actfn: nil) ⇒ FANN

Returns a new instance of FANN.

Parameters:

  • ngens (Integer)

    number of generations 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 of the network

  • noutputs (Integer)

    number of outputs of the network

  • algo (:incremental, :batch, :rprop, :quickprop) (defaults to: nil)

    training algorithm

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

    activation function



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/data_modeler/model/fann.rb', line 15

def initialize ngens:, hidden_layers:, ninputs:, noutputs:, algo: nil, actfn: nil
  @fann_opts = {
    num_inputs: ninputs,
    hidden_neurons: hidden_layers,
    num_outputs: noutputs
  }
  @ngens = ngens
  @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

#fann_optsObject (readonly)

Returns the value of attribute fann_opts.



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

def fann_opts
  @fann_opts
end

#ngensObject (readonly)

Returns the value of attribute ngens.



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

def ngens
  @ngens
end

Instance Method Details

#resetvoid

This method returns an undefined value.

Resets / initializes the model



29
30
31
32
33
34
35
36
37
# File 'lib/data_modeler/model/fann.rb', line 29

def reset
  @fann = RubyFann::Standard.new fann_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

Parameters:

  • filename (String/path)

    where to save the model



63
64
65
66
67
# File 'lib/data_modeler/model/fann.rb', line 63

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



56
57
58
# File 'lib/data_modeler/model/fann.rb', line 56

def test inputs
  inputs.collect { |i| fann.run i }
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-like<input: Array, target: Array>)

    training set

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

    number of training generations



43
44
45
46
47
48
49
50
51
# File 'lib/data_modeler/model/fann.rb', line 43

def train trainset, ngens=@ngens, report_interval: 1000, desired_error: 1e-10
  # TODO: optimize maybe?
  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