-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e1797d
commit 7b5a470
Showing
8 changed files
with
560 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
core/src/test/java/network/aika/activations/AbstractActivationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package network.aika.activations; | ||
|
||
import network.aika.Document; | ||
import network.aika.Model; | ||
import network.aika.type.TypeRegistry; | ||
import network.aika.neurons.Neuron; | ||
import network.aika.typedefs.NodeDefinition; | ||
import org.junit.jupiter.api.BeforeEach; | ||
|
||
import static network.aika.neurons.RefType.NEURON_EXTERNAL; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public abstract class AbstractActivationTest { | ||
|
||
TypeRegistry typeRegistry; | ||
NodeDefinition nodeDef; | ||
Model model; | ||
Neuron neuron; | ||
Document doc; | ||
|
||
@BeforeEach | ||
public void init() { | ||
typeRegistry = mock(TypeRegistry.class); | ||
nodeDef = new NodeDefinition(typeRegistry, "test") | ||
.setClazz(ConjunctiveActivation.class, Neuron.class); | ||
|
||
doc = mock(Document.class); | ||
model = mock(Model.class); | ||
|
||
neuron = nodeDef.neuron.instantiate(model); | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
core/src/test/java/network/aika/activations/ActivationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package network.aika.activations; | ||
|
||
|
||
import network.aika.Document; | ||
import network.aika.bindingsignal.BindingSignal; | ||
import network.aika.neurons.Neuron; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.Map; | ||
|
||
import static network.aika.activations.TestBSTypes.A; | ||
import static network.aika.activations.TestBSTypes.B; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.mock; | ||
|
||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class ActivationTest extends AbstractActivationTest { | ||
|
||
|
||
@Test | ||
public void testHasConflictingBindingSignals() { | ||
Document doc = mock(Document.class); | ||
Neuron n = mock(Neuron.class); | ||
|
||
BindingSignal bs0 = new BindingSignal(0, doc); | ||
BindingSignal bs1 = new BindingSignal(1, doc); | ||
|
||
Activation act = new ConjunctiveActivation( | ||
null, | ||
null, | ||
1, | ||
n, | ||
doc, | ||
Map.of(A, bs0) | ||
); | ||
|
||
assertFalse( | ||
act.hasConflictingBindingSignals( | ||
Map.of(A, bs0) | ||
) | ||
); | ||
|
||
assertFalse( | ||
act.hasConflictingBindingSignals( | ||
Map.of( | ||
A, bs0, | ||
B, bs1 | ||
) | ||
) | ||
); | ||
|
||
assertTrue( | ||
act.hasConflictingBindingSignals( | ||
Map.of( | ||
A, bs1, | ||
B, bs0 | ||
) | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
public void testHasNewBindingSignals() { | ||
Document doc = mock(Document.class); | ||
Neuron n = mock(Neuron.class); | ||
|
||
BindingSignal bs0 = new BindingSignal(0, doc); | ||
BindingSignal bs1 = new BindingSignal(1, doc); | ||
|
||
Activation act = new ConjunctiveActivation( | ||
null, | ||
null, | ||
1, | ||
n, | ||
doc, | ||
Map.of(A, bs0) | ||
); | ||
|
||
assertTrue( | ||
act.hasNewBindingSignals( | ||
Map.of( | ||
A, bs0, | ||
B, bs1 | ||
) | ||
) | ||
); | ||
|
||
assertFalse( | ||
act.hasNewBindingSignals( | ||
Map.of(A, bs0) | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
public void testBranch() { | ||
BindingSignal bs0 = new BindingSignal(0, doc); | ||
BindingSignal bs1 = new BindingSignal(1, doc); | ||
|
||
Activation parentAct = new ConjunctiveActivation( | ||
null, | ||
null, | ||
1, | ||
neuron, | ||
doc, | ||
Map.of(A, bs0) | ||
); | ||
|
||
Activation childAct = parentAct.branch( | ||
Map.of(B, bs1) | ||
); | ||
|
||
assertEquals(parentAct, childAct.getParent()); | ||
assertEquals(1, childAct.getBindingSignals().size()); | ||
assertEquals(bs1, childAct.getBindingSignal(B)); | ||
} | ||
|
||
@Test | ||
public void testCollectLinkingTargets() { | ||
|
||
} | ||
|
||
@Test | ||
public void testLinkOutgoing() { | ||
|
||
} | ||
|
||
@Test | ||
public void testPropagate() { | ||
|
||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
core/src/test/java/network/aika/activations/ConjunctiveActivationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package network.aika.activations; | ||
|
||
|
||
import network.aika.bindingsignal.BindingSignal; | ||
import network.aika.neurons.ConjunctiveSynapse; | ||
import network.aika.neurons.Neuron; | ||
import network.aika.typedefs.EdgeDefinition; | ||
import network.aika.typedefs.NodeDefinition; | ||
import network.aika.typedefs.SynapseDefinition; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.Map; | ||
|
||
import static network.aika.activations.TestBSTypes.A; | ||
import static network.aika.activations.TestBSTypes.B; | ||
import static network.aika.bindingsignal.Transition.of; | ||
import static network.aika.neurons.RefType.NEURON_EXTERNAL; | ||
|
||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class ConjunctiveActivationTest extends AbstractActivationTest { | ||
|
||
Neuron inputNeuron; | ||
|
||
ConjunctiveSynapse synapse; | ||
|
||
@BeforeEach | ||
@Override | ||
public void init() { | ||
super.init(); | ||
|
||
NodeDefinition inputNodeDef = new NodeDefinition(typeRegistry, "input") | ||
.setClazz(ConjunctiveActivation.class, Neuron.class); | ||
|
||
EdgeDefinition firstInputEdgeDef = new EdgeDefinition(typeRegistry, "test") | ||
.setClazz(Link.class, ConjunctiveSynapse.class) | ||
.setInput(inputNodeDef) | ||
.setOutput(nodeDef); | ||
|
||
SynapseDefinition synapseDefinition = firstInputEdgeDef.synapse | ||
.setTransition(of(A, B)); | ||
|
||
inputNeuron = inputNodeDef.neuron.instantiate(model); | ||
synapse = (ConjunctiveSynapse) synapseDefinition.instantiate(inputNeuron, neuron); | ||
} | ||
|
||
@Test | ||
public void testLinkIncoming() { | ||
BindingSignal bs0 = new BindingSignal(0, doc); | ||
|
||
Activation iAct = inputNeuron.createActivation(null, doc, Map.of(A, bs0)); | ||
Activation oAct = neuron.createActivation(null, doc, Map.of(B, bs0)); | ||
|
||
Assertions.assertNull(oAct.getInputLink(iAct, 0)); | ||
|
||
bs0.addActivation(iAct); | ||
|
||
Assertions.assertNull(oAct.getInputLink(iAct, 0)); | ||
|
||
oAct.linkIncoming(null); | ||
|
||
Assertions.assertNotNull(oAct.getInputLink(iAct, 0)); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
core/src/test/java/network/aika/activations/MinimalNetworkTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package network.aika.activations; | ||
|
||
import network.aika.Config; | ||
import network.aika.Document; | ||
import network.aika.Model; | ||
import network.aika.activations.model.TestTypeModel; | ||
import network.aika.fields.defs.FieldDefinition; | ||
import network.aika.neurons.Neuron; | ||
import network.aika.neurons.Synapse; | ||
import network.aika.typedefs.ActivationDefinition; | ||
import network.aika.typedefs.NeuronDefinition; | ||
import network.aika.typedefs.SynapseDefinition; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static network.aika.activations.TestBSTypes.A; | ||
import static network.aika.neurons.RefType.NEURON_EXTERNAL; | ||
|
||
/** | ||
* | ||
* @author Lukas Molzberger | ||
*/ | ||
public class MinimalNetworkTest { | ||
|
||
|
||
@Test | ||
public void minNetworkTest() { | ||
TestTypeModel typeModel = new TestTypeModel(); | ||
FieldDefinition<NeuronDefinition, Neuron> bias = typeModel.getNeuron().getBias(); | ||
FieldDefinition<SynapseDefinition, Synapse> weight = typeModel.getNeuron().getWeight(); | ||
FieldDefinition<ActivationDefinition, Activation> net = typeModel.getNeuron().getNet(); | ||
|
||
Model m = new Model(typeModel) | ||
.setConfig(new Config()); | ||
|
||
System.out.println("Begin test\n"); | ||
|
||
Neuron inputNeuron = typeModel | ||
.getNeuron() | ||
.getNeuron() | ||
.instantiate(m); | ||
|
||
Neuron outputNeuron = typeModel | ||
.getNeuron() | ||
.getNeuron() | ||
.instantiate(m) | ||
.setFieldValue(bias, 1.0); | ||
|
||
Synapse synapse = typeModel | ||
.getNeuron() | ||
.getSynapse() | ||
.instantiate(inputNeuron, outputNeuron) | ||
.setFieldValue(weight, 10.0) | ||
.setPropagable(m, true); | ||
|
||
|
||
Document doc = new Document(m, 4); | ||
Activation iAct = doc.addToken(inputNeuron, A, 0) | ||
.setFieldValue(net, 5.0); | ||
|
||
Assertions.assertEquals(5.0, iAct.getFieldValue(net)); | ||
|
||
doc.process(); | ||
|
||
System.out.println("Dump results:"); | ||
|
||
Activation oAct = doc.getActivationByNeuron(outputNeuron); | ||
|
||
Assertions.assertEquals(10.0, oAct.getFieldValue(net)); | ||
|
||
doc.disconnect(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package network.aika.activations; | ||
|
||
import network.aika.bindingsignal.BSType; | ||
|
||
public enum TestBSTypes implements BSType { | ||
A, | ||
B; | ||
} |
51 changes: 51 additions & 0 deletions
51
core/src/test/java/network/aika/activations/model/TestActivationFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package network.aika.activations.model; | ||
|
||
import network.aika.fields.ActivationFunction; | ||
|
||
/** | ||
* | ||
* @author Lukas Molzberger | ||
*/ | ||
public enum TestActivationFunction implements ActivationFunction { | ||
|
||
LIMITED_RECTIFIED_LINEAR_UNIT( | ||
x -> Math.max(0.0, Math.min(1.0, x)), | ||
x -> x >= 0.0 && x <= 1.0 ? 1.0 : 0.0 | ||
); | ||
|
||
private final Function f; | ||
private final Function outerGrad; | ||
|
||
TestActivationFunction(Function f, Function outerGrad) { | ||
this.f = f; | ||
this.outerGrad = outerGrad; | ||
} | ||
|
||
public double f(double x) { | ||
return f.f(x); | ||
} | ||
|
||
public double outerGrad(double x) { | ||
return outerGrad.f(x); | ||
} | ||
|
||
interface Function { | ||
double f(double x); | ||
} | ||
} |
Oops, something went wrong.