GraphSAGE node classification prediction

Neo4j Graph Analytics for Snowflake is in Public Preview and is not intended for production use.

In order to apply GraphSAGE node classification prediction, one must first have trained a GraphSAGE node classification model using the GraphSAGE node classification training endpoint.

This page provides instructions for how to use the GraphSAGE node classification prediction endpoint to predict class labels of new nodes.

Endpoint

The endpoint name is graph.gs_nc_predict, and it takes two positional arguments as input.

The first argument is a VARCHAR that specifies which compute pool to use. For this algorithm we recommend using a GPU compute pool if the input graph is large or the model is deep, but otherwise it might be possible to get away with a CPU compute pool.

The second argument is a JSON configuration map. This JSON must contain the three following keys:

Name

Type

Default

Optional

Description

project

Map

n/a

no

Configuration for the input graph

compute

Map

n/a

no

Configuration for algorithm-specific parameters

write

List

n/a

no

Configuration for writing back algorithm results

Configuration

In this section, we describe the configuration parameters that must be provided to the endpoint.

Input graph configuration

Name

Type

Default

Optional

Description

nodeTables

List

n/a

no

A list of table names, which each represent a node label in the input graph

relationshipTables

Map

n/a

no

A map from table names representing relationship types, to maps of configuration for that relationship type in the input graph (details below)

defaultTablePrefix

String

n/a

yes

A default database and schema prefix to use for table names in the input graph. Should be of the format "<database>.<schema>"

If a defaultTablePrefix is not provided, all table names must be qualified with a database and schema name. That is, they should be given a strings of the format "<database>.<schema>.<table>". If a defaultTablePrefix is provided, table names may also be given as "<table>", in which case the prefix will be prepended to them.

All provided node tables and relationship tables must have unique names. Not only must fully qualified table names be unique, but the table names themselves must also be unique.

Node tables

The nodeTables list of the project map must contain an entry for each type of node in the input graph. In each such table, nodes are represented by rows. There must be at least one column in each table; one that represents the node ID, and this column must be named nodeid (case insensitive). Each node ID must be unique within its table. The type of the nodeid column must be either BIGINT or VARCHAR. In addition to the nodeid column, the table may contain additional columns that represent node properties, for example features of the nodes.

Relationship tables

The relationshipTables map of the project must contain an entry for each type of relationship in the input graph. Each key in the relationshipTables map is the name of the table containing the relationships for one type of relationships, and each value is a map of configuration for that type of relationship. The configuration map for each relationship type looks like the following:

Name

Type

Default

Optional

Description

sourceTable

String

n/a

no

The name of the table that contains the source nodes of the relationships

targetTable

String

n/a

no

The name of the table that contains the target nodes of the relationships

orientation

String

"NATURAL"

yes

How to interpret the orientation (direction) of the provided relationships. Possible values are "NATURAL", "REVERSE" and "UNDIRECTED"

In each provided relationship table, relationships are represented by rows. There are exactly two columns that must be present in each relationship table: sourcenodeid and targetnodeid (both case-insensitive). These specify the source and target nodes of the relationship, respectively, and should correspond to the node IDs in the provided source and target tables. The type of the sourcenodeid and targetnodeid columns must be either BIGINT or VARCHAR.

The orientation parameter specifies how to interpret the direction of the relationships. By default, relationships are interpreted as having the "NATURAL" orientation, meaning that they are assumed to be directed from the source node to the target node. If the orientation is set to "REVERSE", the relationships are interpreted as being directed from the target node to the source node. And if the orientation is set to "UNDIRECTED", the relationships are interpreted as being undirected, meaning that they are symmetric and can be traversed in either direction (independently of which node is the source and which is the target).

Please note that in order for GraphSAGE to properly propagate updates of node embeddings, each type of node must be the target of at least one relationship type. The orientation parameter can be useful to add reverse direction relationships for types of nodes that are only the source of relationships (using the "REVERSE" or "UNDIRECTED" orientations).

