Skip to content

Commit 14147d3

Browse files
committed
Modified algorithm.
1 parent 83442a4 commit 14147d3

File tree

5 files changed

+86
-31
lines changed

5 files changed

+86
-31
lines changed

KRHebbian.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "KRHebbian"
3-
s.version = "1.2.1"
3+
s.version = "1.3.0"
44
s.summary = "Non-supervisor that Hebbian self-organization learning method in machine learning. (自分学習アルゴリズム)."
55
s.description = <<-DESC
66
KRHebbian implemented Hebbian algorithm that is a non-supervisor of self-organization algorithm of Machine Learning (自分学習アルゴリズム).

KRHebbian/KRHebbian.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ typedef enum KRHebbianActiveFunctions
1414
KRHebbianActiveFunctionByTanh
1515
}KRHebbianActiveFunctions;
1616

17-
typedef void(^KRHebbianCompletion)(BOOL success, NSArray *weights, NSInteger totalIteration);
18-
typedef void(^KRHebbianIteration)(NSInteger iteration, NSArray *weights);
17+
typedef void(^KRHebbianCompletion)(BOOL success, NSArray *outputs, NSArray *weights, NSInteger totalIteration);
18+
typedef void(^KRHebbianIteration)(NSInteger iteration, NSArray *outputs, NSArray *weights);
19+
typedef void(^KRHebbianDirectOutput)(NSArray *outputs, NSArray *weights);
1920

2021
@interface KRHebbian : NSObject
2122

2223
@property (nonatomic, strong) NSMutableArray *patterns;
2324
@property (nonatomic, strong) NSMutableArray *weights;
25+
@property (nonatomic, strong) NSMutableArray *outputs;
2426
@property (nonatomic, assign) float learningRate;
2527
@property (nonatomic, assign) NSInteger maxIteration;
2628
@property (nonatomic, assign) float convergenceValue;
@@ -37,6 +39,8 @@ typedef void(^KRHebbianIteration)(NSInteger iteration, NSArray *weights);
3739
-(void)initializeWeights:(NSArray *)_initWeights;
3840
-(void)training;
3941
-(void)trainingWithCompletion:(KRHebbianCompletion)_completion;
42+
-(void)directOutputAtInputs:(NSArray *)_inputs completion:(KRHebbianDirectOutput)_completion;
43+
-(void)reset;
4044

4145
-(void)setTrainingCompletion:(KRHebbianCompletion)_block;
4246
-(void)setTrainingIteraion:(KRHebbianIteration)_block;

KRHebbian/KRHebbian.m

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88

99
#import "KRHebbian.h"
1010

11+
#define DEFAULT_LEARNING_RATE 0.5f
12+
#define DEFAULT_ITERATION 0
13+
#define DEFAULT_MAX_ITERATION 1
14+
#define DEFAULT_CONVERGENCE_VALUE 0.0f
15+
#define DEFAULT_DELTA_SUMMATION 0.0f
16+
1117
@interface KRHebbian ()
1218

