ai4r 1.13 → 2.0
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 +174 -0
- data/examples/classifiers/hyperpipes_data.csv +14 -0
- data/examples/classifiers/hyperpipes_example.rb +22 -0
- data/examples/classifiers/ib1_example.rb +12 -0
- data/examples/classifiers/id3_example.rb +15 -10
- data/examples/classifiers/id3_graphviz_example.rb +17 -0
- data/examples/classifiers/logistic_regression_example.rb +11 -0
- data/examples/classifiers/naive_bayes_attributes_example.rb +13 -0
- data/examples/classifiers/naive_bayes_example.rb +12 -13
- data/examples/classifiers/one_r_example.rb +27 -0
- data/examples/classifiers/parameter_tutorial.rb +29 -0
- data/examples/classifiers/prism_nominal_example.rb +15 -0
- data/examples/classifiers/prism_numeric_example.rb +21 -0
- data/examples/classifiers/simple_linear_regression_example.rb +14 -11
- data/examples/classifiers/zero_and_one_r_example.rb +34 -0
- data/examples/classifiers/zero_one_r_data.csv +8 -0
- data/examples/clusterers/clusterer_example.rb +40 -34
- data/examples/clusterers/dbscan_example.rb +17 -0
- data/examples/clusterers/dendrogram_example.rb +17 -0
- data/examples/clusterers/hierarchical_dendrogram_example.rb +20 -0
- data/examples/clusterers/kmeans_custom_example.rb +26 -0
- data/examples/genetic_algorithm/bitstring_example.rb +41 -0
- data/examples/genetic_algorithm/genetic_algorithm_example.rb +26 -18
- data/examples/genetic_algorithm/kmeans_seed_tuning.rb +45 -0
- data/examples/neural_network/backpropagation_example.rb +48 -48
- data/examples/neural_network/hopfield_example.rb +45 -0
- data/examples/neural_network/patterns_with_base_noise.rb +39 -39
- data/examples/neural_network/patterns_with_noise.rb +41 -39
- data/examples/neural_network/train_epochs_callback.rb +25 -0
- data/examples/neural_network/training_patterns.rb +39 -39
- data/examples/neural_network/transformer_text_classification.rb +78 -0
- data/examples/neural_network/xor_example.rb +23 -22
- data/examples/reinforcement/q_learning_example.rb +10 -0
- data/examples/som/som_data.rb +155 -152
- data/examples/som/som_multi_node_example.rb +12 -13
- data/examples/som/som_single_example.rb +12 -15
- data/examples/transformer/decode_classifier_example.rb +68 -0
- data/examples/transformer/deterministic_example.rb +10 -0
- data/examples/transformer/seq2seq_example.rb +16 -0
- data/lib/ai4r/classifiers/classifier.rb +24 -16
- data/lib/ai4r/classifiers/gradient_boosting.rb +64 -0
- data/lib/ai4r/classifiers/hyperpipes.rb +119 -43
- data/lib/ai4r/classifiers/ib1.rb +122 -32
- data/lib/ai4r/classifiers/id3.rb +524 -145
- data/lib/ai4r/classifiers/logistic_regression.rb +96 -0
- data/lib/ai4r/classifiers/multilayer_perceptron.rb +75 -59
- data/lib/ai4r/classifiers/naive_bayes.rb +95 -34
- data/lib/ai4r/classifiers/one_r.rb +112 -44
- data/lib/ai4r/classifiers/prism.rb +167 -76
- data/lib/ai4r/classifiers/random_forest.rb +72 -0
- data/lib/ai4r/classifiers/simple_linear_regression.rb +83 -58
- data/lib/ai4r/classifiers/support_vector_machine.rb +91 -0
- data/lib/ai4r/classifiers/votes.rb +57 -0
- data/lib/ai4r/classifiers/zero_r.rb +71 -30
- data/lib/ai4r/clusterers/average_linkage.rb +46 -27
- data/lib/ai4r/clusterers/bisecting_k_means.rb +50 -44
- data/lib/ai4r/clusterers/centroid_linkage.rb +52 -36
- data/lib/ai4r/clusterers/cluster_tree.rb +50 -0
- data/lib/ai4r/clusterers/clusterer.rb +29 -14
- data/lib/ai4r/clusterers/complete_linkage.rb +42 -31
- data/lib/ai4r/clusterers/dbscan.rb +134 -0
- data/lib/ai4r/clusterers/diana.rb +75 -49
- data/lib/ai4r/clusterers/k_means.rb +270 -135
- data/lib/ai4r/clusterers/median_linkage.rb +49 -33
- data/lib/ai4r/clusterers/single_linkage.rb +196 -88
- data/lib/ai4r/clusterers/ward_linkage.rb +51 -35
- data/lib/ai4r/clusterers/ward_linkage_hierarchical.rb +25 -10
- data/lib/ai4r/clusterers/weighted_average_linkage.rb +48 -32
- data/lib/ai4r/data/data_set.rb +223 -103
- data/lib/ai4r/data/parameterizable.rb +31 -25
- data/lib/ai4r/data/proximity.rb +62 -62
- data/lib/ai4r/data/statistics.rb +46 -35
- data/lib/ai4r/experiment/classifier_evaluator.rb +84 -32
- data/lib/ai4r/experiment/split.rb +39 -0
- data/lib/ai4r/genetic_algorithm/chromosome_base.rb +43 -0
- data/lib/ai4r/genetic_algorithm/genetic_algorithm.rb +92 -170
- data/lib/ai4r/genetic_algorithm/tsp_chromosome.rb +83 -0
- data/lib/ai4r/hmm/hidden_markov_model.rb +134 -0
- data/lib/ai4r/neural_network/activation_functions.rb +37 -0
- data/lib/ai4r/neural_network/backpropagation.rb +399 -134
- data/lib/ai4r/neural_network/hopfield.rb +175 -58
- data/lib/ai4r/neural_network/transformer.rb +194 -0
- data/lib/ai4r/neural_network/weight_initializations.rb +40 -0
- data/lib/ai4r/reinforcement/policy_iteration.rb +66 -0
- data/lib/ai4r/reinforcement/q_learning.rb +51 -0
- data/lib/ai4r/search/a_star.rb +76 -0
- data/lib/ai4r/search/bfs.rb +50 -0
- data/lib/ai4r/search/dfs.rb +50 -0
- data/lib/ai4r/search/mcts.rb +118 -0
- data/lib/ai4r/search.rb +12 -0
- data/lib/ai4r/som/distance_metrics.rb +29 -0
- data/lib/ai4r/som/layer.rb +28 -17
- data/lib/ai4r/som/node.rb +61 -32
- data/lib/ai4r/som/som.rb +158 -41
- data/lib/ai4r/som/two_phase_layer.rb +21 -25
- data/lib/ai4r/version.rb +3 -0
- data/lib/ai4r.rb +57 -28
- metadata +79 -109
- data/README.rdoc +0 -39
- data/test/classifiers/hyperpipes_test.rb +0 -84
- data/test/classifiers/ib1_test.rb +0 -78
- data/test/classifiers/id3_test.rb +0 -220
- data/test/classifiers/multilayer_perceptron_test.rb +0 -79
- data/test/classifiers/naive_bayes_test.rb +0 -43
- data/test/classifiers/one_r_test.rb +0 -62
- data/test/classifiers/prism_test.rb +0 -85
- data/test/classifiers/simple_linear_regression_test.rb +0 -37
- data/test/classifiers/zero_r_test.rb +0 -50
- data/test/clusterers/average_linkage_test.rb +0 -51
- data/test/clusterers/bisecting_k_means_test.rb +0 -66
- data/test/clusterers/centroid_linkage_test.rb +0 -53
- data/test/clusterers/complete_linkage_test.rb +0 -57
- data/test/clusterers/diana_test.rb +0 -69
- data/test/clusterers/k_means_test.rb +0 -167
- data/test/clusterers/median_linkage_test.rb +0 -53
- data/test/clusterers/single_linkage_test.rb +0 -122
- data/test/clusterers/ward_linkage_hierarchical_test.rb +0 -81
- data/test/clusterers/ward_linkage_test.rb +0 -53
- data/test/clusterers/weighted_average_linkage_test.rb +0 -53
- data/test/data/data_set_test.rb +0 -104
- data/test/data/proximity_test.rb +0 -87
- data/test/data/statistics_test.rb +0 -65
- data/test/experiment/classifier_evaluator_test.rb +0 -76
- data/test/genetic_algorithm/chromosome_test.rb +0 -57
- data/test/genetic_algorithm/genetic_algorithm_test.rb +0 -81
- data/test/neural_network/backpropagation_test.rb +0 -82
- data/test/neural_network/hopfield_test.rb +0 -72
- data/test/som/som_test.rb +0 -97
data/lib/ai4r/som/som.rb
CHANGED
@@ -1,19 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Author:: Thomas Kern
|
2
4
|
# License:: MPL 1.1
|
3
5
|
# Project:: ai4r
|
4
|
-
# Url::
|
6
|
+
# Url:: https://github.com/SergioFierens/ai4r
|
5
7
|
#
|
6
8
|
# You can redistribute it and/or modify it under the terms of
|
7
9
|
# the Mozilla Public License version 1.1 as published by the
|
8
10
|
# Mozilla Foundation at http://www.mozilla.org/MPL/MPL-1.1.txt
|
9
11
|
|
10
|
-
|
11
|
-
require
|
12
|
-
|
13
|
-
|
12
|
+
require_relative '../data/parameterizable'
|
13
|
+
require 'yaml'
|
14
|
+
require_relative 'layer'
|
15
|
+
require_relative 'two_phase_layer'
|
16
|
+
require_relative 'node'
|
14
17
|
|
15
18
|
module Ai4r
|
16
|
-
|
17
19
|
# A self-organizing map (SOM) or self-organizing feature map (SOFM) is a type
|
18
20
|
# of artificial neural network that is trained using unsupervised learning to
|
19
21
|
# produce a low-dimensional (typically two-dimensional), discretized
|
@@ -24,7 +26,6 @@ module Ai4r
|
|
24
26
|
# 'Neural Networks for Applied Sciences and Engineering'
|
25
27
|
|
26
28
|
module Som
|
27
|
-
|
28
29
|
# = Introduction
|
29
30
|
#
|
30
31
|
# This is an implementation of a Kohonen Self-Organizing Maps
|
@@ -41,39 +42,49 @@ module Ai4r
|
|
41
42
|
#
|
42
43
|
# = Parameters
|
43
44
|
# * dim => dimension of the input vector
|
44
|
-
# *
|
45
|
+
# * rows => number of rows for the map
|
46
|
+
# * columns => number of columns for the map
|
45
47
|
# * layer => instante of a layer-algorithm class
|
46
48
|
#
|
47
49
|
# = About the project
|
48
50
|
# Author:: Thomas Kern
|
49
51
|
# License:: MPL 1.1
|
50
|
-
# Url::
|
52
|
+
# Url:: https://github.com/SergioFierens/ai4r
|
51
53
|
|
54
|
+
# Encapsulates a self-organizing map composed of interconnected nodes.
|
52
55
|
class Som
|
53
|
-
|
54
56
|
include Ai4r::Data::Parameterizable
|
55
57
|
|
56
|
-
parameters_info :
|
57
|
-
:
|
58
|
-
:
|
59
|
-
:
|
58
|
+
parameters_info rows: 'number of rows for the map',
|
59
|
+
columns: 'number of columns for the map',
|
60
|
+
nodes: 'array holding all nodes of the map',
|
61
|
+
dimension: 'sets the dimension of the input',
|
62
|
+
layer: 'instance of a layer, defines how the training algorithm works',
|
63
|
+
epoch: 'number of finished epochs',
|
64
|
+
init_weight_options: 'Hash with :range and :seed to initialize node weights'
|
60
65
|
|
61
|
-
def initialize(dim,
|
66
|
+
def initialize(dim, rows, columns, layer, init_weight_options = { range: 0..1, seed: nil })
|
62
67
|
@layer = layer
|
63
68
|
@dimension = dim
|
64
|
-
@
|
65
|
-
@
|
69
|
+
@rows = rows
|
70
|
+
@columns = columns
|
71
|
+
@nodes = Array.new(rows * columns)
|
66
72
|
@epoch = 0
|
67
73
|
@cache = {}
|
74
|
+
opts = { range: 0..1, random_seed: nil, seed: nil }.merge(init_weight_options)
|
75
|
+
opts[:random_seed] = opts[:random_seed] || opts[:seed]
|
76
|
+
@init_weight_options = opts
|
68
77
|
end
|
69
78
|
|
70
79
|
# finds the best matching unit (bmu) of a certain input in all the @nodes
|
71
80
|
# returns an array of length 2 => [node, distance] (distance is of eucledian type, not
|
72
81
|
# a neighborhood distance)
|
82
|
+
# @param input [Object]
|
83
|
+
# @return [Object]
|
73
84
|
def find_bmu(input)
|
74
85
|
bmu = @nodes.first
|
75
86
|
dist = bmu.distance_to_input input
|
76
|
-
@nodes[1
|
87
|
+
@nodes[1..].each do |node|
|
77
88
|
tmp_dist = node.distance_to_input(input)
|
78
89
|
if tmp_dist <= dist
|
79
90
|
dist = tmp_dist
|
@@ -84,6 +95,11 @@ module Ai4r
|
|
84
95
|
end
|
85
96
|
|
86
97
|
# adjusts all nodes within a certain radius to the bmu
|
98
|
+
# @param input [Object]
|
99
|
+
# @param bmu [Object]
|
100
|
+
# @param radius [Object]
|
101
|
+
# @param learning_rate [Object]
|
102
|
+
# @return [Object]
|
87
103
|
def adjust_nodes(input, bmu, radius, learning_rate)
|
88
104
|
@nodes.each do |node|
|
89
105
|
dist = node.distance_to_node(bmu[0])
|
@@ -91,30 +107,43 @@ module Ai4r
|
|
91
107
|
|
92
108
|
influence = @layer.influence_decay dist, radius
|
93
109
|
node.weights.each_with_index do |weight, index|
|
94
|
-
node.weights[index] +=
|
110
|
+
node.weights[index] += influence * learning_rate * (input[index] - weight)
|
95
111
|
end
|
96
112
|
end
|
97
113
|
end
|
98
114
|
|
99
115
|
# main method for the som. trains the map with the passed data vector
|
100
116
|
# calls train_step as long as train_step returns false
|
101
|
-
|
102
|
-
|
117
|
+
# @param data [Object]
|
118
|
+
# @param error_threshold [Object]
|
119
|
+
# @return [Object]
|
120
|
+
def train(data, error_threshold: nil)
|
121
|
+
errors = []
|
122
|
+
while @epoch < @layer.epochs
|
123
|
+
error = train_step(data, error_threshold: error_threshold)
|
124
|
+
errors << error
|
125
|
+
yield error if block_given?
|
103
126
|
end
|
127
|
+
errors
|
104
128
|
end
|
105
129
|
|
106
130
|
# calculates the global distance error for all data entries
|
131
|
+
# @param data [Object]
|
132
|
+
# @return [Object]
|
107
133
|
def global_error(data)
|
108
|
-
data.inject(0) {|sum,entry| sum + find_bmu(entry)[1]**2 }
|
109
|
-
|
134
|
+
data.inject(0) { |sum, entry| sum + (find_bmu(entry)[1]**2) }
|
135
|
+
end
|
110
136
|
|
111
|
-
#
|
112
|
-
#
|
113
|
-
#
|
114
|
-
#
|
115
|
-
#
|
116
|
-
|
117
|
-
|
137
|
+
# Train the map for one epoch using +data+.
|
138
|
+
# Returns the computed +global_error+ for that epoch. If a block is
|
139
|
+
# provided, the error is yielded instead of returned.
|
140
|
+
# Training stops early when +error_threshold+ is defined and the computed
|
141
|
+
# error falls below it.
|
142
|
+
# @param data [Object]
|
143
|
+
# @param error_threshold [Object]
|
144
|
+
# @return [Object]
|
145
|
+
def train_step(data, error_threshold: nil)
|
146
|
+
return nil if @epoch >= @layer.epochs
|
118
147
|
|
119
148
|
radius = @layer.radius_decay @epoch
|
120
149
|
learning_rate = @layer.learning_rate_decay @epoch
|
@@ -124,32 +153,120 @@ module Ai4r
|
|
124
153
|
end
|
125
154
|
|
126
155
|
@epoch += 1
|
127
|
-
|
156
|
+
error = global_error(data)
|
157
|
+
@epoch = @layer.epochs if error_threshold && error <= error_threshold
|
158
|
+
yield error if block_given?
|
159
|
+
error
|
128
160
|
end
|
129
161
|
|
130
|
-
# returns the node at position (x,y) in the
|
162
|
+
# returns the node at position (x,y) in the map
|
163
|
+
# @param x [Object]
|
164
|
+
# @param y [Object]
|
165
|
+
# @return [Object]
|
131
166
|
def get_node(x, y)
|
132
|
-
raise(
|
133
|
-
|
167
|
+
raise ArgumentError, "invalid node coordinates (#{x}, #{y})" if check_param_for_som(x, y)
|
168
|
+
|
169
|
+
@nodes[y + (x * @columns)]
|
134
170
|
end
|
135
171
|
|
136
|
-
# intitiates the map by creating (@
|
172
|
+
# intitiates the map by creating (@rows * @columns) nodes
|
173
|
+
# @return [Object]
|
137
174
|
def initiate_map
|
138
|
-
|
139
|
-
|
175
|
+
seed = @init_weight_options[:random_seed]
|
176
|
+
rng = seed.nil? ? Random.new : Random.new(seed)
|
177
|
+
@nodes.each_index do |i|
|
178
|
+
options = @init_weight_options.merge(distance_metric: @layer.distance_metric, rng: rng)
|
179
|
+
@nodes[i] = Node.create i, @rows, @columns, @dimension, options
|
140
180
|
end
|
141
181
|
end
|
142
182
|
|
183
|
+
# Serialize this SOM to a Ruby Hash. The resulting structure includes
|
184
|
+
# map dimensions, layer parameters and all node weights.
|
185
|
+
# @return [Object]
|
186
|
+
def to_h
|
187
|
+
{
|
188
|
+
'rows' => @rows,
|
189
|
+
'columns' => @columns,
|
190
|
+
'dimension' => @dimension,
|
191
|
+
'epoch' => @epoch,
|
192
|
+
'layer' => {
|
193
|
+
'class' => @layer.class.name,
|
194
|
+
'parameters' => @layer.get_parameters,
|
195
|
+
'initial_learning_rate' => @layer.instance_variable_get('@initial_learning_rate')
|
196
|
+
},
|
197
|
+
'nodes' => @nodes.map(&:weights)
|
198
|
+
}
|
199
|
+
end
|
200
|
+
|
201
|
+
# Build a new SOM instance from the Hash generated by +to_h+.
|
202
|
+
# @param hash [Object]
|
203
|
+
# @return [Object]
|
204
|
+
def self.from_h(hash)
|
205
|
+
layer_class = Object.const_get(hash['layer']['class'])
|
206
|
+
raw_params = hash['layer']['parameters'] || {}
|
207
|
+
params = {}
|
208
|
+
raw_params.each do |k, v|
|
209
|
+
params[k.to_s] = v
|
210
|
+
end
|
211
|
+
ilr = hash['layer']['initial_learning_rate']
|
212
|
+
|
213
|
+
layer = if layer_class == TwoPhaseLayer
|
214
|
+
layer_class.new(
|
215
|
+
params['nodes'],
|
216
|
+
ilr || 0.9,
|
217
|
+
params['epochs'],
|
218
|
+
0,
|
219
|
+
0.1,
|
220
|
+
0
|
221
|
+
)
|
222
|
+
else
|
223
|
+
layer_class.new(
|
224
|
+
params['nodes'],
|
225
|
+
params['radius'],
|
226
|
+
params['epochs'],
|
227
|
+
ilr || 0.7
|
228
|
+
)
|
229
|
+
end
|
230
|
+
layer.set_parameters(params) if layer.respond_to?(:set_parameters)
|
231
|
+
|
232
|
+
som = Som.new(hash['dimension'], hash['rows'], hash['columns'], layer)
|
233
|
+
som.epoch = hash['epoch']
|
234
|
+
som.nodes = hash['nodes'].each_with_index.map do |weights, i|
|
235
|
+
node = Node.new
|
236
|
+
node.id = i
|
237
|
+
node.x = i % hash['columns']
|
238
|
+
node.y = (i / hash['columns'].to_f).to_i
|
239
|
+
node.weights = weights.dup
|
240
|
+
node.instantiated_weight = weights.dup
|
241
|
+
node
|
242
|
+
end
|
243
|
+
som
|
244
|
+
end
|
245
|
+
|
246
|
+
# Save this SOM to a YAML file.
|
247
|
+
# @param path [Object]
|
248
|
+
# @return [Object]
|
249
|
+
def save_yaml(path)
|
250
|
+
File.write(path, YAML.dump(to_h))
|
251
|
+
end
|
252
|
+
|
253
|
+
# Load a SOM from a YAML file created by +save_yaml+.
|
254
|
+
# @param path [Object]
|
255
|
+
# @return [Object]
|
256
|
+
def self.load_yaml(path)
|
257
|
+
from_h(YAML.load_file(path))
|
258
|
+
end
|
259
|
+
|
143
260
|
private
|
144
261
|
|
145
262
|
# checks whether or not there is a node in the map at the coordinates (x,y).
|
146
263
|
# x is the row, y the column indicator
|
264
|
+
# @param x [Object]
|
265
|
+
# @param y [Object]
|
266
|
+
# @return [Object]
|
147
267
|
def check_param_for_som(x, y)
|
148
|
-
y > @
|
268
|
+
y > @columns - 1 || x > @rows - 1 || x.negative? || y.negative?
|
149
269
|
end
|
150
|
-
|
151
270
|
end
|
152
|
-
|
153
271
|
end
|
154
|
-
|
155
272
|
end
|
@@ -1,19 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Author:: Thomas Kern
|
2
4
|
# License:: MPL 1.1
|
3
5
|
# Project:: ai4r
|
4
|
-
# Url::
|
6
|
+
# Url:: https://github.com/SergioFierens/ai4r
|
5
7
|
#
|
6
8
|
# You can redistribute it and/or modify it under the terms of
|
7
9
|
# the Mozilla Public License version 1.1 as published by the
|
8
10
|
# Mozilla Foundation at http://www.mozilla.org/MPL/MPL-1.1.txt
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
+
require_relative '../data/parameterizable'
|
13
|
+
require_relative 'layer'
|
12
14
|
|
13
15
|
module Ai4r
|
14
|
-
|
15
16
|
module Som
|
16
|
-
|
17
17
|
# responsible for the implementation of the algorithm's decays, extends the class Layer.
|
18
18
|
# currently overrides the radius and learning rate decay methods of Layer.
|
19
19
|
# Has two phases, phase one has a decay in both the learning rate and the radius. The number
|
@@ -31,11 +31,12 @@ module Ai4r
|
|
31
31
|
# * phase_one_learning_rate => sets the learning rate for phase one
|
32
32
|
# * phase_two_learning_rate => sets the learning rate for phase two
|
33
33
|
|
34
|
+
# Layer that trains in two distinct phases with different learning rates.
|
34
35
|
class TwoPhaseLayer < Layer
|
35
|
-
|
36
|
+
# @return [Object]
|
36
37
|
def initialize(nodes, learning_rate = 0.9, phase_one = 150, phase_two = 100,
|
37
|
-
|
38
|
-
super
|
38
|
+
phase_one_learning_rate = 0.1, phase_two_learning_rate = 0, options = {})
|
39
|
+
super(nodes, nodes, phase_one + phase_two, learning_rate, options)
|
39
40
|
@phase_one = phase_one
|
40
41
|
@phase_two = phase_two
|
41
42
|
@lr = @initial_learning_rate
|
@@ -43,24 +44,21 @@ module Ai4r
|
|
43
44
|
@phase_one_learning_rate = phase_one_learning_rate
|
44
45
|
@phase_two_learning_rate = phase_two_learning_rate
|
45
46
|
|
46
|
-
@radius_reduction = @phase_one / (nodes/2.0 - 1) + 1
|
47
|
-
@delta_lr = (@lr - @phase_one_learning_rate)/ @phase_one
|
47
|
+
@radius_reduction = (@phase_one / ((nodes / 2.0) - 1)) + 1
|
48
|
+
@delta_lr = (@lr - @phase_one_learning_rate) / @phase_one
|
48
49
|
@radius = (nodes / 2.0).to_i
|
49
50
|
end
|
50
51
|
|
51
52
|
# two different values will be returned, depending on the phase
|
52
53
|
# in phase one, the radius will incrementially reduced by 1 every @radius_reduction time
|
53
54
|
# in phase two, the radius is fixed to 1
|
55
|
+
# @param epoch [Object]
|
56
|
+
# @return [Object]
|
54
57
|
def radius_decay(epoch)
|
55
|
-
if epoch > @phase_one
|
56
|
-
return 1
|
57
|
-
else
|
58
|
-
if (epoch % @radius_reduction) == 0
|
59
|
-
@radius -= 1
|
60
|
-
end
|
61
|
-
@radius
|
62
|
-
end
|
58
|
+
return 1 if epoch > @phase_one
|
63
59
|
|
60
|
+
@radius -= 1 if (epoch % @radius_reduction).zero?
|
61
|
+
@radius
|
64
62
|
end
|
65
63
|
|
66
64
|
# two different values will be returned, depending on the phase
|
@@ -69,22 +67,20 @@ module Ai4r
|
|
69
67
|
# the decay value of the learning rate) is reset as well
|
70
68
|
# in phase two, the newly reset delta_lr rate will be used to incrementially reduce the
|
71
69
|
# learning rate
|
70
|
+
# @param epoch [Object]
|
71
|
+
# @return [Object]
|
72
72
|
def learning_rate_decay(epoch)
|
73
73
|
if epoch < @phase_one
|
74
74
|
@lr -= @delta_lr
|
75
|
-
|
75
|
+
@lr
|
76
76
|
elsif epoch == @phase_one
|
77
77
|
@lr = @phase_one_learning_rate
|
78
|
-
@delta_lr = (@phase_one_learning_rate - @phase_two_learning_rate)
|
79
|
-
|
78
|
+
@delta_lr = (@phase_one_learning_rate - @phase_two_learning_rate) / @phase_two
|
79
|
+
@lr
|
80
80
|
else
|
81
81
|
@lr -= @delta_lr
|
82
82
|
end
|
83
83
|
end
|
84
|
-
|
85
84
|
end
|
86
|
-
|
87
85
|
end
|
88
|
-
|
89
86
|
end
|
90
|
-
|
data/lib/ai4r/version.rb
ADDED
data/lib/ai4r.rb
CHANGED
@@ -1,35 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'ai4r/version'
|
4
|
+
|
1
5
|
# Data
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
+
require_relative 'ai4r/data/data_set'
|
7
|
+
require_relative 'ai4r/data/statistics'
|
8
|
+
require_relative 'ai4r/data/proximity'
|
9
|
+
require_relative 'ai4r/data/parameterizable'
|
10
|
+
|
6
11
|
# Clusterers
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
require_relative 'ai4r/clusterers/clusterer'
|
13
|
+
require_relative 'ai4r/clusterers/k_means'
|
14
|
+
require_relative 'ai4r/clusterers/bisecting_k_means'
|
15
|
+
require_relative 'ai4r/clusterers/single_linkage'
|
16
|
+
require_relative 'ai4r/clusterers/complete_linkage'
|
17
|
+
require_relative 'ai4r/clusterers/average_linkage'
|
18
|
+
require_relative 'ai4r/clusterers/weighted_average_linkage'
|
19
|
+
require_relative 'ai4r/clusterers/centroid_linkage'
|
20
|
+
require_relative 'ai4r/clusterers/median_linkage'
|
21
|
+
require_relative 'ai4r/clusterers/ward_linkage'
|
22
|
+
require_relative 'ai4r/clusterers/ward_linkage_hierarchical'
|
23
|
+
require_relative 'ai4r/clusterers/diana'
|
24
|
+
require_relative 'ai4r/clusterers/dbscan'
|
25
|
+
|
19
26
|
# Classifiers
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
27
|
+
require_relative 'ai4r/classifiers/classifier'
|
28
|
+
require_relative 'ai4r/classifiers/id3'
|
29
|
+
require_relative 'ai4r/classifiers/multilayer_perceptron'
|
30
|
+
require_relative 'ai4r/classifiers/prism'
|
31
|
+
require_relative 'ai4r/classifiers/one_r'
|
32
|
+
require_relative 'ai4r/classifiers/zero_r'
|
33
|
+
require_relative 'ai4r/classifiers/hyperpipes'
|
34
|
+
require_relative 'ai4r/classifiers/naive_bayes'
|
35
|
+
require_relative 'ai4r/classifiers/ib1'
|
36
|
+
require_relative 'ai4r/classifiers/random_forest'
|
37
|
+
require_relative 'ai4r/classifiers/gradient_boosting'
|
38
|
+
require_relative 'ai4r/classifiers/support_vector_machine'
|
39
|
+
|
28
40
|
# Neural networks
|
29
|
-
|
30
|
-
|
41
|
+
require_relative 'ai4r/neural_network/backpropagation'
|
42
|
+
require_relative 'ai4r/neural_network/hopfield'
|
43
|
+
require_relative 'ai4r/neural_network/transformer'
|
44
|
+
|
31
45
|
# Genetic Algorithms
|
32
|
-
|
46
|
+
require_relative 'ai4r/genetic_algorithm/genetic_algorithm'
|
47
|
+
|
48
|
+
# Reinforcement Learning
|
49
|
+
require_relative 'ai4r/reinforcement/q_learning'
|
50
|
+
require_relative 'ai4r/reinforcement/policy_iteration'
|
51
|
+
|
52
|
+
# Search
|
53
|
+
require_relative 'ai4r/search/bfs'
|
54
|
+
require_relative 'ai4r/search/dfs'
|
55
|
+
require_relative 'ai4r/search/mcts'
|
56
|
+
require_relative 'ai4r/search'
|
57
|
+
|
58
|
+
# Hidden Markov Models
|
59
|
+
require_relative 'ai4r/hmm/hidden_markov_model'
|
60
|
+
|
33
61
|
# SOM
|
34
|
-
|
62
|
+
require_relative 'ai4r/som/som'
|
35
63
|
|
64
|
+
# Search Algorithms
|