Skip to content

Commit 419d1e7

Browse files
author
lukas.molzberger
committed
- isInputSideInstantiable, isOutputSideInstantiable, CategoryInputSynapse instantiation
1 parent d858dc6 commit 419d1e7

File tree

20 files changed

+173
-96
lines changed

20 files changed

+173
-96
lines changed

core/src/main/java/network/aika/elements/activations/Activation.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ public boolean checkPrimary() {
381381
return true;
382382
}
383383

384-
public <IL extends Link> Optional<IL> getInputLinkByType(Class<IL> linkType) {
384+
public <IL> Optional<IL> getInputLinkByType(Class<IL> linkType) {
385385
return getInputLinksByType(linkType)
386386
.findAny();
387387
}
@@ -519,23 +519,19 @@ public boolean isActiveTemplateInstance() {
519519

520520
public abstract CategoryInputLink getActiveCategoryInputLink();
521521

522-
public Activation<N> resolveAbstractInputActivation() {
523-
return isInstantiable() ?
522+
public Activation<N> resolveAbstractInputActivation(boolean isSynapseInstantiable) {
523+
return isSynapseInstantiable && neuron.isInstantiable() ?
524524
getActiveTemplateInstance() :
525525
this;
526526
}
527527

528528
public Activation getActiveTemplateInstance() {
529529
CategoryInputLink l = getActiveCategoryInputLink();
530530
return l != null && l.getInput() != null ?
531-
l.getInput().getActiveTemplateInstance() :
531+
l.getInput().getActiveCategoryInput() :
532532
null;
533533
}
534534

535-
public boolean isInstantiable() {
536-
return neuron.isInstantiable() && neuron.isAbstract();
537-
}
538-
539535
public Activation<N> instantiateTemplateNode() {
540536
if(!neuron.isInstantiable())
541537
return null;
@@ -574,12 +570,12 @@ public Activation<N> instantiateTemplateNode() {
574570
return ti;
575571
}
576572

577-
private void linkTemplateAndInstance(Activation<N> ti) {
573+
protected void linkTemplateAndInstance(Activation<N> ti) {
578574
CategoryInputLink cl = getActiveCategoryInputLink();
579575
if(cl == null)
580576
cl = createCategoryInputLink();
581577

582-
cl.instantiateTemplate(cl.getInput(), ti, (Link) cl);
578+
cl.instantiateCategoryLink(ti);
583579
}
584580

585581
public CategoryInputLink createCategoryInputLink() {
@@ -595,24 +591,30 @@ public CategoryInputLink createCategoryInputLink() {
595591

596592
public void instantiateTemplateEdges(Activation<N> instanceAct) {
597593
getInputLinks()
594+
.filter(l -> l.getSynapse().isOutputSideInstantiable())
598595
.filter(l -> l.getInput() != null)
599-
.filter(l -> l.getInput().isFired(INNER_FEEDBACK))
596+
.filter(l -> l.getInput().isFired(INNER_FEEDBACK) || l instanceof CategoryInputLink) // Should this condition rely on the annealing value instead of instanceof?
600597
.forEach(l ->
601598
l.instantiateTemplate(
602-
l.getInput().resolveAbstractInputActivation(),
599+
l.getInput().resolveAbstractInputActivation(
600+
l.getSynapse().isInputSideInstantiable()
601+
),
603602
instanceAct
604603
)
605604
);
606605

607606
instanceAct.initFromTemplate(this);
608607

609608
getOutputLinks()
609+
.filter(l -> l.getSynapse().isInputSideInstantiable())
610610
.filter(l -> !(l instanceof CategoryLink))
611611
.filter(l -> l.getOutput().isFired(INNER_FEEDBACK))
612612
.forEach(l ->
613613
l.instantiateTemplate(
614614
instanceAct,
615-
l.getOutput().resolveAbstractInputActivation()
615+
l.getOutput().resolveAbstractInputActivation(
616+
l.getSynapse().isOutputSideInstantiable()
617+
)
616618
)
617619
);
618620
}

core/src/main/java/network/aika/elements/activations/CategoryActivation.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import network.aika.Document;
2020
import network.aika.elements.links.*;
21+
import network.aika.elements.synapses.CategoryInputSynapse;
22+
import network.aika.elements.synapses.Synapse;
2123
import network.aika.text.TextReference;
2224
import network.aika.elements.neurons.CategoryNeuron;
2325

@@ -38,6 +40,42 @@ public CategoryInputLink getActiveCategoryInputLink() {
3840
throw new UnsupportedOperationException();
3941
}
4042

43+
@Override
44+
protected void linkTemplateAndInstance(Activation<CategoryNeuron> ti) {
45+
if(ti.getOutputLinksByType(CategoryInputLink.class)
46+
.count() == 0)
47+
createCategoryInputLink(ti);
48+
}
49+
50+
public void createCategoryInputLink(Activation<CategoryNeuron> ti) {
51+
CategoryInputSynapse cis = ti.getNeuron().getOutgoingCategoryInputSynapse();
52+
if(cis == null) {
53+
instantiateCategoryInputSynapse(ti);
54+
return;
55+
}
56+
57+
Synapse s = ((Synapse)cis);
58+
Activation catInputAct = getActiveCategoryInput();
59+
60+
s.createAndInitLink(ti, catInputAct);
61+
}
62+
63+
private void instantiateCategoryInputSynapse(Activation<CategoryNeuron> ti) {
64+
Link cil = (Link) getOutputLinksByType(CategoryInputLink.class)
65+
.findFirst()
66+
.orElse(null);
67+
68+
if(cil == null)
69+
return;
70+
71+
Activation catInputAct = getActiveCategoryInput();
72+
73+
cil.instantiateTemplate(
74+
ti,
75+
catInputAct
76+
);
77+
}
78+
4179
@Override
4280
public Activation getTemplate() {
4381
return getOutputLinksByType(CategoryInputLink.class)
@@ -48,6 +86,18 @@ public Activation getTemplate() {
4886

4987
@Override
5088
public Activation getActiveTemplateInstance() {
89+
Activation inputAct = getActiveCategoryInput();
90+
if(inputAct == null)
91+
return null;
92+
93+
CategoryInputLink cil = inputAct.getActiveCategoryInputLink();
94+
if(cil == null)
95+
return null;
96+
97+
return cil.getInput();
98+
}
99+
100+
public Activation getActiveCategoryInput() {
51101
return getCategoryInputs()
52102
.filter(Activation::isActiveTemplateInstance)
53103
.max(

core/src/main/java/network/aika/elements/links/CategoryInputLink.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
import network.aika.elements.activations.Activation;
2020
import network.aika.elements.activations.CategoryActivation;
21+
import network.aika.elements.synapses.CategoryInputSynapse;
2122
import network.aika.elements.synapses.CategorySynapse;
2223
import network.aika.elements.synapses.Synapse;
24+
import network.aika.queue.steps.Instantiation;
2325

2426
/**
2527
*
@@ -35,17 +37,25 @@ public interface CategoryInputLink {
3537

3638
CategorySynapse createCategorySynapse();
3739

38-
default void instantiateTemplate(CategoryActivation iAct, Activation oAct, Link template) {
39-
if(iAct == null || oAct == null)
40+
default void instantiateCategoryLink(Activation instanceAct) {
41+
Link tl = (Link) this;
42+
CategoryActivation categoryAct = (CategoryActivation) tl.getInput();
43+
44+
if(categoryAct == null || instanceAct == null)
4045
return;
4146

42-
Link l = iAct.getInputLink(oAct, getSynapse().getSynapseId());
47+
Link l = categoryAct.getInputLink(instanceAct, getSynapse().getSynapseId());
4348
if(l != null)
4449
return;
4550

4651
CategorySynapse s = createCategorySynapse();
47-
s.initFromTemplate(oAct.getNeuron(), iAct.getNeuron(), getSynapse());
52+
s.initFromTemplate(instanceAct.getNeuron(), categoryAct.getNeuron(), getSynapse());
53+
54+
CategoryInputSynapse cis = (CategoryInputSynapse) getSynapse();
55+
s.setWeight(cis.getInitialCategorySynapseWeight());
56+
57+
s.createLinkFromTemplate(instanceAct, categoryAct, tl);
4858

49-
s.createLinkFromTemplate(oAct, iAct, template);
59+
Instantiation.add(categoryAct);
5060
}
5161
}

core/src/main/java/network/aika/elements/links/Link.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import network.aika.elements.Element;
2222
import network.aika.elements.Type;
2323
import network.aika.elements.activations.Activation;
24+
import network.aika.elements.activations.CategoryActivation;
2425
import network.aika.elements.activations.bsslots.BindingSignalSlot;
2526
import network.aika.elements.activations.StateType;
2627
import network.aika.elements.activations.bsslots.SingleBSSlot;
@@ -149,9 +150,6 @@ public void instantiateTemplate(I iAct, O oAct) {
149150
if(iAct == null || oAct == null)
150151
return;
151152

152-
if(!synapse.isInstantiable())
153-
return;
154-
155153
S s = (S) synapse.instantiateTemplate(
156154
iAct.getNeuron(),
157155
oAct.getNeuron()

core/src/main/java/network/aika/elements/links/types/BindingCategoryInputLink.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,4 @@ public CategorySynapse createCategorySynapse() {
3939
return new BindingCategorySynapse();
4040
}
4141

42-
@Override
43-
public void instantiateTemplate(CategoryActivation iAct, BindingActivation oAct) {
44-
instantiateTemplate(iAct, oAct, this);
45-
}
4642
}

core/src/main/java/network/aika/elements/links/types/InhibitoryCategoryInputLink.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import network.aika.elements.synapses.*;
2424
import network.aika.elements.synapses.types.InhibitoryCategoryInputSynapse;
2525
import network.aika.elements.synapses.types.InhibitoryCategorySynapse;
26-
import network.aika.fields.Field;
2726

2827
import static network.aika.elements.activations.types.InhibitoryActivation.crossConnectFields;
2928

@@ -47,8 +46,4 @@ public CategorySynapse createCategorySynapse() {
4746
return new InhibitoryCategorySynapse();
4847
}
4948

50-
@Override
51-
public void instantiateTemplate(CategoryActivation iAct, InhibitoryActivation oAct) {
52-
instantiateTemplate(iAct, oAct, this);
53-
}
5449
}

core/src/main/java/network/aika/elements/links/types/PatternCategoryInputLink.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,4 @@ public CategorySynapse createCategorySynapse() {
4141
@Override
4242
protected void connectGradientFields() {
4343
}
44-
45-
@Override
46-
public void instantiateTemplate(CategoryActivation iAct, PatternActivation oAct) {
47-
instantiateTemplate(iAct, oAct, this);
48-
}
4944
}

core/src/main/java/network/aika/elements/neurons/CategoryNeuron.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public CategoryInputSynapse getOutgoingCategoryInputSynapse() {
4444
return getOutputSynapseByType(CategoryInputSynapse.class);
4545
}
4646

47+
protected void initFromTemplate(Neuron templateN) {
48+
initParamsFromTemplate(templateN);
49+
}
50+
4751
@Override
4852
public CategorySynapse createCategorySynapse() {
4953
throw new UnsupportedOperationException();

core/src/main/java/network/aika/elements/neurons/Neuron.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,18 @@ public <R extends N> R instantiateTemplate() {
239239
}
240240

241241
protected void initFromTemplate(Neuron templateN) {
242+
initParamsFromTemplate(templateN);
243+
244+
CategoryInputSynapse cis = templateN.getCategoryInputSynapse();
245+
if(cis == null)
246+
throw new MissingInputCategoryNeuron(templateN);
247+
248+
createCategorySynapse()
249+
.setWeight(cis.getInitialCategorySynapseWeight())
250+
.link(this, cis.getInput());
251+
}
252+
253+
protected void initParamsFromTemplate(Neuron templateN) {
242254
bias.setInitialValue(
243255
templateN.getBias().getUpdatedValue()
244256
);
@@ -247,14 +259,6 @@ protected void initFromTemplate(Neuron templateN) {
247259
InitParams ip = templateN.initParams;
248260
setTargetNet(ip.targetNet);
249261
}
250-
251-
CategoryInputSynapse cis = templateN.getCategoryInputSynapse();
252-
if(cis == null)
253-
throw new MissingInputCategoryNeuron(templateN);
254-
255-
createCategorySynapse()
256-
.setWeight(cis.getInitialInstanceWeight())
257-
.link(this, cis.getInput());
258262
}
259263

260264
public N setInstantiable(boolean instantiable) {
@@ -263,13 +267,15 @@ public N setInstantiable(boolean instantiable) {
263267
return (N) this;
264268
}
265269

266-
public N setInstantiable(boolean instantiable, boolean includeSyns) {
267-
if (includeSyns)
268-
getInputSynapses().forEach(s ->
269-
s.setInstantiable(instantiable)
270-
);
270+
public N setInputSynapsesInstantiable(boolean inputSideInstantiable, boolean outputSideInstantiable) {
271+
getInputSynapses()
272+
.stream()
273+
.filter(s -> !(s instanceof CategoryInputSynapse))
274+
.forEach(s ->
275+
s.setInstantiable(inputSideInstantiable, outputSideInstantiable)
276+
);
271277

272-
return setInstantiable(instantiable);
278+
return (N) this;
273279
}
274280

275281
public boolean isInstantiable() {

core/src/main/java/network/aika/elements/synapses/CategoryInputSynapse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ default boolean isTrainingAllowed() {
3535

3636
void setInitialCategorySynapseWeight(double initialCategorySynapseWeight);
3737

38-
double getInitialInstanceWeight();
38+
double getInitialCategorySynapseWeight();
3939

4040
S setWeight(double w);
4141

0 commit comments

Comments
 (0)