Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pakkinlau/aa07123a7b02dae73a70d16f1a3242ec to your computer and use it in GitHub Desktop.
Save pakkinlau/aa07123a7b02dae73a70d16f1a3242ec to your computer and use it in GitHub Desktop.
print_pytorch_geometric_data_info.ipynb aims to print out PyTorch Graph Data flexibly
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Nodes:\n",
" document:\n",
" x=torch.Size([10050, 34])\n",
" year=torch.Size([10050])\n",
" language=torch.Size([10050])\n",
" context:\n",
" x=torch.Size([903, 1])\n",
" year=torch.Size([903])\n",
" region=torch.Size([903])\n",
" prediction_target:\n",
" popularity=torch.Size([301, 3])\n",
" sentiment=torch.Size([301, 3])\n",
"\n",
"Edges:\n",
" ('document', 'semantic', 'document'):\n",
" edge_index=torch.Size([2, 30029])\n",
" edge_type=torch.Size([30029])\n",
" ('document', 'bridging', 'context'):\n",
" edge_index=torch.Size([2, 20168])\n",
" ('context', 'temporal', 'context'):\n",
" edge_index=torch.Size([2, 900])\n"
]
}
],
"source": [
"import torch\n",
"from torch_geometric.data import HeteroData\n",
"\n",
"def print_data_size_info(file_path):\n",
" data = torch.load(file_path)\n",
" \n",
" if not isinstance(data, HeteroData):\n",
" print(\"The loaded data is not a HeteroData object.\")\n",
" return\n",
" \n",
" print(\"Nodes:\")\n",
" for node_type in data.node_types:\n",
" print(f\" {node_type}:\")\n",
" for attr, tensor in data[node_type].items():\n",
" print(f\" {attr}={tensor.size()}\")\n",
" \n",
" print(\"\\nEdges:\")\n",
" for edge_type in data.edge_types:\n",
" print(f\" {edge_type}:\")\n",
" for attr, tensor in data[edge_type].items():\n",
" print(f\" {attr}={tensor.size()}\")\n",
"\n",
"# Example usage\n",
"file_path = 'eurochinaGNN_dataset_revised_bridging_edges.pt'\n",
"print_data_size_info(file_path)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment