hopfield 1.1 → 1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +1 -1
- data/ext/hopfield/extconf.rb +3 -0
- data/ext/hopfield/hopfield.c +47 -0
- data/lib/hopfield/training.rb +15 -8
- data/lib/hopfield.rb +1 -1
- metadata +17 -19
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6f1e7bbc674b32b5f95647dcb42109ce6a37bc0b
|
4
|
+
data.tar.gz: 083b4ff167702baf75a0dd050e3fd75de42f1c97
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 110a161f19c05846bde6fa84d4775af490ec44c7c3352b71338ca4c436ce793ac3b27b09e410fc70b85b1337c33c15f59fd9eefd85c339b9a99c57032d1aebd3
|
7
|
+
data.tar.gz: 56c9fb943f7be0e5d23983c5db41e8739b1c10118252828bfa9a21f40af5d42cf69aa59ae1437d28bd70e426b2bbb1e2aa7224dc7d51470345f46544a4d47759
|
data/README.md
CHANGED
@@ -24,7 +24,7 @@ network.runs # how many propagations it took
|
|
24
24
|
```
|
25
25
|
|
26
26
|
## TODO
|
27
|
-
-
|
27
|
+
- Improve C extension to boost performance
|
28
28
|
- Turn the random picking of neurons into pseudo randomness to prevent the same neuron to be propagated over and over again
|
29
29
|
- Implement the Storkey learning rule to provide an alternative for the already implemented Hebbian learning rule.
|
30
30
|
- Release the examples
|
@@ -0,0 +1,47 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
|
3
|
+
static VALUE m_hopfield;
|
4
|
+
|
5
|
+
static VALUE hopfield_calculate_weights_hebbian(VALUE self, VALUE patterns, VALUE neurons_count) {
|
6
|
+
Check_Type(patterns, T_ARRAY);
|
7
|
+
Check_Type(neurons_count, T_FIXNUM);
|
8
|
+
|
9
|
+
int n_count = FIX2INT(neurons_count);
|
10
|
+
|
11
|
+
VALUE weights;
|
12
|
+
weights = rb_ary_new2(n_count);
|
13
|
+
for(int i = 0; i < n_count-1; i++) {
|
14
|
+
rb_ary_store(weights, i, rb_ary_new2(n_count));
|
15
|
+
}
|
16
|
+
|
17
|
+
int patterns_count = (int) RARRAY_LEN(patterns);
|
18
|
+
|
19
|
+
for(int i = 0; i < n_count; i++) {
|
20
|
+
for(int j = (i+1); j < n_count; j++) {
|
21
|
+
if (j == i)
|
22
|
+
continue;
|
23
|
+
|
24
|
+
float weight = 0.0;
|
25
|
+
for(int p = 0; p < patterns_count; p ++) {
|
26
|
+
weight += FIX2INT(rb_ary_entry(rb_ary_entry(patterns, p), i)) * FIX2INT(rb_ary_entry(rb_ary_entry(patterns, p), j));
|
27
|
+
}
|
28
|
+
|
29
|
+
weight = weight / patterns_count;
|
30
|
+
|
31
|
+
int w_i = (i < j) ? i : j;
|
32
|
+
int w_j = (j > i) ? j : i;
|
33
|
+
|
34
|
+
rb_ary_store(rb_ary_entry(weights, w_i), w_j, rb_float_new(weight));
|
35
|
+
}
|
36
|
+
}
|
37
|
+
|
38
|
+
return weights;
|
39
|
+
}
|
40
|
+
|
41
|
+
/* ruby calls this to load the extension */
|
42
|
+
void Init_hopfield(void) {
|
43
|
+
|
44
|
+
m_hopfield = rb_define_module("Hopfield");
|
45
|
+
|
46
|
+
rb_define_module_function(m_hopfield, "calculate_weights_hebbian", hopfield_calculate_weights_hebbian, 2);
|
47
|
+
}
|
data/lib/hopfield/training.rb
CHANGED
@@ -5,6 +5,8 @@ module Hopfield
|
|
5
5
|
HEBBIAN_RULE = 1
|
6
6
|
STORKEY_RULE = 2
|
7
7
|
|
8
|
+
USE_C_EXTENSION = true
|
9
|
+
|
8
10
|
class Training
|
9
11
|
attr_accessor :patterns, :neurons, :weights, :pattern_dimensions, :rule
|
10
12
|
|
@@ -48,18 +50,23 @@ module Hopfield
|
|
48
50
|
# Neurons are fully connected; every neuron has a weight value for every other neuron
|
49
51
|
case rule
|
50
52
|
when Hopfield::HEBBIAN_RULE
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
53
|
+
if USE_C_EXTENSION
|
54
|
+
self.weights = Hopfield::calculate_weights_hebbian(self.patterns, self.neurons.count)
|
55
|
+
else
|
56
|
+
# Ruby equivalent of the calculate_weights_hebbian C function
|
57
|
+
self.neurons.count.times do |i|
|
58
|
+
for j in ((i+1)...self.neurons.count) do
|
59
|
+
next if i == j
|
60
|
+
weight = 0.0
|
61
|
+
self.patterns.each do |pattern|
|
62
|
+
weight += pattern[i] * pattern[j]
|
63
|
+
end
|
64
|
+
set_weight(i, j, weight / self.patterns.count)
|
57
65
|
end
|
58
|
-
set_weight(i, j, weight / self.patterns.count)
|
59
66
|
end
|
60
67
|
end
|
61
68
|
when Hopfield::STORKEY_RULE
|
62
|
-
|
69
|
+
# Still has to be implemented in both Ruby and C
|
63
70
|
else
|
64
71
|
abort 'Unknown learning rule specified, either use Hopfield::STORKEY_RULE or Hopfield::HEBBIAN_RULE'
|
65
72
|
end
|
data/lib/hopfield.rb
CHANGED
metadata
CHANGED
@@ -1,46 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hopfield
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '1.
|
5
|
-
prerelease:
|
4
|
+
version: '1.2'
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Bart Olsthoorn
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-
|
11
|
+
date: 2013-07-03 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rspec
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: chunky_png
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - '>='
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - '>='
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
description: Hopfield networks can be used for smart pattern recollections. It's recalling
|
@@ -48,37 +43,40 @@ description: Hopfield networks can be used for smart pattern recollections. It's
|
|
48
43
|
email:
|
49
44
|
- bartolsthoorn@gmail.com
|
50
45
|
executables: []
|
51
|
-
extensions:
|
46
|
+
extensions:
|
47
|
+
- ext/hopfield/extconf.rb
|
52
48
|
extra_rdoc_files: []
|
53
49
|
files:
|
54
50
|
- lib/hopfield/network.rb
|
55
51
|
- lib/hopfield/neuron.rb
|
56
52
|
- lib/hopfield/training.rb
|
57
53
|
- lib/hopfield.rb
|
54
|
+
- ext/hopfield/hopfield.c
|
55
|
+
- ext/hopfield/extconf.rb
|
58
56
|
- LICENSE
|
59
57
|
- README.md
|
60
58
|
homepage: http://github.com/bartolsthoorn/hopfield-ruby
|
61
|
-
licenses:
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
62
|
post_install_message:
|
63
63
|
rdoc_options: []
|
64
64
|
require_paths:
|
65
65
|
- lib
|
66
66
|
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
-
none: false
|
68
67
|
requirements:
|
69
|
-
- -
|
68
|
+
- - '>='
|
70
69
|
- !ruby/object:Gem::Version
|
71
70
|
version: '0'
|
72
71
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
72
|
requirements:
|
75
|
-
- -
|
73
|
+
- - '>='
|
76
74
|
- !ruby/object:Gem::Version
|
77
75
|
version: '0'
|
78
76
|
requirements: []
|
79
77
|
rubyforge_project:
|
80
|
-
rubygems_version:
|
78
|
+
rubygems_version: 2.0.3
|
81
79
|
signing_key:
|
82
|
-
specification_version:
|
80
|
+
specification_version: 4
|
83
81
|
summary: Ruby implementation of a Hopfield Network
|
84
82
|
test_files: []
|