-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPBGNeuron.m
183 lines (148 loc) · 3.03 KB
/
PBGNeuron.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// PBGNeuron.m
// neurosis
//
// Created by Patrick B. Gibson on 27/10/07.
// Copyright 2007 Patrick B. Gibson. All rights reserved.
//
#import "PBGNeuron.h"
#include <stdlib.h>
@interface PBGNeuron (Private)
- (double)evaluate;
@end
@implementation PBGNeuron
- (id)initWithID:(int)i networkSize:(int)netSize threshold:(BOOL)t
{
self = [super init];
if (self != nil) {
neuronID = i;
inputConnectionsArray = [[NSMutableArray alloc] initWithCapacity:3];
double high = 2.4 / netSize;
double low = -2.4 / netSize;
value = 0;
valueWasExplicitlySet = NO;
errorGradient = 0;
usingThreshold = t;
if (usingThreshold)
threshold = (rand() / ( (double) (RAND_MAX) + 1.0)) * (high - low) + low;
else
threshold = 0;
activationFunction = PBGSigmoidFunction;
}
return self;
}
- (double)outputValue
{
if (valueWasExplicitlySet) {
return value;
} else {
return [self evaluate];
}
}
- (NSString *)description
{
return [NSString stringWithFormat:@"(%d)", neuronID];
}
- (void)addInputConnection:(PBGWeightedConnection *)connection
{
[inputConnectionsArray addObject:connection];
}
- (PBGWeightedConnection *)connectionToNeuron:(PBGNeuron *)neuron
{
for (PBGWeightedConnection *connection in inputConnectionsArray){
if ([connection inputNeuron] == neuron){
return connection;
}
}
return nil;
}
- (void)setValue:(double)newValue
{
valueWasExplicitlySet = YES;
value = newValue;
}
- (void)setThreshold:(double)t
{
threshold = t;
}
- (double)threshold
{
return threshold;
}
- (NSMutableArray *)inputConnectionsArray
{
return inputConnectionsArray;
}
- (void)setNewThreshold:(double)t
{
newThreshold = t;
}
- (void)updateNow
{
if (DEBUG_LOGGING)
NSLog(@"%@ Updating...", self);
[self setThreshold:newThreshold];
for (PBGWeightedConnection *connection in inputConnectionsArray) {
[connection updateNow];
}
}
- (double)errorGradientUsingExpectedOutput:(double)expectedOutput
{
double errorDelta = expectedOutput - value;
errorGradient = value * (1 - value) * errorDelta;
return errorGradient;
}
- (double)errorGradient
{
return errorGradient;
}
- (void)setErrorGradient:(double)e
{
errorGradient = e;
}
- (void)dealloc
{
[inputConnectionsArray release];
[super dealloc];
}
@end
@implementation PBGNeuron (Private)
- (double)evaluate
{
double sigma = 0;
for (PBGWeightedConnection *connection in inputConnectionsArray) {
sigma += [connection outputValue];
}
if (usingThreshold)
sigma += -1 * threshold;
switch (activationFunction) {
default:
case PBGSignFunction:
if (sigma >= 0) {
value = 1;
} else {
value = -1;
}
break;
case PBGStepFunction:
if (sigma >= 0) {
value = 1;
} else {
value = 0;
}
break;
case PBGSigmoidFunction:
value = 1/(1+exp(-sigma));
break;
case PBGSigmoidFunctionHyperbolic:
value = (2 * 1.716)/(1+exp(-sigma * 0.667)) - 1.716;
break;
case PBGLinearFunction:
value = sigma;
break;
}
if (DEBUG_LOGGING)
NSLog(@"Neuron %@ is returning value of %f", self, value);
return value;
}
@end