Skip to content

Commit 2b1862e

Browse files
author
lukas.molzberger
committed
- work on instantiation
1 parent b4502da commit 2b1862e

File tree

17 files changed

+77
-110
lines changed

17 files changed

+77
-110
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,20 @@ public interface CategoryInputLink {
3535

3636
Synapse getSynapse();
3737

38-
CategorySynapse createCategorySynapse();
39-
4038
default void instantiateCategoryLink(Activation instanceAct) {
4139
Link tl = (Link) this;
4240
CategoryActivation categoryAct = (CategoryActivation) tl.getInput();
4341

4442
if(categoryAct == null || instanceAct == null)
4543
return;
4644

47-
CategorySynapse s = createCategorySynapse();
45+
CategorySynapse s = getOutput().getNeuron().createCategorySynapse();
46+
4847
s.initFromTemplate(instanceAct.getNeuron(), categoryAct.getNeuron(), getSynapse());
4948

49+
s.setInputSideInstantiable(false);
50+
s.setOutputSideInstantiable(false);
51+
5052
CategoryInputSynapse cis = (CategoryInputSynapse) getSynapse();
5153
s.setWeight(cis.getInitialCategorySynapseWeight());
5254

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,4 @@ public BindingCategoryInputLink(BindingCategoryInputSynapse s, CategoryActivatio
3434
super(s, input, output);
3535
}
3636

37-
@Override
38-
public CategorySynapse createCategorySynapse() {
39-
return new BindingCategorySynapse();
40-
}
41-
4237
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,4 @@ public InhibitoryCategoryInputLink(InhibitoryCategoryInputSynapse s, CategoryAct
4040
crossConnectFields((InhibitoryActivation) act, output)
4141
);
4242
}
43-
44-
@Override
45-
public CategorySynapse createCategorySynapse() {
46-
return new InhibitoryCategorySynapse();
47-
}
48-
4943
}

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
@@ -33,11 +33,6 @@ public PatternCategoryInputLink(PatternCategoryInputSynapse s, CategoryActivatio
3333
super(s, input, output);
3434
}
3535

36-
@Override
37-
public CategorySynapse createCategorySynapse() {
38-
return new PatternCategorySynapse();
39-
}
40-
4136
@Override
4237
protected void connectGradientFields() {
4338
}

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ protected void initFromTemplate(Neuron templateN) {
247247

248248
createCategorySynapse()
249249
.setWeight(cis.getInitialCategorySynapseWeight())
250+
.setInputSideInstantiable(false)
251+
.setOutputSideInstantiable(false)
250252
.link(this, cis.getInput());
251253
}
252254

@@ -267,17 +269,6 @@ public N setInstantiable(boolean instantiable) {
267269
return (N) this;
268270
}
269271

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-
);
277-
278-
return (N) this;
279-
}
280-
281272
public boolean isInstantiable() {
282273
return instantiable;
283274
}

