Class: BackPropogation::ComputationalGraph
- Inherits:
-
Object
- Object
- BackPropogation::ComputationalGraph
- Defined in:
- lib/ml_algorithms.rb
Constant Summary collapse
- PRIORITY =
Hash['(' => 0, '+' => 1, '-' => 1, '*' => 2, '/' => 2, '^' => 3]
- TEMPLATES =
{ operand: /^\s*([^\+\-\*\/\(\)\^\s]+)\s*(.*)/, string: /^\s*([\+\-\*\/\^])\s*(.*)/, brackets: /^\s*\(\s*(.*)/, nested: /^\s*\)\s*(.*)/ }
Instance Attribute Summary collapse
-
#graph ⇒ Object
Returns the value of attribute graph.
Class Method Summary collapse
- .parse_brackets(left, stack) ⇒ Object
- .parse_default(left, stack) ⇒ Object
- .parse_nested(left, right, stack) ⇒ Object
- .parse_operand(left, right, stack) ⇒ Object
- .parse_string(left, right, i_str, stack) ⇒ Object
-
.polish_parser(i_str, stack) ⇒ Object
String preprocessing algorithm expression for computation.
Instance Method Summary collapse
-
#backward_pass(loss_value) ⇒ Object
Compute a gradient value for inputs.
-
#forward_pass(variables_val) ⇒ Object
Compute a value of expression.
-
#initialize(expr_s) ⇒ ComputationalGraph
constructor
A new instance of ComputationalGraph.
Constructor Details
#initialize(expr_s) ⇒ ComputationalGraph
Returns a new instance of ComputationalGraph.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ml_algorithms.rb', line 14 def initialize(expr_s) exprproc = ComputationalGraph::polish_parser(expr_s, []) pregraph = [] @graph = [] exprproc.split.each do |elem| case elem when '+' dot = ComputationalGates::SummGate.new(elem) dot.connect(pregraph.pop,pregraph.pop) when '*' dot = ComputationalGates::MultGate.new(elem) dot.connect(pregraph.pop,pregraph.pop) when '/' dot = ComputationalGates::DivGate.new(elem) scnd = pregraph.pop frst = pregraph.pop dot.connect(frst,scnd) else dot = ComputationalGates::CompGate.new(elem) end pregraph.push(dot) @graph.push(dot) end end |
Instance Attribute Details
#graph ⇒ Object
Returns the value of attribute graph.
13 14 15 |
# File 'lib/ml_algorithms.rb', line 13 def graph @graph end |
Class Method Details
.parse_brackets(left, stack) ⇒ Object
83 84 85 |
# File 'lib/ml_algorithms.rb', line 83 def self.parse_brackets(left, stack) polish_parser(left, stack) end |
.parse_default(left, stack) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/ml_algorithms.rb', line 87 def self.parse_default(left, stack) return '' if stack.empty? raise ArgumentError, 'Error: Excess of opening brackets.' unless PRIORITY[stack.last] > 0 stack.pop + ' ' + polish_parser(left, stack) end |
.parse_nested(left, right, stack) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/ml_algorithms.rb', line 76 def self.parse_nested(left, right, stack) raise ArgumentError, 'Error: Excess of closing brackets.' if stack.empty? head = stack.pop PRIORITY[head].positive? ? head + ' ' + polish_parser(right, stack) : polish_parser(left, stack) end |
.parse_operand(left, right, stack) ⇒ Object
64 65 66 |
# File 'lib/ml_algorithms.rb', line 64 def self.parse_operand(left, right, stack) left + ' ' + polish_parser(right, stack) end |
.parse_string(left, right, i_str, stack) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/ml_algorithms.rb', line 68 def self.parse_string(left, right, i_str, stack) if stack.empty? || PRIORITY[stack.last] < PRIORITY[left] polish_parser(right, stack.push(left)) else stack.pop + ' ' + polish_parser(i_str, stack) end end |
.polish_parser(i_str, stack) ⇒ Object
String preprocessing algorithm expression for computation
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ml_algorithms.rb', line 95 def self.polish_parser(i_str, stack) case i_str when TEMPLATES[:operand] parse_operand(Regexp.last_match(1), Regexp.last_match(2), stack) when TEMPLATES[:string] parse_string(Regexp.last_match(1), Regexp.last_match(2), i_str, stack) when TEMPLATES[:brackets] parse_brackets(Regexp.last_match(1), stack.push('(')) when TEMPLATES[:nested] parse_nested(Regexp.last_match(1), i_str, stack) else parse_default(i_str, stack) end end |
Instance Method Details
#backward_pass(loss_value) ⇒ Object
Compute a gradient value for inputs
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ml_algorithms.rb', line 50 def backward_pass(loss_value) param_grad = Hash.new() @graph.last.bckwrd = loss_value @graph.reverse.each do |elem| if elem.class != ComputationalGates::CompGate elem.backward_pass else param_grad[elem.name] = elem.bckwrd end end param_grad end |
#forward_pass(variables_val) ⇒ Object
Compute a value of expression
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ml_algorithms.rb', line 39 def forward_pass(variables_val) @graph.each do |elem| if elem.class != ComputationalGates::CompGate elem.forward_pass else elem.frwrd = variables_val[elem.name] end end graph.last.frwrd end |