Skip to content

Commit 2d26b6c

Browse files
author
Lukas Molzberger
committed
Activation.Builder
1 parent fbeadda commit 2d26b6c

11 files changed

+208
-122
lines changed

src/main/java/org/aika/Neuron.java

Lines changed: 11 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -56,110 +56,37 @@ public Neuron(Model m, INeuron n) {
5656
* @param end The range end
5757
*/
5858
public Activation addInput(Document doc, int begin, int end) {
59-
return addInput(doc, begin, end, null, doc.bottom);
59+
return addInput(doc, begin, end, null);
6060
}
6161

62-
63-
/**
64-
* Propagate an input activation into the network.
65-
*
66-
* @param doc The current document
67-
* @param begin The range begin
68-
* @param end The range end
69-
* @param value The activation value of this input activation
70-
* @param targetValue The target activation value for supervised learning
71-
*/
72-
public Activation addInput(Document doc, int begin, int end, double value, Double targetValue) {
73-
return addInput(doc, begin, end, null, doc.bottom, value, targetValue, 0);
74-
}
75-
76-
77-
/**
78-
* Propagate an input activation into the network.
79-
*
80-
* @param doc The current document
81-
* @param begin The range begin
82-
* @param end The range end
83-
* @param value The activation value of this input activation
84-
* @param targetValue The target activation value for supervised learning
85-
*/
86-
public Activation addInput(Document doc, int begin, int end, double value, Double targetValue, int fired) {
87-
return addInput(doc, begin, end, null, doc.bottom, value, targetValue, fired);
88-
}
89-
90-
91-
/**
92-
* Propagate an input activation into the network.
93-
*
94-
* @param doc The current document
95-
* @param begin The range begin
96-
* @param end The range end
97-
* @param o The interpretation node
98-
*/
99-
public Activation addInput(Document doc, int begin, int end, InterpretationNode o) {
100-
return addInput(doc, begin, end, null, o);
101-
}
102-
103-
10462
/**
10563
* Propagate an input activation into the network.
10664
*
10765
* @param doc The current document
10866
* @param begin The range begin
10967
* @param end The range end
110-
* @param rid The relational id (e.g. the word position)
68+
* @param relationalId
11169
*/
112-
public Activation addInput(Document doc, int begin, int end, Integer rid) {
113-
return addInput(doc, begin, end, rid, doc.bottom);
70+
public Activation addInput(Document doc, int begin, int end, Integer relationalId) {
71+
return addInput(doc,
72+
new Activation.Builder()
73+
.setRange(begin, end)
74+
.setRelationalId(relationalId)
75+
);
11476
}
11577

11678

11779
/**
11880
* Propagate an input activation into the network.
11981
*
12082
* @param doc The current document
121-
* @param begin The range begin
122-
* @param end The range end
123-
* @param rid The relational id (e.g. the word position)
124-
* @param o The interpretation node
83+
* @param inputAct
12584
*/
126-
public Activation addInput(Document doc, int begin, int end, Integer rid, InterpretationNode o) {
127-
return addInput(doc, begin, end, rid, o, 1.0);
85+
public Activation addInput(Document doc, Activation.Builder inputAct) {
86+
return get(doc).addInput(doc, inputAct);
12887
}
12988

13089

131-
/**
132-
* Propagate an input activation into the network.
133-
*
134-
* @param doc The current document
135-
* @param begin The range begin
136-
* @param end The range end
137-
* @param rid The relational id (e.g. the word position)
138-
* @param o The interpretation node
139-
* @param value The activation value of this input activation
140-
*/
141-
public Activation addInput(Document doc, int begin, int end, Integer rid, InterpretationNode o, double value) {
142-
return addInput(doc, begin, end, rid, o, value, null, 0);
143-
}
144-
145-
/**
146-
* Propagate an input activation into the network.
147-
*
148-
* @param doc The current document
149-
* @param begin The range begin
150-
* @param end The range end
151-
* @param rid The relational id (e.g. the word position)
152-
* @param o The interpretation node
153-
* @param value The activation value of this input activation
154-
* @param targetValue The target activation value for supervised learning
155-
*/
156-
public Activation addInput(Document doc, int begin, int end, Integer rid, InterpretationNode o, double value, Double targetValue, int fired) {
157-
return get(doc).addInput(doc, begin, end, rid, o, value, targetValue, fired);
158-
}
159-
160-
161-
162-
16390
/**
16491
* Creates a neuron with the given bias.
16592
*

src/main/java/org/aika/neuron/Activation.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,5 +497,50 @@ public Activation getActivation() {
497497
}
498498
}
499499

500+
501+
public static class Builder {
502+
public Range range;
503+
public Integer rid;
504+
public InterpretationNode interpretation;
505+
public double value = 1.0;
506+
public Double targetValue;
507+
public int fired;
508+
509+
510+
public Builder setRange(int begin, int end) {
511+
this.range = new Range(begin, end);
512+
return this;
513+
}
514+
515+
public Builder setRange(Range range) {
516+
this.range = range;
517+
return this;
518+
}
519+
520+
public Builder setRelationalId(Integer rid) {
521+
this.rid = rid;
522+
return this;
523+
}
524+
525+
public Builder setInterpretation(InterpretationNode interpretation) {
526+
this.interpretation = interpretation;
527+
return this;
528+
}
529+
530+
public Builder setValue(double value) {
531+
this.value = value;
532+
return this;
533+
}
534+
535+
public Builder setTargetValue(Double targetValue) {
536+
this.targetValue = targetValue;
537+
return this;
538+
}
539+
540+
public Builder setFired(int fired) {
541+
this.fired = fired;
542+
return this;
543+
}
544+
}
500545
}
501546

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -180,29 +180,26 @@ public INeuron(Model m, String label, String outputText) {
180180
* Propagate an input activation into the network.
181181
*
182182
* @param doc The current document
183-
* @param begin The range begin
184-
* @param end The range end
185-
* @param rid The relational id (e.g. the word position)
186-
* @param o The interpretation node
187-
* @param value The activation value of this input activation
183+
* @param input
188184
*/
189-
public Activation addInput(Document doc, int begin, int end, Integer rid, InterpretationNode o, double value, Double targetValue, int fired) {
190-
Node.addActivationAndPropagate(doc, new NodeActivation.Key(node.get(doc), new Range(begin, end), rid, o), Collections.emptySet());
185+
public Activation addInput(Document doc, Activation.Builder input) {
186+
InterpretationNode interpr = input.interpretation != null ? input.interpretation : doc.bottom;
187+
Node.addActivationAndPropagate(doc, new NodeActivation.Key(node.get(doc), input.range, input.rid, interpr), Collections.emptySet());
191188

192189
doc.propagate();
193190

194-
Activation act = Activation.get(doc, this, rid, new Range(begin, end), Range.Relation.EQUALS, o, InterpretationNode.Relation.EQUALS);
195-
State s = new State(value, fired, NormWeight.ZERO_WEIGHT);
191+
Activation act = Activation.get(doc, this, input.rid, input.range, Range.Relation.EQUALS, interpr, InterpretationNode.Relation.EQUALS);
192+
State s = new State(input.value, input.fired, NormWeight.ZERO_WEIGHT);
196193
act.rounds.set(0, s);
197-
act.inputValue = value;
198-
act.targetValue = targetValue;
194+
act.inputValue = input.value;
195+
act.targetValue = input.targetValue;
199196

200197
doc.inputNeuronActivations.add(act);
201198
doc.finallyActivatedNeurons.add(act.key.node.neuron.get(doc));
202199

203200
doc.ubQueue.add(act);
204201

205-
if(targetValue != null) {
202+
if(input.targetValue != null) {
206203
doc.supervisedTraining.targetActivations.add(act);
207204
}
208205

src/site/usage.html

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,11 @@ <h3>Named Entity Recognition / Entity Resolution example</h3>
226226
for(String w: doc.getContent().split(" ")) {
227227
int j = i + w.length();
228228

229-
inputNeurons.get(w).addInput(doc, i, j, wordPos);
229+
inputNeurons.get(w).addInput(doc,
230+
new Activation.Builder()
231+
.setRange(i, j)
232+
.setRelationalId(wordPos)
233+
);
230234

231235
i = j + 1;
232236
wordPos++;
@@ -374,7 +378,7 @@ <h3>Mutual exclusion example</h3>
374378
.setRangeMatch(CONTAINED_IN)
375379
);
376380

377-
// Finally addInput adds all the inputs to the suppressing neuron.
381+
// Finally we add all the inputs to the suppressing neuron.
378382
Neuron.init(
379383
pSuppr,
380384
0.0,
@@ -574,7 +578,11 @@ <h3>Pattern matching example</h3>
574578
for(int i = 0; i < doc.length(); i++) {
575579
char c = doc.getContent().charAt(i);
576580
if(c != ' ') {
577-
inputNeurons.get(c).addInput(doc, i, i + 1, wordPos);
581+
inputNeurons.get(c).addInput(doc,
582+
new Activation.Builder()
583+
.setRange(i, i + 1)
584+
.setRelationalId(wordPos)
585+
);
578586
} else {
579587
wordPos++;
580588
}

src/test/java/org/aika/network/ActivationOutputsTest.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ public void simpleAddActivationTest1() {
164164

165165

166166
InterpretationNode o1 = InterpretationNode.addPrimitive(doc);
167-
inA.addInput(doc, 0, 1, o1);
167+
inA.addInput(doc, new Activation.Builder()
168+
.setRange(0, 1)
169+
.setInterpretation(o1)
170+
);
168171

169172
Activation outB1 = Activation.get(doc, outB, null, new Range(0, 1), Range.Relation.CONTAINS, null, null);
170173
Assert.assertTrue(containsOutputActivation(inA.get().getFirstActivation(doc).neuronOutputs, outB1));
@@ -189,7 +192,11 @@ public void simpleAddActivationTest2() {
189192
).get();
190193

191194

192-
inA.addInput(doc, 0, 1, InterpretationNode.addPrimitive(doc));
195+
inA.addInput(doc,
196+
new Activation.Builder()
197+
.setRange(0, 1)
198+
.setInterpretation(InterpretationNode.addPrimitive(doc))
199+
);
193200

194201
Activation outB1 = Activation.get(doc, outB, null, new Range(0, 1), Range.Relation.CONTAINS, null, null);
195202

@@ -217,7 +224,13 @@ public void removeRemoveDestinationActivation() {
217224

218225

219226
InterpretationNode o1 = InterpretationNode.addPrimitive(doc);
220-
inA.addInput(doc, 0, 1, 0, o1);
227+
inA.addInput(doc,
228+
new Activation.Builder()
229+
.setRange(0, 1)
230+
.setRelationalId(0)
231+
.setInterpretation(o1)
232+
);
233+
221234
Activation outB1 = Activation.get(doc, outB, null, new Range(0, 1), Range.Relation.CONTAINS, null, null);
222235

223236
Assert.assertTrue(containsOutputActivation(inA.get().getFirstActivation(doc).neuronOutputs, outB1));

src/test/java/org/aika/network/AddSynapseTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.aika.network;
1818

1919

20+
import org.aika.neuron.Activation;
2021
import org.aika.neuron.Synapse;
2122
import org.aika.Model;
2223
import org.aika.Neuron;
@@ -58,7 +59,11 @@ public void testAddSynapse() {
5859

5960
int i = 0;
6061
for(Neuron in: inputNeurons.values()) {
61-
in.addInput(doc, i * 2, (i * 2) + 1, i);
62+
in.addInput(doc,
63+
new Activation.Builder()
64+
.setRange(i * 2, (i * 2) + 1)
65+
.setRelationalId(i)
66+
);
6267

6368
i++;
6469
}

src/test/java/org/aika/network/NegationTest.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,21 @@ public void testTwoNegativeInputs1() {
8787

8888
InterpretationNode o1 = InterpretationNode.addPrimitive(doc);
8989

90-
inB.addInput(doc, 2, 7, o1);
90+
inB.addInput(doc,
91+
new Activation.Builder()
92+
.setRange(2, 7)
93+
.setInterpretation(o1)
94+
);
9195

9296
System.out.println(doc.neuronActivationsToString(true, false, true));
9397

9498
InterpretationNode o2 = InterpretationNode.addPrimitive(doc);
9599

96-
inC.addInput(doc, 4, 9, o2);
100+
inC.addInput(doc,
101+
new Activation.Builder()
102+
.setRange(4, 9)
103+
.setInterpretation(o2)
104+
);
97105

98106
System.out.println(doc.neuronActivationsToString(true, false, true));
99107

@@ -154,12 +162,20 @@ public void testTwoNegativeInputs2() {
154162
System.out.println(doc.neuronActivationsToString(true, false, true));
155163

156164
InterpretationNode ob = InterpretationNode.addPrimitive(doc);
157-
inB.addInput(doc, 2, 7, ob);
165+
inB.addInput(doc,
166+
new Activation.Builder()
167+
.setRange(2, 7)
168+
.setInterpretation(ob)
169+
);
158170

159171
System.out.println(doc.neuronActivationsToString(true, false, true));
160172

161173
InterpretationNode oc = InterpretationNode.addPrimitive(doc);
162-
inC.addInput(doc, 4, 9, oc);
174+
inC.addInput(doc,
175+
new Activation.Builder()
176+
.setRange(4, 9)
177+
.setInterpretation(oc)
178+
);
163179

164180
System.out.println(doc.neuronActivationsToString(true, false, true));
165181

@@ -213,7 +229,11 @@ public void testSimpleNegation1() {
213229

214230
InterpretationNode o = InterpretationNode.addPrimitive(doc);
215231

216-
inS.addInput(doc, 3, 8, o);
232+
inS.addInput(doc,
233+
new Activation.Builder()
234+
.setRange(3, 8)
235+
.setInterpretation(o)
236+
);
217237

218238
System.out.println(doc.neuronActivationsToString(true, false, true));
219239

@@ -273,7 +293,11 @@ public void testSimpleNegation2() {
273293

274294
InterpretationNode o = InterpretationNode.addPrimitive(doc);
275295

276-
inS.addInput(doc, 3, 8, o);
296+
inS.addInput(doc,
297+
new Activation.Builder()
298+
.setRange(3, 8)
299+
.setInterpretation(o)
300+
);
277301

278302
System.out.println(doc.neuronActivationsToString(true, false, true));
279303

@@ -337,7 +361,11 @@ public void testSimpleNegation3() {
337361

338362
System.out.println(doc.neuronActivationsToString(true, false, true));
339363

340-
inS.addInput(doc, 3, 8, o);
364+
inS.addInput(doc,
365+
new Activation.Builder()
366+
.setRange(3, 8)
367+
.setInterpretation(o)
368+
);
341369

342370
System.out.println(doc.neuronActivationsToString(true, false, true));
343371

0 commit comments

Comments
 (0)