|
396 | 396 | "id": "54158f1c", |
397 | 397 | "metadata": {}, |
398 | 398 | "outputs": [], |
399 | | - "source": [ |
400 | | - "### Examining the trained model\n", |
401 | | - "# The trained model will be saved at: `GbmlConfig.SharedConfig.TrainedModelMetadata.trained_model_uri`\n", |
402 | | - "print(\n", |
403 | | - " frozen_config.gbml_config_pb.shared_config.trained_model_metadata.trained_model_uri\n", |
404 | | - ")\n", |
405 | | - "\n", |
406 | | - "# You can load the model locally and play around with it:\n", |
407 | | - "import torch\n", |
408 | | - "from torch_geometric.data import HeteroData\n", |
409 | | - "\n", |
410 | | - "from examples.link_prediction.models import init_example_gigl_heterogeneous_model\n", |
411 | | - "from gigl.common import UriFactory\n", |
412 | | - "from gigl.src.common.types.graph_data import EdgeType, NodeType\n", |
413 | | - "from gigl.src.common.utils.model import load_state_dict_from_uri\n", |
414 | | - "\n", |
415 | | - "edge_types = frozen_config.task_metadata_pb_wrapper.get_supervision_edge_types()\n", |
416 | | - "print(f\"Supervision edge types: {edge_types}\")\n", |
417 | | - "edge_type = edge_types[0]\n", |
418 | | - "query_node_type = edge_type.src_node_type\n", |
419 | | - "labeled_node_type = edge_type.dst_node_type\n", |
420 | | - "\n", |
421 | | - "graph_metadata = frozen_config.graph_metadata_pb_wrapper\n", |
422 | | - "\n", |
423 | | - "# Build dicts of a specific node type and edge type to feature dimension.\n", |
424 | | - "# This dimension is the *input* dimension of the model.\n", |
425 | | - "node_feature_dims: dict[NodeType, int] = {\n", |
426 | | - " graph_metadata.condensed_node_type_to_node_type_map[\n", |
427 | | - " condensed_node_type\n", |
428 | | - " ]: node_feature_dim\n", |
429 | | - " for condensed_node_type, node_feature_dim in frozen_config.preprocessed_metadata_pb_wrapper.condensed_node_type_to_feature_dim_map.items()\n", |
430 | | - "}\n", |
431 | | - "edge_feature_dims: dict[EdgeType, int] = {\n", |
432 | | - " graph_metadata.condensed_edge_type_to_edge_type_map[\n", |
433 | | - " condensed_edge_type\n", |
434 | | - " ]: edge_feature_dim\n", |
435 | | - " for condensed_edge_type, edge_feature_dim in frozen_config.preprocessed_metadata_pb_wrapper.condensed_edge_type_to_feature_dim_map.items()\n", |
436 | | - "}\n", |
437 | | - "model = init_example_gigl_heterogeneous_model(\n", |
438 | | - " node_type_to_feature_dim=node_feature_dims,\n", |
439 | | - " edge_type_to_feature_dim=edge_feature_dims,\n", |
440 | | - " device=torch.device(\"cpu\"),\n", |
441 | | - " state_dict=load_state_dict_from_uri(\n", |
442 | | - " UriFactory.create_uri(\n", |
443 | | - " frozen_config.gbml_config_pb.shared_config.trained_model_metadata.trained_model_uri\n", |
444 | | - " )\n", |
445 | | - " ),\n", |
446 | | - ")\n", |
447 | | - "print(model)\n", |
448 | | - "\n", |
449 | | - "# Create some random data to test the model.\n", |
450 | | - "example_data = HeteroData()\n", |
451 | | - "example_data[query_node_type].x = torch.rand((10, node_feature_dims[node_type]))\n", |
452 | | - "example_data[labeled_node_type].x = torch.rand(\n", |
453 | | - " (10, node_feature_dims[labeled_node_type])\n", |
454 | | - ")\n", |
455 | | - "example_data[edge_type].edge_index = torch.randint(0, 10, (2, 20))\n", |
456 | | - "embeddings = model(\n", |
457 | | - " example_data,\n", |
458 | | - " device=torch.device(\"cpu\"),\n", |
459 | | - " output_node_types=[edge_type.src_node_type, edge_type.dst_node_type],\n", |
460 | | - ")\n", |
461 | | - "print(embeddings)" |
462 | | - ] |
| 399 | + "source": "### Examining the trained model\n# The trained model will be saved at: `GbmlConfig.SharedConfig.TrainedModelMetadata.trained_model_uri`\nprint(\n frozen_config.gbml_config_pb.shared_config.trained_model_metadata.trained_model_uri\n)\n\n# You can load the model locally and play around with it:\nimport torch\nfrom torch_geometric.data import HeteroData\n\nfrom examples.link_prediction.models import init_example_gigl_heterogeneous_model\nfrom gigl.common import UriFactory\nfrom gigl.src.common.utils.model import load_state_dict_from_uri\n\nedge_types = frozen_config.task_metadata_pb_wrapper.get_supervision_edge_types()\nprint(f\"Supervision edge types: {edge_types}\")\nedge_type = edge_types[0]\nquery_node_type = edge_type.src_node_type\nlabeled_node_type = edge_type.dst_node_type\n\ngraph_metadata = frozen_config.graph_metadata_pb_wrapper\n\n# Map each node/edge type to its feature dimension, which is the *input*\n# dimension of the model.\nnode_feature_dims = frozen_config.node_type_to_feature_dim_map\nedge_feature_dims = frozen_config.edge_type_to_feature_dim_map\nmodel = init_example_gigl_heterogeneous_model(\n node_type_to_feature_dim=node_feature_dims,\n edge_type_to_feature_dim=edge_feature_dims,\n device=torch.device(\"cpu\"),\n state_dict=load_state_dict_from_uri(\n UriFactory.create_uri(\n frozen_config.gbml_config_pb.shared_config.trained_model_metadata.trained_model_uri\n )\n ),\n)\nprint(model)\n\n# Create some random data to test the model.\nexample_data = HeteroData()\nexample_data[query_node_type].x = torch.rand((10, node_feature_dims[node_type]))\nexample_data[labeled_node_type].x = torch.rand(\n (10, node_feature_dims[labeled_node_type])\n)\nexample_data[edge_type].edge_index = torch.randint(0, 10, (2, 20))\nembeddings = model(\n example_data,\n device=torch.device(\"cpu\"),\n output_node_types=[edge_type.src_node_type, edge_type.dst_node_type],\n)\nprint(embeddings)" |
463 | 400 | }, |
464 | 401 | { |
465 | 402 | "cell_type": "markdown", |
|
0 commit comments