Class: Neuronet::Scale

Inherits:
Object
  • Object
show all
Defined in:
lib/neuronet/scale.rb

Overview

Neuronet::Scale is a class to help scale problems to fit within a network’s “field of view”. Given a list of values, it finds the minimum and maximum values and establishes a mapping to a scaled set of numbers between minus one and one (-1,1).

Direct Known Subclasses

Gaussian

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factor: 1.0, center: nil, spread: nil) ⇒ Scale

If the value of center is provided, then that value will be used instead of calculating it from the values passed to method #set. Likewise, if spread is provided, that value of spread will be used.



16
17
18
19
20
# File 'lib/neuronet/scale.rb', line 16

def initialize(factor: 1.0, center: nil, spread: nil)
  @factor = factor
  @center = center
  @spread = spread
end

Instance Attribute Details

#centerObject

Returns the value of attribute center.



10
11
12
# File 'lib/neuronet/scale.rb', line 10

def center
  @center
end

#spreadObject

Returns the value of attribute spread.



10
11
12
# File 'lib/neuronet/scale.rb', line 10

def spread
  @spread
end

Instance Method Details

#mapped(inputs) ⇒ Object Also known as: mapped_input, mapped_output



34
35
36
37
# File 'lib/neuronet/scale.rb', line 34

def mapped(inputs)
  factor = 1.0 / (@factor * @spread)
  inputs.map { |value| factor * (value - @center) }
end

#reset(inputs) ⇒ Object



29
30
31
32
# File 'lib/neuronet/scale.rb', line 29

def reset(inputs)
  @center = @spread = nil
  set(inputs)
end

#set(inputs) ⇒ Object



22
23
24
25
26
27
# File 'lib/neuronet/scale.rb', line 22

def set(inputs)
  min, max = inputs.minmax
  @center ||= (max + min) / 2.0
  @spread ||= (max - min) / 2.0
  self
end

#unmapped(outputs) ⇒ Object Also known as: unmapped_input, unmapped_output

Note that it could also unmap inputs, but outputs is typically what’s being transformed back.



43
44
45
46
# File 'lib/neuronet/scale.rb', line 43

def unmapped(outputs)
  factor = @factor * @spread
  outputs.map { |value| (factor * value) + @center }
end