Skip to content

Commit 9027b28

Browse files
author
Lukas Molzberger
committed
work on the meta network
1 parent 53a9462 commit 9027b28

File tree

1 file changed

+49
-43
lines changed

1 file changed

+49
-43
lines changed

src/main/java/org/aika/training/MetaNetwork.java

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.aika.training;
1818

1919

20+
import org.aika.Utils;
2021
import org.aika.neuron.Synapse;
2122
import org.aika.Model;
2223
import org.aika.neuron.Neuron;
@@ -53,7 +54,7 @@ public class MetaNetwork {
5354
public static void train(Document doc) {
5455
long v = doc.visitedCounter++;
5556

56-
Map<Activation, Target> metaActivations = new TreeMap<>();
57+
Map<Activation, List<Target>> metaActivations = new TreeMap<>();
5758

5859
List<INeuron> inhibitoryNeurons = doc.finallyActivatedNeurons
5960
.stream()
@@ -76,16 +77,23 @@ public static void train(Document doc) {
7677
}
7778

7879
Activation metaNeuronAct = getMetaNeuronAct(inhibAct);
79-
if (metaNeuronAct != null && metaNeuronAct.visited != v) {
80-
metaNeuronAct.visited = v;
81-
metaActivations.put(metaNeuronAct, new Target(targetNeuron, newNeuron, n.provider));
80+
if (metaNeuronAct != null) {
81+
List<Target> targets = metaActivations.get(metaNeuronAct);
82+
if(targets == null) {
83+
targets = new ArrayList<>();
84+
metaActivations.put(metaNeuronAct, targets);
85+
}
86+
87+
targets.add(new Target(targetNeuron, newNeuron, n.provider));
8288
}
8389
}
8490
}
8591
}
8692

87-
for(Map.Entry<Activation, Target> me: metaActivations.entrySet()) {
88-
transferMetaSynapses(doc, metaActivations, me.getKey(), me.getValue(), v);
93+
for(Map.Entry<Activation, List<Target>> me: metaActivations.entrySet()) {
94+
for(Target t: me.getValue()) {
95+
transferMetaSynapses(doc, metaActivations, me.getKey(), t, v);
96+
}
8997
}
9098
}
9199

@@ -113,56 +121,35 @@ private static Activation getMetaNeuronAct(Activation inhibAct) {
113121
}
114122

115123

116-
private static boolean checkForNewlyCreatedActivations(Activation sAct) {
117-
for(SynapseActivation sa: sAct.neuronInputs) {
118-
if(sa.input.finalDecision == UNKNOWN) {
119-
return false;
120-
}
121-
}
122-
123-
return true;
124-
}
125-
126-
127-
private static void transferMetaSynapses(Document doc, Map<Activation, Target> metaActivations, Activation metaAct, Target t, long v) {
124+
private static void transferMetaSynapses(Document doc, Map<Activation, List<Target>> metaActivations, Activation metaAct, Target t, long v) {
128125
TreeSet<Synapse> inputSynapses = new TreeSet<>(Synapse.INPUT_SYNAPSE_COMP);
129126

130127
Integer ridOffset = computeRidOffset(metaAct);
131128
for (SynapseActivation sa : metaAct.getFinalInputActivations()) {
132-
MetaSynapse ss = sa.synapse.meta;
133-
if (ss != null && (ss.metaWeight != 0.0 || ss.metaBias != 0.0)) {
129+
MetaSynapse inputMetaSyanpse = sa.synapse.meta;
130+
Synapse.Key osk = sa.synapse.key;
131+
132+
if (inputMetaSyanpse != null && (inputMetaSyanpse.metaWeight != 0.0 || inputMetaSyanpse.metaBias != 0.0)) {
134133
Neuron ina = sa.input.key.node.neuron;
135134

136-
List<SynapseActivation> inputs = ina.get().type == INeuron.Type.INHIBITORY && ss.metaWeight >= 0.0 ?
135+
List<SynapseActivation> inputs = ina.get().type == INeuron.Type.INHIBITORY && inputMetaSyanpse.metaWeight >= 0.0 ?
137136
sa.input.getFinalInputActivations() :
138137
Collections.singletonList(sa);
139138

140139
for(SynapseActivation isa: inputs) {
141140
Neuron in = isa.input.getNeuron();
142-
if(in.get(doc).type == INeuron.Type.META) {
143-
Target it = metaActivations.get(isa.input);
144-
in = it != null ? it.targetNeuron : null;
145-
}
141+
Integer rid = isa.input.key.rid;
142+
Integer nRid = Utils.nullSafeSub(rid, false, ridOffset, false);
146143

147-
if(in != null) {
148-
Integer rid = isa.input.key.rid;
149-
Synapse.Key osk = sa.synapse.key;
150-
Synapse.Key nsk = new Synapse.Key(
151-
osk.isRecurrent,
152-
osk.relativeRid != null ?
153-
osk.relativeRid :
154-
(ss.metaRelativeRid && ridOffset != null && rid != null ? rid - ridOffset : null),
155-
osk.absoluteRid,
156-
osk.rangeMatch,
157-
osk.rangeOutput
158-
);
159-
160-
Synapse ns = new Synapse(in, t.targetNeuron, nsk);
161-
if (!ns.exists()) {
162-
ns.updateDelta(doc, ss.metaWeight, ss.metaBias);
163-
164-
inputSynapses.add(ns);
144+
if(in.get(doc).type == INeuron.Type.META) {
145+
List<Target> inputTargets = metaActivations.get(isa.input);
146+
if(inputTargets != null) {
147+
for (Target it : metaActivations.get(isa.input)) {
148+
createOrLookupSynapse(doc, t, inputSynapses, inputMetaSyanpse, osk, nRid, it.targetNeuron);
149+
}
165150
}
151+
} else {
152+
createOrLookupSynapse(doc, t, inputSynapses, inputMetaSyanpse, osk, nRid, in);
166153
}
167154
}
168155
}
@@ -193,6 +180,25 @@ private static void transferMetaSynapses(Document doc, Map<Activation, Target> m
193180
doc.propagate();
194181
}
195182

183+
private static void createOrLookupSynapse(Document doc, Target t, TreeSet<Synapse> inputSynapses, MetaSynapse inputMetaSyanpse, Synapse.Key osk, Integer nRid, Neuron in) {
184+
Synapse.Key nsk = new Synapse.Key(
185+
osk.isRecurrent,
186+
osk.relativeRid != null ?
187+
osk.relativeRid :
188+
(inputMetaSyanpse.metaRelativeRid ? nRid : null),
189+
osk.absoluteRid,
190+
osk.rangeMatch,
191+
osk.rangeOutput
192+
);
193+
194+
Synapse ns = new Synapse(in, t.targetNeuron, nsk);
195+
if (!ns.exists()) {
196+
ns.updateDelta(doc, inputMetaSyanpse.metaWeight, inputMetaSyanpse.metaBias);
197+
198+
inputSynapses.add(ns);
199+
}
200+
}
201+
196202

197203
public static String showDelta(INeuron n, Set<Synapse> synapses) {
198204
StringBuilder sb = new StringBuilder();

0 commit comments

Comments
 (0)