Example #1
0
    def test_forward(self):
        gnn = GraphNeuralNetwork(vertex_size=4, vector_size=2)
        gnn.W = np.arange(1, 5).reshape(2, 2)
        graph = np.array([[0, 0, 1, 0], [0, 0, 1, 1], [1, 1, 0, 1],
                          [0, 1, 1, 0]])

        # 集約1回目
        actual_output1 = gnn.forward(graph, 1).tolist()
        expected_output1 = [8., 16.]
        self.assertEqual(expected_output1, actual_output1)
        actual_x1 = gnn.x.tolist()
        expected_x1 = [[1., 2.], [2., 4.], [3., 6.], [2., 4.]]
        self.assertEqual(expected_x1, actual_x1)

        # 集約2回目
        expected_output2 = [126., 180.]
        actual_output2 = gnn.forward(graph, 1).tolist()
        self.assertEqual(expected_output2, actual_output2)
        actual_x2 = gnn.x.tolist()
        expected_x2 = [[21., 30.], [35., 50.], [35., 50.], [35., 50.]]
        self.assertEqual(expected_x2, actual_x2)

        # 集約3回目
        expected_output3 = [1406., 2052.]
        actual_output3 = gnn.forward(graph, 1).tolist()
        self.assertEqual(expected_output3, actual_output3)
        actual_x3 = gnn.x.tolist()
        expected_x3 = [[185., 270.], [370., 540.], [481., 702.], [370., 540.]]
        self.assertEqual(expected_x3, actual_x3)
Example #2
0
    def test_forward(self):
        gnn = GraphNeuralNetwork(vector_size=2)
        gnn.params["W"] = np.arange(1, 5).reshape(2, 2)
        gnn.params["A"] = np.arange(1, 3)
        gnn.params["b"] = np.array([1])
        vertex_size = 4
        graph = [[0, 0, 1, 0], [0, 0, 1, 1], [1, 1, 0, 1], [0, 1, 1, 0]]
        expected = [[41.], [487.], [5511.]]  # 集約1~3回

        for i in range(0, 3):
            actual = gnn.forward(graph, vertex_size, i + 1).tolist()
            self.assertEqual(expected[i], actual)