GNN
GNN( *, exp_database: Optional[str] = None, exp_schema: Optional[str] = None, database: Optional[str] = None, schema: Optional[str] = None, graph: Optional[Graph] = None, property_transformer: Optional[PropertyTransformer] = None, train: Optional[b.Relationship | b.Fragment | b.Chain] = None, validation: Optional[b.Relationship | b.Fragment | b.Chain] = None, test: Optional[b.Relationship | b.Fragment | b.Chain] = None, source_concept: Optional[b.Concept] = None, target_concept: Optional[b.Concept] = None, task_type: Optional[str] = None, eval_metric: Optional[str] = None, use_current_time: bool = True, has_time_column: Optional[bool] = None, model_database: Optional[str] = None, model_schema: Optional[str] = None, model_name: Optional[str] = None, version_name: Optional[str] = None, model_run_id: Optional[str] = None, task: Optional[b.Relationship] = None, name: Optional[str] = None, test_batch_size: Optional[int] = None, stream_logs: bool = True, extract_embeddings: bool = False, dataset_alias: Optional[str] = None, parallel_reasoners_init: bool = True, **train_params: Any)Train, load, register and predict with a Graph Neural Network.
GNN supports two workflows:
- Fit workflow — provide
graph,train,validation, and atask_type, then callGNN.fitfollowed byGNN.predictions. - Load workflow — provide a previously trained model via
model_run_idor the four registry parameters (model_database,model_schema,model_name,version_name), then callGNN.loadfollowed byGNN.predictions.
In either workflow you can register the trained model in
the Snowflake Model Registry using the GNN.register_model method.
In a deployed model the same GNN is model content: rai models deploy installs the tables it reads, and
Students.predictions = gnn.predictions(domain=Test) declares a block that
scores Test with a registered model on every refresh. gnn.fit() trains
over the deployed tables and registers the result under model_name /
version_name, the model the block scores. Every Snowflake location
defaults to the deployment’s schema.
Parameters
(exp_databasestr, default:None) - Snowflake database for experiment storage. Defaults to the deployment’s database in a deployed model.
(exp_schemastr, default:None) - Snowflake schema for experiment storage. Defaults to the deployment’s schema in a deployed model.
(databasestr, default:None) - Snowflake database to save predictions in.
(schemastr, default:None) - Snowflake schema to save predictions in.
(graphGraph, default:None) - The knowledge graph with edges defined. Required for the fit workflow.
(property_transformerPropertyTransformer, default:None) - Column-level semantic type annotations. If omitted, all column types are auto-inferred.
(trainRelationship or Fragment, default:None) - Training split relationship. Required for the fit workflow.
(validationRelationship or Fragment, default:None) - Validation split relationship. Required for the fit workflow.
(source_conceptConcept, default:None) - Source concept for the load workflow (inferred fromtrainin the fit workflow).
(target_conceptConcept, default:None) - Target concept for link-prediction tasks in the load workflow (inferred fromtrainin the fit workflow).
(task_typestr, default:None) - One of"binary_classification","multiclass_classification","multilabel_classification","regression","link_prediction", or"repeated_link_prediction". Required for the fit workflow. In the load workflow, inferred automatically from the registered model if not provided.
(eval_metricstr, default:None) - Evaluation metric compatible with the chosentask_type(e.g."roc_auc","accuracy","rmse","link_prediction_precision@5").
(use_current_timebool, default:True) - Use the current timestamp as the prediction time. Default isTrue.
(has_time_columnbool, default:None) - Set toTruewhen the task relationships use theatkeyword for temporal ordering. In the load workflow, inferred automatically from the registered model if not provided.
(model_databasestr, default:None) - Snowflake database of a registered model (load workflow).
(model_schemastr, default:None) - Snowflake schema of a registered model (load workflow).
(model_namestr, default:None) - Name of the registered model (load workflow).
(version_namestr, default:None) - Version of the registered model (load workflow).
(model_run_idstr, default:None) - Run ID of a previously trained model (load workflow).
(testRelationship, default:None) - A labeled split held out from training and model selection, with the shape oftrain. In a deployed model the deployment installs a table for it, whichscore(domain=test)evaluates on.
(taskRelationship, default:None) - The labeled relationship the GNN predicts, declared without data: its first field is the source concept and its last the label, whose type the predicted labels take. A GNN that only scores has no training split to read these off; whentrainis given it must have the same shape.
(namestr, default:None) - Names this GNN in a deployed model.reasoners.predictive.gnns.<name>inraiconfig.yamlties the name to a registered model and version per profile, which the GNN scores with in place ofmodel_database,model_schema,model_nameandversion_name. A GNN that only scores must have one configured; a GNN that trains may have none.
(test_batch_sizeint, default:None) - Batch size used during inference.
(stream_logsbool, default:True) - Stream training logs to stdout. Default isTrue.
(extract_embeddingsbool, default:False) - Extract node embeddings during prediction. Default isFalse.
(dataset_aliasstr, default:None) - User chosen alias for the dataset.
(parallel_reasoners_initbool, default:True) - Initialize the Predictive and Logic reasoners in parallel. Default isTrue.
(**train_paramsAny, default:{}) - Additional hyperparameters forwarded to the GNN trainer (e.g.n_epochs,lr,train_batch_size,device,head_layers). Ignored in the load workflow.
Examples
Assuming the setup from the module-level Quick Start
(relationalai.semantics.reasoners.predictive):
gnn = GNN( exp_database="EXPERIMENTS_DB", exp_schema="EXPERIMENTS_SCHEMA", graph=gnn_graph, property_transformer=property_transformer, source_concept=Students, train=Train, validation=Validation, task_type="binary_classification", eval_metric="roc_auc", device="cuda", n_epochs=5,)gnn.fit()Students.predictions = gnn.predictions(domain=Test)Load a registered model and predict:
gnn = GNN( exp_database="EXPERIMENTS_DB", exp_schema="EXPERIMENTS_SCHEMA", model_database="MODELS_DB", model_schema="MODELS_SCHEMA", model_name="MY_MODEL", version_name="V1", source_concept=Students,)gnn.load()Students.predictions = gnn.predictions(domain=Test)Methods
.fit()
GNN.fit() -> GNNTrain the GNN model.
Materializes graph and task tables, creates a trainer, and submits a
training job. If training has already completed or is in progress,
calling fit() again is a no-op.
Returns:
GNN- This instance, so that calls can be chained (e.g.gnn.fit().predictions(domain=Test)).
Raises:
ValueError- If called in a load workflow.
Notes:
In a deployed model, fit() exports nothing. It first deploys the
model this GNN belongs to, which installs the train and validation
tables, and returns at once when nothing installed changed (a new
hyperparameter changes nothing installed). It then trains over those
tables. When this GNN names a registered model, through
reasoners.predictive.gnns or model_name / version_name, the
run is registered there; otherwise GNN.register_model registers it.
Referenced By:
.load()
GNN.load() -> GNNLoad a previously trained model for prediction.
Resolves the model from the Snowflake Model Registry (when
model_database, model_schema, model_name, and
version_name were provided) or by model_run_id, and
prepares the trainer for prediction. If the model is already
loaded, calling load() again is a no-op.
Returns:
GNN- This instance, so that calls can be chained (e.g.gnn.load().predictions(domain=Test)).
Raises:
ValueError- If called in a fit workflow, or if the specified model or version does not exist.
Notes:
In a deployed model, load() only checks that the registered model
this GNN names exists. A deployed block loads that model itself on
every refresh, so there is no trainer to prepare.
Referenced By:
.predictions()
GNN.predictions(domain: b.Relationship | b.Fragment | b.Chain) -> b.RelationshipGenerate predictions on a test domain.
Materializes the test table, submits a prediction job, and returns a
Relationship that can be assigned to a
concept field for downstream querying.
The prediction attributes available on the returned relationship depend on the task type:
- Classification:
.probs,.predicted_labels - Regression:
.predicted_value - Link prediction:
.rank,.scores,.predicted_<target>
Parameters:
(domainRelationship or Fragment or Chain) - The test split relationship (e.g. theTestrelationship defined during data modeling).
Returns:
Relationship- A prediction relationship to be assigned to the source concept (e.g.User.predictions = gnn.predictions(domain=Test)).
Raises:
TypeError- Ifdomainis not a Relationship, Fragment, or Chain.ValueError- If the model has not been fitted or loaded, or if the test domain schema does not match the training schema.
Notes:
In a deployed model this is a declaration rather than a job: it
installs the tables the engine reads and a block that scores domain
with the registered model on every refresh, and returns the same
relationship, filled by that block. predict_df is the one-off
that returns the rows as a DataFrame instead.
Referenced By:
.score()
GNN.score(domain: Optional[b.Relationship | b.Fragment | b.Chain] = None) -> dictScore the fitted or loaded model against a labeled domain.
Unlike GNN.predictions, score() needs true labels to compare
against — pass a labeled split with the same shape as train
(source concept plus label column), or omit domain to score
against the validation split already configured on this instance.
Requires the same graph / train (node and edge tables) used to
fit the model, since scoring re-derives features the same way
GNN.fit did.
Parameters:
(domainRelationship, Fragment, or Chain, default:None) - A labeled split to score against. Defaults to thevalidationsplit passed to the constructor.
Returns:
dict- Evaluation metrics computed by the backend for this task type.
Raises:
ValueError- If no domain is available (neither passed nor configured viavalidation=at construction), ifgraph/trainwere not provided at construction, or if the model has not been fitted or loaded.TypeError- Ifdomainis not a Relationship, Fragment, or Chain.
Notes:
In a deployed model, score() evaluates on what the deployment
installed. Without domain, or with the validation split, it
returns the best epoch’s validation metrics from this process’s
fit() ({"val_metrics": ..., "best_epoch": ...}) and submits
nothing. With the test split it submits a scoring job over the
installed test table, for the run this process trained or else the
registered model this GNN names, and returns the job’s result
({"test_metrics": ...}). The train split, and the validation
split of a GNN that did not fit in this process, are scored the same way,
so a GNN that GNN.load-ed a registered model scores any of the three.
Referenced By:
.register_model()
GNN.register_model( model_database: str, model_schema: str, model_name: str, version_name: str, *, comment: Optional[str] = None) -> NoneRegister a trained model in the Snowflake Model Registry.
After registration the model can be loaded in a later session by
passing the same model_database, model_schema,
model_name, and version_name to the GNN constructor.
Parameters:
(model_databasestr) - Snowflake database for the model registry entry.
(model_schemastr) - Snowflake schema for the model registry entry.
(model_namestr) - Name under which to register the model.
(version_namestr) - Version label (e.g."v1").
(commentstr, default:None) - Free-text comment stored alongside the registry entry.
Raises:
ValueError- If the model is already registered, or has not been fitted / loaded.
Examples:
gnn.fit()gnn.register_model( model_database="MODELS_DB", model_schema="MODELS_SCHEMA", model_name="STUDENT_CHURN", version_name="V1", comment="First baseline model",)Referenced By:
.display_dataset_diagram()
GNN.display_dataset_diagram(show_dtypes: bool = False) -> NoneDisplay the dataset schema inline in a Jupyter notebook.
Renders a diagram of the tables, columns, and foreign-key
relationships of the prepared dataset as an SVG in the current
notebook cell. Requires the Graphviz dot command-line tool
to be installed (brew install graphviz or
apt install graphviz).
Parameters:
(show_dtypesbool, default:False) - Include column data types in the diagram. Default isFalse.
Raises:
ValueError- If no dataset has been prepared yet (i.e.GNN.fithas not been called).
Examples:
gnn.fit()gnn.display_dataset_diagram(show_dtypes=True).save_dataset_diagram()
GNN.save_dataset_diagram(path: str, show_dtypes: bool = False) -> NoneSave the dataset schema diagram to a file.
Writes a diagram of the tables, columns, and foreign-key
relationships of the prepared dataset to path. The output
format is inferred from the file extension (e.g. svg,
png, pdf). Requires the Graphviz dot command-line
tool to be installed (brew install graphviz or
apt install graphviz).
Parameters:
(pathstr) - Destination file path, e.g."schema.svg"or"schema.png".
(show_dtypesbool, default:False) - Include column data types in the diagram. Default isFalse.
Raises:
ValueError- If no dataset has been prepared yet (i.e.GNN.fithas not been called).
Examples:
gnn.fit()gnn.save_dataset_diagram("schema.svg")