Class: Newral::Functions::Block

Inherits:
Base
  • Object
show all
Defined in:
lib/newral/functions/block.rb

Overview

as its ruby we of course have to also offer the possibility for blocks

Instance Attribute Summary

Attributes inherited from Base

#center

Instance Method Summary collapse

Methods inherited from Base

#calculate_descent, #calculate_error, #calculate_for_center_distance, create_random, #error_gradient_approximation, #find_minimum, #move_random, #move_several, #move_with_gradient

Constructor Details

#initialize(directions: 0, params: [], &block) ⇒ Block

Returns a new instance of Block.

Raises:



10
11
12
13
14
15
# File 'lib/newral/functions/block.rb', line 10

def initialize( directions:0, params:[], &block )
  raise Errors::NoBlock unless block_given?
  @calculate_block = block
  @params = params
  @directions = directions || params.size
end

Instance Method Details

#calculate(input) ⇒ Object



17
18
19
# File 'lib/newral/functions/block.rb', line 17

def calculate( input ) 
  @calculate_block.call input, @params 
end

#move(direction: 0, step: 0.01, step_percentage: nil) ⇒ Object



21
22
23
24
25
# File 'lib/newral/functions/block.rb', line 21

def move( direction: 0, step:0.01, step_percentage: nil )
  raise Errors::InvalidDirection unless direction >0 && direction<@directions
  @params[direction]=(step_percentage ? @params[direction]*(1+step_percentage.to_f/100) : @params[direction]+step )
  self
end

#number_of_directionsObject



27
28
29
# File 'lib/newral/functions/block.rb', line 27

def number_of_directions 
  @directions
end