Class: Qoa::NeuralNetwork
- Inherits:
-
Object
- Object
- Qoa::NeuralNetwork
- Includes:
- Err::Validations, LossFunctions, Training, Utils
- Defined in:
- lib/qoa/neural_network.rb
Instance Attribute Summary collapse
-
#activation_func ⇒ Object
readonly
Returns the value of attribute activation_func.
-
#batch_size ⇒ Object
readonly
Returns the value of attribute batch_size.
-
#decay_rate ⇒ Object
readonly
Returns the value of attribute decay_rate.
-
#dropout_rate ⇒ Object
readonly
Returns the value of attribute dropout_rate.
-
#epsilon ⇒ Object
readonly
Returns the value of attribute epsilon.
-
#hidden_layers ⇒ Object
readonly
Returns the value of attribute hidden_layers.
-
#input_nodes ⇒ Object
readonly
Returns the value of attribute input_nodes.
-
#l1_lambda ⇒ Object
readonly
Returns the value of attribute l1_lambda.
-
#l2_lambda ⇒ Object
readonly
Returns the value of attribute l2_lambda.
-
#learning_rate ⇒ Object
readonly
Returns the value of attribute learning_rate.
-
#output_nodes ⇒ Object
readonly
Returns the value of attribute output_nodes.
Instance Method Summary collapse
- #calculate_loss(inputs, targets, loss_function = :cross_entropy_loss) ⇒ Object
-
#initialize(input_nodes, hidden_layers, output_nodes, learning_rate, dropout_rate, activation_func = :leaky_relu, decay_rate = 0.9, epsilon = 1e-8, batch_size = 10, l1_lambda = 0.0, l2_lambda = 0.0) ⇒ NeuralNetwork
constructor
A new instance of NeuralNetwork.
- #query(inputs) ⇒ Object
Methods included from Err::Validations
#validate_calculate_loss_args, #validate_constructor_args, #validate_query_args, #validate_train_args
Methods included from LossFunctions
binary_cross_entropy, categorical_cross_entropy, cross_entropy_loss, mean_absolute_error, mean_squared_error
Methods included from Utils
Methods included from Training
#backward_pass, #calculate_regularization_penalty, #conv_weight_delta, #convolution, #forward_pass, #pool_weight_delta, #pooling, #train, #train_batch, #train_with_early_stopping
Methods included from MatrixHelpers
#apply_dropout, #apply_function, #matrix_add, #matrix_multiply, #matrix_multiply_element_wise, #matrix_pow, #matrix_subtract, #mean, #normalize, #scalar_add, #scalar_multiply, #scale_and_shift, #transpose, #update_beta, #update_gamma, #variance
Constructor Details
#initialize(input_nodes, hidden_layers, output_nodes, learning_rate, dropout_rate, activation_func = :leaky_relu, decay_rate = 0.9, epsilon = 1e-8, batch_size = 10, l1_lambda = 0.0, l2_lambda = 0.0) ⇒ NeuralNetwork
Returns a new instance of NeuralNetwork.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/qoa/neural_network.rb', line 19 def initialize(input_nodes, hidden_layers, output_nodes, learning_rate, dropout_rate, activation_func = :leaky_relu, decay_rate = 0.9, epsilon = 1e-8, batch_size = 10, l1_lambda = 0.0, l2_lambda = 0.0) # validate_constructor_args(input_nodes, hidden_layers, output_nodes, learning_rate, dropout_rate, activation_func, decay_rate, epsilon, batch_size, l1_lambda, l2_lambda) @input_nodes = input_nodes @hidden_layers = hidden_layers @output_nodes = output_nodes @learning_rate = learning_rate @activation_func = activation_func @dropout_rate = dropout_rate @decay_rate = decay_rate @epsilon = epsilon @batch_size = batch_size @l1_lambda = l1_lambda @l2_lambda = l2_lambda @layers = [] @layers << Qoa::Layers::Layer.new(input_nodes, hidden_layers[0].is_a?(Array) ? hidden_layers[0][1] : hidden_layers[0]) hidden_layers.each_cons(2) do |l1, l2| l1_size = l1.is_a?(Array) ? l1[1] : l1 l2_size = l2.is_a?(Array) ? l2[1] : l2 if l1.is_a?(Array) && l2.is_a?(Array) && l1[0] == :conv && l2[0] == :conv @layers << Qoa::Layers::ConvolutionalLayer.new(l1_size, l2_size, l1[2], l1[3]) elsif l1.is_a?(Array) && l1[0] == :conv && l2.is_a?(Numeric) @layers << Qoa::Layers::ConvolutionalLayer.new(l1_size, l2_size, l1[2], l1[3]) elsif l1.is_a?(Numeric) && l2.is_a?(Array) && l2[0] == :conv @layers << Qoa::Layers::ConvolutionalLayer.new(l1_size, l2_size, l2[2], l2[3]) elsif l1.is_a?(Array) && l1[0] == :pool && l2.is_a?(Numeric) @layers << Qoa::Layers::PoolingLayer.new(l1_size, l2_size, l1[2], l1[3]) elsif l1.is_a?(Numeric) && l2.is_a?(Array) && l2[0] == :pool @layers << Qoa::Layers::PoolingLayer.new(l1_size, l2_size, l2[2], l2[3]) else @layers << Qoa::Layers::Layer.new(l1_size, l2_size) end end @layers << Qoa::Layers::Layer.new(hidden_layers[-1].is_a?(Array) ? hidden_layers[-1][1] : hidden_layers[-1], output_nodes) end |
Instance Attribute Details
#activation_func ⇒ Object (readonly)
Returns the value of attribute activation_func.
17 18 19 |
# File 'lib/qoa/neural_network.rb', line 17 def activation_func @activation_func end |
#batch_size ⇒ Object (readonly)
Returns the value of attribute batch_size.
17 18 19 |
# File 'lib/qoa/neural_network.rb', line 17 def batch_size @batch_size end |
#decay_rate ⇒ Object (readonly)
Returns the value of attribute decay_rate.
17 18 19 |
# File 'lib/qoa/neural_network.rb', line 17 def decay_rate @decay_rate end |
#dropout_rate ⇒ Object (readonly)
Returns the value of attribute dropout_rate.
17 18 19 |
# File 'lib/qoa/neural_network.rb', line 17 def dropout_rate @dropout_rate end |
#epsilon ⇒ Object (readonly)
Returns the value of attribute epsilon.
17 18 19 |
# File 'lib/qoa/neural_network.rb', line 17 def epsilon @epsilon end |
#hidden_layers ⇒ Object (readonly)
Returns the value of attribute hidden_layers.
17 18 19 |
# File 'lib/qoa/neural_network.rb', line 17 def hidden_layers @hidden_layers end |
#input_nodes ⇒ Object (readonly)
Returns the value of attribute input_nodes.
17 18 19 |
# File 'lib/qoa/neural_network.rb', line 17 def input_nodes @input_nodes end |
#l1_lambda ⇒ Object (readonly)
Returns the value of attribute l1_lambda.
17 18 19 |
# File 'lib/qoa/neural_network.rb', line 17 def l1_lambda @l1_lambda end |
#l2_lambda ⇒ Object (readonly)
Returns the value of attribute l2_lambda.
17 18 19 |
# File 'lib/qoa/neural_network.rb', line 17 def l2_lambda @l2_lambda end |
#learning_rate ⇒ Object (readonly)
Returns the value of attribute learning_rate.
17 18 19 |
# File 'lib/qoa/neural_network.rb', line 17 def learning_rate @learning_rate end |
#output_nodes ⇒ Object (readonly)
Returns the value of attribute output_nodes.
17 18 19 |
# File 'lib/qoa/neural_network.rb', line 17 def output_nodes @output_nodes end |
Instance Method Details
#calculate_loss(inputs, targets, loss_function = :cross_entropy_loss) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/qoa/neural_network.rb', line 65 def calculate_loss(inputs, targets, loss_function = :cross_entropy_loss) validate_calculate_loss_args(inputs, targets, loss_function) total_loss = 0.0 inputs.zip(targets).each do |input, target| prediction = query(input) total_loss += LossFunctions.send(loss_function, prediction, target) end total_loss / inputs.size end |
#query(inputs) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/qoa/neural_network.rb', line 58 def query(inputs) validate_query_args(inputs) layer_outputs = forward_pass(inputs) layer_outputs.last.flatten end |