test_tree_conv_op.py 6.2 KB
Newer Older
Z
zhaozhehao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed 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.

import numpy as np
16
from paddle.fluid.framework import program_guard, Program
Z
zhaozhehao 已提交
17
from op_test import OpTest
18 19
import unittest
import paddle.fluid as fluid
Z
zhaozhehao 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58


def collect_node_patch(og, max_depth):
    """
    The naive method to construct patches
    :param og: original graph
    :param max_depth: the depth of convolution filters
    :return: convolution patches
    """

    def gen(node, max_depth):
        collected = [(node, 1, 1, 0, max_depth)]

        def recurse_helper(node, depth):
            if depth > max_depth:
                return
            l = len(og[node])
            for idx, c in enumerate(og[node], 1):
                if depth + 1 < max_depth:
                    collected.append((c, idx, l, depth + 1, max_depth))
                    recurse_helper(c, depth + 1)

        recurse_helper(node, 0)
        return collected

    res = []
    for u in range(1, len(og)):
        lis = gen(u, max_depth)
        if len(lis) > 0:
            res.append(lis)
    return res


class TestTreeConvOp(OpTest):
    def setUp(self):
        self.n = 17
        self.fea_size = 3
        self.output_size = 1
        self.max_depth = 2
59
        self.batch_size = 2
Z
zhaozhehao 已提交
60 61 62 63 64 65 66 67 68
        self.num_filters = 1
        adj_array = [
            1, 2, 1, 3, 1, 4, 1, 5, 2, 6, 2, 7, 2, 8, 4, 9, 4, 10, 5, 11, 6, 12,
            6, 13, 9, 14, 9, 15, 9, 16, 9, 17
        ]
        adj = np.array(adj_array).reshape((1, self.n - 1, 2)).astype('int32')
        adj = np.tile(adj, (self.batch_size, 1, 1))
        self.op_type = 'tree_conv'
        vectors = np.random.random(
69
            (self.batch_size, self.n, self.fea_size)).astype('float64')
Z
zhaozhehao 已提交
70 71 72 73
        self.inputs = {
            'EdgeSet': adj,
            'NodesVector': vectors,
            'Filter': np.random.random((self.fea_size, 3, self.output_size,
74
                                        self.num_filters)).astype('float64')
Z
zhaozhehao 已提交
75 76 77 78 79 80
        }
        self.attrs = {'max_depth': self.max_depth}
        vectors = []
        for i in range(self.batch_size):
            vector = self.get_output_naive(i)
            vectors.append(vector)
81
        self.outputs = {'Out': np.array(vectors).astype('float64'), }
Z
zhaozhehao 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(
            ['NodesVector', 'Filter'], 'Out', max_relative_error=0.5)

    def get_output_naive(self, batch_id):
        og = [[] for i in range(1, self.n + 2)]
        st = np.array(self.inputs['EdgeSet'][batch_id]).tolist()
        for e in st:
            og[e[0]].append(e[1])
        patches = collect_node_patch(og, self.max_depth)
96
        W = np.array(self.inputs['Filter']).astype('float64')
Z
zhaozhehao 已提交
97 98 99 100 101 102 103 104 105 106 107
        W = np.transpose(W, axes=[1, 0, 2, 3])
        vec = []
        for i, patch in enumerate(patches, 1):
            result = np.zeros((1, W.shape[2], W.shape[3]))
            for v in patch:
                eta_t = float(v[4] - v[3]) / float(v[4])
                eta_l = (1.0 - eta_t) * (0.5 if v[2] == 1 else
                                         float(v[1] - 1.0) / float(v[2] - 1.0))
                eta_r = (1.0 - eta_t) * (1.0 - eta_l)
                x = self.inputs['NodesVector'][batch_id][v[0] - 1]
                eta = np.array([eta_l, eta_r, eta_t]).reshape(
108
                    (3, 1)).astype('float64')
Z
zhaozhehao 已提交
109 110 111 112 113 114 115 116 117 118
                Wconvi = np.tensordot(eta, W, axes=([0], [0]))
                x = np.array(x).reshape((1, 1, self.fea_size))
                res = np.tensordot(x, Wconvi, axes=2)
                result = result + res
            vec.append(result)
        vec = np.concatenate(vec, axis=0)
        vec = np.concatenate(
            [
                vec, np.zeros(
                    (self.n - vec.shape[0], W.shape[2], W.shape[3]),
119
                    dtype='float64')
Z
zhaozhehao 已提交
120 121 122
            ],
            axis=0)
        return vec
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164


class TestTreeConv_OpError(unittest.TestCase):
    def test_errors(self):
        with program_guard(Program(), Program()):
            nodes_vector_1 = np.random.random((10, 5)).astype("float32")
            edge_set_1 = fluid.layers.data(
                name='edge_set_1', shape=[10, 2], dtype='float32')
            # the nodes_vector of tree_conv must be Variable.
            self.assertRaises(TypeError, fluid.contrib.layers.tree_conv,
                              nodes_vector_1, edge_set_1, 3)

            nodes_vector_2 = fluid.layers.data(
                name='vectors2', shape=[10, 5], dtype='float32')
            edge_set_2 = np.random.random((10, 2)).astype("float32")
            # the edge_set of tree_conv must be Variable.
            self.assertRaises(TypeError, fluid.contrib.layers.tree_conv,
                              nodes_vector_2, edge_set_2, 3)


class TestDygraphTreeConv_OpError(unittest.TestCase):
    def test_errors(self):
        with program_guard(Program(), Program()):
            TreeConv = fluid.dygraph.nn.TreeConv(
                feature_size=5, output_size=6, num_filters=1, max_depth=2)
            nodes_vector_1 = np.random.random((10, 5)).astype("float32")
            edge_set_1 = fluid.layers.data(
                name='edge_set_1', shape=[10, 2], dtype='float32')
            # the nodes_vector of TreeConv must be Variable.
            self.assertRaises(TypeError, TreeConv, nodes_vector_1, edge_set_1,
                              3)

            nodes_vector_2 = fluid.layers.data(
                name='vectors2', shape=[10, 5], dtype='float32')
            edge_set_2 = np.random.random((10, 2)).astype("float32")
            # the edge_set of TreeConv must be Variable.
            self.assertRaises(TypeError, TreeConv, nodes_vector_2, edge_set_2,
                              3)


if __name__ == "__main__":
    unittest.main()