Skip to content

Commit

Permalink
Modified algorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalvar committed Nov 8, 2015
1 parent 83442a4 commit 14147d3
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 31 deletions.
2 changes: 1 addition & 1 deletion KRHebbian.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "KRHebbian"
s.version = "1.2.1"
s.version = "1.3.0"
s.summary = "Non-supervisor that Hebbian self-organization learning method in machine learning. (自分学習アルゴリズム)."
s.description = <<-DESC
KRHebbian implemented Hebbian algorithm that is a non-supervisor of self-organization algorithm of Machine Learning (自分学習アルゴリズム).
Expand Down
8 changes: 6 additions & 2 deletions KRHebbian/KRHebbian.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ typedef enum KRHebbianActiveFunctions
KRHebbianActiveFunctionByTanh
}KRHebbianActiveFunctions;

typedef void(^KRHebbianCompletion)(BOOL success, NSArray *weights, NSInteger totalIteration);
typedef void(^KRHebbianIteration)(NSInteger iteration, NSArray *weights);
typedef void(^KRHebbianCompletion)(BOOL success, NSArray *outputs, NSArray *weights, NSInteger totalIteration);
typedef void(^KRHebbianIteration)(NSInteger iteration, NSArray *outputs, NSArray *weights);
typedef void(^KRHebbianDirectOutput)(NSArray *outputs, NSArray *weights);

@interface KRHebbian : NSObject

@property (nonatomic, strong) NSMutableArray *patterns;
@property (nonatomic, strong) NSMutableArray *weights;
@property (nonatomic, strong) NSMutableArray *outputs;
@property (nonatomic, assign) float learningRate;
@property (nonatomic, assign) NSInteger maxIteration;
@property (nonatomic, assign) float convergenceValue;
Expand All @@ -37,6 +39,8 @@ typedef void(^KRHebbianIteration)(NSInteger iteration, NSArray *weights);
-(void)initializeWeights:(NSArray *)_initWeights;
-(void)training;
-(void)trainingWithCompletion:(KRHebbianCompletion)_completion;
-(void)directOutputAtInputs:(NSArray *)_inputs completion:(KRHebbianDirectOutput)_completion;
-(void)reset;

-(void)setTrainingCompletion:(KRHebbianCompletion)_block;
-(void)setTrainingIteraion:(KRHebbianIteration)_block;
Expand Down
69 changes: 54 additions & 15 deletions KRHebbian/KRHebbian.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

#import "KRHebbian.h"

#define DEFAULT_LEARNING_RATE 0.5f
#define DEFAULT_ITERATION 0
#define DEFAULT_MAX_ITERATION 1
#define DEFAULT_CONVERGENCE_VALUE 0.0f
#define DEFAULT_DELTA_SUMMATION 0.0f

@interface KRHebbian ()

