conv.py 12.0 KB
Newer Older
Y
yelrose 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# Copyright (c) 2019 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.
"""This package implements common layers to help building
graph neural networks.
"""
import paddle.fluid as fluid
from pgl import graph_wrapper
from pgl.utils import paddle_helper

W
wangwenjin 已提交
21
__all__ = ['gcn', 'gat', 'gin', 'GaAN']
Y
yelrose 已提交
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


def gcn(gw, feature, hidden_size, activation, name, norm=None):
    """Implementation of graph convolutional neural networks (GCN)

    This is an implementation of the paper SEMI-SUPERVISED CLASSIFICATION
    WITH GRAPH CONVOLUTIONAL NETWORKS (https://arxiv.org/pdf/1609.02907.pdf).

    Args:
        gw: Graph wrapper object (:code:`StaticGraphWrapper` or :code:`GraphWrapper`)

        feature: A tensor with shape (num_nodes, feature_size).

        hidden_size: The hidden size for gcn.

        activation: The activation for the output.

        name: Gcn layer names.

        norm: If :code:`norm` is not None, then the feature will be normalized. Norm must
              be tensor with shape (num_nodes,) and dtype float32.

    Return:
        A tensor with shape (num_nodes, hidden_size)
    """

    def send_src_copy(src_feat, dst_feat, edge_feat):
        return src_feat["h"]

    size = feature.shape[-1]
    if size > hidden_size:
        feature = fluid.layers.fc(feature,
                                  size=hidden_size,
                                  bias_attr=False,
Y
Yelrose 已提交
56
                                  param_attr=fluid.ParamAttr(name=name))
Y
yelrose 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69

    if norm is not None:
        feature = feature * norm

    msg = gw.send(send_src_copy, nfeat_list=[("h", feature)])

    if size > hidden_size:
        output = gw.recv(msg, "sum")
    else:
        output = gw.recv(msg, "sum")
        output = fluid.layers.fc(output,
                                 size=hidden_size,
                                 bias_attr=False,
Y
Yelrose 已提交
70
                                 param_attr=fluid.ParamAttr(name=name))
Y
yelrose 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 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

    if norm is not None:
        output = output * norm

    bias = fluid.layers.create_parameter(
        shape=[hidden_size],
        dtype='float32',
        is_bias=True,
        name=name + '_bias')
    output = fluid.layers.elementwise_add(output, bias, act=activation)
    return output


def gat(gw,
        feature,
        hidden_size,
        activation,
        name,
        num_heads=8,
        feat_drop=0.6,
        attn_drop=0.6,
        is_test=False):
    """Implementation of graph attention networks (GAT)

    This is an implementation of the paper GRAPH ATTENTION NETWORKS
    (https://arxiv.org/abs/1710.10903).

    Args:
        gw: Graph wrapper object (:code:`StaticGraphWrapper` or :code:`GraphWrapper`)

        feature: A tensor with shape (num_nodes, feature_size).

        hidden_size: The hidden size for gat.

        activation: The activation for the output.

        name: Gat layer names.

        num_heads: The head number in gat.

        feat_drop: Dropout rate for feature.

        attn_drop: Dropout rate for attention.

        is_test: Whether in test phrase.

    Return:
        A tensor with shape (num_nodes, hidden_size * num_heads)
    """

    def send_attention(src_feat, dst_feat, edge_feat):
        output = src_feat["left_a"] + dst_feat["right_a"]
        output = fluid.layers.leaky_relu(
            output, alpha=0.2)  # (num_edges, num_heads)
        return {"alpha": output, "h": src_feat["h"]}

    def reduce_attention(msg):
        alpha = msg["alpha"]  # lod-tensor (batch_size, seq_len, num_heads)
        h = msg["h"]
        alpha = paddle_helper.sequence_softmax(alpha)
        old_h = h
        h = fluid.layers.reshape(h, [-1, num_heads, hidden_size])
        alpha = fluid.layers.reshape(alpha, [-1, num_heads, 1])
        if attn_drop > 1e-15:
            alpha = fluid.layers.dropout(
                alpha,
                dropout_prob=attn_drop,
                is_test=is_test,
                dropout_implementation="upscale_in_train")
        h = h * alpha
        h = fluid.layers.reshape(h, [-1, num_heads * hidden_size])
        h = fluid.layers.lod_reset(h, old_h)
        return fluid.layers.sequence_pool(h, "sum")

    if feat_drop > 1e-15:
        feature = fluid.layers.dropout(
            feature,
            dropout_prob=feat_drop,
            is_test=is_test,
            dropout_implementation='upscale_in_train')

    ft = fluid.layers.fc(feature,
                         hidden_size * num_heads,
                         bias_attr=False,
Y
Yelrose 已提交
155
                         param_attr=fluid.ParamAttr(name=name + '_weight'))
