Skip to content

Commit 86d0913

Browse files
author
Lukas Molzberger
committed
discovery fixed, removed legacy code
1 parent 6edd62e commit 86d0913

File tree

8 files changed

+113
-58
lines changed

8 files changed

+113
-58
lines changed

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,6 @@ public Activation addInput(Document doc, int begin, int end) {
5858
return addInput(doc, begin, end, null, doc.bottom);
5959
}
6060

61-
/**
62-
* Propagate an input activation into the network.
63-
*
64-
* @param doc The current document
65-
* @param begin The range begin
66-
* @param end The range end
67-
* @param value The activation value of this input activation
68-
*/
69-
public Activation addInput(Document doc, int begin, int end, double value) {
70-
return addInput(doc, begin, end, null, doc.bottom, value);
71-
}
7261

7362
/**
7463
* Propagate an input activation into the network.

src/main/java/org/aika/lattice/AndNode.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ void addActivation(Document doc, Key ak, Collection<NodeActivation<?>> directInp
103103
}
104104

105105

106-
public void propagateAddedActivation(Document doc, NodeActivation act, InterprNode removedConflict) {
107-
apply(doc, act, removedConflict);
106+
public void propagateAddedActivation(Document doc, NodeActivation act) {
107+
apply(doc, act);
108108
}
109109

110110

@@ -121,7 +121,7 @@ public void cleanup() {
121121

122122

123123
@Override
124-
void apply(Document doc, NodeActivation<AndNode> act, InterprNode removedConflict) {
124+
void apply(Document doc, NodeActivation<AndNode> act) {
125125

126126
for(NodeActivation<?> pAct: act.inputs.values()) {
127127
Node<?, NodeActivation<?>> pn = pAct.key.n;
@@ -136,7 +136,7 @@ void apply(Document doc, NodeActivation<AndNode> act, InterprNode removedConflic
136136

137137
Provider<AndNode> nlp = getAndChild(nRef);
138138
if (nlp != null) {
139-
addNextLevelActivation(doc, act, secondAct, nlp, removedConflict);
139+
addNextLevelActivation(doc, act, secondAct, nlp);
140140
}
141141
}
142142
}
@@ -145,9 +145,7 @@ void apply(Document doc, NodeActivation<AndNode> act, InterprNode removedConflic
145145
pn.lock.releaseReadLock();
146146
}
147147

148-
if(removedConflict == null) {
149-
OrNode.processCandidate(doc, this, act, false);
150-
}
148+
OrNode.processCandidate(doc, this, act, false);
151149
}
152150

153151

@@ -225,6 +223,7 @@ public static AndNode createNextLevelNode(Model m, int threadId, Node n, Refinem
225223
nln.init();
226224
} else {
227225
m.removeProvider(nln.provider);
226+
nln = null;
228227
}
229228
}
230229

@@ -236,11 +235,11 @@ public static AndNode createNextLevelNode(Model m, int threadId, Node n, Refinem
236235
}
237236

238237

239-
public static void addNextLevelActivation(Document doc, NodeActivation<AndNode> act, NodeActivation<AndNode> secondAct, Provider<AndNode> pnlp, InterprNode conflict) {
238+
public static void addNextLevelActivation(Document doc, NodeActivation<AndNode> act, NodeActivation<AndNode> secondAct, Provider<AndNode> pnlp) {
240239
// TODO: check if the activation already exists
241240
Key ak = act.key;
242241
InterprNode o = InterprNode.add(doc, true, ak.o, secondAct.key.o);
243-
if (o != null && (conflict == null || o.contains(conflict, false))) {
242+
if (o != null) {
244243
AndNode nlp = pnlp.get();
245244
nlp.addActivation(
246245
doc,

src/main/java/org/aika/lattice/InputNode.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class InputNode extends Node<InputNode, NodeActivation<InputNode>> {
5252
public Neuron inputNeuron;
5353

5454
// Key: Output Neuron
55-
Map<SynapseKey, Synapse> synapses;
55+
public Map<SynapseKey, Synapse> synapses;
5656

5757
public ReadWriteLock synapseLock = new ReadWriteLock();
5858

@@ -121,9 +121,9 @@ public void addActivation(Document doc, NodeActivation inputAct) {
121121
}
122122

123123

124-
public void propagateAddedActivation(Document doc, NodeActivation act, InterprNode removedConflict) {
124+
public void propagateAddedActivation(Document doc, NodeActivation act) {
125125
if (!key.isRecurrent) {
126-
apply(doc, act, removedConflict);
126+
apply(doc, act);
127127
}
128128
}
129129

@@ -145,30 +145,27 @@ Collection<Refinement> collectNodeAndRefinements(Refinement newRef) {
145145
/**
146146
* @param doc
147147
* @param act
148-
* @param removedConflict This parameter contains a removed conflict if it is not null. In this case only expand activations that contain this removed conflict.
149148
*/
150149
@Override
151-
void apply(Document doc, NodeActivation act, InterprNode removedConflict) {
150+
void apply(Document doc, NodeActivation act) {
152151

153152
lock.acquireReadLock();
154153
if (andChildren != null) {
155154
for (Map.Entry<Refinement, Provider<AndNode>> me : andChildren.entrySet()) {
156155
Provider<InputNode> refInput = me.getKey().input;
157156
InputNode in = refInput.getIfNotSuspended();
158157
if (in != null) {
159-
addNextLevelActivations(doc, in, me.getKey(), me.getValue(), act, removedConflict);
158+
addNextLevelActivations(doc, in, me.getKey(), me.getValue(), act);
160159
}
161160
}
162161
}
163162
lock.releaseReadLock();
164163

165-
if (removedConflict == null) {
166-
OrNode.processCandidate(doc, this, act, false);
167-
}
164+
OrNode.processCandidate(doc, this, act, false);
168165
}
169166

170167

171-
private static void addNextLevelActivations(Document doc, InputNode secondNode, Refinement ref, Provider<AndNode> pnlp, NodeActivation act, InterprNode removedConflict) {
168+
private static void addNextLevelActivations(Document doc, InputNode secondNode, Refinement ref, Provider<AndNode> pnlp, NodeActivation act) {
172169
ThreadState th = secondNode.getThreadState(doc.threadId, false);
173170
if (th == null || th.activations.isEmpty()) return;
174171

@@ -189,7 +186,7 @@ private static void addNextLevelActivations(Document doc, InputNode secondNode,
189186

190187
s.forEach(secondAct -> {
191188
InterprNode o = InterprNode.add(doc, true, ak.o, secondAct.key.o);
192-
if (o != null && (removedConflict == null || o.contains(removedConflict, false))) {
189+
if (o != null) {
193190
AndNode nlp = pnlp.get();
194191
nlp.addActivation(doc,
195192
new NodeActivation.Key(
@@ -247,8 +244,8 @@ public void discover(Document doc, NodeActivation<InputNode> act, DiscoveryConfi
247244
!in.key.isRecurrent &&
248245
!(key.startRangeOutput && in.key.startRangeOutput) &&
249246
!(key.endRangeOutput && in.key.endRangeOutput) &&
250-
srm.compare(act.key.r.begin, secondAct.key.r.begin) &&
251-
erm.compare(act.key.r.end, secondAct.key.r.end)
247+
srm.compare(secondAct.key.r.begin, act.key.r.begin) &&
248+
erm.compare(secondAct.key.r.end, act.key.r.end)
252249
) {
253250
in.visitedDiscover = v;
254251
AndNode nln = AndNode.createNextLevelNode(doc.m, doc.threadId, this, ref, discoveryConfig);

src/main/java/org/aika/lattice/Node.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ public abstract class Node<T extends Node, A extends NodeActivation<T>> extends
5858

5959
private static final Logger log = LoggerFactory.getLogger(Node.class);
6060

61-
TreeMap<ReverseAndRefinement, Refinement> reverseAndChildren;
62-
TreeMap<Refinement, Provider<AndNode>> andChildren;
63-
TreeSet<OrEntry> orChildren;
64-
TreeSet<OrEntry> allOrChildren;
61+
public TreeMap<ReverseAndRefinement, Refinement> reverseAndChildren;
62+
public TreeMap<Refinement, Provider<AndNode>> andChildren;
63+
public TreeSet<OrEntry> orChildren;
64+
public TreeSet<OrEntry> allOrChildren;
6565

6666
public int level;
6767

@@ -173,15 +173,14 @@ public ThreadState<T, A> getThreadState(int threadId, boolean create) {
173173
*
174174
* @param doc
175175
* @param act
176-
* @param conflict
177176
*/
178-
public abstract void propagateAddedActivation(Document doc, A act, InterprNode conflict);
177+
public abstract void propagateAddedActivation(Document doc, A act);
179178

180179
public abstract boolean isAllowedOption(int threadId, InterprNode n, NodeActivation<?> act, long v);
181180

182181
public abstract double computeSynapseWeightSum(Integer offset, INeuron n);
183182

184-
abstract void apply(Document doc, A act, InterprNode conflict);
183+
abstract void apply(Document doc, A act);
185184

186185
public abstract void discover(Document doc, NodeActivation<T> act, DiscoveryConfig discoveryConfig);
187186

@@ -325,7 +324,7 @@ A processAddedActivation(Document doc, Key<T> ak, Collection<NodeActivation> inp
325324

326325
act.link(inputActs);
327326

328-
propagateAddedActivation(doc, act, null);
327+
propagateAddedActivation(doc, act);
329328
} else {
330329
act.link(inputActs);
331330
}

src/main/java/org/aika/lattice/OrNode.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,8 @@ private boolean checkSelfReferencing(Document doc, NodeActivation inputAct) {
164164
}
165165

166166

167-
public void propagateAddedActivation(Document doc, Activation act, InterprNode removedConflict) {
168-
if(removedConflict == null) {
169-
neuron.get().propagateAddedActivation(doc, act);
170-
}
167+
public void propagateAddedActivation(Document doc, Activation act) {
168+
neuron.get().propagateAddedActivation(doc, act);
171169
}
172170

173171

@@ -184,15 +182,13 @@ public void cleanup() {
184182

185183

186184
@Override
187-
public void apply(Document doc, Activation act, InterprNode conflict) {
188-
if(conflict == null) {
189-
OrNode.processCandidate(doc, this, act, false);
190-
}
185+
public void apply(Document doc, Activation act) {
186+
OrNode.processCandidate(doc, this, act, false);
191187
}
192188

193189

194190
@Override
195-
public void discover(Document doc, NodeActivation act, DiscoveryConfig doscoveryConfig) {
191+
public void discover(Document doc, NodeActivation act, DiscoveryConfig discoveryConfig) {
196192
}
197193

198194

src/site/resources.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ <h3>Aika Version 0.10 (2017-11-03)</h3>
6262
</li>
6363
<li>
6464
Removed some old experimental training code and provided two APIs for training and pattern discovery.
65-
The APIs allow to implement some heuristics when deciding which synapses should be created or which
65+
The APIs allow to implement heuristics when deciding which synapses should be created or which
6666
patterns should be selected.
6767
</li>
6868
<li>

src/test/java/org/aika/lattice/PatternDiscoveryTest.java

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.aika.corpus.Range;
2424
import org.aika.lattice.AndNode.Refinement;
2525
import org.aika.network.TestHelper;
26+
import org.aika.neuron.Synapse;
2627
import org.junit.Assert;
2728
import org.junit.Test;
2829

@@ -71,7 +72,7 @@ public void resetFrequency(Model m) {
7172
}
7273

7374

74-
private boolean checkRidRange(Node n) {
75+
private boolean checkRidRange(Node n, int x) {
7576
if(n instanceof InputNode) return true;
7677
AndNode an = (AndNode) n;
7778

@@ -81,7 +82,7 @@ private boolean checkRidRange(Node n) {
8182
maxRid = Math.max(maxRid, ref.rid);
8283
}
8384
}
84-
return maxRid < 1;
85+
return maxRid < x;
8586
}
8687

8788

@@ -106,7 +107,7 @@ public void discoverPatterns() {
106107
DiscoveryConfig discoveryConfig = new DiscoveryConfig()
107108
.setCounter((d, n) -> count(d, n))
108109
.setCheckExpandable(n -> ((NodeStatistic) n.statistic).frequency >= 1)
109-
.setCheckValidPattern(n -> checkRidRange(n));
110+
.setCheckValidPattern(n -> checkRidRange(n, 1));
110111

111112
doc.bestInterpretation = Arrays.asList(doc.bottom);
112113
doc.discoverPatterns(discoveryConfig);
@@ -401,4 +402,78 @@ public void discoverPatterns() {
401402
Assert.assertNull(TestHelper.get(doc, pCNode, new Range(0, 1), doc.bottom));
402403

403404
}
405+
406+
407+
@Test
408+
public void testSimplePattern() {
409+
Model m = new Model();
410+
m.setNodeStatisticFactory(() -> new NodeStatistic());
411+
412+
Neuron inA = m.createNeuron("A");
413+
Neuron inB = m.createNeuron("B");
414+
415+
416+
InputNode.add(m,
417+
new Synapse.Key(
418+
false,
419+
0,
420+
null,
421+
Range.Operator.EQUALS,
422+
Range.Mapping.START,
423+
true,
424+
Range.Operator.GREATER_THAN_EQUAL,
425+
Range.Mapping.END,
426+
false
427+
),
428+
inA.get()
429+
);
430+
431+
InputNode.add(m,
432+
new Synapse.Key(
433+
false,
434+
0,
435+
null,
436+
Range.Operator.LESS_THAN_EQUAL,
437+
Range.Mapping.START,
438+
false,
439+
Range.Operator.EQUALS,
440+
Range.Mapping.END,
441+
true
442+
),
443+
inB.get()
444+
);
445+
446+
DiscoveryConfig discoveryConfig = new DiscoveryConfig()
447+
.setCounter((d, n) -> count(d, n))
448+
.setCheckExpandable(n -> ((NodeStatistic) n.statistic).frequency >= 1)
449+
.setCheckValidPattern(n -> checkRidRange(n, 2));
450+
{
451+
Document doc = m.createDocument("ab", 0);
452+
453+
inA.addInput(doc, 0, 1, 0);
454+
inB.addInput(doc, 1, 2, 1);
455+
456+
doc.process();
457+
458+
doc.discoverPatterns(discoveryConfig);
459+
460+
doc.clearActivations();
461+
}
462+
463+
{
464+
Document doc = m.createDocument("ab", 0);
465+
466+
inA.addInput(doc, 0, 1, 0);
467+
inB.addInput(doc, 1, 2, 1);
468+
469+
doc.process();
470+
471+
System.out.println(doc.nodeActivationsToString(true, true));
472+
473+
Assert.assertNotNull(inA.get().outputNodes.firstEntry().getValue().get().andChildren.firstEntry().getValue().get().getFirstActivation(doc));
474+
475+
doc.clearActivations();
476+
}
477+
478+
}
404479
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public void testTraining1() {
7979

8080
{
8181
Document doc = m.createDocument("Bla");
82-
inA.addInput(doc, 0, 3, 1.0);
83-
inB.addInput(doc, 0, 3, 1.0);
82+
inA.addInput(doc, 0, 3, 1.0, 0.0);
83+
inB.addInput(doc, 0, 3, 1.0, 0.0);
8484

8585
doc.process();
8686

@@ -109,8 +109,8 @@ public void testTraining1() {
109109

110110
{
111111
Document doc = m.createDocument("Bla");
112-
inA.addInput(doc, 0, 3, 1.0);
113-
inB.addInput(doc, 0, 3, 1.0);
112+
inA.addInput(doc, 0, 3, 1.0, 0.0);
113+
inB.addInput(doc, 0, 3, 1.0, 0.0);
114114

115115
doc.process();
116116

0 commit comments

Comments
 (0)