How to Utilize the StellarGraph Machine Learning Library

Aug 9, 2022 | Data Science

Welcome to your comprehensive guide on how to make the most of the StellarGraph Machine Learning library! StellarGraph is a powerful Python library designed for machine learning tasks on graphs and networks. This article will take you through the essentials of getting started with the library, tackling common problems, and understanding the underlying code with an analogy.

Table of Contents

Introduction

The StellarGraph library offers cutting-edge algorithms for graph machine learning, making it easier to discover patterns and answer questions about graph-structured data. StellarGraph can tackle various machine learning tasks, such as:

  • Representation learning for nodes and edges
  • Node or edge classification and attribute inference
  • Classification of entire graphs
  • Link prediction

The library is built on top of TensorFlow 2 and its high-level API, Keras, ensuring a user-friendly and modular experience.

Getting Started

To dive into StellarGraph, check out detailed and narrated examples that align with your data or problem. You can run these examples directly in Google Colab or Binder. Alternatively, clone the repository and download the demos locally:

bash
curl -L https://github.com/stellargraph/stellargraph/archive/master.zip | tar -xz --strip=1 stellargraph-master/demos

Getting Help

If you encounter a problem or need advice, numerous resources are available:

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Example: GCN

One of the most popular algorithms for graph machine learning is the Graph Convolutional Network (GCN). Let’s break it down using an analogy:

Imagine a classroom where each student (node) interacts with their peers (edges). In this environment, students share knowledge and receive homework help, leading to enhanced learning. Here’s how the GCN works in a similar way:

python
import pandas as pd
from sklearn import model_selection

def load_my_data():
    # Load data into Pandas DataFrames
    ...

nodes, edges, targets = load_my_data()

# Use scikit-learn to compute training and test sets
train_targets, test_targets = model_selection.train_test_split(targets, train_size=0.5)

Just like students needing to work together, nodes need to be prepared and categorized. The input data is compiled into a compatible format for StellarGraph, after which the graph machine learning model can be constructed.

python
import stellargraph as sg
import tensorflow as tf

# Convert raw data into StellarGraphs format for efficient processing
graph = sg.StellarGraph(nodes, edges)
generator = sg.mapper.FullBatchNodeGenerator(graph, method='gcn')

# Create a GCN model with specified layer sizes
gcn = sg.layer.GCN(layer_sizes=[16, 16], generator=generator)
x_inp, x_out = gcn.in_out_tensors()

# Add a layer to compute predictions
predictions = tf.keras.layers.Dense(units=len(ground_truth_targets.columns), activation='softmax')(x_out)

# Build Keras model
model = tf.keras.Model(inputs=x_inp, outputs=predictions)

This setup is akin to how students learn from each other to draw conclusions – a dynamic environment where feedback fosters improvement. The model can be trained and evaluated using the Keras methods.

python
# Compile model for training with Adam optimiser and loss function
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# Train model on training set
model.fit(generator.flow(train_targets.index, train_targets), epochs=5)

# Evaluate on test set
(loss, accuracy) = model.evaluate(generator.flow(test_targets.index, test_targets))
print(f"Test set: loss = {loss}, accuracy = {accuracy}")

Algorithms

StellarGraph supports a variety of algorithms for graph machine learning, enabling tasks ranging from representation learning to classification:

  • Graph Convolutional Networks (GCN)
  • GraphSAGE
  • Node2Vec
  • Many others

Installation

StellarGraph requires Python 3.6 or higher. You can install it using the following commands:

  • From PyPI:
  • pip install stellargraph
    pip install stellargraph[demos]
    
  • Using Anaconda:
  • conda install -c stellargraph stellargraph
    
  • From GitHub source:
  • git clone https://github.com/stellargraph/stellargraph.git
    cd stellargraph
    pip install .
    

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Conclusion

Now you have a foundational understanding of the StellarGraph library, its installation process, and how to implement it in your own projects. Happy graph learning!

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox