Class: Newral::Functions::RadialBasisFunctionNetwork

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

Instance Attribute Summary collapse

Attributes inherited from Base

#center

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(centers: [], weights: [], bias_weight: 1, klass: Newral::Functions::Gaussian) ⇒ RadialBasisFunctionNetwork

Returns a new instance of RadialBasisFunctionNetwork.



5
6
7
8
9
10
11
12
13
14
# File 'lib/newral/functions/radial_basis_function_network.rb', line 5

def initialize( centers:[], weights: [], bias_weight: 1, klass: Newral::Functions::Gaussian )
  @klass = klass 
  @weights = weights.dup
  @centers = centers.dup
  @functions = []
  @weights.each_with_index do |weight,idx|
    @functions << klass.new( center:centers[idx], factor: weight )
  end
  @functions << Line.new(factor:0,bias: bias_weight) if bias_weight != 0
end

Instance Attribute Details

#functionsObject (readonly)

Returns the value of attribute functions.



4
5
6
# File 'lib/newral/functions/radial_basis_function_network.rb', line 4

def functions
  @functions
end

#weightsObject (readonly)

Returns the value of attribute weights.



4
5
6
# File 'lib/newral/functions/radial_basis_function_network.rb', line 4

def weights
  @weights
end

Class Method Details

.create_random(length: 3, low_range: -9,, high_range: 9, klass: Newral::Functions::Gaussian) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/newral/functions/radial_basis_function_network.rb', line 25

def self.create_random(  length: 3, low_range: -9, high_range: 9, klass: Newral::Functions::Gaussian  )
   weights = []
   centers = []
   length.times do 
     weights << low_range+rand(high_range-low_range)
     centers << [low_range+rand(high_range-low_range)]
   end
   self.new( centers:centers, weights: weights, bias_weight: low_range+rand(high_range-low_range), klass: klass  )
end

Instance Method Details

#calculate(input) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/newral/functions/radial_basis_function_network.rb', line 16

def calculate( input )
  result = 0
  @functions.each do |function|
    result = result + function.calculate( input )
  end
  result.to_f
end

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



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/newral/functions/radial_basis_function_network.rb', line 39

def move( direction: 0, step:0.01, step_percentage: nil )
  raise Errors::InvalidDirection if direction >= number_of_directions
  if direction < @weights.size 
    @weights = @weights.dup
    @weights[direction] = step_percentage ? @weights[direction]*(1+step_percentage.to_f/100) : @weights[direction]+step
  else
    mod = @centers.first.size 
    @centers = @centers.dup
    @centers[(direction-@weights.size)/mod][(direction-@weights.size)%mod] = step_percentage ? @centers[(direction-@weights.size)/mod][(direction-@weights.size)%mod]*(1+step_percentage.to_f/100) : @centers[(direction-@weights.size)/mod][(direction-@weights.size)%mod]+step
  end 
  self
end

#number_of_directionsObject



35
36
37
# File 'lib/newral/functions/radial_basis_function_network.rb', line 35

def number_of_directions
  @weights.size+@centers.collect{ |c| c.size }.sum
end