Class: DeepMiner::Perceptron
- Inherits:
-
Object
- Object
- DeepMiner::Perceptron
- Defined in:
- lib/deep_miner/perceptron.rb
Overview
ajcost
Perceptron is class that implements a single layer neural net Implemenation is made with vertex_matrix class
Instance Attribute Summary collapse
-
#change_matrix_i ⇒ Object
readonly
Returns the value of attribute change_matrix_i.
-
#change_matrix_o ⇒ Object
readonly
Returns the value of attribute change_matrix_o.
-
#hidden ⇒ Object
readonly
Returns the value of attribute hidden.
-
#hidden_delta ⇒ Object
readonly
Returns the value of attribute hidden_delta.
-
#hidden_delta_v ⇒ Object
readonly
Returns the value of attribute hidden_delta_v.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#out_delta ⇒ Object
readonly
Returns the value of attribute out_delta.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#output_delta_v ⇒ Object
readonly
Returns the value of attribute output_delta_v.
-
#weight_matrix_ho ⇒ Object
readonly
Returns the value of attribute weight_matrix_ho.
-
#weight_matrix_ih ⇒ Object
readonly
Returns the value of attribute weight_matrix_ih.
Instance Method Summary collapse
- #back_propogate(target, eta, momentum) ⇒ Object
-
#initialize(input, hidden, output) ⇒ Perceptron
constructor
A new instance of Perceptron.
- #predict(input_vector) ⇒ Object
-
#train(train_data, expect, epoch_iter = 50, eta, m) ⇒ Object
Train the network with training input, expectations, input, and epoch num.
Constructor Details
#initialize(input, hidden, output) ⇒ Perceptron
Returns a new instance of Perceptron.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/deep_miner/perceptron.rb', line 13 def initialize(input, hidden, output) fail ArgumentError, 'Input must be Array' unless input.is_a? Array fail ArgumentError, 'Output must be Array' unless output.is_a? Array @input = input @hidden = hidden @output = output # Create activation vectors and weight matrices and change matrices init_activations init_weight_matrices init_change_matrices end |
Instance Attribute Details
#change_matrix_i ⇒ Object (readonly)
Returns the value of attribute change_matrix_i.
10 11 12 |
# File 'lib/deep_miner/perceptron.rb', line 10 def change_matrix_i @change_matrix_i end |
#change_matrix_o ⇒ Object (readonly)
Returns the value of attribute change_matrix_o.
10 11 12 |
# File 'lib/deep_miner/perceptron.rb', line 10 def change_matrix_o @change_matrix_o end |
#hidden ⇒ Object (readonly)
Returns the value of attribute hidden.
7 8 9 |
# File 'lib/deep_miner/perceptron.rb', line 7 def hidden @hidden end |
#hidden_delta ⇒ Object (readonly)
Returns the value of attribute hidden_delta.
11 12 13 |
# File 'lib/deep_miner/perceptron.rb', line 11 def hidden_delta @hidden_delta end |
#hidden_delta_v ⇒ Object (readonly)
Returns the value of attribute hidden_delta_v.
9 10 11 |
# File 'lib/deep_miner/perceptron.rb', line 9 def hidden_delta_v @hidden_delta_v end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
7 8 9 |
# File 'lib/deep_miner/perceptron.rb', line 7 def input @input end |
#out_delta ⇒ Object (readonly)
Returns the value of attribute out_delta.
11 12 13 |
# File 'lib/deep_miner/perceptron.rb', line 11 def out_delta @out_delta end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
7 8 9 |
# File 'lib/deep_miner/perceptron.rb', line 7 def output @output end |
#output_delta_v ⇒ Object (readonly)
Returns the value of attribute output_delta_v.
9 10 11 |
# File 'lib/deep_miner/perceptron.rb', line 9 def output_delta_v @output_delta_v end |
#weight_matrix_ho ⇒ Object (readonly)
Returns the value of attribute weight_matrix_ho.
8 9 10 |
# File 'lib/deep_miner/perceptron.rb', line 8 def weight_matrix_ho @weight_matrix_ho end |
#weight_matrix_ih ⇒ Object (readonly)
Returns the value of attribute weight_matrix_ih.
8 9 10 |
# File 'lib/deep_miner/perceptron.rb', line 8 def weight_matrix_ih @weight_matrix_ih end |
Instance Method Details
#back_propogate(target, eta, momentum) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/deep_miner/perceptron.rb', line 38 def back_propogate(target, eta, momentum) fail ArgumentError, 'Target must be Array' unless target.is_a? Array fail ArgumentError, 'Target and Output must have same size' unless target.size == @output.size # Calculate errors for output layer errors_output(target) # Calculate errors for hidden layer errors_hidden # Update weight vector from hidden to outputs update_output_weight_vector(eta, momentum) # Update weight vector from inputs to hidden update_hidden_weight_vector(eta, momentum) calculate_error(target) end |
#predict(input_vector) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/deep_miner/perceptron.rb', line 25 def predict(input_vector) fail ArgumentError, 'Input vector must be Array' unless input_vector.is_a? Array fail ArgumentError, 'Predict input must be same size as input' unless input_vector.size == @input.size # Set activation vector for the input layer @act_input.apply_new_matrix([] << input_vector) # Calculate activation vector for the hidden layer calculate_activation_hidden # Calculate activation vector for the output layer calculate_activation_output end |
#train(train_data, expect, epoch_iter = 50, eta, m) ⇒ Object
Train the network with training input, expectations, input, and epoch num
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/deep_miner/perceptron.rb', line 57 def train(train_data, expect, epoch_iter = 50, eta, m) fail ArgumentError, 'Training must be Array' unless train_data.is_a? Array fail ArgumentError, 'Expectation must be Array' unless expect.is_a? Array fail ArgumentError, 'Data and Expectation must be same size' unless train_data.size == expect.size 1.upto(epoch_iter) do error = 0.0 train_data.zip(expect) do |sample, target| predict(sample) error += back_propogate(target, eta, m) end end end |