Skip to content

Commit cab8cde

Browse files
authored
Add GLE quick start example in the overview getting started page (#2939)
1 parent badf3f3 commit cab8cde

File tree

1 file changed

+87
-5
lines changed

1 file changed

+87
-5
lines changed

docs/overview/getting_started.md

+87-5
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ Graph neural networks (GNNs) combines superiority of both graph analytics and ma
116116

117117
````{dropdown} Prepare data and engine for learning
118118
119-
In our example, we train a Graph Convolutional Network (GCN) model to classify the nodes (papers) into 349 categories, each representing a venue (e.g., pre-print or conference). To accomplish this, we first launch a learning engine and construct a graph with features, following the previous step.
119+
In our example, we train a supervised GraphSAGE model to classify the nodes (papers) into 349 categories, each representing a venue (e.g., pre-print or conference). To accomplish this, we first launch a learning engine and construct a graph with features, following the previous step.
120120
121121
```python
122122
# define the features for learning
@@ -153,8 +153,8 @@ from graphscope.learning.examples import EgoGraphSAGE
153153
from graphscope.learning.examples import EgoSAGESupervisedDataLoader
154154
from graphscope.learning.examples.tf.trainer import LocalTrainer
155155
156-
# supervised GCN.
157-
def train_gcn(graph, node_type, edge_type, class_num, features_num,
156+
# supervised GraphSAGE.
157+
def train_sage(graph, node_type, edge_type, class_num, features_num,
158158
hops_num=2, nbrs_num=[25, 10], epochs=2,
159159
hidden_dim=256, in_drop_rate=0.5, learning_rate=0.01,
160160
):
@@ -195,7 +195,7 @@ def train_gcn(graph, node_type, edge_type, class_num, features_num,
195195
trainer.train(train_data.iterator, loss, optimizer, epochs=epochs)
196196
trainer.test(test_data.iterator, test_acc)
197197
198-
train_gcn(lg, node_type="paper", edge_type="cites",
198+
train_sage(lg, node_type="paper", edge_type="cites",
199199
class_num=349, # output dimension
200200
features_num=130, # input dimension, 128 + kcore + triangle count
201201
)
@@ -265,7 +265,89 @@ print(q2.all().result()) # should print [[v[2], v[3], v[0], v[1]]]
265265

266266

267267
## Graph Learning Quick Start
268+
GNN model training with GraphScope is easy and straightforward. You can use the `graphscope` package to train a GNN model on your local machine. Note that
269+
`tensorflow` is required to run the following example.
268270

269-
TODO(LiSu):
271+
````{dropdown} Example: Training GraphSAGE Model in GraphScope
272+
```python
273+
274+
import graphscope
275+
from graphscope.dataset import load_ogbn_mag
276+
277+
g = load_ogbn_mag()
278+
279+
# define the features for learning
280+
paper_features = [f"feat_{i}" for i in range(128)]
281+
282+
# launch a learning engine.
283+
lg = graphscope.graphlearn(g, nodes=[("paper", paper_features)],
284+
edges=[("paper", "cites", "paper")],
285+
gen_labels=[
286+
("train", "paper", 100, (0, 75)),
287+
("val", "paper", 100, (75, 85)),
288+
("test", "paper", 100, (85, 100))
289+
])
290+
291+
try:
292+
# https://www.tensorflow.org/guide/migrate
293+
import tensorflow.compat.v1 as tf
294+
tf.disable_v2_behavior()
295+
except ImportError:
296+
import tensorflow as tf
297+
298+
import graphscope.learning
299+
from graphscope.learning.examples import EgoGraphSAGE
300+
from graphscope.learning.examples import EgoSAGESupervisedDataLoader
301+
from graphscope.learning.examples.tf.trainer import LocalTrainer
302+
303+
# supervised GraphSAGE
304+
def train_sage(graph, node_type, edge_type, class_num, features_num,
305+
hops_num=2, nbrs_num=[25, 10], epochs=2,
306+
hidden_dim=256, in_drop_rate=0.5, learning_rate=0.01,
307+
):
308+
graphscope.learning.reset_default_tf_graph()
309+
310+
dimensions = [features_num] + [hidden_dim] * (hops_num - 1) + [class_num]
311+
model = EgoGraphSAGE(dimensions, act_func=tf.nn.relu, dropout=in_drop_rate)
312+
313+
# prepare train dataset
314+
train_data = EgoSAGESupervisedDataLoader(
315+
graph, graphscope.learning.Mask.TRAIN,
316+
node_type=node_type, edge_type=edge_type, nbrs_num=nbrs_num, hops_num=hops_num,
317+
)
318+
train_embedding = model.forward(train_data.src_ego)
319+
train_labels = train_data.src_ego.src.labels
320+
loss = tf.reduce_mean(
321+
tf.nn.sparse_softmax_cross_entropy_with_logits(
322+
labels=train_labels, logits=train_embedding,
323+
)
324+
)
325+
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
326+
327+
# prepare test dataset
328+
test_data = EgoSAGESupervisedDataLoader(
329+
graph, graphscope.learning.Mask.TEST,
330+
node_type=node_type, edge_type=edge_type, nbrs_num=nbrs_num, hops_num=hops_num,
331+
)
332+
test_embedding = model.forward(test_data.src_ego)
333+
test_labels = test_data.src_ego.src.labels
334+
test_indices = tf.math.argmax(test_embedding, 1, output_type=tf.int32)
335+
test_acc = tf.div(
336+
tf.reduce_sum(tf.cast(tf.math.equal(test_indices, test_labels), tf.float32)),
337+
tf.cast(tf.shape(test_labels)[0], tf.float32),
338+
)
339+
340+
# train and test
341+
trainer = LocalTrainer()
342+
trainer.train(train_data.iterator, loss, optimizer, epochs=epochs)
343+
trainer.test(test_data.iterator, test_acc)
344+
345+
train_sage(lg, node_type="paper", edge_type="cites",
346+
class_num=349, # output dimension
347+
features_num=128, # input dimension
348+
)
349+
350+
```
351+
````
270352

271353

0 commit comments

Comments
 (0)