Class: Newral::Training::Greedy

Inherits:
Object
  • Object
show all
Defined in:
lib/newral/training/greedy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input: [], output: [], iterations: 10**5, klass: Newral::Functions::Polynomial, klass_args: {}, start_function: nil) ⇒ Greedy

Returns a new instance of Greedy.



5
6
7
8
9
10
11
12
# File 'lib/newral/training/greedy.rb', line 5

def initialize( input: [], output: [], iterations:10**5, klass: Newral::Functions::Polynomial, klass_args: {}, start_function: nil   )
  @input = input
  @output = output
  @iterations = iterations
  @klass = klass
  @klass_args = klass_args
  @best_function = start_function
end

Instance Attribute Details

#best_errorObject (readonly)

Returns the value of attribute best_error.



4
5
6
# File 'lib/newral/training/greedy.rb', line 4

def best_error
  @best_error
end

#best_functionObject (readonly)

Returns the value of attribute best_function.



4
5
6
# File 'lib/newral/training/greedy.rb', line 4

def best_function
  @best_function
end

#inputObject (readonly)

Returns the value of attribute input.



4
5
6
# File 'lib/newral/training/greedy.rb', line 4

def input
  @input
end

Instance Method Details

#process(start_fresh: false) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/newral/training/greedy.rb', line 15

def process( start_fresh: false )
  @best_function = case 
    when start_fresh then @klass.create_random( @klass_args ) 
    when  @best_function then @best_function
  else 
     @klass.create_random( @klass_args )
  end
  @best_error = @best_function.calculate_error( input: @input, output: @output )
  @iterations.times do |i|
    function = @best_function.dup.move_random # move random is easier to implement with different function types
    error = function.calculate_error( input: @input, output: @output )
    if error < @best_error
      @best_error = error 
      @best_function = function
    end 
  end 
  @best_function
end