1319
@property (nonatomic, assign) NSInteger iteration;
@@ -39,14 +45,16 @@ -(float)_fOfSgn:(double)_sgnValue
3945
*/
4046
-(double)_fOfNetWithInputs:(NSArray *)_inputs
4147
{
42-
float _sum = 0.0f;
48+
double _sum = 0.0f;
4349
NSInteger _index = 0;
4450
for( NSNumber *_xValue in _inputs )
4551
{
46-
_sum += [_xValue floatValue] * [[self.weights objectAtIndex:_index] floatValue];
52+
_sum += [_xValue doubleValue] * [[self.weights objectAtIndex:_index] doubleValue];
4753
++_index;
4854
}
4955

56+
NSLog(@"_sum : %lf", _sum);
57+
5058
double _activatedValue = 0.0f;
5159
switch (self.activeFunction)
5260
{
@@ -63,11 +71,10 @@ -(double)_fOfNetWithInputs:(NSArray *)_inputs
6371
/*
6472
* @ Step 3. 求 New Weights
6573
*/
66-
-(void)_turningWeightsByInputs:(NSArray *)_inputs
74+
-(void)_turningWeightsByInputs:(NSArray *)_inputs netOutput:(double)_netOutput
6775
{
6876
NSArray *_weights = self.weights;
6977
float _learningRate = self.learningRate;
70-
double _netOutput = [self _fOfNetWithInputs:_inputs];
7178
NSMutableArray *_newWeights = [NSMutableArray new];
7279
NSInteger _index = 0;
7380
for( NSNumber *_weightValue in _weights )
@@ -82,6 +89,20 @@ -(void)_turningWeightsByInputs:(NSArray *)_inputs
8289
[self.weights addObjectsFromArray:_newWeights];
8390
}
8491

92+
// Start in calculate net outputs and tune the weights (if needed)
93+
-(void)_doTrainAndDoesWannaTuneWeights:(BOOL)_goTuning
94+
{
95+
for( NSArray *_inputs in self.patterns )
96+
{
97+
double _netOutput = [self _fOfNetWithInputs:_inputs];
98+
[self.outputs addObject:[NSNumber numberWithDouble:_netOutput]];
99+
if( _goTuning )
100+
{
101+
[self _turningWeightsByInputs:_inputs netOutput:_netOutput];
102+
}
103+
}
104+
}
105+
85106
@end
86107

87108
@implementation KRHebbian
@@ -101,16 +122,17 @@ -(instancetype)init
101122
self = [super init];
102123
if( self )
103124
{
104-
_learningRate = 0.5f;
125+
_learningRate = DEFAULT_LEARNING_RATE;
105126
_weights = [NSMutableArray new];
106127
_patterns = [NSMutableArray new];
128+
_outputs = [NSMutableArray new];
107129

108-
_iteration = 0;
109-
_maxIteration = 1;
110-
_convergenceValue = 0.0f;
130+
_iteration = DEFAULT_ITERATION;
131+
_maxIteration = DEFAULT_MAX_ITERATION;
132+
_convergenceValue = DEFAULT_CONVERGENCE_VALUE;
111133

112134
_activeFunction = KRHebbianActiveFunctionBySgn;
113-
_deltaSummation = 0.0f;
135+
_deltaSummation = DEFAULT_DELTA_SUMMATION;
114136
}
115137
return self;
116138
}
@@ -132,13 +154,11 @@ -(void)initializeWeights:(NSArray *)_initWeights
132154

133155
-(void)training
134156
{
157+
[_outputs removeAllObjects];
135158
_lastDeltaSummation = _deltaSummation;
136159
_deltaSummation = 0.0f;
137160
++_iteration;
138-
for( NSArray *_inputs in _patterns )
139-
{
140-
[self _turningWeightsByInputs:_inputs];
141-
}
161+
[self _doTrainAndDoesWannaTuneWeights:YES];
142162

143163
/*
144164
* @ 收斂方法 (擇一)
@@ -149,14 +169,14 @@ -(void)training
149169
{
150170
if( nil != _trainingCompletion )
151171
{
152-
_trainingCompletion(YES, _weights, _iteration);
172+
_trainingCompletion(YES, _outputs, _weights, _iteration);
153173
}
154174
}
155175
else
156176
{
157177
if( nil != _trainingIteraion )
158178
{
159-
_trainingIteraion(_iteration, _weights);
179+
_trainingIteraion(_iteration, _outputs, _weights);
160180
}
161181
[self training];
162182
}
@@ -168,6 +188,25 @@ -(void)trainingWithCompletion:(KRHebbianCompletion)_completion
168188
[self training];
169189
}
170190

191+
-(void)directOutputAtInputs:(NSArray *)_inputs completion:(KRHebbianDirectOutput)_completion
192+
{
193+
[self reset];
194+
[self addPatterns:_inputs];
195+
[self _doTrainAndDoesWannaTuneWeights:NO];
196+
if( nil != _completion )
197+
{
198+
_completion(_outputs, _weights);
199+
}
200+
}
201+
202+
-(void)reset
203+
{
204+
_maxIteration = DEFAULT_MAX_ITERATION;
205+
_iteration = DEFAULT_ITERATION;
206+
[_patterns removeAllObjects];
207+
[_outputs removeAllObjects];
208+
}
209+
171210
#pragma --mark Block Setters
172211
-(void)setTrainingCompletion:(KRHebbianCompletion)_block
173212
{

KRHebbianAlgorithm/ViewController.m

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,17 @@ - (void)viewDidLoad
2626
[_hebbian addPatterns:@[@0.0f, @1.5f, @-2.0f, @1.0f]]; // X1
2727
[_hebbian addPatterns:@[@-1.5f, @-2.0f, @-0.5f, @1.0f]]; // X2
2828
[_hebbian initializeWeights:@[@0.5f, @0.0f, @-1.0f, @1.0f]];
29-
[_hebbian setTrainingIteraion:^(NSInteger iteration, NSArray *weights) {
30-
NSLog(@"%li iteration = %@", iteration, weights);
29+
30+
[_hebbian setTrainingIteraion:^(NSInteger iteration, NSArray *outputs, NSArray *weights) {
31+
NSLog(@"Training %li iteration = %@, outputs = %@", iteration, weights, outputs);
3132
}];
32-
[_hebbian trainingWithCompletion:^(BOOL success, NSArray *weights, NSInteger totalIteration) {
33-
NSLog(@"%li iteration = %@", totalIteration, weights);
33+
34+
[_hebbian trainingWithCompletion:^(BOOL success, NSArray *outputs, NSArray *weights, NSInteger totalIteration) {
35+
NSLog(@"Trained %li iteration = %@, outputs = %@", totalIteration, weights, outputs);
36+
// Start in verifying
37+
[_hebbian directOutputAtInputs:@[@-0.5f, @-1.0f, @-0.2f, @0.5f] completion:^(NSArray *outputs, NSArray *weights) {
38+
NSLog(@"Verified weights = %@, outputs = %@", weights, outputs);
39+
}];
3440
}];
3541
}
3642

README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ KRHebbian implemented Hebbian algorithm that is a non-supervisor of self-organiz
66

77
```ruby
88
platform :ios, '7.0'
9-
pod "KRHebbian", "~> 1.2.1"
9+
pod "KRHebbian", "~> 1.3.0"
1010
```
1111

1212
## How To Get Started
@@ -18,24 +18,30 @@ pod "KRHebbian", "~> 1.2.1"
1818

1919
#### Sample
2020
``` objective-c
21-
KRHebbian *_hebbian = [KRHebbian sharedAlgorithm];
21+
KRHebbian *_hebbian = [KRHebbian sharedAlgorithm];
2222
_hebbian.activeFunction = KRHebbianActiveFunctionBySgn; // Tanh() for [-1.0, 1.0], Sgn() for (-1, 1)
23-
_hebbian.learningRate = 0.8f;
24-
_hebbian.maxIteration = 1;
23+
_hebbian.learningRate = 1.0f;
24+
_hebbian.maxIteration = 1;
2525
[_hebbian addPatterns:@[@0.0f, @1.5f, @-2.0f, @1.0f]]; // X1
2626
[_hebbian addPatterns:@[@-1.5f, @-2.0f, @-0.5f, @1.0f]]; // X2
2727
[_hebbian initializeWeights:@[@0.5f, @0.0f, @-1.0f, @1.0f]];
28-
[_hebbian setTrainingIteraion:^(NSInteger iteration, NSArray *weights) {
29-
NSLog(@"%li iteration = %@", iteration, weights);
28+
29+
[_hebbian setTrainingIteraion:^(NSInteger iteration, NSArray *outputs, NSArray *weights) {
30+
NSLog(@"Training %li iteration = %@, outputs = %@", iteration, weights, outputs);
3031
}];
31-
[_hebbian trainingWithCompletion:^(BOOL success, NSArray *weights, NSInteger totalIteration) {
32-
NSLog(@"%li iteration = %@", totalIteration, weights);
32+
33+
[_hebbian trainingWithCompletion:^(BOOL success, NSArray *outputs, NSArray *weights, NSInteger totalIteration) {
34+
NSLog(@"Trained %li iteration = %@, outputs = %@", totalIteration, weights, outputs);
35+
// Start in verifying
36+
[_hebbian directOutputAtInputs:@[@-0.5f, @-1.0f, @-0.2f, @0.5f] completion:^(NSArray *outputs, NSArray *weights) {
37+
NSLog(@"Verified weights = %@, outputs = %@", weights, outputs);
38+
}];
3339
}];
3440
```
3541
3642
## Version
3743
38-
V1.2.1
44+
V1.3.0
3945
4046
## LICENSE
4147

0 commit comments

Comments
 (0)