Class: RANN::GradientChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/rann/gradient_checker.rb

Class Method Summary collapse

Class Method Details

.check(network, inputs, targets, dvec) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rann/gradient_checker.rb', line 7

def self.check network, inputs, targets, dvec
  gradapprox = []

  network.params.size.times do |i|
    thetaplus = network.params.dup
    thetaplus[i] = thetaplus[i] + epsilon
    thetaminus = network.params.dup
    thetaminus[i] = thetaminus[i] - epsilon

    network.impose thetaplus
    outputs = network.evaluate inputs
    error_thetaplus = error outputs, targets
    network.reset!

    network.impose thetaminus
    outputs = network.evaluate inputs
    error_thetaminus = error outputs, targets
    network.reset!

    gradapprox[i] = (error_thetaplus - error_thetaminus) / (epsilon * 2)
  end

  gradapprox.each.with_index.with_object [] do |(ga, i), res|
    res << i unless in_epsilon? ga, dvec[i]
  end
end

.epsilonObject



3
4
5
# File 'lib/rann/gradient_checker.rb', line 3

def self.epsilon
  10.to_d ** -4
end

.error(outputs, targets) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/rann/gradient_checker.rb', line 34

def self.error outputs, targets
  total_squared_error = 0.to_d

  targets.size.times do |i|
    total_squared_error += (targets[i] - outputs[i]) ** 2 / 2
  end

  total_squared_error
end

.in_epsilon?(exp, act, epsilon = 0.001) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rann/gradient_checker.rb', line 44

def self.in_epsilon? exp, act, epsilon = 0.001
  # delta = [exp.abs, act.abs].min * epsilon
  delta = epsilon
  n = (exp - act).abs
  msg = "Expected |#{exp} - #{act}| (#{n}) to be <= #{delta}"

  if delta >= n
    true
  else
    puts msg

    false
  end
end