Project: GraphScope (App Definition and Development / Database)
File: tutorial_node_classification_local.md

This tutorial presents an end-to-end example that illustrates how GraphScope trains the EgoGraphSAGE model for a node classification task. To demonstrate this, we utilize the ogbn-mag dataset, which is a heterogeneous academic citation network that constitutes a subset of the larger Microsoft Academic Graph. This dataset encompasses four types of entities, including papers, authors, institutions, and fields of study. Additionally, it comprises four types of directed relations. Regarding the heterogeneous ogbn-mag data, the GNN task aims to predict the class of a paper. To accomplish this, we employ both attribute and structural information to classify papers. Specifically, in the graph, each paper node features a 128-dimensional word2vec vector that represents its content. This vector is obtained by averaging the embeddings of words in the paper's title and abstract. It's worth noting that the embeddings of individual words are pre-trained. ```python try: import tensorflow.compat.v1 as tf tf.disablev2behavior() except ImportError: import tensorflow as tf import graphscope as gs from graphscope.dataset import loadogbnmag from graphscope.learning.examples import EgoGraphSAGE from graphscope.learning.examples import EgoSAGESupervisedDataLoader from graphscope.learning.examples.tf.trainer import LocalTrainer gs.setoption(showlog=True) graph = loadogbnmag() print(graph) ``` ```python def train(graph, nodetype, edgetype, classnum, featuresnum, hopsnum=2, nbrsnum=[25, 10], epochs=2, hiddendim=256, indroprate=0.5, learningrate=0.01 ): gs.learning.resetdefaulttf_graph() dimensions = [featuresnum] + [hiddendim] * (hopsnum - 1) + [classnum] model = EgoGraphSAGE(dimensions, actfunc=tf.nn.relu, dropout=indrop_rate) train_data = EgoSAGESupervisedDataLoader( graph, gs.learning.Mask.TRAIN, nodetype=nodetype, edgetype=edgetype, nbrsnum=nbrsnum, hopsnum=hopsnum, ) trainembedding = model.forward(traindata.src_ego) trainlabels = traindata.src_ego.src.labels loss = tf.reduce_mean( tf.nn.sparsesoftmaxcrossentropywith_logits( labels=trainlabels, logits=trainembedding, ) ) optimizer = tf.train.AdamOptimizer(learningrate=learningrate) test_data = EgoSAGESupervisedDataLoader( graph, gs.learning.Mask.TEST, nodetype=nodetype, edgetype=edgetype, nbrsnum=nbrsnum, hopsnum=hopsnum, ) testembedding = model.forward(testdata.src_ego) testlabels = testdata.src_ego.src.labels testindices = tf.math.argmax(testembedding, 1, output_type=tf.int32) test_acc = tf.div( tf.reducesum(tf.cast(tf.math.equal(testindices, test_labels), tf.float32)), tf.cast(tf.shape(test_labels)[0], tf.float32), ) trainer = LocalTrainer() trainer.train(train_data.iterator, loss, optimizer, epochs=epochs) trainer.test(testdata.iterator, testacc) ``` ```python i_features = [] for i in range(128): ifeatures.append("feat" + str(i)) lg = sess.graphlearn( graph, nodes=[("paper", i_features)], edges=[("paper", "cites", "paper")], gen_labels=[ ("train", "paper", 100, (0, 75)), ("val", "paper", 100, (75, 85)), ("test", "paper", 100, (85, 100)), ], ) ``` ```python train(lg, nodetype="paper", edgetype="cites", class_num=349, # output dimension features_num=128, # input dimension ) ```