Skip to content

Commit aff1808

Browse files
weight, bias, limit -> private
1 parent a119c7f commit aff1808

File tree

7 files changed

+66
-62
lines changed

7 files changed

+66
-62
lines changed

src/main/java/network/aika/Document.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ public void notifyWeightModified(Synapse synapse) {
460460
*/
461461
public void commit() {
462462
modifiedWeights.forEach((n, inputSyns) -> {
463-
n.commit(this, inputSyns);
463+
n.commit(inputSyns);
464464
Converter.convert(threadId, this, n, inputSyns);
465465
});
466466
modifiedWeights.clear();

src/main/java/network/aika/lattice/Converter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class Converter {
3939
s1.linksAnyOutput() || s1.isIdentity()
4040
);
4141
if (r != 0) return r;
42-
r = Double.compare(s2.weight, s1.weight);
42+
r = Double.compare(s2.getWeight(), s1.getWeight());
4343
if (r != 0) return r;
4444
return Integer.compare(s1.getId(), s2.getId());
4545
};

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -405,14 +405,10 @@ public Activation addInput(Document doc, Activation.Builder input) {
405405
}
406406

407407

408-
public void commit(Document doc, Collection<Synapse> modifiedSynapses) {
408+
public void commit(Collection<Synapse> modifiedSynapses) {
409409
synapseSummary.updateNeuronBias(biasDelta);
410410

411411
for (Synapse s : modifiedSynapses) {
412-
if(s.toBeDeleted) {
413-
s.update(doc, -s.weight, 0.0, s.limit);
414-
}
415-
416412
INeuron in = s.getInput().get();
417413
in.lock.acquireWriteLock();
418414
try {
@@ -423,7 +419,7 @@ public void commit(Document doc, Collection<Synapse> modifiedSynapses) {
423419
in.lock.releaseWriteLock();
424420
}
425421

426-
if(s.toBeDeleted) {
422+
if(s.isZero()) {
427423
s.unlink();
428424
}
429425
}
@@ -728,7 +724,7 @@ public String toString() {
728724

729725
public String toStringWithSynapses() {
730726
SortedSet<Synapse> is = new TreeSet<>((s1, s2) -> {
731-
int r = Double.compare(s2.weight, s1.weight);
727+
int r = Double.compare(s2.getWeight(), s1.getWeight());
732728
if (r != 0) return r;
733729
return Integer.compare(s1.getInput().id, s2.getInput().id);
734730
});
@@ -742,7 +738,7 @@ public String toStringWithSynapses() {
742738
sb.append(Utils.round(bias));
743739
for (Synapse s : is) {
744740
sb.append(", ");
745-
sb.append(Utils.round(s.weight));
741+
sb.append(Utils.round(s.getWeight()));
746742
sb.append(":");
747743
sb.append(s.getInput().toString());
748744
}
@@ -796,15 +792,15 @@ public void updateNeuronBias(double biasDelta) {
796792

797793
public void updateSynapse(Synapse s) {
798794
if (!s.isInactive()) {
799-
biasSum -= s.bias;
795+
biasSum -= s.getBias();
800796
biasSum += s.getNewBias();
801797

802-
updateSum(s.isRecurrent(), s.isNegative(), -(s.limit * s.weight));
803-
updateSum(s.isRecurrent(), s.getNewWeight() <= 0.0, (s.limit + s.limitDelta) * s.getNewWeight());
798+
updateSum(s.isRecurrent(), s.isNegative(), -(s.getLimit() * s.getWeight()));
799+
updateSum(s.isRecurrent(), s.getNewWeight() <= 0.0, s.getNewLimit() * s.getNewWeight());
804800

805801
if(s.getInput().get().isPassiveInputNeuron() && !s.isNegative()) {
806-
posPassiveSum -= !s.isNegative() ? (s.limit * s.weight) : 0.0;
807-
posPassiveSum += s.getNewWeight() > 0.0 ? ((s.limit + s.limitDelta) * s.getNewWeight()) : 0.0;
802+
posPassiveSum -= !s.isNegative() ? (s.getLimit() * s.getWeight()) : 0.0;
803+
posPassiveSum += s.getNewWeight() > 0.0 ? (s.getNewLimit() * s.getNewWeight()) : 0.0;
808804
}
809805

810806
if(!s.isRecurrent()) {

src/main/java/network/aika/neuron/Neuron.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ private boolean init(Document doc, Double bias, ActivationFunction activationFun
241241

242242
relationBuilders.forEach(input -> input.connect(this));
243243

244-
n.commit(doc, modifiedSynapses);
244+
n.commit(modifiedSynapses);
245245

246246
return Converter.convert(model.defaultThreadId, doc, n, modifiedSynapses);
247247
}

src/main/java/network/aika/neuron/Synapse.java

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public class Synapse implements Writable {
6161
public static double DISJUNCTION_THRESHOLD = 0.6;
6262
public static double CONJUNCTION_THRESHOLD = 0.4;
6363

64+
public static double TOLERANCE = 0.0000001;
65+
6466

6567
public static final Comparator<Synapse> INPUT_SYNAPSE_COMP = (s1, s2) -> {
6668
int r = s1.input.compareTo(s2.input);
@@ -92,27 +94,14 @@ public class Synapse implements Writable {
9294

9395
private boolean inactive;
9496

95-
/**
96-
* The weight of this synapse.
97-
*/
98-
public double weight;
99-
100-
/**
101-
* The weight delta of this synapse. The converter will use it to compute few internal
102-
* parameters and then createOrReplace the weight variable.
103-
*/
104-
public double weightDelta;
97+
private double weight;
98+
private double weightDelta;
10599

100+
private double bias;
101+
private double biasDelta;
106102

107-
public double bias;
108-
109-
public double biasDelta;
110-
111-
public double limit;
112-
113-
public double limitDelta;
114-
115-
public boolean toBeDeleted;
103+
private double limit;
104+
private double limitDelta;
116105

117106
private boolean isConjunction;
118107
private boolean isDisjunction;
@@ -201,6 +190,32 @@ public boolean isDisjunction() {
201190
return isDisjunction;
202191
}
203192

193+
194+
public double getWeight() {
195+
return weight;
196+
}
197+
198+
public double getBias() {
199+
return bias;
200+
}
201+
202+
public double getLimit() {
203+
return limit;
204+
}
205+
206+
public double getNewWeight() {
207+
return weight + weightDelta;
208+
}
209+
210+
public double getNewBias() {
211+
return bias + biasDelta;
212+
}
213+
214+
public double getNewLimit() {
215+
return limit + limitDelta;
216+
}
217+
218+
204219
public void link() {
205220
INeuron in = input.get();
206221
INeuron out = output.get();
@@ -354,6 +369,11 @@ public void commit() {
354369
}
355370

356371

372+
public boolean isZero() {
373+
return Math.abs(weight) < TOLERANCE && Math.abs(bias) < TOLERANCE ;
374+
}
375+
376+
357377
public enum State {
358378
NEW,
359379
OLD
@@ -520,17 +540,6 @@ public static Synapse createOrReplace(Document doc, Integer synapseId, Neuron in
520540
return synapse;
521541
}
522542

523-
524-
public double getNewWeight() {
525-
return weight + weightDelta;
526-
}
527-
528-
529-
public double getNewBias() {
530-
return bias + biasDelta;
531-
}
532-
533-
534543
public Set<Integer> linksOutput() {
535544
Set<Integer> results = new TreeSet<>();
536545
for(Map.Entry<Integer, Relation> me: relations.entrySet()) {

src/main/java/network/aika/neuron/activation/Activation.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public State computeValueAndWeight(int round) {
354354

355355
if (iAct == this) continue;
356356

357-
double x = Math.min(s.limit, is.s.value) * s.weight;
357+
double x = Math.min(s.getLimit(), is.s.value) * s.getWeight();
358358
if(s.getDistanceFunction() != null) {
359359
x *= s.getDistanceFunction().f(iAct, this);
360360
}
@@ -370,7 +370,7 @@ public State computeValueAndWeight(int round) {
370370

371371
if(n.passiveInputSynapses != null) {
372372
for(Synapse s: n.passiveInputSynapses.values()) {
373-
double x = s.weight * s.getInput().get(doc).passiveInputFunction.getActivationValue(s, this);
373+
double x = s.getWeight() * s.getInput().get(doc).passiveInputFunction.getActivationValue(s, this);
374374

375375
net += x;
376376
if(!s.isNegative()) {
@@ -427,10 +427,10 @@ public boolean isActiveable() {
427427

428428
double iv = 0.0;
429429
if(!l.isNegative() && l.input.decision != EXCLUDED) {
430-
iv = Math.min(l.synapse.limit, l.input.upperBound);
430+
iv = Math.min(l.synapse.getLimit(), l.input.upperBound);
431431
}
432432

433-
double x = iv * s.weight;
433+
double x = iv * s.getWeight();
434434
if(s.getDistanceFunction() != null) {
435435
x *= s.getDistanceFunction().f(iAct, this);
436436
}
@@ -439,7 +439,7 @@ public boolean isActiveable() {
439439

440440
if(n.passiveInputSynapses != null) {
441441
for(Synapse s: n.passiveInputSynapses.values()) {
442-
double x = s.weight * s.getInput().get(doc).passiveInputFunction.getActivationValue(s, this);
442+
double x = s.getWeight() * s.getInput().get(doc).passiveInputFunction.getActivationValue(s, this);
443443

444444
net += x;
445445
}
@@ -486,26 +486,26 @@ public void computeBounds() {
486486

487487
if (iAct == this) continue;
488488

489-
double x = s.weight;
489+
double x = s.getWeight();
490490
if(s.getDistanceFunction() != null) {
491491
x *= s.getDistanceFunction().f(iAct, this);
492492
}
493493

494494
if (s.isNegative()) {
495495
if (!s.isRecurrent() && !iAct.checkSelfReferencing(false, 0, v)) {
496-
ub += Math.min(s.limit, iAct.lowerBound) * x;
496+
ub += Math.min(s.getLimit(), iAct.lowerBound) * x;
497497
}
498498

499-
lb += s.limit * x;
499+
lb += s.getLimit() * x;
500500
} else {
501-
ub += Math.min(s.limit, iAct.upperBound) * x;
502-
lb += Math.min(s.limit, iAct.lowerBound) * x;
501+
ub += Math.min(s.getLimit(), iAct.upperBound) * x;
502+
lb += Math.min(s.getLimit(), iAct.lowerBound) * x;
503503
}
504504
}
505505

506506
if(n.passiveInputSynapses != null) {
507507
for(Synapse s: n.passiveInputSynapses.values()) {
508-
double x = s.weight * s.getInput().get(doc).passiveInputFunction.getActivationValue(s, this);
508+
double x = s.getWeight() * s.getInput().get(doc).passiveInputFunction.getActivationValue(s, this);
509509

510510
ub += x;
511511
lb += x;
@@ -1036,7 +1036,7 @@ public String identityToString() {
10361036
public String linksToString() {
10371037
StringBuilder sb = new StringBuilder();
10381038
for(Link l: inputLinks.values()) {
1039-
sb.append(" " + l.input.getLabel() + " W:" + l.synapse.weight + "\n");
1039+
sb.append(" " + l.input.getLabel() + " W:" + l.synapse.getWeight() + "\n");
10401040
}
10411041

10421042
return sb.toString();

src/test/java/network/aika/lattice/ConverterTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void testConverter() {
109109

110110
out.get().setBias(1.5);
111111

112-
out.get().commit(null, out.get().inputSynapses.values());
112+
out.get().commit(out.get().inputSynapses.values());
113113
Converter.convert(0, null, out.get(), out.get().inputSynapses.values());
114114

115115
System.out.println(out.get().node.get().logicToString());
@@ -275,13 +275,12 @@ public void testConverter2() {
275275
Assert.assertEquals(2, out.get().node.get().andParents.size());
276276

277277

278-
inD.inMemoryOutputSynapses.firstEntry().getValue().weightDelta = -1.5f;
278+
inD.inMemoryOutputSynapses.firstEntry().getValue().updateDelta(null, -1.5, 0, 0);
279279

280-
out.get().commit(null, out.get().inputSynapses.values());
280+
out.get().commit(out.get().inputSynapses.values());
281281
Converter.convert( 0, null, out.get(), out.get().inputSynapses.values());
282282
System.out.println(out.get().node.get().logicToString());
283283
Assert.assertEquals(1, out.get().node.get().andParents.size());
284-
285284
}
286285

287286

0 commit comments

Comments
 (0)