Y
yelrose 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    left_a = fluid.layers.create_parameter(
        shape=[num_heads, hidden_size],
        dtype='float32',
        name=name + '_gat_l_A')
    right_a = fluid.layers.create_parameter(
        shape=[num_heads, hidden_size],
        dtype='float32',
        name=name + '_gat_r_A')
    reshape_ft = fluid.layers.reshape(ft, [-1, num_heads, hidden_size])
    left_a_value = fluid.layers.reduce_sum(reshape_ft * left_a, -1)
    right_a_value = fluid.layers.reduce_sum(reshape_ft * right_a, -1)

    msg = gw.send(
        send_attention,
        nfeat_list=[("h", ft), ("left_a", left_a_value),
                    ("right_a", right_a_value)])
    output = gw.recv(msg, reduce_attention)
    bias = fluid.layers.create_parameter(
        shape=[hidden_size * num_heads],
        dtype='float32',
        is_bias=True,
        name=name + '_bias')
    bias.stop_gradient = True
    output = fluid.layers.elementwise_add(output, bias, act=activation)
    return output
W
Webbley 已提交
181 182


W
Webbley 已提交
183 184 185 186 187 188 189
def gin(gw,
        feature,
        hidden_size,
        activation,
        name,
        init_eps=0.0,
        train_eps=False):
W
Webbley 已提交
190 191 192 193 194
    """Implementation of Graph Isomorphism Network (GIN) layer.

    This is an implementation of the paper How Powerful are Graph Neural Networks?
    (https://arxiv.org/pdf/1810.00826.pdf).

195 196 197
    In their implementation, all MLPs have 2 layers. Batch normalization is applied
    on every hidden layer.

W
Webbley 已提交
198 199 200 201 202 203 204
    Args:
        gw: Graph wrapper object (:code:`StaticGraphWrapper` or :code:`GraphWrapper`)

        feature: A tensor with shape (num_nodes, feature_size).

        name: GIN layer names.

W
Webbley 已提交
205 206 207 208
        hidden_size: The hidden size for gin.

        activation: The activation for the output.

W
Webbley 已提交
209 210 211 212 213 214 215
        init_eps: float, optional
            Initial :math:`\epsilon` value, default is 0.

        train_eps: bool, optional
            if True, :math:`\epsilon` will be a learnable parameter.

    Return:
W
Webbley 已提交
216
        A tensor with shape (num_nodes, hidden_size).
W
Webbley 已提交
217 218 219 220 221 222 223 224
    """

    def send_src_copy(src_feat, dst_feat, edge_feat):
        return src_feat["h"]

    epsilon = fluid.layers.create_parameter(
        shape=[1, 1],
        dtype="float32",
W
Webbley 已提交
225 226 227
        attr=fluid.ParamAttr(name="%s_eps" % name),
        default_initializer=fluid.initializer.ConstantInitializer(
            value=init_eps))
W
Webbley 已提交
228 229 230 231 232

    if not train_eps:
        epsilon.stop_gradient = True

    msg = gw.send(send_src_copy, nfeat_list=[("h", feature)])
W
Webbley 已提交
233
    output = gw.recv(msg, "sum") + feature * (epsilon + 1.0)
W
Webbley 已提交
234

W
Webbley 已提交
235 236
    output = fluid.layers.fc(output,
                             size=hidden_size,
237 238 239 240
                             act=None,
                             param_attr=fluid.ParamAttr(name="%s_w_0" % name),
                             bias_attr=fluid.ParamAttr(name="%s_b_0" % name))

W
Webbley 已提交
241 242 243 244 245 246 247 248 249 250
    output = fluid.layers.layer_norm(
        output,
        begin_norm_axis=1,
        param_attr=fluid.ParamAttr(
            name="norm_scale_%s" % (name),
            initializer=fluid.initializer.Constant(1.0)),
        bias_attr=fluid.ParamAttr(
            name="norm_bias_%s" % (name),
            initializer=fluid.initializer.Constant(0.0)), )

