Class: Ai4r::Classifiers::MultilayerPerceptron

Inherits:
Classifier
  • Object
show all
Defined in:
lib/ai4r/classifiers/multilayer_perceptron.rb

Overview

Introduction

The idea behind the MultilayerPerceptron classifier is to train a Multilayer Perceptron neural network with the provided examples, and predict the class for new data items.

Parameters

Use class method get_parameters_info to obtain details on the algorithm parameters. Use set_parameters to set values for this parameters. See Parameterizable module documentation.

  • :network_class => Neural network implementation class.

    By default: Ai4r::NeuralNetwork::Backpropagation.

  • :network_parameters => Parameters to be forwarded to the back end neural ntework.

  • :hidden_layers => Hidden layer structure. E.g. [8, 6] will generate 2 hidden layers with 8 and 6 neurons each. By default []

  • :training_iterations => How many times the training should be repeated. By default: 1000.

:active_node_value => Default: 1

:inactive_node_value => Default: 1

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Data::Parameterizable

#get_parameters, included, #set_parameters

Constructor Details

#initializeMultilayerPerceptron

Returns a new instance of MultilayerPerceptron


54
55
56
57
58
59
60
61
# File 'lib/ai4r/classifiers/multilayer_perceptron.rb', line 54

def initialize
  @network_class = Ai4r::NeuralNetwork::Backpropagation
  @hidden_layers = []
  @training_iterations = 500
  @network_parameters = {}
  @active_node_value = 1
  @inactive_node_value = 0
end

Instance Attribute Details

#class_valueObject (readonly)

Returns the value of attribute class_value


41
42
43
# File 'lib/ai4r/classifiers/multilayer_perceptron.rb', line 41

def class_value
  @class_value
end

#data_setObject (readonly)

Returns the value of attribute data_set


41
42
43
# File 'lib/ai4r/classifiers/multilayer_perceptron.rb', line 41

def data_set
  @data_set
end

#domainsObject (readonly)

Returns the value of attribute domains


41
42
43
# File 'lib/ai4r/classifiers/multilayer_perceptron.rb', line 41

def domains
  @domains
end

#networkObject (readonly)

Returns the value of attribute network


41
42
43
# File 'lib/ai4r/classifiers/multilayer_perceptron.rb', line 41

def network
  @network
end

Instance Method Details

#build(data_set) ⇒ Object

Build a new MultilayerPerceptron classifier. You must provide a DataSet instance as parameter. The last attribute of each item is considered as the item class.


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ai4r/classifiers/multilayer_perceptron.rb', line 66

def build(data_set)
  data_set.check_not_empty
  @data_set = data_set
  @domains = @data_set.build_domains.collect {|domain| domain.to_a}
  @outputs = @domains.last.length
  @inputs = 0
  @domains[0...-1].each {|domain| @inputs += domain.length}
  @structure = [@inputs] + @hidden_layers + [@outputs]
  @network = @network_class.new @structure
  @training_iterations.times do
    data_set.data_items.each do |data_item|
      input_values = data_to_input(data_item[0...-1])
      output_values = data_to_output(data_item.last)
      @network.train(input_values, output_values)
    end
  end
  return self
end

#eval(data) ⇒ Object

You can evaluate new data, predicting its class. e.g.

classifier.eval(['New York',  '<30', 'F'])  # => 'Y'

88
89
90
91
92
# File 'lib/ai4r/classifiers/multilayer_perceptron.rb', line 88

def eval(data)
  input_values = data_to_input(data)
  output_values = @network.eval(input_values)
  return @domains.last[get_max_index(output_values)]
end

#get_rulesObject

Multilayer Perceptron Classifiers cannot generate human-readable rules.


96
97
98
# File 'lib/ai4r/classifiers/multilayer_perceptron.rb', line 96

def get_rules
  return "raise 'Neural networks classifiers do not generate human-readable rules.'"
end