Class: Newral::Networks::Perceptron
- Inherits:
-
Object
- Object
- Newral::Networks::Perceptron
- Defined in:
- lib/newral/networks/perceptron.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#bias ⇒ Object
readonly
Returns the value of attribute bias.
-
#inputs ⇒ Object
readonly
Returns the value of attribute inputs.
-
#last_output ⇒ Object
readonly
Returns the value of attribute last_output.
-
#weights ⇒ Object
readonly
Returns the value of attribute weights.
Instance Method Summary collapse
- #add_input(object) ⇒ Object
- #calculate_input(input) ⇒ Object
- #calculate_value ⇒ Object
-
#initialize(weights: [], bias: 0, weight_length: nil, max_random_weight: 1, min_random_weight: -1 )) ⇒ Perceptron
constructor
to create random weights just specify weight_length.
- #move(direction: 0, step: 0.01, step_percentage: nil) ⇒ Object
-
#number_of_directions ⇒ Object
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.
- #output ⇒ Object
- #set_weights_and_bias(weights: [], bias: nil) ⇒ Object
- #update_with_vector(inputs) ⇒ Object
Constructor Details
#initialize(weights: [], bias: 0, weight_length: nil, max_random_weight: 1, min_random_weight: -1 )) ⇒ Perceptron
to create random weights just specify weight_length
12 13 14 15 16 17 18 19 20 21 |
# 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 |
Instance Attribute Details
#bias ⇒ Object (readonly)
Returns the value of attribute bias.
10 11 12 |
# File 'lib/newral/networks/perceptron.rb', line 10 def bias @bias end |
#inputs ⇒ Object (readonly)
Returns the value of attribute inputs.
10 11 12 |
# File 'lib/newral/networks/perceptron.rb', line 10 def inputs @inputs end |
#last_output ⇒ Object (readonly)
Returns the value of attribute last_output.
10 11 12 |
# File 'lib/newral/networks/perceptron.rb', line 10 def last_output @last_output end |
#weights ⇒ Object (readonly)
Returns the value of attribute weights.
10 11 12 |
# File 'lib/newral/networks/perceptron.rb', line 10 def weights @weights end |
Instance Method Details
#add_input(object) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# 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) ⇒ Object
38 39 40 |
# File 'lib/newral/networks/perceptron.rb', line 38 def calculate_input( input ) input.kind_of?( Perceptron ) ? input.last_output : input end |
#calculate_value ⇒ Object
28 29 30 31 32 33 34 35 36 |
# 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) ⇒ Object
69 70 71 72 73 74 75 76 77 |
# 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 ⇒ Object
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
64 65 66 |
# File 'lib/newral/networks/perceptron.rb', line 64 def number_of_directions @weights.size+1 end |
#output ⇒ Object
42 43 44 |
# 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) ⇒ Object
23 24 25 26 |
# 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) ⇒ Object
46 47 48 49 |
# File 'lib/newral/networks/perceptron.rb', line 46 def update_with_vector( inputs ) @inputs = inputs output end |