Class: Phren::Moldmaker

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Moldmaker

Returns a new instance of Moldmaker.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :molding_rate (Float)
  • :momentum (Float)
  • :max_iterations (Integer)


7
8
9
10
11
# File 'lib/moldmaker.rb', line 7

def initialize(opts = {})
@molding_rate = opts[:molding_rate]
@momentum = opts[:momentum]
@max_iterations = opts[:max_iterations]
end

Instance Method Details

#irritate(stimuli) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/moldmaker.rb', line 28

def irritate(stimuli)
  
  0.upto(@network.num_of_inputs-1) { |i|
    @network.layers[0].neurons[i].value = stimuli[i]
  }
  
  
  
  1.upto(@network.num_of_layers-1) { |l|
    @network.layers[l].neurons.each { |n|
      n.value = net_input(n)
      n.value = sig(n.value)
    }
  }
  
end

#learn(expected_response) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/moldmaker.rb', line 57

def learn(expected_response)

  @network.layers[@network.num_of_layers-1].neurons.each_with_index { |n, i|
    expected_value = expected_response[i]
    n.error_signal = n.value * (1 - n.value) * (n.value - expected_value)
  }

  # compute deltas - error_signal
  
  
  (@network.num_of_layers-2).downto(0) { |l|
    @network.layers[l].neurons.each { |n|
      n.error_signal = n.value * (1 - n.value) * net_output(n)
    }
  }

  # TODO reduce it to one forloop
  1.upto(@network.num_of_layers-1) { |l|
    @network.layers[l].neurons.each { |n|
      @network.synapses.keys.each { |k|
        if k[1] == n.id[0]
          @network.synapses[k].each { |s|
            if (s.to == n)
              w = s.weight
              s.weight = s.weight - (@molding_rate * n.error_signal * s.from.value)
              #puts "Just updated synapse from Neuron(Layer:#{s.from.id[0]},#{s.from.id[1]} to Neuron(Layer:#{s.to.id[0]},#{s.to.id[1]}) !!"
            #  puts "From #{w} became #{s.weight} with error signal #{n.error_signal} \n"
            #  puts "\n\n"
            end
          }
        end
      }
    }
  }
  
end

#mold(network, stimulus, expected_responses) ⇒ Object

Parameters:

  • an (Phren::Network)

    architecture of network

  • with (Array)

    elements array of inputs

  • with (Array)

    elements array of outputs



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/moldmaker.rb', line 16

def mold(network, stimulus, expected_responses)
  @network = network

  1.upto(@max_iterations) { |iter|
    stimulus.each_index { |i|
      self.irritate(stimulus[i])
      self.learn(expected_responses[i])
    }
  }
  
end

#net_input(neuron) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/moldmaker.rb', line 99

def net_input(neuron)
  net_j = 0
  @network.synapses.keys.each { |k|
    if k[1] == neuron.id[0]
      @network.synapses[k].each { |s|
        if (s.to == neuron)
          # from_synapses << s
          net_j += (s.from.value * s.weight)
        end
      }
    end
  }
  return net_j
end

#net_output(neuron) ⇒ Object

να καλείται ως προς το δίκτυο



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/moldmaker.rb', line 115

def net_output(neuron)
  net_o = 0
  @network.synapses.keys.each { |k|
    if k[0] == neuron.id[0]
      @network.synapses[k].each { |s|
        if (s.from == neuron)
          net_o += (s.to.error_signal * s.weight)
        end
      }
    end
  }
  return net_o
end

#sig(x) ⇒ Object



94
95
96
# File 'lib/moldmaker.rb', line 94

def sig(x)
  return (1 / (1 + Math.exp(-x)))
end

#test(inputs) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/moldmaker.rb', line 45

def test(inputs)
  outputs = []
  irritate(inputs)
  @network.layers[@network.num_of_layers-1].neurons.each { |n|
    outputs << n.value
  }

  return outputs
  
end