def test_save(self):
        net = Net.Network([2, 3, 4])
        net.random()

        os.chdir("..")
        os.chdir("..")

        net.save("networkSaveTest")

        netLoad = Net.Network()
        netLoad.load("networkSaveTest")

        for l, lay in enumerate(net.layers):
            for n, nd in enumerate(lay.nodes):
                self.assertEqual(
                    nd.activation, netLoad.layers[l].nodes[n].activation,
                    "Value of a node should match after loading network")
                self.assertEqual(
                    nd.zActivation, netLoad.layers[l].nodes[n].zActivation,
                    "Activation of a node should match after loading network")
                self.assertEqual(
                    nd.bias, netLoad.layers[l].nodes[n].bias,
                    "Bias of a node should match after loading network")
                for c, con in enumerate(nd.connections):
                    self.assertEqual(
                        con.weight,
                        netLoad.layers[l].nodes[n].connections[c].weight,
                        "Weight of a connection should match after loading network"
                    )

        os.remove("saves/networkSaveTest.txt")
 def test_getText(self):
     n = Net.Node(bias=1.1,
                  connections=[Net.Connection(1.3),
                               Net.Connection(-2.2)])
     n.activation = 2.5
     s = n.getText()
     expect = "Node: value: 2.5, bias:1.1, weight:1.3, weight:-2.2, \n"
     self.assertTrue(
         s == expect,
         "Text of node should be \n\"" + expect + "\" was \n\"" + s + "\"")
Пример #3
0
    def test_combineList(self):
        lis1 = [1, 2, 3, 4]
        lis2 = [2, 3, 5, 6]
        N.combineList(lis1, lis2)
        self.assertEqual(lis1, [3, 5, 8, 10], "the values in the list should be equal to the combined values of: " +
                         str(lis1) + " and " + str(lis2))

        lis1 = [1, 2, [2, 5], 4]
        lis2 = [2, 3, [-3, 8], 6]
        N.combineList(lis1, lis2)
        self.assertEqual(lis1, [3, 5, [-1, 13], 10], "the values in the list should be equal to the combined "
                                                     "values of: " + str(lis1) + " and " + str(lis2))
Пример #4
0
    def test_calculate(self):
        lay = Net.Layer((3, 2))
        for n in lay.nodes:
            self.assertTrue(
                n.activation == 0,
                "Before calculations, value should be 0, was: " +
                str(n.activation))

        inLay = Net.Layer((2, 0))
        inLay.random()
        inLay.nodes[0].activation = .4
        inLay.nodes[1].activation = -.5
        lay.calculate(inLay)
        for n in lay.nodes:
            self.assertFalse(n.activation == 0,
                             "After calculations, value should not be 0")
Пример #5
0
    def test_init(self):
        lay = Net.Layer()
        self.assertTrue(
            lay.nodes == [],
            "By default, nodes should be empty, was: " + str(lay.nodes))

        lay = Net.Layer([])
        self.assertTrue(
            lay.nodes == [],
            "When created with an empty list, nodes should be empty,"
            "was: " + str(lay.nodes))

        lay = Net.Layer(None)
        self.assertTrue(lay.nodes == [],
                        "None nodes should be empty, was: " + str(lay.nodes))

        lay = Net.Layer((2, 3))
        length = len(lay.nodes)
        self.assertTrue(length == 2,
                        "The layer should have 2 nodes, had: " + str(length))
        length = len(lay.nodes[0].connections)
        self.assertTrue(
            length == 3,
            "Each node should have 3 connections, node 0 had: " + str(length))
        length = len(lay.nodes[1].connections)
        self.assertTrue(
            length == 3,
            "Each node should have 3 connections, node 1 had: " + str(length))

        nodes = [Net.Node(1), Net.Node(2)]
        lay = Net.Layer(nodes)
        self.assertTrue(
            lay.nodes == nodes,
            "Nodes should match: " + str(nodes) + "was: " + str(lay.nodes))
    def test_init(self):
        n = Net.Node()
        self.assertTrue(n.bias == 0,
                        "Default bias should be 0, was: " + str(n.bias))
        self.assertTrue(
            n.connections == [],
            "Default connections should be empty, was: " + str(n.connections))
        self.assertTrue(n.activation == 0, "Default value should be 0")

        n = Net.Node(None)
        self.assertTrue(
            n.connections == [],
            "None connections should be empty, was: " + str(n.connections))

        n = Net.Node(connections=[])
        self.assertTrue(
            n.connections == [],
            "Empty list connections should be empty, was: " +
            str(n.connections))

        cons = [Net.Connection(1), Net.Connection(2)]
        n = Net.Node(bias=2, connections=cons)
        self.assertTrue(
            n.connections == cons, "List with 2 connections should be " +
            str(cons) + ", was:" + str(n.connections))
        self.assertTrue(n.bias == 2,
                        "Given bias should be 2, was: " + str(n.bias))

        n = Net.Node(connections=3)
        self.assertTrue(
            len(n.connections) == 3, "Node should have 3 connections")
