neural-network 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/neural_network.rb +107 -0
  3. metadata +44 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: df40c3471ad990891b169a66c8b26d39e77f5e37
4
+ data.tar.gz: cfbf1226ab8513ab5d9a3df4a58bcac4912b5e26
5
+ SHA512:
6
+ metadata.gz: af33bbb7b386f64131c8fb3f344c85d8e145fd176576a046022e6843b3d45f1838776ee3c0c427115a4e318f025322cd9680be69d39a2d1f8152a36343d9cebc
7
+ data.tar.gz: c79c571498df2c9c67880de43ceda14e934772cf88b2130c02bb7cde7f448461aadce065e4c1b0d20f13e5f84eac984551b1d942b8d783dc2adbfafc269320cd
@@ -0,0 +1,107 @@
1
+ class NeuralNetwork
2
+ attr_accessor :neurons, :weights, :biases, :names
3
+ def initialize(sizes)
4
+ @weights = []
5
+ @biases = []
6
+ @neurons = []
7
+ @names = {}
8
+ @rng = Distribution::Normal.rng
9
+ num_layers = sizes.length
10
+ (0..num_layers - 1).each do |i|
11
+ @neurons << []
12
+ unless i == 0
13
+ @biases << []
14
+ @weights << []
15
+ end
16
+ (0..sizes[i] - 1).each do |j|
17
+ @neurons[-1] << 0.0
18
+ unless i == 0
19
+ @biases[-1] << Util.random_normal
20
+ @weights[-1] << []
21
+ (0..sizes[i - 1] - 1).each do |k|
22
+ @weights[-1][j] << Util.random_normal
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ def to_hash
29
+ hash = {}
30
+ hash['weights'] = Marshal.load(Marshal.dump(@weights))
31
+ hash['biases'] = Marshal.load(Marshal.dump(@biases))
32
+ hash['neurons'] = Marshal.load(Marshal.dump(@neurons))
33
+ hash['names'] = {}
34
+ names.each do |key, value|
35
+ hash['names'][key] = value
36
+ end
37
+ hash
38
+ end
39
+ def self.from_hash(hash)
40
+ sizes = []
41
+ hash['neurons'].each do |layer|
42
+ sizes << layer.length
43
+ end
44
+ n = NewNeuralNetwork.new(sizes)
45
+ (0..n.neurons.length - 1).each do |i|
46
+ (0..n.neurons[i].length - 1).each do |j|
47
+ n.neurons[i][j] = hash['neurons'][i][j]
48
+ end
49
+ end
50
+ (0..n.weights.length - 1).each do |i|
51
+ (0..n.weights[i].length - 1).each do |j|
52
+ (0..n.neurons[i].length - 1).each do |k|
53
+ n.weights[i][j][k] = hash['weights'][i][j][k]
54
+ end
55
+ end
56
+ end
57
+ hash['names'].each do |key, value|
58
+ n.names[key] = value
59
+ end
60
+ n
61
+ end
62
+ def outputs
63
+ @neurons.length > 0 ? @neurons[-1] : []
64
+ end
65
+ def inputs
66
+ @neurons.length > 0 ? @neurons[0] : []
67
+ end
68
+ def think
69
+ feed_forward
70
+ end
71
+ def feed_forward
72
+ zs = []
73
+ (1..@neurons.length - 1).each do |i|
74
+ a = Matrix.rows(@weights[i - 1], false)
75
+ b = Matrix.columns([@neurons[i - 1]])
76
+ c = Matrix.columns([@biases[i - 1]])
77
+ outputs = a * b + c
78
+ zs << outputs
79
+ (0..@neurons[i].length - 1).each do |j|
80
+ @neurons[i][j] = Util.sigmoid(outputs[j, 0])
81
+ end
82
+ end
83
+ outputs
84
+ end
85
+ def name_neuron(i, j, name)
86
+ @names[name] = [i, j]
87
+ end
88
+ def [](name)
89
+ @neurons[@names[name][0]][@names[name][1]]
90
+ end
91
+ def []=(name, value)
92
+ @neurons[@names[name][0]][@names[name][1]] = value
93
+ end
94
+ def sizes
95
+ s = []
96
+ @neurons.each do |layer|
97
+ s << layer.length
98
+ end
99
+ s
100
+ end
101
+
102
+ private
103
+
104
+ def sigmoid(x)
105
+ 1.0 / (1.0 + Math::E ** (-x.to_f))
106
+ end
107
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: neural-network
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - interrobang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/neural_network.rb
20
+ homepage:
21
+ licenses:
22
+ - GPL-3.0
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.6.8
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: A simple feed-forward Neural Network gem
44
+ test_files: []