associative_memory 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/.rspec +1 -0
- data/.rvmrc +1 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +50 -0
- data/Guardfile +5 -0
- data/History.txt +12 -0
- data/Manifest.txt +14 -0
- data/README.rdoc +129 -0
- data/Rakefile +11 -0
- data/lib/associative_memory.rb +91 -0
- data/lib/associative_memory/network.rb +62 -0
- data/spec/associative_memory/network_spec.rb +64 -0
- data/spec/associative_memory_spec.rb +75 -0
- data/spec/spec_helper.rb +7 -0
- metadata +117 -0
data/.gemtest
ADDED
File without changes
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour --format documentation
|
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.2@associative_memory
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
RedCloth (4.2.9)
|
5
|
+
activesupport (2.3.14)
|
6
|
+
diff-lcs (1.1.3)
|
7
|
+
ffi (1.0.11)
|
8
|
+
guard (1.0.1)
|
9
|
+
ffi (>= 0.5.0)
|
10
|
+
thor (~> 0.14.6)
|
11
|
+
guard-rspec (0.7.0)
|
12
|
+
guard (>= 0.10.0)
|
13
|
+
hoe (3.0.1)
|
14
|
+
rake (~> 0.8)
|
15
|
+
multi_json (1.2.0)
|
16
|
+
newgem (1.5.3)
|
17
|
+
RedCloth (>= 4.1.1)
|
18
|
+
activesupport (~> 2.3.4)
|
19
|
+
hoe (>= 2.4.0)
|
20
|
+
rubigen (>= 1.5.3)
|
21
|
+
syntax (>= 1.0.0)
|
22
|
+
rake (0.9.2.2)
|
23
|
+
rspec (2.9.0)
|
24
|
+
rspec-core (~> 2.9.0)
|
25
|
+
rspec-expectations (~> 2.9.0)
|
26
|
+
rspec-mocks (~> 2.9.0)
|
27
|
+
rspec-core (2.9.0)
|
28
|
+
rspec-expectations (2.9.0)
|
29
|
+
diff-lcs (~> 1.1.3)
|
30
|
+
rspec-mocks (2.9.0)
|
31
|
+
rubigen (1.5.8)
|
32
|
+
activesupport (>= 2.3.5, < 3.2.0)
|
33
|
+
simplecov (0.6.1)
|
34
|
+
multi_json (~> 1.0)
|
35
|
+
simplecov-html (~> 0.5.3)
|
36
|
+
simplecov-html (0.5.3)
|
37
|
+
syntax (1.0.0)
|
38
|
+
thor (0.14.6)
|
39
|
+
|
40
|
+
PLATFORMS
|
41
|
+
ruby
|
42
|
+
|
43
|
+
DEPENDENCIES
|
44
|
+
guard
|
45
|
+
guard-rspec
|
46
|
+
hoe
|
47
|
+
newgem
|
48
|
+
rake
|
49
|
+
rspec
|
50
|
+
simplecov
|
data/Guardfile
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
guard 'rspec', :version => 2, :cli => '--colour --format documentation', :focus => true, :all_on_start => true, :all_after_pass => false, :keep_failed => false do
|
2
|
+
watch(%r{^spec/.+_spec\.rb$})
|
3
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
4
|
+
watch('spec/spec_helper.rb') { "spec" }
|
5
|
+
end
|
data/History.txt
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
|
2
|
+
=== 0.2 2012-04-24
|
3
|
+
|
4
|
+
* Completed named interface set for hereoassociative network implementation
|
5
|
+
|
6
|
+
=== 0.1 2012-04-24
|
7
|
+
|
8
|
+
* Completed heteroassociative neural network implementation
|
9
|
+
|
10
|
+
=== 0.0.1 2012-03-27
|
11
|
+
|
12
|
+
* Initial development work, module layout, starting on heteroassociative network
|
data/Manifest.txt
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
.rspec
|
2
|
+
.rvmrc
|
3
|
+
Gemfile
|
4
|
+
Gemfile.lock
|
5
|
+
Guardfile
|
6
|
+
History.txt
|
7
|
+
Manifest.txt
|
8
|
+
README.rdoc
|
9
|
+
Rakefile
|
10
|
+
lib/associative_memory.rb
|
11
|
+
lib/associative_memory/network.rb
|
12
|
+
spec/associative_memory/network_spec.rb
|
13
|
+
spec/associative_memory_spec.rb
|
14
|
+
spec/spec_helper.rb
|
data/README.rdoc
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
# Associative Memory
|
2
|
+
|
3
|
+
http://dann.stayskal.com/software/associative_memory
|
4
|
+
|
5
|
+
## Description
|
6
|
+
|
7
|
+
This is a ruby gem that lets you implement categorization systems with ease.
|
8
|
+
|
9
|
+
**Associative memory neural networks** make it easy to identify probable patterns between sets of named data points. It can be cumbersome to interface with the neural network directly, however, as a typical convergence matrix has a fixed size and training period, which limits how useful they can be to an integrated system.
|
10
|
+
|
11
|
+
associative_memory simplifies these kind of machine learning models by offering dynamically configurable input and output sets, and a convergence model that adapts to the inputs you give it each time. This allows your code to concentrate on extrapolating meaningful patterns rather than juggling bitmasks and transposition matrices.
|
12
|
+
|
13
|
+
Under the hood, associative_memory implements a hetero-associative recurrent neural network designed according to Kosko's landmark paper (http://sipi.usc.edu/~kosko/BAM.pdf) establishing the model. The model then dynamically rebuilds and adapts this network to accomodate new inputs as necessary.
|
14
|
+
|
15
|
+
## Synopsis
|
16
|
+
|
17
|
+
First, you'll want to tell `associative_memory` what you know about the set of things you're dealing with:
|
18
|
+
|
19
|
+
require 'associative_memory'
|
20
|
+
@animals = AssociativeMemory.new
|
21
|
+
|
22
|
+
@animals.associate([:tail, :fur, :legs, :paws], [:cats, :rats])
|
23
|
+
@animals.associate([:fins, :swimming], [:fish])
|
24
|
+
@animals.associate([:tail, :shell], [:turtles])
|
25
|
+
@animals.associate([:arms, :legs], [:humans])
|
26
|
+
@animals.associate([:swimming], [:humans, :rats, :turtles])
|
27
|
+
@animals.associate([:running], [:humans, :rats, :cats])
|
28
|
+
|
29
|
+
Once you've done that, you can start asking it questions about patterns you've told it about:
|
30
|
+
|
31
|
+
running_things = @animals.describe([:running])
|
32
|
+
[:cats, :rats, :humans].each do |thing|
|
33
|
+
running_things.should include(thing)
|
34
|
+
end
|
35
|
+
@animals.describe([:humans]).should == [:arms, :legs, :running, :swimming]
|
36
|
+
@animals.describe([:swimming]).should == [:rats, :fish, :humans, :turtles]
|
37
|
+
@animals.describe([:tail]).should == [:cats, :rats, :fish, :turtles]
|
38
|
+
|
39
|
+
Furthermore, it will be able to extrapolate patterns from data not explicitly taught:
|
40
|
+
|
41
|
+
@animals.describe([:fish]).should include(:tail)
|
42
|
+
|
43
|
+
If you have more patterns to input, you can do it at any time:
|
44
|
+
|
45
|
+
@animals.associate([:jumping], [:humans, :rats, :cats])
|
46
|
+
@animals.describe([:humans]).should include(:jumping)
|
47
|
+
|
48
|
+
## Installation
|
49
|
+
|
50
|
+
When using RVM:
|
51
|
+
|
52
|
+
$ gem install associative_memory
|
53
|
+
|
54
|
+
When using Bundler:
|
55
|
+
|
56
|
+
# Add to your Gemfile
|
57
|
+
gem "associative_memory"
|
58
|
+
|
59
|
+
# Then install through Bundler
|
60
|
+
$ bundle install
|
61
|
+
|
62
|
+
Otherwise:
|
63
|
+
|
64
|
+
$ sudo gem install associative_memory
|
65
|
+
|
66
|
+
## TODO
|
67
|
+
|
68
|
+
* Implement auto-associative neural network model (v.0.3)
|
69
|
+
* Streamline network class with Matrix rather than Array
|
70
|
+
|
71
|
+
## Maintenance
|
72
|
+
|
73
|
+
If you would like to help maintain or improve this gem, I welcome your patches. The build environment of this gem is streamlined for test-driven development using bundler, rvm, rspec, and guard. To get it setup, you'll need to have Ruby Version Manager (http://beginrescueend.com/) installed, then do the following:
|
74
|
+
|
75
|
+
$ git clone git@github.com:danndalf/associative_memory
|
76
|
+
$ cd associative_memory
|
77
|
+
# ...and accept the .rvmrc
|
78
|
+
# have RVM build ruby 1.9 if necessary
|
79
|
+
$ gem install bundler
|
80
|
+
$ bundle install
|
81
|
+
$ bundle exec guard start
|
82
|
+
|
83
|
+
Once guard starts, it will run through the full test suite. After any changes are made to the libraries or specs, guard will re-run the relevant tests. To re-run the full test suite, press enter at tie `> ` prompt in guard.
|
84
|
+
|
85
|
+
After each test run, [simplecov](https://github.com/colszowka/simplecov) will generate a test coverage report in `coverage/index.html`. This should show 100% coverage across all files when running the full test suite.
|
86
|
+
|
87
|
+
If you would like to patch this gem:
|
88
|
+
|
89
|
+
* Fork this repository
|
90
|
+
* Write your tests
|
91
|
+
* Make your changes
|
92
|
+
* Once all tests are passing and `simplecov` tells you all files are 100% covered, commit and push your changes
|
93
|
+
* Send me a pull request
|
94
|
+
* Wait for me to respond
|
95
|
+
|
96
|
+
This will help me integrate your patch as quickly and reliably as possible.
|
97
|
+
|
98
|
+
If you'd rather report a bug, please [open an issue on github](https://github.com/danndalf/associative_memory/issues).
|
99
|
+
|
100
|
+
## Resources
|
101
|
+
|
102
|
+
* Support: http://dann.stayskal.com/software/associative_memory
|
103
|
+
|
104
|
+
* Source code: http://github.com/danndalf/associative_memory
|
105
|
+
|
106
|
+
## License
|
107
|
+
|
108
|
+
This module is available under The MIT License.
|
109
|
+
|
110
|
+
Copyright (c) 2012 Dann Stayskal <dann@stayskal.com>.
|
111
|
+
|
112
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
113
|
+
a copy of this software and associated documentation files (the
|
114
|
+
'Software'), to deal in the Software without restriction, including
|
115
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
116
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
117
|
+
permit persons to whom the Software is furnished to do so, subject to
|
118
|
+
the following conditions:
|
119
|
+
|
120
|
+
The above copyright notice and this permission notice shall be
|
121
|
+
included in all copies or substantial portions of the Software.
|
122
|
+
|
123
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
124
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
125
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
126
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
127
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
128
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
129
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'associative_memory/network'
|
2
|
+
|
3
|
+
module AssociativeMemory
|
4
|
+
|
5
|
+
VERSION = '0.2.0'
|
6
|
+
class << self
|
7
|
+
|
8
|
+
attr_accessor :associated_pairs, :network, :input_keyspace, :output_keyspace
|
9
|
+
|
10
|
+
def new(options={})
|
11
|
+
self.associated_pairs = []
|
12
|
+
self.input_keyspace = []
|
13
|
+
self.output_keyspace = []
|
14
|
+
return self
|
15
|
+
end
|
16
|
+
|
17
|
+
def associate(inputs=[], outputs=[])
|
18
|
+
existing_input_keyspace_length = self.input_keyspace.length
|
19
|
+
existing_output_keyspace_length = self.output_keyspace.length
|
20
|
+
|
21
|
+
# Determine updated keyspace mapping
|
22
|
+
self.associated_pairs.push([inputs, outputs])
|
23
|
+
self.input_keyspace = self.associated_pairs.map{|pair| pair[0]}.flatten.uniq.sort
|
24
|
+
self.output_keyspace = self.associated_pairs.map{|pair| pair[1]}.flatten.uniq.sort
|
25
|
+
@input_bitmask = self.input_keyspace.map{|element| if inputs.include?(element) then 1 else 0 end }
|
26
|
+
@output_bitmask = self.output_keyspace.map{|element| if outputs.include?(element) then 1 else 0 end }
|
27
|
+
|
28
|
+
# If this new association changes the cardinality of our input or
|
29
|
+
# output pattern space, refresh structure of the network
|
30
|
+
if self.input_keyspace.length != existing_input_keyspace_length || self.output_keyspace.length != existing_output_keyspace_length
|
31
|
+
self.network = AssociativeMemory::Network.new
|
32
|
+
self.associated_pairs.each{|pair|
|
33
|
+
pairwise_input_bitmask = self.input_keyspace.map{|element| if pair[0].include?(element) then 1 else 0 end }
|
34
|
+
pairwise_output_bitmask = self.output_keyspace.map{|element| if pair[1].include?(element) then 1 else 0 end }
|
35
|
+
self.network.learn(pairwise_input_bitmask,pairwise_output_bitmask)
|
36
|
+
}
|
37
|
+
else
|
38
|
+
self.network.learn(@input_bitmask, @output_bitmask)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def describe(vector)
|
43
|
+
description = []
|
44
|
+
|
45
|
+
# Search forward through the input keyspace
|
46
|
+
input_bitmask = self.input_keyspace.map{|input_key| if vector.include?(input_key) then 1 else 0 end }
|
47
|
+
if input_bitmask.include?(1)
|
48
|
+
convergence_bitmask = self.network.converge_and_bitmask_input(input_bitmask)
|
49
|
+
self.output_keyspace.each_with_index do |output_key, index|
|
50
|
+
if convergence_bitmask[index] == 1
|
51
|
+
description.push(output_key)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Search backwards through the output keyspace
|
57
|
+
output_bitmask = self.output_keyspace.map{|output_key| if vector.include?(output_key) then 1 else 0 end }
|
58
|
+
if output_bitmask.include?(1)
|
59
|
+
convergence_bitmask = self.network.converge_and_bitmask_output(output_bitmask)
|
60
|
+
convergence_vector = self.network.converge_output(output_bitmask)
|
61
|
+
self.input_keyspace.each_with_index do |input_key, index|
|
62
|
+
if convergence_bitmask[index] == 1
|
63
|
+
description.push(input_key)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
return description.sort.uniq
|
69
|
+
end
|
70
|
+
|
71
|
+
def valid?
|
72
|
+
return self.associated_pairs.length > 0 &&self.input_keyspace.length > 0 &&self.output_keyspace.length > 0 && self.network.valid?
|
73
|
+
end
|
74
|
+
|
75
|
+
def pretty_inspect
|
76
|
+
"associative_memory object: #{self.object_id}\n\n" +
|
77
|
+
"associated_pairs\n" +
|
78
|
+
"----------------\n" +
|
79
|
+
self.associated_pairs.map{|a| a.inspect}.join("\n") + "\n\n" +
|
80
|
+
"input_keyspace\n" +
|
81
|
+
"--------------\n" +
|
82
|
+
self.input_keyspace.inspect + "\n\n" +
|
83
|
+
"output_keyspace\n" +
|
84
|
+
"---------------\n" +
|
85
|
+
self.output_keyspace.inspect + "\n\n" +
|
86
|
+
"convergence network\n" +
|
87
|
+
"-------------------\n" +
|
88
|
+
self.network.matrix.map{|a| a.inspect}.join("\n") + "\n\n"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'matrix'
|
2
|
+
|
3
|
+
module AssociativeMemory
|
4
|
+
class Network
|
5
|
+
|
6
|
+
attr_accessor :matrix
|
7
|
+
|
8
|
+
def initialize(options={})
|
9
|
+
@matrix = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def empty?
|
13
|
+
self.matrix.length == 0
|
14
|
+
end
|
15
|
+
|
16
|
+
def valid?
|
17
|
+
self.matrix.length > 0
|
18
|
+
end
|
19
|
+
|
20
|
+
def learn(input, output)
|
21
|
+
input_buffer = []
|
22
|
+
output_buffer = []
|
23
|
+
input.each_with_index do |scalar, index|
|
24
|
+
input_buffer[index] = 2 * scalar - 1
|
25
|
+
end
|
26
|
+
output.each_with_index do |scalar, index|
|
27
|
+
output_buffer[index] = 2 * scalar - 1
|
28
|
+
end
|
29
|
+
input.each_with_index do |input_scalar, input_index|
|
30
|
+
output.each_with_index do |output_scalar, output_index|
|
31
|
+
self.matrix[input_index] ||= []
|
32
|
+
self.matrix[input_index][output_index] ||= 0
|
33
|
+
self.matrix[input_index][output_index] += input_buffer[input_index] * output_buffer[output_index]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def converge_input(input)
|
39
|
+
output_vector = Matrix.row_vector(input) * Matrix.rows(self.matrix)
|
40
|
+
return output_vector.row(0).to_a
|
41
|
+
end
|
42
|
+
|
43
|
+
def converge_and_bitmask_input(input)
|
44
|
+
bitmask(converge_input(input))
|
45
|
+
end
|
46
|
+
|
47
|
+
def converge_output(output)
|
48
|
+
input_vector = Matrix.row_vector(output) * Matrix.rows(self.matrix).transpose
|
49
|
+
return input_vector.row(0).to_a
|
50
|
+
end
|
51
|
+
|
52
|
+
def converge_and_bitmask_output(output)
|
53
|
+
bitmask(converge_output(output))
|
54
|
+
end
|
55
|
+
|
56
|
+
def bitmask(vector)
|
57
|
+
vector.map do |element|
|
58
|
+
if element < 0 then 0 else 1 end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe AssociativeMemory::Network do
|
4
|
+
let(:training_data) {[
|
5
|
+
{:input => [1, 0, 1, 0, 1, 0], :output => [1, 1, 0, 0], :converged_output => [5, 7, -5, -7], :converged_input => [4, -4, 4, -4, 4, -4] },
|
6
|
+
{:input => [1, 1, 1, 0, 0, 0], :output => [0, 1, 1, 0], :converged_output => [-1, 5, 1, -5], :converged_input => [2, 2, 2, -2, -2, -2] },
|
7
|
+
{:input => [0, 1, 0, 1, 0, 1], :output => [0, 0, 1, 1], :converged_output => [-5, -7, 5, 7], :converged_input => [-4, 4, -4, 4, -4, 4] }
|
8
|
+
]}
|
9
|
+
describe "a new associative memory network" do
|
10
|
+
before do
|
11
|
+
@network = AssociativeMemory::Network.new
|
12
|
+
end
|
13
|
+
it "should be a kind of associative memory network" do
|
14
|
+
@network.should be_a_kind_of(AssociativeMemory::Network)
|
15
|
+
end
|
16
|
+
it "should be empty" do
|
17
|
+
@network.empty?.should be_true
|
18
|
+
end
|
19
|
+
it "should not be valid until we learn a pattern" do
|
20
|
+
@network.valid?.should be_false
|
21
|
+
end
|
22
|
+
it "should be valid once we learn a pattern" do
|
23
|
+
@network.learn(training_data[0][:input], training_data[0][:output])
|
24
|
+
@network.valid?.should be_true
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "training a network" do
|
29
|
+
before do
|
30
|
+
@network = AssociativeMemory::Network.new
|
31
|
+
end
|
32
|
+
it "should build a valid convergence matrix from a single data point" do
|
33
|
+
@network.learn(training_data[0][:input], training_data[0][:output])
|
34
|
+
@network.matrix.should == [[1, 1, -1, -1], [-1, -1, 1, 1], [1, 1, -1, -1], [-1, -1, 1, 1], [1, 1, -1, -1], [-1, -1, 1, 1]]
|
35
|
+
end
|
36
|
+
it "should build a valid convergence matrix from all data" do
|
37
|
+
training_data.each do |pair|
|
38
|
+
@network.learn(pair[:input], pair[:output])
|
39
|
+
end
|
40
|
+
@network.matrix.should == [[1, 3, -1, -3], [-3, -1, 3, 1], [1, 3, -1, -3], [-1, -3, 1, 3], [3, 1, -3, -1], [-1, -3, 1, 3]]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "converging a network" do
|
45
|
+
before do
|
46
|
+
@network = AssociativeMemory::Network.new
|
47
|
+
training_data.each do |pair|
|
48
|
+
@network.learn(pair[:input], pair[:output])
|
49
|
+
end
|
50
|
+
end
|
51
|
+
it "should rebuild all available output data from learned input data" do
|
52
|
+
training_data.each do |pair|
|
53
|
+
@network.converge_input(pair[:input]).should == pair[:converged_output]
|
54
|
+
@network.converge_and_bitmask_input(pair[:input]).should == pair[:output]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
it "should rebuild all available input data from learned output data" do
|
58
|
+
training_data.each do |pair|
|
59
|
+
@network.converge_output(pair[:output]).should == pair[:converged_input]
|
60
|
+
@network.converge_and_bitmask_output(pair[:output]).should == pair[:input]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe AssociativeMemory do
|
4
|
+
|
5
|
+
it "should have a valid version number" do
|
6
|
+
AssociativeMemory::VERSION.should match(/\d+\.\d+\.\d+/)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "providing some association pairs" do
|
10
|
+
before do
|
11
|
+
@animals = AssociativeMemory.new
|
12
|
+
@animals.associate([:tail, :shell], [:turtles])
|
13
|
+
@animals.associate([:arms, :legs], [:humans])
|
14
|
+
end
|
15
|
+
it "should construct a valid memory" do
|
16
|
+
@animals.valid?.should be_true
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "describing a memory" do
|
21
|
+
before do
|
22
|
+
@animals = AssociativeMemory.new
|
23
|
+
@animals.associate([:tail, :fur, :legs, :paws], [:cats, :rats])
|
24
|
+
@animals.associate([:fins, :swimming], [:fish])
|
25
|
+
@animals.associate([:tail, :shell], [:turtles])
|
26
|
+
@animals.associate([:arms, :legs], [:humans])
|
27
|
+
@animals.associate([:swimming], [:humans, :rats, :turtles])
|
28
|
+
@animals.associate([:running], [:humans, :rats, :cats])
|
29
|
+
end
|
30
|
+
it "should reconstruct things statically trained" do
|
31
|
+
running_things = @animals.describe([:running])
|
32
|
+
[:cats, :rats, :humans].each do |thing|
|
33
|
+
running_things.should include(thing)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
it "should reconstruct things dynamically trained (over multiple learnings)" do
|
37
|
+
@animals.describe([:humans]).should == [:arms, :legs, :running, :swimming]
|
38
|
+
@animals.describe([:swimming]).should == [:fish, :humans, :rats, :turtles]
|
39
|
+
@animals.describe([:tail]).should == [:cats, :fish, :rats, :turtles]
|
40
|
+
end
|
41
|
+
it "should reconstruct generalizations from things not explicitly trained" do
|
42
|
+
@animals.describe([:fish]).should include(:tail)
|
43
|
+
end
|
44
|
+
it "should dynamically re-associate patterns from existing data when further trained" do
|
45
|
+
@animals.associate([:jumping], [:humans, :rats])
|
46
|
+
@animals.describe([:humans]).should include(:jumping)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "data structure inspection" do
|
51
|
+
before do
|
52
|
+
@animals = AssociativeMemory.new
|
53
|
+
@animals.associate([:tail, :fur, :legs, :paws], [:cats, :rats])
|
54
|
+
@animals.associate([:fins, :swimming], [:fish])
|
55
|
+
@inspection = @animals.pretty_inspect
|
56
|
+
end
|
57
|
+
it "should include labels of pairs, keyspaces, and the convergence network" do
|
58
|
+
@inspection.should match /associated_pairs/
|
59
|
+
@inspection.should match /input_keyspace/
|
60
|
+
@inspection.should match /output_keyspace/
|
61
|
+
@inspection.should match /convergence network/
|
62
|
+
end
|
63
|
+
it "should include the associative_memory object_id" do
|
64
|
+
@inspection.should match /associative_memory object: \d*/
|
65
|
+
end
|
66
|
+
it "should include output of trained keyspaces" do
|
67
|
+
@inspection.should match /[:fins, :fur, :legs, :paws, :swimming, :tail]/
|
68
|
+
@inspection.should match /[:cats, :rats, :fish]/
|
69
|
+
end
|
70
|
+
it "should include output of the convergence network" do
|
71
|
+
@inspection.should match /[-2, -2, 2]/
|
72
|
+
@inspection.should match /[2, 2, -2]/
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: associative_memory
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dann Stayskal
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-25 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rdoc
|
16
|
+
requirement: &16462500 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.10'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *16462500
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: newgem
|
27
|
+
requirement: &16462040 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.5.3
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *16462040
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: hoe
|
38
|
+
requirement: &16461600 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '3.0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *16461600
|
47
|
+
description: ! 'This is a ruby gem that lets you implement categorization systems
|
48
|
+
with ease.
|
49
|
+
|
50
|
+
|
51
|
+
**Associative memory neural networks** make it easy to identify probable patterns
|
52
|
+
between sets of named data points. It can be cumbersome to interface with the neural
|
53
|
+
network directly, however, as a typical convergence matrix has a fixed size and
|
54
|
+
training period, which limits how useful they can be to an integrated system.
|
55
|
+
|
56
|
+
|
57
|
+
associative_memory simplifies these kind of machine learning models by offering
|
58
|
+
dynamically configurable input and output sets, and a convergence model that adapts
|
59
|
+
to the inputs you give it each time. This allows your code to concentrate on extrapolating
|
60
|
+
meaningful patterns rather than juggling bitmasks and transposition matrices.
|
61
|
+
|
62
|
+
|
63
|
+
Under the hood, associative_memory implements a hetero-associative recurrent neural
|
64
|
+
network designed according to Kosko''s landmark paper (http://sipi.usc.edu/~kosko/BAM.pdf)
|
65
|
+
establishing the model. The model then dynamically rebuilds and adapts this network
|
66
|
+
to accomodate new inputs as necessary.'
|
67
|
+
email:
|
68
|
+
- dann@stayskal.com
|
69
|
+
executables: []
|
70
|
+
extensions: []
|
71
|
+
extra_rdoc_files:
|
72
|
+
- History.txt
|
73
|
+
- Manifest.txt
|
74
|
+
- README.rdoc
|
75
|
+
files:
|
76
|
+
- .rspec
|
77
|
+
- .rvmrc
|
78
|
+
- Gemfile
|
79
|
+
- Gemfile.lock
|
80
|
+
- Guardfile
|
81
|
+
- History.txt
|
82
|
+
- Manifest.txt
|
83
|
+
- README.rdoc
|
84
|
+
- Rakefile
|
85
|
+
- lib/associative_memory.rb
|
86
|
+
- lib/associative_memory/network.rb
|
87
|
+
- spec/associative_memory/network_spec.rb
|
88
|
+
- spec/associative_memory_spec.rb
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
- .gemtest
|
91
|
+
homepage: http://dann.stayskal.com/software/associative_memory
|
92
|
+
licenses: []
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options:
|
95
|
+
- --main
|
96
|
+
- README.rdoc
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
101
|
+
requirements:
|
102
|
+
- - ! '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project: associative_memory
|
113
|
+
rubygems_version: 1.8.15
|
114
|
+
signing_key:
|
115
|
+
specification_version: 3
|
116
|
+
summary: This is a ruby gem that lets you implement categorization systems with ease
|
117
|
+
test_files: []
|