Пример #7
0
 def test_getText(self):
     lay = Net.Layer((3, 4))
     s = lay.getText()
     expect = "Layer:\n" \
              "Node: value: 0, bias:0, weight:0.0, weight:0.0, weight:0.0, weight:0.0, \n" \
              "Node: value: 0, bias:0, weight:0.0, weight:0.0, weight:0.0, weight:0.0, \n" \
              "Node: value: 0, bias:0, weight:0.0, weight:0.0, weight:0.0, weight:0.0, \n"
     self.assertTrue(expect == s,
                     "Text of layer should be:\n" + expect + "\nwas:\n" + s)
 def test_feedInputs(self):
     net = Net.Network([4, 2])
     expect = [1, 2, 5, -3]
     net.feedInputs(expect)
     for i in range(4):
         v = net.layers[0].nodes[i].activation
         self.assertTrue(
             v == expect[i], "Input node value should be " +
             str(expect[i]) + ", was: " + str(v))
    def test_init(self):
        net = Net.Network()
        self.assertTrue(
            net.layers == [],
            "With default network, layers should be empty list, was: " +
            str(net.layers))

        net = Net.Network([])
        self.assertTrue(
            net.layers == [],
            "With empty list network, layers should be empty list,"
            " was: " + str(net.layers))

        net = Net.Network(None)
        self.assertTrue(
            net.layers == [],
            "With none layers, layers should be empty list, was: " +
            str(net.layers))
 def test_getOutputs(self):
     net = Net.Network([1, 4])
     net.layers[-1].nodes[0].activation = 3
     net.layers[-1].nodes[1].activation = -2
     net.layers[-1].nodes[2].activation = 2
     net.layers[-1].nodes[3].activation = 7
     self.assertTrue(
         net.getOutputs() == [3, -2, 2, 7],
         "Outputs should be: [3, -2, 2, 7],"
         " was: " + str(net.getOutputs()))
    def test_save(self):
        n = Net.Node(bias=2.1,
                     connections=[Net.Connection(1),
                                  Net.Connection(-.5)])
        n.activation = .3
        n.zActivation = .2
        with open("nodeSaveTest.txt", "w") as f:
            n.save(f)

        loadN = Net.Node()
        with open("nodeSaveTest.txt", "r") as f:
            loadN.load(f)

        self.assertEqual(
            n.activation, loadN.activation,
            "After saving and loading this node, the value should be .3 was: "
            + str(loadN.activation))
        self.assertEqual(
            n.zActivation, loadN.zActivation,
            "After saving and loading this node,"
            "the activation should be .2 was: " + str(loadN.zActivation))
        self.assertEqual(
            n.bias, loadN.bias,
            "After saving and loading this node, the bias should be .2 was: " +
            str(loadN.bias))
        self.assertEqual(
            len(loadN.connections), 2, "After saving and loading this node,"
            "there should be 2 connections, found: " +
            str(len(loadN.connections)))
        self.assertEqual(
            loadN.connections[0].weight, 1,
            "After saving and loading this node,"
            "the first connection should be 1, was: " +
            str(loadN.connections[0].weight))
        self.assertEqual(
            loadN.connections[1].weight, -.5,
            "After saving and loading this node,"
            "the second connection should be -.5, was: " +
            str(loadN.connections[1].weight))

        os.remove("nodeSaveTest.txt")
Пример #12
0
    def test_save(self):
        c = Net.Connection(2.1)
        with open("connectionSaveTest.txt", "w") as f:
            c.save(f)

        with open("connectionSaveTest.txt", "r") as f:
            value = float(f.readline())

        self.assertEqual(value, c.weight, "After saving and loading this connection, the weight should be 2.1, was: " +
                         str(value))

        os.remove("connectionSaveTest.txt")