Algorithm configuration

The following parameters can be configured for the GraphSAGE node classification prediction endpoint:

Name

Type

Default

Optional

Description

modelname

String

n/a

no

The name of the trained model to use

batchSize

Integer

Inherited

yes

The number of target nodes to predict on in each batch. If not provided, the evaluation batch size that was used when training the model will be used

randomSeed

Integer

A random integer

yes

A number used to seed all randomness of the computation

Example

For our example we will use an IMDB dataset with actors, directors, movies, and genres. These all have keywords associated with them, which we will use as features for the nodes. They are connected by relationships where actors act in movies and directors direct movies. The goal is to predict the genre of movies.

We have a database called imdb that contains the tables:

  • actor with columns nodeid and plot_keywords

  • movie with columns nodeid, plot_keywords and genre

  • director with columns nodeid and plot_keywords

  • acted_in with columns sourcenodeid and targetnodeid that represent actor and movie node IDs

  • directed_in with columns sourcenodeid and targetnodeid that represent director and movie node IDs

The plot_keywords columns contain keywords associated with the nodes, encoded as vectors of floats. The genre column contains the target class labels for the movie nodes, which we want to predict.

You can upload this dataset to your snowflake account by following the instructions at github: neo4j-product-examples/snowflake-graph-analytics.

The prediction query

We assume a model named nc-imdb has been trained using the GraphSAGE node classification training endpoint (see the example).

In the following predict query we specify the project configuration as we did during training. We only need to specify the modelname in the compute configuration as the rest is inherited from the training configuration.

Please also note that we provide write configuration to specify the tables where the computed predictions will be stored.

To run the query, there is a required setup of grants for the application, your consumer role and your environment. Please see the Getting started page for more on this.

We also assume that the application name is the default Neo4j_Graph_Analytics. If you chose a different app name during installation, please replace it with that.

CALL Neo4j_Graph_Analytics.graph.gs_nc_predict('GPU_NV_S', {
    'project': {
        'defaultTablePrefix': 'imdb.gml',
        'nodeTables': ['actor', 'director', 'movie'],
        'relationshipTables': {
            'acted_in': {
                'sourceTable': 'actor',
                'targetTable': 'movie',
                'orientation': 'UNDIRECTED'
            },
            'directed_in': {
                'sourceTable': 'director',
                'targetTable': 'movie',
                'orientation': 'UNDIRECTED'
            }
        }
    },
    'compute': {
        'modelname': 'nc-imdb'
    },
    'write': [{
        'nodeLabel': 'movie',
        'outputTable': 'imdb.gml.genre_predictions'
    }]
});

The above query should produce a result similar to the one below.

JOB_ID

JOB_START

JOB_END

JOB_RESULT

job_2223e0806c9842ddb3fe4b028335a500

2025-04-29 12:18:05.515

2025-04-29 12:18:51.723

{ "node_output_stats": { "movie": { "row_count": 4661, "table_name": "imdb.gml.genre_predictions" } } }

We can inspect the predictions and probabilities for the "first" 10 nodes by running

SELECT * FROM IMDB.GML.genre_predictions LIMIT 10;

which yields

NODEID	PREDICTED_CLASS	       PREDICTED_PROBABILITIES
  4467	              2	  [0.006307,0.028976,0.964717]
  4571	              2   [0.003825,0.039170,0.957005]
  3865	              2   [0.006841,0.058649,0.934510]
  2382	              2   [0.007916,0.048241,0.943842]
  2071	              2   [0.007994,0.015378,0.976628]
  3239	              1   [0.019813,0.941223,0.038963]
  2975	              1   [0.006499,0.946676,0.046826]
  4075	              1   [0.004404,0.948304,0.047292]
  2765	              2   [0.024119,0.007341,0.968539]
    29                0	  [0.915931,0.006881,0.077187]