multi_class.py 9.8 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 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 59 60 61 62 63 64 65 66 67 68 69 70 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 155 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 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
# 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 file provides the multi class task for testing the embedding 
learned by LINE model.
"""
import argparse
import time
import math
import os
import random

import numpy as np
import sklearn.metrics
from sklearn.metrics import f1_score

import pgl
from pgl.utils import op
import paddle.fluid as fluid
import paddle.fluid.layers as l
from pgl.utils.logger import log
from data_loader import FlickrDataset


def set_seed(seed):
    """Set global random seed.
    """
    random.seed(seed)
    np.random.seed(seed)


def node_classify_model(graph,
                        num_labels,
                        embed_dim=16,
                        name='node_classify_task'):
    """Build node classify model.

    Args:
        graph: The :code:`Graph` data object.

        num_labels: The number of labels.

        embed_dim: The dimension of embedding.

        name: The name of the model.
    """
    pyreader = l.py_reader(
        capacity=70,
        shapes=[[-1, 1], [-1, num_labels]],
        dtypes=['int64', 'float32'],
        lod_levels=[0, 0],
        name=name + '_pyreader',
        use_double_buffer=True)
    nodes, labels = l.read_file(pyreader)
    embed_nodes = l.embedding(
        input=nodes, size=[graph.num_nodes, embed_dim], param_attr='shared_w')
    embed_nodes.stop_gradient = True
    logits = l.fc(input=embed_nodes, size=num_labels)
    loss = l.sigmoid_cross_entropy_with_logits(logits, labels)
    loss = l.reduce_mean(loss)
    prob = l.sigmoid(logits)
    topk = l.reduce_sum(labels, -1)
    return {
        'pyreader': pyreader,
        'loss': loss,
        'prob': prob,
        'labels': labels,
        'topk': topk
    }
    #  return pyreader, loss, prob, labels, topk


def node_classify_generator(graph,
                            all_nodes=None,
                            batch_size=512,
                            epoch=1,
                            shuffle=True):
    """Data generator for node classify.

    Args:
        graph: The :code:`Graph` data object.

        all_nodes: the total number of nodes.

        batch_size: batch size for training.

        epoch: The number of epochs.

        shuffle: Random shuffle of data.
    """

    if all_nodes is None:
        all_nodes = np.arange(graph.num_nodes)

    def batch_nodes_generator(shuffle=shuffle):
        """Batch nodes generator.
        """
        perm = np.arange(len(all_nodes), dtype=np.int64)
        if shuffle:
            np.random.shuffle(perm)
        start = 0
        while start < len(all_nodes):
            yield all_nodes[perm[start:start + batch_size]]
            start += batch_size

    def wrapper():
        """Wrapper function.
        """
        for _ in range(epoch):
            for batch_nodes in batch_nodes_generator():
                batch_nodes_expanded = np.expand_dims(batch_nodes,
                                                      -1).astype(np.int64)
                batch_labels = graph.node_feat['group_id'][batch_nodes].astype(
                    np.float32)
                yield [batch_nodes_expanded, batch_labels]

    return wrapper


def topk_f1_score(labels,
                  probs,
                  topk_list=None,
                  average="macro",
                  threshold=None):
    """Calculate top K F1 score.
    """
    assert topk_list is not None or threshold is not None, "one of topklist and threshold should not be None"
    if threshold is not None:
        preds = probs > threshold
    else:
        preds = np.zeros_like(labels, dtype=np.int64)
        for idx, (prob, topk) in enumerate(zip(np.argsort(probs), topk_list)):
            preds[idx][prob[-int(topk):]] = 1
    return f1_score(labels, preds, average=average)


def main(args):
    """The main funciton for nodes classify task.
    """
    set_seed(args.seed)
    log.info(args)
    dataset = FlickrDataset(args.data_path, train_percentage=args.percent)

    train_steps = (len(dataset.train_index) // args.batch_size) * args.epochs
    place = fluid.CUDAPlace(0) if args.use_cuda else fluid.CPUPlace()
    train_prog = fluid.Program()
    test_prog = fluid.Program()
    startup_prog = fluid.Program()

    with fluid.program_guard(train_prog, startup_prog):
        with fluid.unique_name.guard():
            train_model = node_classify_model(
                dataset.graph,
                dataset.num_groups,
                embed_dim=args.embed_dim,
                name='train')

            lr = l.polynomial_decay(args.lr, train_steps, 0.0001)
            adam = fluid.optimizer.Adam(lr)
            adam.minimize(train_model['loss'])
    with fluid.program_guard(test_prog, startup_prog):
        with fluid.unique_name.guard():
            test_model = node_classify_model(
                dataset.graph,
                dataset.num_groups,
                embed_dim=args.embed_dim,
                name='test')
    test_prog = test_prog.clone(for_test=True)
    exe = fluid.Executor(place)
    exe.run(startup_prog)

    train_model['pyreader'].decorate_tensor_provider(
        node_classify_generator(
            dataset.graph,
            dataset.train_index,
            batch_size=args.batch_size,
            epoch=args.epochs))
    test_model['pyreader'].decorate_tensor_provider(
        node_classify_generator(
            dataset.graph,
            dataset.test_index,
            batch_size=args.batch_size,
            epoch=1))

    def existed_params(var):
        """existed_params
        """
        if not isinstance(var, fluid.framework.Parameter):
            return False
        return os.path.exists(os.path.join(args.ckpt_path, var.name))

    fluid.io.load_vars(
        exe, args.ckpt_path, main_program=train_prog, predicate=existed_params)
    step = 0
    prev_time = time.time()
    train_model['pyreader'].start()

    while 1:
        try:
            train_loss_val, train_probs_val, train_labels_val, train_topk_val = exe.run(
                train_prog,
                fetch_list=[
                    train_model['loss'], train_model['prob'],
                    train_model['labels'], train_model['topk']
                ],
                return_numpy=True)
            train_macro_f1 = topk_f1_score(train_labels_val, train_probs_val,
                                           train_topk_val, "macro",
                                           args.threshold)
            train_micro_f1 = topk_f1_score(train_labels_val, train_probs_val,
                                           train_topk_val, "micro",
                                           args.threshold)
            step += 1
            log.info("Step %d " % step + "Train Loss: %f " % train_loss_val +
                     "Train Macro F1: %f " % train_macro_f1 +
                     "Train Micro F1: %f " % train_micro_f1)
        except fluid.core.EOFException:
            train_model['pyreader'].reset()
            break

        test_model['pyreader'].start()
        test_probs_vals, test_labels_vals, test_topk_vals = [], [], []
        while 1:
            try:
                test_loss_val, test_probs_val, test_labels_val, test_topk_val = exe.run(
                    test_prog,
                    fetch_list=[
                        test_model['loss'], test_model['prob'],
                        test_model['labels'], test_model['topk']
                    ],
                    return_numpy=True)
                test_probs_vals.append(
                    test_probs_val), test_labels_vals.append(test_labels_val)
                test_topk_vals.append(test_topk_val)
            except fluid.core.EOFException:
                test_model['pyreader'].reset()
                test_probs_array = np.concatenate(test_probs_vals)
                test_labels_array = np.concatenate(test_labels_vals)
                test_topk_array = np.concatenate(test_topk_vals)
                test_macro_f1 = topk_f1_score(
                    test_labels_array, test_probs_array, test_topk_array,
                    "macro", args.threshold)
                test_micro_f1 = topk_f1_score(
                    test_labels_array, test_probs_array, test_topk_array,
                    "micro", args.threshold)
                log.info("\t\tStep %d " % step + "Test Loss: %f " %
                         test_loss_val + "Test Macro F1: %f " % test_macro_f1 +
                         "Test Micro F1: %f " % test_micro_f1)
                break


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='LINE')
    parser.add_argument(
        '--data_path',
        type=str,
        default='./data/flickr/',
        help='dataset for training')
    parser.add_argument("--use_cuda", action='store_true', help="use_cuda")
    parser.add_argument("--epochs", type=int, default=10)
    parser.add_argument("--seed", type=int, default=1667)
    parser.add_argument(
        "--lr", type=float, default=0.025, help='learning rate')
    parser.add_argument("--embed_dim", type=int, default=128)
    parser.add_argument("--batch_size", type=int, default=256)
    parser.add_argument("--threshold", type=float, default=None)
    parser.add_argument(
        "--percent",
        type=float,
        default=0.5,
        help="the percentage of data as training data")
    parser.add_argument(
        "--ckpt_path", type=str, default="./checkpoints/model/model_epoch_0/")
    args = parser.parse_args()
    main(args)