Class: Phren::Network

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(architecture, opts = {}) ⇒ Network

Returns a new instance of Network.

Parameters:

  • architecture (Array)

    of network



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

def initialize(architecture, opts = {})
  
  @synapses = {}
  @layers = {}

  @num_of_inputs = architecture.first
  @num_of_outputs = architecture.last
  
  @num_of_layers = architecture.length

  # TODO try to implemet it with map
  @layers[0] = Layer.new(architecture.first, 0)       
  1.upto(@num_of_layers-1) { |i| # create layers
    @layers[-i] = Layer.new(1, -i) # bias layer
    @layers[i] = Layer.new(architecture[i], i)
  }
  
  @layers.keys.each { |k|  # connect layers
    if( k < 0) # is bias
      connect_layers(@layers[k], @layers[-k])
    elsif( k > 0) # is hidden or output layer
      connect_layers(@layers[k-1], @layers[k])
    end
  }

end

Instance Attribute Details

#layersObject (readonly)

Returns the value of attribute layers.



5
6
7
# File 'lib/network.rb', line 5

def layers
  @layers
end

#num_of_inputsObject (readonly)

Returns the value of attribute num_of_inputs.



6
7
8
# File 'lib/network.rb', line 6

def num_of_inputs
  @num_of_inputs
end

#num_of_layersObject (readonly)

Returns the value of attribute num_of_layers.



5
6
7
# File 'lib/network.rb', line 5

def num_of_layers
  @num_of_layers
end

#num_of_outputsObject (readonly)

Returns the value of attribute num_of_outputs.



6
7
8
# File 'lib/network.rb', line 6

def num_of_outputs
  @num_of_outputs
end

#synapsesObject (readonly)

Returns the value of attribute synapses.



5
6
7
# File 'lib/network.rb', line 5

def synapses
  @synapses
end

Instance Method Details

#connect_input_to_output_layerObject

According to dontveter.com/bpr/basics.html is faster for xor problem



37
38
39
# File 'lib/network.rb', line 37

def connect_input_to_output_layer
  connect_layers(@layers[0], @layers[@num_of_layers-1])
end

#connect_layers(from, to) ⇒ Object



41
42
43
44
45
# File 'lib/network.rb', line 41

def connect_layers(from, to)
  @synapses[[from.id, to.id]] = Array.new(from.length * to.length){ |index|
    Synapse.new(from.neurons[index % from.length], to.neurons[index % to.length], 0) # 0 could be replace with random
  }
end