Class: Sabina::AutoEncoder

Inherits:
MultilayerPerceptron show all
Defined in:
lib/sabina/auto_encoder.rb

Direct Known Subclasses

SparseAutoEncoder

Constant Summary

Constants inherited from MultilayerPerceptron

MultilayerPerceptron::LAMBDA, MultilayerPerceptron::MU

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MultilayerPerceptron

#initialize, #learn, #load_config, #propagate_backward, #propagate_forward

Constructor Details

This class inherits a constructor from Sabina::MultilayerPerceptron

Class Method Details

.load_csv(file_name) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/sabina/auto_encoder.rb', line 4

def self.load_csv(file_name)
  table = CSV.table(file_name)
  table.map do |data|
    x = data[0..-2]
    { :x => x, :d => x }
  end
end

Instance Method Details

#check_layersObject

Check if ‘@layers` is valid.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sabina/auto_encoder.rb', line 13

def check_layers
  super

  if @layers.size != 3
    raise "The number of layers must be three."
  end

  if @layers.first.size != @layers.last.size
    raise "The number of units of the input layer must be equal to that of the output layer."
  end
end

#error(test_data) ⇒ Object

Error function (a example of squared error)



26
27
28
29
30
# File 'lib/sabina/auto_encoder.rb', line 26

def error(test_data)
  d = Matrix.columns( test_data.map { |data| data[:d] } )
  y = propagate_forward(d)
  (d - y).to_a.flatten.inject(0.0) { |sum, v| sum + v**2 }
end

#next_input_data(input_data) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/sabina/auto_encoder.rb', line 32

def next_input_data(input_data)
  x = Matrix.columns( input_data.map { |data| data[:x] } )
  propagate_forward(x)
  @Z[1].t.to_a.map do |z|
    { :x => z, :d => z }
  end
end

#updateObject

Update the weights of this auto-encoder.



41
42
43
44
# File 'lib/sabina/auto_encoder.rb', line 41

def update
  super
  @layers[2].W = Marshal.load(Marshal.dump(@layers[1].W.t))
end