Skip to content

Commit f5a37cf

Browse files
moved type parameter from Neuron.init to createNeuron
1 parent d31c27e commit f5a37cf

32 files changed

+417
-551
lines changed

src/main/java/network/aika/ActivationFunction.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
public enum ActivationFunction {
55

6+
NULL_FUNCTION(x -> 0.0),
67
RECTIFIED_HYPERBOLIC_TANGENT(x -> Math.max(0.0, Math.tanh(x))),
78
RECTIFIED_LINEAR_UNIT(x -> Math.max(0.0, x)),
89
LIMITED_RECTIFIED_LINEAR_UNIT(x -> Math.max(0.0, Math.min(1.0, x)));

src/main/java/network/aika/Model.java

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public class Model {
5656

5757

5858
protected SynapseFactory synapseFactory = (input, output, id) -> new Synapse(input, output, id);
59-
protected NeuronFactory neuronFactory = (label, outputText, type, actF) -> new INeuron(this, label, outputText, type, actF);
6059
protected LinkerFactory linkerFactory = (doc) -> new Linker(doc);
6160

6261
public SearchNode.SkipSelectStep skipSelectStep = (act) -> false;
@@ -99,14 +98,6 @@ public void setSynapseFactory(SynapseFactory synapseFactory) {
9998
this.synapseFactory = synapseFactory;
10099
}
101100

102-
public NeuronFactory getNeuronFactory() {
103-
return neuronFactory;
104-
}
105-
106-
public void setNeuronFactory(NeuronFactory neuronFactory) {
107-
this.neuronFactory = neuronFactory;
108-
}
109-
110101
public SuspensionHook getSuspensionHook() {
111102
return suspensionHook;
112103
}
@@ -135,25 +126,27 @@ public void setSkipSelectStep(SearchNode.SkipSelectStep skipSelectStep) {
135126
}
136127

137128

138-
public Neuron createNeuron() {
139-
return createNeuron(null);
129+
public Neuron createNeuron(Type type) {
130+
return createNeuron(null, type);
140131
}
141132

142133

143-
public Neuron createNeuron(String label) {
144-
return createNeuron(label, null);
134+
public Neuron createNeuron(String label, Type type) {
135+
return createNeuron(label, type, type.getDefaultActivationFunction(), null);
145136
}
146137

147138

148-
public Neuron createNeuron(String label, String outputText) {
149-
INeuron n = neuronFactory.createNeuron(label, outputText, INPUT, ActivationFunction.RECTIFIED_HYPERBOLIC_TANGENT);
150-
return n.getProvider();
139+
public Neuron createNeuron(String label, Type type, ActivationFunction actF) {
140+
return new INeuron(this, label, null, type, type.getDefaultActivationFunction()).getProvider();
141+
}
142+
143+
public Neuron createNeuron(String label, Type type, String outputText) {
144+
return new INeuron(this, label, outputText, type, type.getDefaultActivationFunction()).getProvider();
151145
}
152146

153147

154-
public Neuron createNeuron(String label, String outputText, Type type, ActivationFunction actF) {
155-
INeuron n = neuronFactory.createNeuron(label, outputText, type, actF);
156-
return n.getProvider();
148+
public Neuron createNeuron(String label, Type type, ActivationFunction actF, String outputText) {
149+
return new INeuron(this, label, outputText, type, actF).getProvider();
157150
}
158151

159152

@@ -287,10 +280,6 @@ public interface SynapseFactory {
287280
}
288281

289282

290-
public interface NeuronFactory {
291-
INeuron createNeuron(String label, String outputText, Type type, ActivationFunction actF);
292-
}
293-
294283
public interface LinkerFactory {
295284

296285
Linker createLinker(Document doc);

src/main/java/network/aika/neuron/INeuron.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828

2929
import java.io.*;
3030
import java.util.*;
31-
import java.util.function.Predicate;
3231
import java.util.stream.Collectors;
3332
import java.util.stream.Stream;
3433

34+
import static network.aika.ActivationFunction.*;
3535
import static network.aika.neuron.INeuron.Type.EXCITATORY;
3636
import static network.aika.neuron.INeuron.Type.INPUT;
3737
import static network.aika.neuron.Synapse.State.CURRENT;
@@ -60,14 +60,24 @@ public class INeuron extends AbstractNode<Neuron> implements Comparable<INeuron>
6060
public static final INeuron MIN_NEURON = new INeuron();
6161
public static final INeuron MAX_NEURON = new INeuron();
6262

63-
String label;
64-
Type type;
63+
private String label;
64+
private Type type;
6565

6666

6767
public enum Type {
68-
INPUT,
69-
EXCITATORY,
70-
INHIBITORY
68+
INPUT(NULL_FUNCTION),
69+
EXCITATORY(RECTIFIED_HYPERBOLIC_TANGENT),
70+
INHIBITORY(RECTIFIED_LINEAR_UNIT);
71+
72+
private ActivationFunction defaultActivationFunction;
73+
74+
Type(ActivationFunction defaultActivationFunction) {
75+
this.defaultActivationFunction = defaultActivationFunction;
76+
}
77+
78+
public ActivationFunction getDefaultActivationFunction() {
79+
return defaultActivationFunction;
80+
}
7181
}
7282

7383

@@ -352,6 +362,11 @@ private INeuron() {
352362
}
353363

354364

365+
public INeuron(Model m, String label, Type type, ActivationFunction actF) {
366+
this(m, label, null, type, actF);
367+
}
368+
369+
355370
public INeuron(Model m, String label, String outputText, Type type, ActivationFunction actF) {
356371
this.label = label;
357372
this.type = type;

src/main/java/network/aika/neuron/Neuron.java

Lines changed: 14 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -98,36 +98,11 @@ public static Neuron init(Neuron n, Builder... inputs) {
9898

9999

100100
public static Neuron init(Document doc, Neuron n, Builder... inputs) {
101-
if(n.init(doc, null, null, null, getSynapseBuilders(inputs), getRelationBuilders(inputs))) {
101+
if(n.init(doc, null, getSynapseBuilders(inputs), getRelationBuilders(inputs))) {
102102
return n;
103103
} else return null;
104104
}
105105

106-
/**
107-
* Creates a neuron with the given bias.
108-
*
109-
* @param n
110-
* @param bias
111-
* @param inputs
112-
* @return
113-
*/
114-
public static Neuron init(Neuron n, double bias, Type type, Builder... inputs) {
115-
return init(n, bias, type, getSynapseBuilders(inputs), getRelationBuilders(inputs));
116-
}
117-
118-
119-
/**
120-
* Creates a neuron with the given bias.
121-
*
122-
* @param n
123-
* @param bias
124-
* @param inputs
125-
* @return
126-
*/
127-
public static Neuron init(Document doc, Neuron n, double bias, Type type, Builder... inputs) {
128-
return init(doc, n, bias, null, type, getSynapseBuilders(inputs), getRelationBuilders(inputs));
129-
}
130-
131106

132107
/**
133108
* Creates a neuron with the given bias.
@@ -137,8 +112,8 @@ public static Neuron init(Document doc, Neuron n, double bias, Type type, Builde
137112
* @param inputs
138113
* @return
139114
*/
140-
public static Neuron init(Neuron n, double bias, ActivationFunction activationFunction, Type type, Builder... inputs) {
141-
return init(n, bias, activationFunction, type, getSynapseBuilders(inputs), getRelationBuilders(inputs));
115+
public static Neuron init(Neuron n, double bias, Builder... inputs) {
116+
return init(n, bias, getSynapseBuilders(inputs), getRelationBuilders(inputs));
142117
}
143118

144119

@@ -150,28 +125,10 @@ public static Neuron init(Neuron n, double bias, ActivationFunction activationFu
150125
* @param inputs
151126
* @return
152127
*/
153-
public static Neuron init(Document doc, Neuron n, double bias, ActivationFunction activationFunction, Type type, Builder... inputs) {
154-
return init(doc, n, bias, activationFunction, type, getSynapseBuilders(inputs), getRelationBuilders(inputs));
128+
public static Neuron init(Document doc, Neuron n, double bias, Builder... inputs) {
129+
return init(doc, n, bias, getSynapseBuilders(inputs), getRelationBuilders(inputs));
155130
}
156131

157-
158-
/**
159-
* Creates a neuron with the given bias.
160-
*
161-
* @param n
162-
* @param bias
163-
* @param synapseBuilders
164-
* @param relationBuilders
165-
* @return
166-
*/
167-
public static Neuron init(Neuron n, double bias, Type type, Collection<Synapse.Builder> synapseBuilders, Collection<Relation.Builder> relationBuilders) {
168-
return init(n, bias, null, type, synapseBuilders, relationBuilders);
169-
}
170-
171-
172-
public static Neuron init(Neuron n, double bias, Type type, Collection<Neuron.Builder> inputs) {
173-
return init(n, bias, null, type, getSynapseBuilders(inputs), getRelationBuilders(inputs));
174-
}
175132

176133

177134
/**
@@ -183,14 +140,14 @@ public static Neuron init(Neuron n, double bias, Type type, Collection<Neuron.Bu
183140
* @param relationBuilders
184141
* @return
185142
*/
186-
public static Neuron init(Neuron n, double bias, ActivationFunction activationFunction, Type type, Collection<Synapse.Builder> synapseBuilders, Collection<Relation.Builder> relationBuilders) {
187-
if(n.init((Document) null, bias, activationFunction, type, synapseBuilders, relationBuilders)) return n;
143+
public static Neuron init(Neuron n, double bias, Collection<Synapse.Builder> synapseBuilders, Collection<Relation.Builder> relationBuilders) {
144+
if(n.init((Document) null, bias, synapseBuilders, relationBuilders)) return n;
188145
return null;
189146
}
190147

191148

192-
public static Neuron init(Neuron n, double bias, ActivationFunction activationFunction, Type type, Collection<Neuron.Builder> inputs) {
193-
if(n.init((Document) null, bias, activationFunction, type, getSynapseBuilders(inputs), getRelationBuilders(inputs))) return n;
149+
public static Neuron init(Neuron n, double bias, Collection<Neuron.Builder> inputs) {
150+
if(n.init((Document) null, bias, getSynapseBuilders(inputs), getRelationBuilders(inputs))) return n;
194151
return null;
195152
}
196153

@@ -203,29 +160,21 @@ public static Neuron init(Neuron n, double bias, ActivationFunction activationFu
203160
* @param relationBuilders
204161
* @return
205162
*/
206-
public static Neuron init(Document doc, Neuron n, double bias, ActivationFunction activationFunction, Type type, Collection<Synapse.Builder> synapseBuilders, Collection<Relation.Builder> relationBuilders) {
207-
if(n.init(doc, bias, activationFunction, type, synapseBuilders, relationBuilders)) return n;
163+
public static Neuron init(Document doc, Neuron n, double bias, Collection<Synapse.Builder> synapseBuilders, Collection<Relation.Builder> relationBuilders) {
164+
if(n.init(doc, bias, synapseBuilders, relationBuilders)) return n;
208165
return null;
209166
}
210167

211168

212-
public static Neuron init(Document doc, Neuron n, double bias, ActivationFunction activationFunction, Type type, Collection<Neuron.Builder> inputs) {
213-
if(n.init(doc, bias, activationFunction, type, getSynapseBuilders(inputs), getRelationBuilders(inputs))) return n;
169+
public static Neuron init(Document doc, Neuron n, double bias, Collection<Neuron.Builder> inputs) {
170+
if(n.init(doc, bias, getSynapseBuilders(inputs), getRelationBuilders(inputs))) return n;
214171
return null;
215172
}
216173

217174

218-
private boolean init(Document doc, Double bias, ActivationFunction activationFunction, Type type, Collection<Synapse.Builder> synapseBuilders, Collection<Relation.Builder> relationBuilders) {
175+
private boolean init(Document doc, Double bias, Collection<Synapse.Builder> synapseBuilders, Collection<Relation.Builder> relationBuilders) {
219176
INeuron n = get();
220177

221-
if(activationFunction != null) {
222-
n.activationFunction = activationFunction;
223-
}
224-
225-
if(type != null) {
226-
n.type = type;
227-
}
228-
229178
if(bias != null) {
230179
n.setBias(bias);
231180
}

src/test/java/network/aika/lattice/ActivationsTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.TreeMap;
3333

3434
import static network.aika.neuron.INeuron.Type.EXCITATORY;
35+
import static network.aika.neuron.INeuron.Type.INPUT;
3536
import static network.aika.neuron.Synapse.OUTPUT;
3637
import static network.aika.neuron.activation.Activation.BEGIN;
3738
import static network.aika.neuron.activation.Activation.END;
@@ -48,9 +49,9 @@ public class ActivationsTest {
4849
public void addActivationsTest() {
4950
Model m = new Model();
5051

51-
Neuron inA = m.createNeuron("A");
52+
Neuron inA = m.createNeuron("A", INPUT);
5253

53-
Neuron pA = Neuron.init(m.createNeuron("pA"), 0.5, EXCITATORY,
54+
Neuron pA = Neuron.init(m.createNeuron("pA", EXCITATORY), 0.5,
5455
new Synapse.Builder()
5556
.setSynapseId(0)
5657
.setNeuron(inA)
@@ -83,7 +84,7 @@ public void addActivationsTest() {
8384
public void testGetActivationReturnsFirstFired() {
8485
Model m = new Model();
8586

86-
Neuron in = m.createNeuron("A");
87+
Neuron in = m.createNeuron("A", INPUT);
8788
OrNode inNode = in.get().getInputNode().get();
8889

8990
Document doc = new Document(m, "aaaaaaaaaa", 0);

0 commit comments

Comments
 (0)