class Newral::Networks::Perceptron
Attributes
bias[R]
inputs[R]
last_output[R]
weights[R]
Public Class Methods
new(weights:[],bias:0, weight_length: nil, max_random_weight:1, min_random_weight:-1 )
click to toggle source
to create random weights just specify weight_length
# File lib/newral/networks/perceptron.rb, line 12 def initialize(weights:[],bias:0, weight_length: nil, max_random_weight:1, min_random_weight:-1 ) raise Errors::WeightsAndWeightLengthGiven if weight_length && ( weights || ( weights && weights.size > 0 )) @max_random_weight = max_random_weight @min_random_weight= min_random_weight @weights =weights || [] @weights = weight_length.times.collect{ |i| (max_random_weight-min_random_weight)*rand()+min_random_weight } if weight_length @bias = bias || (weight_length && (max_random_weight-min_random_weight)*rand()+min_random_weight ) || 0 @inputs = [] @last_output = nil end
Public Instance Methods
add_input( object )
click to toggle source
# File lib/newral/networks/perceptron.rb, line 51 def add_input( object ) if object.kind_of?( Array ) @inputs=object else @inputs << object end # automatically add a weight if number of inputs exceeds weight size weights << (@max_random_weight-@min_random_weight)*rand()+@min_random_weight if weights.length < @inputs.length end
calculate_input( input )
click to toggle source
# File lib/newral/networks/perceptron.rb, line 38 def calculate_input( input ) input.kind_of?( Perceptron ) ? input.last_output : input end
calculate_value()
click to toggle source
# File lib/newral/networks/perceptron.rb, line 28 def calculate_value raise Errors::InputAndWeightSizeDiffer, "weights: #{@weights.size }, #{ @inputs.size }" unless @weights.size == @inputs.size value = 0 @inputs.each_with_index do |input,idx| val = calculate_input( input ) value = value+val*@weights[idx] end value = value+@bias end
move( direction: 0, step:0.01, step_percentage: nil )
click to toggle source
# File lib/newral/networks/perceptron.rb, line 69 def move( direction: 0, step:0.01, step_percentage: nil ) raise Errors::InvalidDirection if direction >= number_of_directions if direction < @weights.size @weights[direction] = step_percentage ? @weights[direction]*(1+step_percentage.to_f/100) : @weights[direction]+step else @bias = step_percentage ? @bias*(1+step_percentage.to_f/100) : @bias+step end self end
number_of_directions()
click to toggle source
move + number of directions are just needed for some training algorithms not typically used for neural networks (like greedy) mainly implemented here for a proove of concept
# File lib/newral/networks/perceptron.rb, line 64 def number_of_directions @weights.size+1 end
output()
click to toggle source
# File lib/newral/networks/perceptron.rb, line 42 def output @last_output = calculate_value <= 0 ? 0 : 1 end
set_weights_and_bias( weights:[], bias: nil )
click to toggle source
# File lib/newral/networks/perceptron.rb, line 23 def set_weights_and_bias( weights:[], bias: nil ) @weights = weights @bias = bias || 0 # if none specified end
update_with_vector( inputs )
click to toggle source
# File lib/newral/networks/perceptron.rb, line 46 def update_with_vector( inputs ) @inputs = inputs output end