@property (nonatomic, assign) NSInteger iteration;
Expand Down Expand Up @@ -39,14 +45,16 @@ -(float)_fOfSgn:(double)_sgnValue
*/
-(double)_fOfNetWithInputs:(NSArray *)_inputs
{
float _sum = 0.0f;
double _sum = 0.0f;
NSInteger _index = 0;
for( NSNumber *_xValue in _inputs )
{
_sum += [_xValue floatValue] * [[self.weights objectAtIndex:_index] floatValue];
_sum += [_xValue doubleValue] * [[self.weights objectAtIndex:_index] doubleValue];
++_index;
}

NSLog(@"_sum : %lf", _sum);

double _activatedValue = 0.0f;
switch (self.activeFunction)
{
Expand All @@ -63,11 +71,10 @@ -(double)_fOfNetWithInputs:(NSArray *)_inputs
/*
* @ Step 3. 求 New Weights
*/
-(void)_turningWeightsByInputs:(NSArray *)_inputs
-(void)_turningWeightsByInputs:(NSArray *)_inputs netOutput:(double)_netOutput
{
NSArray *_weights = self.weights;
float _learningRate = self.learningRate;
double _netOutput = [self _fOfNetWithInputs:_inputs];
NSMutableArray *_newWeights = [NSMutableArray new];
NSInteger _index = 0;
for( NSNumber *_weightValue in _weights )
Expand All @@ -82,6 +89,20 @@ -(void)_turningWeightsByInputs:(NSArray *)_inputs
[self.weights addObjectsFromArray:_newWeights];
}

// Start in calculate net outputs and tune the weights (if needed)
-(void)_doTrainAndDoesWannaTuneWeights:(BOOL)_goTuning
{
for( NSArray *_inputs in self.patterns )
{
double _netOutput = [self _fOfNetWithInputs:_inputs];
[self.outputs addObject:[NSNumber numberWithDouble:_netOutput]];
if( _goTuning )
{
[self _turningWeightsByInputs:_inputs netOutput:_netOutput];
}
}
}

@end

@implementation KRHebbian
Expand All @@ -101,16 +122,17 @@ -(instancetype)init
self = [super init];
if( self )
{
_learningRate = 0.5f;
_learningRate = DEFAULT_LEARNING_RATE;
_weights = [NSMutableArray new];
_patterns = [NSMutableArray new];
_outputs = [NSMutableArray new];

_iteration = 0;
_maxIteration = 1;
_convergenceValue = 0.0f;
_iteration = DEFAULT_ITERATION;
_maxIteration = DEFAULT_MAX_ITERATION;
_convergenceValue = DEFAULT_CONVERGENCE_VALUE;

_activeFunction = KRHebbianActiveFunctionBySgn;
_deltaSummation = 0.0f;
_deltaSummation = DEFAULT_DELTA_SUMMATION;
}
return self;
}
Expand All @@ -132,13 +154,11 @@ -(void)initializeWeights:(NSArray *)_initWeights

-(void)training
{
[_outputs removeAllObjects];
_lastDeltaSummation = _deltaSummation;
_deltaSummation = 0.0f;
++_iteration;
for( NSArray *_inputs in _patterns )
{
[self _turningWeightsByInputs:_inputs];
}
[self _doTrainAndDoesWannaTuneWeights:YES];

/*
* @ 收斂方法 (擇一)
Expand All @@ -149,14 +169,14 @@ -(void)training
{
if( nil != _trainingCompletion )
{
_trainingCompletion(YES, _weights, _iteration);
_trainingCompletion(YES, _outputs, _weights, _iteration);
}
}
else
{
if( nil != _trainingIteraion )
{
_trainingIteraion(_iteration, _weights);
_trainingIteraion(_iteration, _outputs, _weights);
}
[self training];
}
Expand All @@ -168,6 +188,25 @@ -(void)trainingWithCompletion:(KRHebbianCompletion)_completion
[self training];
}

-(void)directOutputAtInputs:(NSArray *)_inputs completion:(KRHebbianDirectOutput)_completion
{
[self reset];
[self addPatterns:_inputs];
[self _doTrainAndDoesWannaTuneWeights:NO];
if( nil != _completion )
{
_completion(_outputs, _weights);
}
}

-(void)reset
{
_maxIteration = DEFAULT_MAX_ITERATION;
_iteration = DEFAULT_ITERATION;
[_patterns removeAllObjects];
[_outputs removeAllObjects];
}

#pragma --mark Block Setters
-(void)setTrainingCompletion:(KRHebbianCompletion)_block
{
Expand Down
14 changes: 10 additions & 4 deletions KRHebbianAlgorithm/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ - (void)viewDidLoad
[_hebbian addPatterns:@[@0.0f, @1.5f, @-2.0f, @1.0f]]; // X1
[_hebbian addPatterns:@[@-1.5f, @-2.0f, @-0.5f, @1.0f]]; // X2
[_hebbian initializeWeights:@[@0.5f, @0.0f, @-1.0f, @1.0f]];
[_hebbian setTrainingIteraion:^(NSInteger iteration, NSArray *weights) {
NSLog(@"%li iteration = %@", iteration, weights);

[_hebbian setTrainingIteraion:^(NSInteger iteration, NSArray *outputs, NSArray *weights) {
NSLog(@"Training %li iteration = %@, outputs = %@", iteration, weights, outputs);
}];
[_hebbian trainingWithCompletion:^(BOOL success, NSArray *weights, NSInteger totalIteration) {
NSLog(@"%li iteration = %@", totalIteration, weights);

[_hebbian trainingWithCompletion:^(BOOL success, NSArray *outputs, NSArray *weights, NSInteger totalIteration) {
NSLog(@"Trained %li iteration = %@, outputs = %@", totalIteration, weights, outputs);
// Start in verifying
[_hebbian directOutputAtInputs:@[@-0.5f, @-1.0f, @-0.2f, @0.5f] completion:^(NSArray *outputs, NSArray *weights) {
NSLog(@"Verified weights = %@, outputs = %@", weights, outputs);
}];
}];
}

Expand Down
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ KRHebbian implemented Hebbian algorithm that is a non-supervisor of self-organiz

```ruby
platform :ios, '7.0'
pod "KRHebbian", "~> 1.2.1"
pod "KRHebbian", "~> 1.3.0"
```

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

#### Sample
``` objective-c
KRHebbian *_hebbian = [KRHebbian sharedAlgorithm];
KRHebbian *_hebbian = [KRHebbian sharedAlgorithm];
_hebbian.activeFunction = KRHebbianActiveFunctionBySgn; // Tanh() for [-1.0, 1.0], Sgn() for (-1, 1)
_hebbian.learningRate = 0.8f;
_hebbian.maxIteration = 1;
_hebbian.learningRate = 1.0f;
_hebbian.maxIteration = 1;
[_hebbian addPatterns:@[@0.0f, @1.5f, @-2.0f, @1.0f]]; // X1
[_hebbian addPatterns:@[@-1.5f, @-2.0f, @-0.5f, @1.0f]]; // X2
[_hebbian initializeWeights:@[@0.5f, @0.0f, @-1.0f, @1.0f]];
[_hebbian setTrainingIteraion:^(NSInteger iteration, NSArray *weights) {
NSLog(@"%li iteration = %@", iteration, weights);

[_hebbian setTrainingIteraion:^(NSInteger iteration, NSArray *outputs, NSArray *weights) {
NSLog(@"Training %li iteration = %@, outputs = %@", iteration, weights, outputs);
}];
[_hebbian trainingWithCompletion:^(BOOL success, NSArray *weights, NSInteger totalIteration) {
NSLog(@"%li iteration = %@", totalIteration, weights);

[_hebbian trainingWithCompletion:^(BOOL success, NSArray *outputs, NSArray *weights, NSInteger totalIteration) {
NSLog(@"Trained %li iteration = %@, outputs = %@", totalIteration, weights, outputs);
// Start in verifying
[_hebbian directOutputAtInputs:@[@-0.5f, @-1.0f, @-0.2f, @0.5f] completion:^(NSArray *outputs, NSArray *weights) {
NSLog(@"Verified weights = %@, outputs = %@", weights, outputs);
}];
}];
```
## Version
V1.2.1
V1.3.0
## LICENSE
Expand Down

0 comments on commit 14147d3

Please sign in to comment.