class BackPropogation::ComputationalGraph

Constants

PRIORITY
TEMPLATES

Attributes

graph[RW]

Public Class Methods

new(expr_s) click to toggle source
# 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
parse_brackets(left, stack) click to toggle source
# File lib/ml_algorithms.rb, line 83
def self.parse_brackets(left, stack)
  polish_parser(left, stack)
end
parse_default(left, stack) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

String preprocessing algorithm expression for computation

# 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

Public Instance Methods

backward_pass(loss_value) click to toggle source

Compute a gradient value for inputs

# 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) click to toggle source

Compute a value of expression

# 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