Class: AssociativeMemory::Network
- Inherits:
-
Object
- Object
- AssociativeMemory::Network
- Defined in:
- lib/associative_memory/network.rb
Instance Attribute Summary collapse
-
#matrix ⇒ Object
Returns the value of attribute matrix.
Instance Method Summary collapse
- #bitmask(vector) ⇒ Object
- #converge_and_bitmask_input(input) ⇒ Object
- #converge_and_bitmask_output(output) ⇒ Object
- #converge_input(input) ⇒ Object
- #converge_output(output) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(options = {}) ⇒ Network
constructor
A new instance of Network.
- #learn(input, output) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Network
Returns a new instance of Network.
8 9 10 |
# File 'lib/associative_memory/network.rb', line 8 def initialize(={}) @matrix = [] end |
Instance Attribute Details
#matrix ⇒ Object
Returns the value of attribute matrix.
6 7 8 |
# File 'lib/associative_memory/network.rb', line 6 def matrix @matrix end |
Instance Method Details
#bitmask(vector) ⇒ Object
56 57 58 59 60 |
# File 'lib/associative_memory/network.rb', line 56 def bitmask(vector) vector.map do |element| if element < 0 then 0 else 1 end end end |
#converge_and_bitmask_input(input) ⇒ Object
43 44 45 |
# File 'lib/associative_memory/network.rb', line 43 def converge_and_bitmask_input(input) bitmask(converge_input(input)) end |
#converge_and_bitmask_output(output) ⇒ Object
52 53 54 |
# File 'lib/associative_memory/network.rb', line 52 def converge_and_bitmask_output(output) bitmask(converge_output(output)) end |
#converge_input(input) ⇒ Object
38 39 40 41 |
# File 'lib/associative_memory/network.rb', line 38 def converge_input(input) output_vector = Matrix.row_vector(input) * Matrix.rows(self.matrix) return output_vector.row(0).to_a end |
#converge_output(output) ⇒ Object
47 48 49 50 |
# File 'lib/associative_memory/network.rb', line 47 def converge_output(output) input_vector = Matrix.row_vector(output) * Matrix.rows(self.matrix).transpose return input_vector.row(0).to_a end |
#empty? ⇒ Boolean
12 13 14 |
# File 'lib/associative_memory/network.rb', line 12 def empty? self.matrix.length == 0 end |
#learn(input, output) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/associative_memory/network.rb', line 20 def learn(input, output) input_buffer = [] output_buffer = [] input.each_with_index do |scalar, index| input_buffer[index] = 2 * scalar - 1 end output.each_with_index do |scalar, index| output_buffer[index] = 2 * scalar - 1 end input.each_with_index do |input_scalar, input_index| output.each_with_index do |output_scalar, output_index| self.matrix[input_index] ||= [] self.matrix[input_index][output_index] ||= 0 self.matrix[input_index][output_index] += input_buffer[input_index] * output_buffer[output_index] end end end |
#valid? ⇒ Boolean
16 17 18 |
# File 'lib/associative_memory/network.rb', line 16 def valid? self.matrix.length > 0 end |