core/src/main/java/network/aika/elements/neurons/types/BindingNeuron.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ public BindingCategoryInputSynapse makeAbstract(boolean instantiable) {
9898
.setLabel(getLabel() + CATEGORY_LABEL);
9999

100100
BindingCategoryInputSynapse s = new BindingCategoryInputSynapse()
101-
.link(bindingCategory, this)
102-
.setInstantiable(instantiable, instantiable);
101+
.link(bindingCategory, this);
103102

104103
s.setInitialCategorySynapseWeight(1.0);
105104

core/src/main/java/network/aika/elements/neurons/types/InhibitoryNeuron.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ public InhibitoryCategoryInputSynapse makeAbstract(boolean instantiable) {
6363
.setLabel(getLabel() + CATEGORY_LABEL);
6464

6565
InhibitoryCategoryInputSynapse s = new InhibitoryCategoryInputSynapse()
66-
.link(inhibCategory, this)
67-
.setInstantiable(instantiable, instantiable);
66+
.link(inhibCategory, this);
6867

6968
s.setInitialCategorySynapseWeight(1.0);
7069

core/src/main/java/network/aika/elements/neurons/types/PatternNeuron.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ public PatternCategoryInputSynapse makeAbstract(boolean instantiable) {
8686
.setLabel(getCategoryLabel(getLabel()));
8787

8888
PatternCategoryInputSynapse s = new PatternCategoryInputSynapse()
89-
.link(patternCategory, this)
90-
.setInstantiable(instantiable, instantiable);
89+
.link(patternCategory, this);
9190

9291
s.setInitialCategorySynapseWeight(1.0);
9392

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public Field getSynapseBias() {
140140
return synapseBias;
141141
}
142142

143+
@Override
143144
public boolean isOptional() {
144145
return optional;
145146
}

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

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public abstract class Synapse<S extends Synapse, I extends Neuron, O extends Neu
7777

7878
private Relation relation;
7979

80-
private boolean inputSideInstantiable;
81-
private boolean outputSideInstantiable;
80+
private Boolean inputSideInstantiable;
81+
private Boolean outputSideInstantiable;
8282

8383
protected SumField weight = (SumField) new SumField(this, "weight", TOLERANCE)
8484
.setQueued(getQueue(), TRAINING, false)
@@ -193,6 +193,10 @@ public boolean isWeak() {
193193
return false;
194194
}
195195

196+
public boolean isOptional() {
197+
return false;
198+
}
199+
196200
public static Link getLatentLink(Synapse synA, Synapse synB, Activation iActA, Activation iActB) {
197201
Stream<Link> linksA = iActA.getOutputLinks(synA);
198202
return linksA.filter(l -> synB.getLink(iActB, l.getOutput()) != null)
@@ -271,6 +275,9 @@ public void initFromTemplate(I input, O output, Synapse templateSyn) {
271275
setInput(input);
272276
setOutput(output);
273277

278+
inputSideInstantiable = templateSyn.inputSideInstantiable;
279+
outputSideInstantiable = templateSyn.outputSideInstantiable;
280+
274281
link();
275282

276283
if(templateSyn.relation != null)
@@ -281,19 +288,31 @@ public void initFromTemplate(I input, O output, Synapse templateSyn) {
281288
);
282289
}
283290

284-
public S setInstantiable(boolean inputSideInstantiable, boolean outputSideInstantiable) {
285-
this.inputSideInstantiable = inputSideInstantiable;
286-
this.outputSideInstantiable = outputSideInstantiable;
291+
public S setInputSideInstantiable(Boolean instantiable) {
292+
this.inputSideInstantiable = instantiable;
287293

288294
return (S) this;
289295
}
290296

291-
public boolean isInputSideInstantiable() {
292-
return inputSideInstantiable;
297+
public Boolean isInputSideInstantiable() {
298+
return inputSideInstantiable != null ?
299+
inputSideInstantiable :
300+
input.getNeuron().isInstantiable() &&
301+
(output.getNeuron().isInstantiable() || isOptional());
302+
}
303+
304+
public S setOutputSideInstantiable(Boolean instantiable) {
305+
this.outputSideInstantiable = instantiable;
306+
307+
return (S) this;
293308
}
294309

295-
public boolean isOutputSideInstantiable() {
296-
return outputSideInstantiable;
310+
public Boolean isOutputSideInstantiable() {
311+
boolean cis = this instanceof CategoryInputSynapse;
312+
313+
return outputSideInstantiable != null ?
314+
outputSideInstantiable :
315+
output.getNeuron().isInstantiable() && (!cis || input.getNeuron().isInstantiable());
297316
}
298317

299318
public S link(Neuron input, Neuron output) {
@@ -447,8 +466,15 @@ public void write(DataOutput out) throws IOException {
447466
out.writeLong(output.getId());
448467

449468
weight.write(out);
450-
out.writeBoolean(inputSideInstantiable);
451-
out.writeBoolean(outputSideInstantiable);
469+
470+
out.writeBoolean(inputSideInstantiable != null);
471+
if(inputSideInstantiable != null)
472+
out.writeBoolean(inputSideInstantiable);
473+
474+
out.writeBoolean(outputSideInstantiable != null);
475+
476+
if(outputSideInstantiable != null)
477+
out.writeBoolean(outputSideInstantiable);
452478

453479
out.writeBoolean(relation != null);
454480
if(relation != null)
@@ -470,8 +496,12 @@ public void readFields(DataInput in, Model m) throws IOException {
470496
output = m.lookupNeuronProvider(in.readLong(), SYNAPSE_OUT);
471497

472498
weight.readFields(in);
473-
inputSideInstantiable = in.readBoolean();
474-
outputSideInstantiable = in.readBoolean();
499+
500+
if(in.readBoolean())
501+
inputSideInstantiable = in.readBoolean();
502+
503+
if(in.readBoolean())
504+
outputSideInstantiable = in.readBoolean();
475505

476506
if(in.readBoolean())
477507
relation = Relation.read(in, m);

0 commit comments

Comments
 (0)