Skip to content

Commit 7b5a470

Browse files
Added test cases
1 parent 6e1797d commit 7b5a470

File tree

8 files changed

+560
-0
lines changed

8 files changed

+560
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package network.aika.activations;
2+
3+
import network.aika.Document;
4+
import network.aika.Model;
5+
import network.aika.type.TypeRegistry;
6+
import network.aika.neurons.Neuron;
7+
import network.aika.typedefs.NodeDefinition;
8+
import org.junit.jupiter.api.BeforeEach;
9+
10+
import static network.aika.neurons.RefType.NEURON_EXTERNAL;
11+
import static org.mockito.Mockito.mock;
12+
13+
public abstract class AbstractActivationTest {
14+
15+
TypeRegistry typeRegistry;
16+
NodeDefinition nodeDef;
17+
Model model;
18+
Neuron neuron;
19+
Document doc;
20+
21+
@BeforeEach
22+
public void init() {
23+
typeRegistry = mock(TypeRegistry.class);
24+
nodeDef = new NodeDefinition(typeRegistry, "test")
25+
.setClazz(ConjunctiveActivation.class, Neuron.class);
26+
27+
doc = mock(Document.class);
28+
model = mock(Model.class);
29+
30+
neuron = nodeDef.neuron.instantiate(model);
31+
}
32+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package network.aika.activations;
2+
3+
4+
import network.aika.Document;
5+
import network.aika.bindingsignal.BindingSignal;
6+
import network.aika.neurons.Neuron;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.api.extension.ExtendWith;
9+
import org.mockito.junit.jupiter.MockitoExtension;
10+
11+
import java.util.Map;
12+
13+
import static network.aika.activations.TestBSTypes.A;
14+
import static network.aika.activations.TestBSTypes.B;
15+
import static org.junit.jupiter.api.Assertions.*;
16+
import static org.mockito.Mockito.mock;
17+
18+
19+
@ExtendWith(MockitoExtension.class)
20+
public class ActivationTest extends AbstractActivationTest {
21+
22+
23+
@Test
24+
public void testHasConflictingBindingSignals() {
25+
Document doc = mock(Document.class);
26+
Neuron n = mock(Neuron.class);
27+
28+
BindingSignal bs0 = new BindingSignal(0, doc);
29+
BindingSignal bs1 = new BindingSignal(1, doc);
30+
31+
Activation act = new ConjunctiveActivation(
32+
null,
33+
null,
34+
1,
35+
n,
36+
doc,
37+
Map.of(A, bs0)
38+
);
39+
40+
assertFalse(
41+
act.hasConflictingBindingSignals(
42+
Map.of(A, bs0)
43+
)
44+
);
45+
46+
assertFalse(
47+
act.hasConflictingBindingSignals(
48+
Map.of(
49+
A, bs0,
50+
B, bs1
51+
)
52+
)
53+
);
54+
55+
assertTrue(
56+
act.hasConflictingBindingSignals(
57+
Map.of(
58+
A, bs1,
59+
B, bs0
60+
)
61+
)
62+
);
63+
}
64+
65+
@Test
66+
public void testHasNewBindingSignals() {
67+
Document doc = mock(Document.class);
68+
Neuron n = mock(Neuron.class);
69+
70+
BindingSignal bs0 = new BindingSignal(0, doc);
71+
BindingSignal bs1 = new BindingSignal(1, doc);
72+
73+
Activation act = new ConjunctiveActivation(
74+
null,
75+
null,
76+
1,
77+
n,
78+
doc,
79+
Map.of(A, bs0)
80+
);
81+
82+
assertTrue(
83+
act.hasNewBindingSignals(
84+
Map.of(
85+
A, bs0,
86+
B, bs1
87+
)
88+
)
89+
);
90+
91+
assertFalse(
92+
act.hasNewBindingSignals(
93+
Map.of(A, bs0)
94+
)
95+
);
96+
}
97+
98+
@Test
99+
public void testBranch() {
100+
BindingSignal bs0 = new BindingSignal(0, doc);
101+
BindingSignal bs1 = new BindingSignal(1, doc);
102+
103+
Activation parentAct = new ConjunctiveActivation(
104+
null,
105+
null,
106+
1,
107+
neuron,
108+
doc,
109+
Map.of(A, bs0)
110+
);
111+
112+
Activation childAct = parentAct.branch(
113+
Map.of(B, bs1)
114+
);
115+
116+
assertEquals(parentAct, childAct.getParent());
117+
assertEquals(1, childAct.getBindingSignals().size());
118+
assertEquals(bs1, childAct.getBindingSignal(B));
119+
}
120+
121+
@Test
122+
public void testCollectLinkingTargets() {
123+
124+
}
125+
126+
@Test
127+
public void testLinkOutgoing() {
128+
129+
}
130+
131+
@Test
132+
public void testPropagate() {
133+
134+
}
135+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package network.aika.activations;
2+
3+
4+
import network.aika.bindingsignal.BindingSignal;
5+
import network.aika.neurons.ConjunctiveSynapse;
6+
import network.aika.neurons.Neuron;
7+
import network.aika.typedefs.EdgeDefinition;
8+
import network.aika.typedefs.NodeDefinition;
9+
import network.aika.typedefs.SynapseDefinition;
10+
import org.junit.jupiter.api.Assertions;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
import org.junit.jupiter.api.extension.ExtendWith;
14+
import org.mockito.junit.jupiter.MockitoExtension;
15+
16+
import java.util.Map;
17+
18+
import static network.aika.activations.TestBSTypes.A;
19+
import static network.aika.activations.TestBSTypes.B;
20+
import static network.aika.bindingsignal.Transition.of;
21+
import static network.aika.neurons.RefType.NEURON_EXTERNAL;
22+
23+
24+
@ExtendWith(MockitoExtension.class)
25+
public class ConjunctiveActivationTest extends AbstractActivationTest {
26+
27+
Neuron inputNeuron;
28+
29+
ConjunctiveSynapse synapse;
30+
31+
@BeforeEach
32+
@Override
33+
public void init() {
34+
super.init();
35+
36+
NodeDefinition inputNodeDef = new NodeDefinition(typeRegistry, "input")
37+
.setClazz(ConjunctiveActivation.class, Neuron.class);
38+
39+
EdgeDefinition firstInputEdgeDef = new EdgeDefinition(typeRegistry, "test")
40+
.setClazz(Link.class, ConjunctiveSynapse.class)
41+
.setInput(inputNodeDef)
42+
.setOutput(nodeDef);
43+
44+
SynapseDefinition synapseDefinition = firstInputEdgeDef.synapse
45+
.setTransition(of(A, B));
46+
47+
inputNeuron = inputNodeDef.neuron.instantiate(model);
48+
synapse = (ConjunctiveSynapse) synapseDefinition.instantiate(inputNeuron, neuron);
49+
}
50+
51+
@Test
52+
public void testLinkIncoming() {
53+
BindingSignal bs0 = new BindingSignal(0, doc);
54+
55+
Activation iAct = inputNeuron.createActivation(null, doc, Map.of(A, bs0));
56+
Activation oAct = neuron.createActivation(null, doc, Map.of(B, bs0));
57+
58+
Assertions.assertNull(oAct.getInputLink(iAct, 0));
59+
60+
bs0.addActivation(iAct);
61+
62+
Assertions.assertNull(oAct.getInputLink(iAct, 0));
63+
64+
oAct.linkIncoming(null);
65+
66+
Assertions.assertNotNull(oAct.getInputLink(iAct, 0));
67+
}
68+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package network.aika.activations;
18+
19+
import network.aika.Config;
20+
import network.aika.Document;
21+
import network.aika.Model;
22+
import network.aika.activations.model.TestTypeModel;
23+
import network.aika.fields.defs.FieldDefinition;
24+
import network.aika.neurons.Neuron;
25+
import network.aika.neurons.Synapse;
26+
import network.aika.typedefs.ActivationDefinition;
27+
import network.aika.typedefs.NeuronDefinition;
28+
import network.aika.typedefs.SynapseDefinition;
29+
import org.junit.jupiter.api.Assertions;
30+
import org.junit.jupiter.api.Test;
31+
32+
import static network.aika.activations.TestBSTypes.A;
33+
import static network.aika.neurons.RefType.NEURON_EXTERNAL;
34+
35+
/**
36+
*
37+
* @author Lukas Molzberger
38+
*/
39+
public class MinimalNetworkTest {
40+
41+
42+
@Test
43+
public void minNetworkTest() {
44+
TestTypeModel typeModel = new TestTypeModel();
45+
FieldDefinition<NeuronDefinition, Neuron> bias = typeModel.getNeuron().getBias();
46+
FieldDefinition<SynapseDefinition, Synapse> weight = typeModel.getNeuron().getWeight();
47+
FieldDefinition<ActivationDefinition, Activation> net = typeModel.getNeuron().getNet();
48+
49+
Model m = new Model(typeModel)
50+
.setConfig(new Config());
51+
52+
System.out.println("Begin test\n");
53+
54+
Neuron inputNeuron = typeModel
55+
.getNeuron()
56+
.getNeuron()
57+
.instantiate(m);
58+
59+
Neuron outputNeuron = typeModel
60+
.getNeuron()
61+
.getNeuron()
62+
.instantiate(m)
63+
.setFieldValue(bias, 1.0);
64+
65+
Synapse synapse = typeModel
66+
.getNeuron()
67+
.getSynapse()
68+
.instantiate(inputNeuron, outputNeuron)
69+
.setFieldValue(weight, 10.0)
70+
.setPropagable(m, true);
71+
72+
73+
Document doc = new Document(m, 4);
74+
Activation iAct = doc.addToken(inputNeuron, A, 0)
75+
.setFieldValue(net, 5.0);
76+
77+
Assertions.assertEquals(5.0, iAct.getFieldValue(net));
78+
79+
doc.process();
80+
81+
System.out.println("Dump results:");
82+
83+
Activation oAct = doc.getActivationByNeuron(outputNeuron);
84+
85+
Assertions.assertEquals(10.0, oAct.getFieldValue(net));
86+
87+
doc.disconnect();
88+
}
89+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package network.aika.activations;
2+
3+
import network.aika.bindingsignal.BSType;
4+
5+
public enum TestBSTypes implements BSType {
6+
A,
7+
B;
8+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package network.aika.activations.model;
18+
19+
import network.aika.fields.ActivationFunction;
20+
21+
/**
22+
*
23+
* @author Lukas Molzberger
24+
*/
25+
public enum TestActivationFunction implements ActivationFunction {
26+
27+
LIMITED_RECTIFIED_LINEAR_UNIT(
28+
x -> Math.max(0.0, Math.min(1.0, x)),
29+
x -> x >= 0.0 && x <= 1.0 ? 1.0 : 0.0
30+
);
31+
32+
private final Function f;
33+
private final Function outerGrad;
34+
35+
TestActivationFunction(Function f, Function outerGrad) {
36+
this.f = f;
37+
this.outerGrad = outerGrad;
38+
}
39+
40+
public double f(double x) {
41+
return f.f(x);
42+
}
43+
44+
public double outerGrad(double x) {
45+
return outerGrad.f(x);
46+
}
47+
48+
interface Function {
49+
double f(double x);
50+
}
51+
}

0 commit comments

Comments
 (0)