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{
0 commit comments