def getTestNet():
    net = Net.Network([4, 6, 6, 1])
    for i in range(len(net.layers)):
        for j in range(len(net.layers[i].nodes)):
            net.layers[i].nodes[j].bias = j * .1

        if i > 0:
            for j in range(len(net.layers[i].nodes)):
                for k in range(len(net.layers[i - 1].nodes)):
                    net.layers[i].nodes[j].connections[k].weight = (i * .3 +
                                                                    j) * .1

    net.feedInputs([.1, .2, .3, .4])
    net.calculate()

    return net
Пример #14
0
    def test_averageList(self):
        avg = [5]
        N.averageList(avg, 2)
        self.assertEqual(avg, [2.5], "Each value in each list should be divided by 2 of the original list")

        avg = [5, [4, 7]]
        N.averageList(avg, 2)
        self.assertEqual(avg, [2.5, [2, 3.5]], "Each value in each list should be divided by 2 of the original list")

        avg = [[5, 8], [4, 7]]
        N.averageList(avg, 2)
        self.assertEqual(avg, [[2.5, 4], [2, 3.5]],
                         "Each value in each list should be divided by 2 of the original list")
 def test_feedLayer(self):
     n = Net.Node(bias=1,
                  connections=[Net.Connection(1),
                               Net.Connection(-2)])
     nodes = [Net.Node(), Net.Node()]
     nodes[0].activation = 1.2
     nodes[1].activation = .8
     layer = Net.Layer(nodes)
     n.feedLayer(layer)
     expect = .64565630622
     self.assertAlmostEqual(
         n.activation, expect, 6,
         "After calculating node, it's value should be about " +
         str(expect) + ", was: " + str(n.activation))
    def test_getText(self):
        net = Net.Network([3, 4, 2])
        s = net.getText()
        expect = "Network:\n" \
                 "Layer:\n" \
                 "Node: value: 0, bias:0, \n" \
                 "Node: value: 0, bias:0, \n" \
                 "Node: value: 0, bias:0, \n" \
                 "Layer:\n" \
                 "Node: value: 0, bias:0, weight:0.0, weight:0.0, weight:0.0, \n" \
                 "Node: value: 0, bias:0, weight:0.0, weight:0.0, weight:0.0, \n" \
                 "Node: value: 0, bias:0, weight:0.0, weight:0.0, weight:0.0, \n" \
                 "Node: value: 0, bias:0, weight:0.0, weight:0.0, weight:0.0, \n" \
                 "Layer:\n" \
                 "Node: value: 0, bias:0, weight:0.0, weight:0.0, weight:0.0, weight:0.0, \n" \
                 "Node: value: 0, bias:0, weight:0.0, weight:0.0, weight:0.0, weight:0.0, \n"

        self.assertTrue(
            s == expect,
            "Text from network should be:\n" + expect + "was:\n" + s)
 def test_random(self):
     n = Net.Node(connections=4)
     n.random()
     testRandom(self, n)
Пример #18
0
 def test_sigmoid(self):
     self.assertAlmostEqual(N.sigmoid(0.0), 0.5, 8, "Should correctly return sigmoid value")
     self.assertAlmostEqual(N.sigmoid(5.0), 0.9933071490757, 8, "Should correctly return sigmoid value")
     self.assertAlmostEqual(N.sigmoid(-5.0), 0.006692850924, 8, "Should correctly return sigmoid value")
Пример #19
0
 def test_getText(self):
     c = Net.Connection(-2.1)
     s = c.getText()
     self.assertTrue(s == "weight:-2.1, ", "Incorrect formatting for text of connection, \"" + s + "\"")
Пример #20
0
 def test_size(self):
     lay = Net.Layer((5, 0))
     self.assertTrue(lay.size() == 5,
                     "Size of the layer should be 5, was: " + str(lay.size))
Пример #21
0
 def test_random(self):
     lay = Net.Layer((4, 2))
     lay.random()
     testRandom(self, lay)
