Class: Newral::Functions::Vector

Inherits:
Base
  • Object
show all
Defined in:
lib/newral/functions/vector.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

Constructor Details

#initialize(vector: [1,1], bias: 0) ⇒ Vector

Returns a new instance of Vector.



7
8
9
10
# File 'lib/newral/functions/vector.rb', line 7

def initialize( vector: [1,1], bias: 0 )
  @vector = vector
  @bias = bias
end

Instance Attribute Details

#biasObject

Returns the value of attribute bias.



6
7
8
# File 'lib/newral/functions/vector.rb', line 6

def bias
  @bias
end

#weightsObject

Returns the value of attribute weights.



6
7
8
# File 'lib/newral/functions/vector.rb', line 6

def weights
  @weights
end

Class Method Details

.create_random(length: 2, low_range: -9,, high_range: 9) ⇒ Object



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

def self.create_random(  length: 2, low_range: -9, high_range: 9 )
   vector = []
   length.times do 
     vector << low_range+rand(high_range-low_range)
   end
   self.new( vector: vector, bias: low_range+rand(high_range-low_range) )
end

Instance Method Details

#calculate(input) ⇒ Object



12
13
14
# File 'lib/newral/functions/vector.rb', line 12

def calculate( input ) 
  @vector.zip( input ).map{|x, y| x * y}.sum  + @bias
end

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



28
29
30
31
32
33
34
35
36
# File 'lib/newral/functions/vector.rb', line 28

def move( direction: 0, step:0.01, step_percentage: nil )
  raise Errors::InvalidDirection if direction >= number_of_directions
  if direction < @vector.size 
    @vector[direction] = step_percentage ?  @vector[direction]*(1+step_percentage.to_f/100) : @vector[direction]+step
  else
    @bias = step_percentage ?  @bias*(1+step_percentage.to_f/100) : @bias+step
  end
  self
end

#move_with_gradient(input: [], output: [], learning_rate: 0.01, step: nil) ⇒ Object

step argument is ignored here



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/newral/functions/vector.rb', line 39

def move_with_gradient( input:[], output:[], learning_rate: 0.01, step: nil )
   bias_gradient = 0
   vector_gradient = [0]*@vector.length
   input.each_with_index do |input_vector,idx|
     bias_gradient = bias_gradient-2.0/input.size*( output[idx]-(  @vector.zip( input_vector ).map{|x, y| x * y}.sum + @bias ))
     vector_gradient.each_with_index do |v,idx_2|
        vector_gradient[idx_2]=v-2.0/input.size*input_vector[idx_2]*( output[idx]-( @vector.zip( input_vector ).map{|x, y| x * y}.sum+@bias) )
     end
   end 
  @bias = @bias - (learning_rate * bias_gradient)
  new_vector = []
  @vector.each_with_index do |value,idx| 
    new_vector << value - (learning_rate * vector_gradient[idx])
  end 
  @vector = new_vector
  self
end

#number_of_directionsObject



24
25
26
# File 'lib/newral/functions/vector.rb', line 24

def number_of_directions
  @vector.size+1
end