newral 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +278 -0
- data/Rakefile +10 -0
- data/lib/newral.rb +53 -0
- data/lib/newral/bayes.rb +39 -0
- data/lib/newral/classifier/dendogram.rb +68 -0
- data/lib/newral/classifier/k_means_cluster.rb +45 -0
- data/lib/newral/classifier/node.rb +58 -0
- data/lib/newral/classifier/node_distance.rb +19 -0
- data/lib/newral/data/base.rb +153 -0
- data/lib/newral/data/cluster.rb +37 -0
- data/lib/newral/data/cluster_set.rb +38 -0
- data/lib/newral/data/csv.rb +23 -0
- data/lib/newral/data/idx.rb +48 -0
- data/lib/newral/error_calculation.rb +28 -0
- data/lib/newral/functions/base.rb +102 -0
- data/lib/newral/functions/block.rb +34 -0
- data/lib/newral/functions/gaussian.rb +41 -0
- data/lib/newral/functions/line.rb +52 -0
- data/lib/newral/functions/polynomial.rb +48 -0
- data/lib/newral/functions/radial_basis_function_network.rb +54 -0
- data/lib/newral/functions/ricker_wavelet.rb +13 -0
- data/lib/newral/functions/vector.rb +59 -0
- data/lib/newral/genetic/tree.rb +70 -0
- data/lib/newral/graphs/a_star.rb +12 -0
- data/lib/newral/graphs/cheapest_first.rb +11 -0
- data/lib/newral/graphs/edge.rb +24 -0
- data/lib/newral/graphs/graph.rb +63 -0
- data/lib/newral/graphs/node.rb +11 -0
- data/lib/newral/graphs/path.rb +50 -0
- data/lib/newral/graphs/tree_search.rb +60 -0
- data/lib/newral/networks/backpropagation_network.rb +68 -0
- data/lib/newral/networks/layer.rb +28 -0
- data/lib/newral/networks/network.rb +146 -0
- data/lib/newral/networks/perceptron.rb +84 -0
- data/lib/newral/networks/sigmoid.rb +55 -0
- data/lib/newral/probability.rb +42 -0
- data/lib/newral/probability_set.rb +108 -0
- data/lib/newral/q_learning/base.rb +90 -0
- data/lib/newral/tools.rb +135 -0
- data/lib/newral/training/gradient_descent.rb +36 -0
- data/lib/newral/training/greedy.rb +36 -0
- data/lib/newral/training/hill_climbing.rb +77 -0
- data/lib/newral/training/linear_regression.rb +30 -0
- data/lib/newral/training/linear_regression_matrix.rb +32 -0
- metadata +147 -0
@@ -0,0 +1,36 @@
|
|
1
|
+
module Newral
|
2
|
+
module Training
|
3
|
+
class GradientDescent
|
4
|
+
attr_reader :best_function, :best_error, :input
|
5
|
+
def initialize( input: [], output: [], iterations:10**5, klass: Newral::Functions::Polynomial, klass_args: {}, start_function: nil )
|
6
|
+
@input = input
|
7
|
+
@output = output
|
8
|
+
@iterations = iterations
|
9
|
+
@klass = klass
|
10
|
+
@klass_args = klass_args
|
11
|
+
@best_function = start_function
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
def process( start_fresh: false, learning_rate:0.01, step:0.01 )
|
16
|
+
@best_function = ( start_fresh ? @klass.create_random( @klass_args ) : @best_function || @klass.create_random( @klass_args )).dup
|
17
|
+
@best_error = @best_function.calculate_error( input: @input, output: @output )
|
18
|
+
optimized_error = 0
|
19
|
+
@iterations.times do
|
20
|
+
function = @best_function.dup.move_with_gradient( input: @input, output: @output, learning_rate: learning_rate, step: step )
|
21
|
+
optimized_error = function.calculate_error( input: @input, output: @output )
|
22
|
+
if optimized_error >= @best_error
|
23
|
+
step = step/10 if step > 10**-8 # # slow down
|
24
|
+
learning_rate = learning_rate / 10 if learning_rate > 10**-8
|
25
|
+
else
|
26
|
+
@best_function = function
|
27
|
+
best_error = optimized_error
|
28
|
+
end
|
29
|
+
end
|
30
|
+
@best_error = @best_function.calculate_error( input: @input, output: @output )
|
31
|
+
@best_function
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Newral
|
2
|
+
module Training
|
3
|
+
class Greedy
|
4
|
+
attr_reader :best_function, :best_error, :input
|
5
|
+
def initialize( input: [], output: [], iterations:10**5, klass: Newral::Functions::Polynomial, klass_args: {}, start_function: nil )
|
6
|
+
@input = input
|
7
|
+
@output = output
|
8
|
+
@iterations = iterations
|
9
|
+
@klass = klass
|
10
|
+
@klass_args = klass_args
|
11
|
+
@best_function = start_function
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
def process( start_fresh: false )
|
16
|
+
@best_function = case
|
17
|
+
when start_fresh then @klass.create_random( @klass_args )
|
18
|
+
when @best_function then @best_function
|
19
|
+
else
|
20
|
+
@klass.create_random( @klass_args )
|
21
|
+
end
|
22
|
+
@best_error = @best_function.calculate_error( input: @input, output: @output )
|
23
|
+
@iterations.times do |i|
|
24
|
+
function = @best_function.dup.move_random # move random is easier to implement with different function types
|
25
|
+
error = function.calculate_error( input: @input, output: @output )
|
26
|
+
if error < @best_error
|
27
|
+
@best_error = error
|
28
|
+
@best_function = function
|
29
|
+
end
|
30
|
+
end
|
31
|
+
@best_function
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module Newral
|
2
|
+
module Training
|
3
|
+
class HillClimbing
|
4
|
+
attr_reader :best_function, :best_error, :input, :needed_iterations
|
5
|
+
def initialize( input: [], output: [], iterations:10**5, klass: Newral::Functions::Polynomial, klass_args: {}, start_function: nil )
|
6
|
+
@input = input
|
7
|
+
@output = output
|
8
|
+
@iterations = iterations
|
9
|
+
@klass = klass
|
10
|
+
@klass_args = klass_args
|
11
|
+
@best_function = start_function
|
12
|
+
@needed_iterations = 0
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
def process( start_fresh: false, max_error:0.01 )
|
17
|
+
@best_function = case
|
18
|
+
when start_fresh then @klass.create_random( @klass_args )
|
19
|
+
when @best_function then @best_function
|
20
|
+
else
|
21
|
+
@klass.create_random( @klass_args )
|
22
|
+
end
|
23
|
+
|
24
|
+
function = @best_function.dup.move_random( @klass_args )
|
25
|
+
@best_error = @best_function.calculate_error( input: @input, output: @output )
|
26
|
+
number_of_directions = @best_function.number_of_directions
|
27
|
+
step_sizes = [1]*number_of_directions
|
28
|
+
acceleration = 1.2
|
29
|
+
candidates = [
|
30
|
+
-acceleration,
|
31
|
+
-1/acceleration,
|
32
|
+
0,
|
33
|
+
1/acceleration,
|
34
|
+
acceleration
|
35
|
+
]
|
36
|
+
i=0
|
37
|
+
best_local_function = @best_function.dup
|
38
|
+
before_error = 99999
|
39
|
+
after_error = 1
|
40
|
+
moved = true
|
41
|
+
while i<@iterations && @best_error > max_error
|
42
|
+
moved = false
|
43
|
+
before_error = function.calculate_error( input: @input, output: @output )
|
44
|
+
number_of_directions.times do |direction|
|
45
|
+
best_candidate = -1
|
46
|
+
best_candidate_error = 9999999
|
47
|
+
candidates.each do |candidate|
|
48
|
+
temp_function = function.dup.move( direction: direction, step: step_sizes[ direction ]*candidate)
|
49
|
+
error = temp_function.calculate_error( input: @input, output: @output )
|
50
|
+
if error < best_candidate_error
|
51
|
+
best_candidate = candidate
|
52
|
+
best_candidate_error = error
|
53
|
+
if error < @best_error
|
54
|
+
@best_error = best_candidate_error
|
55
|
+
@best_function = temp_function.dup
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
if best_candidate == 0
|
60
|
+
step_sizes[direction] = step_sizes[direction] / acceleration # take it slower
|
61
|
+
else
|
62
|
+
moved = true
|
63
|
+
function.move( direction: direction, step: step_sizes[ direction ]*best_candidate )
|
64
|
+
# puts "moving #{direction} by #{(step_sizes[ direction ]*candidates[best_candidate]).to_s}"
|
65
|
+
step_sizes[direction] = step_sizes[ direction ] * best_candidate # accelerate
|
66
|
+
end
|
67
|
+
end
|
68
|
+
after_error = function.calculate_error( input: @input, output: @output )
|
69
|
+
i=i+1
|
70
|
+
end
|
71
|
+
@needed_iterations = i
|
72
|
+
@best_function
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Newral
|
2
|
+
module Training
|
3
|
+
class LinearRegression
|
4
|
+
#https://viblo.asia/p/machine-learning-algorithms-from-scratch-with-ruby-linear-regression-RnB5p1aYKPG
|
5
|
+
attr_reader :best_function, :best_error, :input
|
6
|
+
def initialize( input: [], output: [] )
|
7
|
+
@input = input
|
8
|
+
@output = output
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
def process( )
|
13
|
+
input_mean=Newral::Tools.mean @input
|
14
|
+
output_mean =Newral::Tools.mean @output
|
15
|
+
dividend =0
|
16
|
+
divisor = 0
|
17
|
+
@input.each_with_index do |input,idx|
|
18
|
+
dividend = dividend+(input-input_mean)*( @output[idx]-output_mean)
|
19
|
+
divisor = divisor+(input-input_mean)**2
|
20
|
+
end
|
21
|
+
multiplier = dividend/divisor
|
22
|
+
error = output_mean-multiplier*input_mean
|
23
|
+
@best_function = Newral::Functions::Polynomial.new(factors: [multiplier,error])
|
24
|
+
@best_error =@best_function.calculate_error( input: @input, output: @output )
|
25
|
+
@best_function
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Newral
|
2
|
+
module Training
|
3
|
+
class LinearRegressionMatrix
|
4
|
+
#https://stackoverflow.com/questions/24747643/3d-linear-regression
|
5
|
+
attr_reader :best_function, :best_error, :input
|
6
|
+
def initialize( input: [], output: [] )
|
7
|
+
@input = input
|
8
|
+
@output = output
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
def process( )
|
13
|
+
# @input = [[6 ,4 ,11],[ 8 ,5 ,15],[ 12, 9, 25],[ 2, 1, 3]]
|
14
|
+
# @output = [20,30,50,7]
|
15
|
+
array = @input.collect do |input|
|
16
|
+
[1]+input
|
17
|
+
end
|
18
|
+
matrix = Matrix.rows array
|
19
|
+
result = (( matrix.transpose*matrix ).inverse*(matrix.transpose)).to_a
|
20
|
+
|
21
|
+
weights = result.collect do |r|
|
22
|
+
r.size.times.inject(0) { |sum,i| sum = sum + r[i] * @output[i] }
|
23
|
+
end
|
24
|
+
bias = weights[0]
|
25
|
+
@best_function = Newral::Functions::Vector.new vector: weights[1..weights.length-1], bias: bias
|
26
|
+
@best_error =@best_function.calculate_error( input: @input, output: @output )
|
27
|
+
@best_function
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: newral
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- stefan
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: nmatrix
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.2'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.2'
|
69
|
+
description: " Neuralnet for newbies "
|
70
|
+
email:
|
71
|
+
- stefan.nothegger@gmx.de
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- Gemfile
|
77
|
+
- LICENSE
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- lib/newral.rb
|
81
|
+
- lib/newral/bayes.rb
|
82
|
+
- lib/newral/classifier/dendogram.rb
|
83
|
+
- lib/newral/classifier/k_means_cluster.rb
|
84
|
+
- lib/newral/classifier/node.rb
|
85
|
+
- lib/newral/classifier/node_distance.rb
|
86
|
+
- lib/newral/data/base.rb
|
87
|
+
- lib/newral/data/cluster.rb
|
88
|
+
- lib/newral/data/cluster_set.rb
|
89
|
+
- lib/newral/data/csv.rb
|
90
|
+
- lib/newral/data/idx.rb
|
91
|
+
- lib/newral/error_calculation.rb
|
92
|
+
- lib/newral/functions/base.rb
|
93
|
+
- lib/newral/functions/block.rb
|
94
|
+
- lib/newral/functions/gaussian.rb
|
95
|
+
- lib/newral/functions/line.rb
|
96
|
+
- lib/newral/functions/polynomial.rb
|
97
|
+
- lib/newral/functions/radial_basis_function_network.rb
|
98
|
+
- lib/newral/functions/ricker_wavelet.rb
|
99
|
+
- lib/newral/functions/vector.rb
|
100
|
+
- lib/newral/genetic/tree.rb
|
101
|
+
- lib/newral/graphs/a_star.rb
|
102
|
+
- lib/newral/graphs/cheapest_first.rb
|
103
|
+
- lib/newral/graphs/edge.rb
|
104
|
+
- lib/newral/graphs/graph.rb
|
105
|
+
- lib/newral/graphs/node.rb
|
106
|
+
- lib/newral/graphs/path.rb
|
107
|
+
- lib/newral/graphs/tree_search.rb
|
108
|
+
- lib/newral/networks/backpropagation_network.rb
|
109
|
+
- lib/newral/networks/layer.rb
|
110
|
+
- lib/newral/networks/network.rb
|
111
|
+
- lib/newral/networks/perceptron.rb
|
112
|
+
- lib/newral/networks/sigmoid.rb
|
113
|
+
- lib/newral/probability.rb
|
114
|
+
- lib/newral/probability_set.rb
|
115
|
+
- lib/newral/q_learning/base.rb
|
116
|
+
- lib/newral/tools.rb
|
117
|
+
- lib/newral/training/gradient_descent.rb
|
118
|
+
- lib/newral/training/greedy.rb
|
119
|
+
- lib/newral/training/hill_climbing.rb
|
120
|
+
- lib/newral/training/linear_regression.rb
|
121
|
+
- lib/newral/training/linear_regression_matrix.rb
|
122
|
+
homepage: https://github.com/ExistsAndIs1/newral
|
123
|
+
licenses:
|
124
|
+
- MIT
|
125
|
+
metadata:
|
126
|
+
allowed_push_host: https://rubygems.org
|
127
|
+
post_install_message:
|
128
|
+
rdoc_options: []
|
129
|
+
require_paths:
|
130
|
+
- lib
|
131
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: '0'
|
141
|
+
requirements: []
|
142
|
+
rubyforge_project:
|
143
|
+
rubygems_version: 2.4.6
|
144
|
+
signing_key:
|
145
|
+
specification_version: 4
|
146
|
+
summary: Basic Neuralnet
|
147
|
+
test_files: []
|