Пример #22
0
    def test_save(self):
        lay = Net.Layer((2, 3))
        lay.nodes[0].activation = .1
        lay.nodes[0].zActivation = .2
        lay.nodes[0].bias = .3
        lay.nodes[0].connections[0].weight = .11
        lay.nodes[0].connections[1].weight = .12
        lay.nodes[0].connections[2].weight = .13

        lay.nodes[1].activation = .4
        lay.nodes[1].zActivation = .5
        lay.nodes[1].bias = .6
        lay.nodes[1].connections[0].weight = .21
        lay.nodes[1].connections[1].weight = .22
        lay.nodes[1].connections[2].weight = .23

        with open("layerSaveTest.txt", "w") as f:
            lay.save(f)

        layLoad = Net.Layer()
        with open("layerSaveTest.txt", "r") as f:
            layLoad.load(f)

        self.assertEqual(
            len(layLoad.nodes), 2,
            "Layer should have 2 nodes after loading, had " +
            str(len(layLoad.nodes)))
        self.assertEqual(
            len(layLoad.nodes[0].connections), 3,
            "Node 0 should have 3 connections after loading, had " +
            str(len(layLoad.nodes[0].connections)))
        self.assertEqual(
            len(layLoad.nodes[1].connections), 3,
            "Node 1 should have 3 connections after loading, had " +
            str(len(layLoad.nodes[1].connections)))

        self.assertEqual(layLoad.nodes[0].activation, .1,
                         "Testing node 0 activation is correct after load")
        self.assertEqual(layLoad.nodes[0].zActivation, .2,
                         "Testing node 0 zActivation is correct after load")
        self.assertEqual(layLoad.nodes[0].bias, .3,
                         "Testing node 0 bias is correct after load")
        self.assertEqual(
            layLoad.nodes[0].connections[0].weight, .11,
            "Testing node 0 connection weight 0"
            "is correct after load")
        self.assertEqual(
            layLoad.nodes[0].connections[1].weight, .12,
            "Testing node 0 connection weight 1"
            "is correct after load")
        self.assertEqual(
            layLoad.nodes[0].connections[2].weight, .13,
            "Testing node 0 connection weight 2"
            "is correct after load")

        self.assertEqual(layLoad.nodes[1].activation, .4,
                         "Testing node 0 value is correct after load")
        self.assertEqual(layLoad.nodes[1].zActivation, .5,
                         "Testing node 0 value is correct after load")
        self.assertEqual(layLoad.nodes[1].bias, .6,
                         "Testing node 0 value is correct after load")
        self.assertEqual(
            layLoad.nodes[1].connections[0].weight, .21,
            "Testing node 1 connection weight 0"
            "is correct after load")
        self.assertEqual(
            layLoad.nodes[1].connections[1].weight, .22,
            "Testing node 1 connection weight 1"
            "is correct after load")
        self.assertEqual(
            layLoad.nodes[1].connections[2].weight, .23,
            "Testing node 1 connection weight 2"
            "is correct after load")

        os.remove("layerSaveTest.txt")
    def test_calculate(self):
        layers = []

        nodes = [Net.Node([]), Net.Node([])]
        nodes[0].activation = 3.3
        nodes[1].activation = -1.5
        layers.append(Net.Layer(nodes))

        nodes = [
            Net.Node(bias=.2,
                     connections=[Net.Connection(1),
                                  Net.Connection(-1)]),
            Net.Node(bias=-1.3,
                     connections=[Net.Connection(.5),
                                  Net.Connection(.2)])
        ]
        layers.append(Net.Layer(nodes))

        nodes = [
            Net.Node(bias=-.8,
                     connections=[Net.Connection(1.5),
                                  Net.Connection(.2)]),
            Net.Node(bias=.45,
                     connections=[Net.Connection(4),
                                  Net.Connection(3)])
        ]
        layers.append(Net.Layer(nodes))

        net = Net.Network(layers)
        net.calculate()

        expect = [0.688359339593154, 0.9974285761888321]
        actual = net.getOutputs()
        for i in range(2):
            self.assertAlmostEqual(
                expect[i], actual[i], 6, "Network output should be: " +
                str(expect[i]) + " was: " + str(actual[i]))
 def test_random(self):
     net = Net.Network([2, 3, 4])
     net.random()
     for lay in net.layers:
         TestLayer.testRandom(self, lay)
Пример #25
0
 def test_init(self):
     c = Net.Connection()
     self.assertTrue(c.weight == 0, "Weight should be 0 by default, was: " + str(c.weight))
     c = Net.Connection(2)
     self.assertTrue(c.weight == 2, "Weight should be set to 2, was: " + str(c.weight))
Пример #26
0
 def test_dSigmoid(self):
     self.assertAlmostEqual(N.derivSigmoid(0.0), 0.25, 8, "Should correctly return sigmoid value")
     self.assertAlmostEqual(N.derivSigmoid(5.0), 0.0066480566707901, 8, "Should correctly return derivative of sigmoid")
     self.assertAlmostEqual(N.derivSigmoid(-5.0), 0.006648056670790, 8, "Should correctly return derivative of sigmoid")
Пример #27
0
 def test_random(self):
     c = Net.Connection(1)
     c.random()
     testRandom(self, c)