251 252
    if activation is not None:
        output = getattr(fluid.layers, activation)(output)
253 254 255 256 257 258

    output = fluid.layers.fc(output,
                             size=hidden_size,
                             act=activation,
                             param_attr=fluid.ParamAttr(name="%s_w_1" % name),
                             bias_attr=fluid.ParamAttr(name="%s_b_1" % name))
W
Webbley 已提交
259

W
Webbley 已提交
260
    return output
W
wangwenjin 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

def GaAN(gw, feature, hidden_size_a, hidden_size_v, hidden_size_m, hidden_size_o, heads,
        name):
    """
    This is an implementation of the paper GaAN: Gated Attention Networks for Learning 
    on Large and Spatiotemporal Graphs(https://arxiv.org/abs/1803.07294)
    """
    # project the feature of nodes into new vector spaces
    feat_key = fluid.layers.fc(feature, hidden_size_a * heads, bias_attr=False,
                    param_attr=fluid.ParamAttr(name=name + '_project_key'))
    feat_value = fluid.layers.fc(feature, hidden_size_v * heads, bias_attr=False,
                    param_attr=fluid.ParamAttr(name=name + '_project_value'))
    feat_query = fluid.layers.fc(feature, hidden_size_a * heads, bias_attr=False,
                    param_attr=fluid.ParamAttr(name=name + '_project_query'))
    feat_gate = fluid.layers.fc(feature, hidden_size_m, bias_attr=False,
                    param_attr=fluid.ParamAttr(name=name + '_project_gate'))
    
    # send function
    def send_func(src_feat, dst_feat, edge_feat):
        feat_query, feat_key = dst_feat['feat_query'], src_feat['feat_key']
        feat_query = fluid.layers.reshape(feat_query, [-1, heads, hidden_size_a])
        feat_key = fluid.layers.reshape(feat_key, [-1, heads, hidden_size_a])
        alpha = fluid.layers.reduce_sum(feat_key * feat_query, dim=-1)

        return {'dst_node_feat': dst_feat['node_feat'],
                'src_node_feat': src_feat['node_feat'],
                'feat_value': src_feat['feat_value'],
                'alpha': alpha,
                'feat_gate': src_feat['feat_gate']}

    # send stage
    message = gw.send(send_func, nfeat_list=[('node_feat', feature),
                ('feat_key', feat_key), ('feat_value', feat_value),
                ('feat_query', feat_query), ('feat_gate', feat_gate)],
                efeat_list=None,
                )

    # recv function
    def recv_func(message):
        dst_feat = message['dst_node_feat'] # feature of dst nodes on each edge
        src_feat = message['src_node_feat'] # feature of src nodes on each edge
        x = fluid.layers.sequence_pool(dst_feat, 'average') # feature of center nodes
        z = fluid.layers.sequence_pool(src_feat, 'average') # mean feature of neighbors

        # compute gate
        feat_gate = message['feat_gate']
        g_max = fluid.layers.sequence_pool(feat_gate, 'max')
        g = fluid.layers.concat([x, g_max, z], axis=1)
        g = fluid.layers.fc(g, heads, bias_attr=False, act='sigmoid')

        # softmax of attention coefficient
        alpha = message['alpha']
        alpha = paddle_helper.sequence_softmax(alpha)

        feat_value = message['feat_value']
        old = feat_value
        feat_value = fluid.layers.reshape(feat_value, [-1, heads, hidden_size_v])
        feat_value = fluid.layers.elementwise_mul(feat_value, alpha, axis=0)
        feat_value = fluid.layers.reshape(feat_value, [-1, heads * hidden_size_v])
        feat_value = fluid.layers.lod_reset(feat_value, old)
        
        feat_value = fluid.layers.sequence_pool(feat_value, 'sum')
        feat_value = fluid.layers.reshape(feat_value, [-1, heads, hidden_size_v])
        output = fluid.layers.elementwise_mul(feat_value, g, axis=0)
        output = fluid.layers.reshape(output, [-1, heads*hidden_size_v])
        output = fluid.layers.concat([x, output], axis=1)

        return output

    # recv stage
    output = gw.recv(message, recv_func)
    
    # output
    output = fluid.layers.fc(output, hidden_size_o, bias_attr=False,
                    param_attr=fluid.ParamAttr(name=name+'_project_output'))
    outout = fluid.layers.leaky_relu(output, alpha=0.1)
    output = fluid.layers.dropout(output, dropout_prob=0.1)

    return output