Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
698 changes: 349 additions & 349 deletions examples/notebooks/graph_network_shortest_path.ipynb

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions examples/notebooks/intro_mutag_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@
"id": "yzRjo2fLu9A1"
},
"source": [
"\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n",
" \u003ctd\u003e\n",
" \u003ctd\u003e\n",
" \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/gnn/blob/master/examples/notebooks/intro_mutag_example.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n",
" \u003c/td\u003e\n",
" \u003ctd\u003e\n",
" \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/gnn/blob/main/examples/notebooks/intro_mutag_example.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView on GitHub\u003c/a\u003e\n",
" \u003c/td\u003e\n",
"\u003c/table\u003e"
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
" <td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/gnn/blob/master/examples/notebooks/intro_mutag_example.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://github.com/tensorflow/gnn/blob/main/examples/notebooks/intro_mutag_example.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View on GitHub</a>\n",
" </td>\n",
"</table>"
]
},
{
Expand Down
40 changes: 20 additions & 20 deletions examples/notebooks/ogbn_mag_e2e.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@
"id": "ev9vJpM94c3i"
},
"source": [
"\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n",
" \u003ctd\u003e\n",
" \u003ctd\u003e\n",
" \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/gnn/blob/master/examples/notebooks/ogbn_mag_e2e.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n",
" \u003c/td\u003e\n",
" \u003ctd\u003e\n",
" \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/gnn/blob/main/examples/notebooks/ogbn_mag_e2e.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView on GitHub\u003c/a\u003e\n",
" \u003c/td\u003e\n",
"\u003c/table\u003e"
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
" <td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/gnn/blob/master/examples/notebooks/ogbn_mag_e2e.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://github.com/tensorflow/gnn/blob/main/examples/notebooks/ogbn_mag_e2e.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View on GitHub</a>\n",
" </td>\n",
"</table>"
]
},
{
Expand Down Expand Up @@ -183,7 +183,7 @@
"\n",
"OGBN-MAG defines a split of node set \"papers\" into **train, validation and test nodes**, based on its \"year\" feature:\n",
"\n",
" * \"train\" has the 629,571 papers with `year\u003c=2017`,\n",
" * \"train\" has the 629,571 papers with `year<=2017`,\n",
" * \"validation\" has the 64,879 papers with `year==2018`, and\n",
" * \"test\" has the 41,939 papers with `year==2019`.\n",
"\n",
Expand Down Expand Up @@ -301,7 +301,7 @@
"\n",
"def create_sampling_model(\n",
" full_graph_tensor: tfgnn.GraphTensor, sizes: Mapping[str, int]\n",
") -\u003e tf.keras.Model:\n",
") -> tf.keras.Model:\n",
"\n",
" def edge_sampler(sampling_op: tfgnn.sampler.SamplingOp):\n",
" edge_set_name = sampling_op.edge_set_name\n",
Expand Down Expand Up @@ -363,10 +363,10 @@
},
"outputs": [],
"source": [
"def seed_dataset(years: tf.Tensor, split_name: str) -\u003e tf.data.Dataset:\n",
"def seed_dataset(years: tf.Tensor, split_name: str) -> tf.data.Dataset:\n",
" \"\"\"Seed dataset as indices of papers within split years.\"\"\"\n",
" if split_name == \"train\":\n",
" mask = years \u003c= 2017 # 629,571 examples\n",
" mask = years <= 2017 # 629,571 examples\n",
" elif split_name == \"validation\":\n",
" mask = years == 2018 # 64,879 examples\n",
" elif split_name == \"test\":\n",
Expand Down Expand Up @@ -408,7 +408,7 @@
" self._split_name = split_name\n",
" self.input_graph_spec = self._sampling_model.output.spec\n",
"\n",
" def get_dataset(self, context: tf.distribute.InputContext) -\u003e tf.data.Dataset:\n",
" def get_dataset(self, context: tf.distribute.InputContext) -> tf.data.Dataset:\n",
" \"\"\"Creates TF dataset.\"\"\"\n",
" self._seed_dataset = seed_dataset(self._years, self._split_name)\n",
" ds = self._seed_dataset.shard(\n",
Expand All @@ -424,7 +424,7 @@
" )\n",
" return ds.unbatch().prefetch(tf.data.AUTOTUNE)\n",
"\n",
" def sample(self, seeds: tf.Tensor) -\u003e tfgnn.GraphTensor:\n",
" def sample(self, seeds: tf.Tensor) -> tfgnn.GraphTensor:\n",
" seeds = tf.cast(seeds, tf.int64)\n",
" batch_size = tf.size(seeds)\n",
" # samples subgraphs for each seed independently as [[seed1], [seed2], ...]\n",
Expand Down Expand Up @@ -787,7 +787,7 @@
" units=node_state_dim,\n",
" message_dim=message_dim,\n",
" receiver_tag=tfgnn.SOURCE,\n",
" node_set_names=None if i \u003c num_graph_updates-1 else [\"paper\"],\n",
" node_set_names=None if i < num_graph_updates-1 else [\"paper\"],\n",
" simple_conv_reduce_type=\"mean|sum\",\n",
" state_dropout_rate=state_dropout_rate,\n",
" l2_regularization=l2_regularization,\n",
Expand Down Expand Up @@ -879,7 +879,7 @@
" model_dir=\"/tmp/gnn_model/\",\n",
" callbacks=None,\n",
" steps_per_epoch=steps_per_epoch,\n",
" validation_steps=validation_steps, # \u003c\u003c\u003c Remove if not training for real.\n",
" validation_steps=validation_steps, # <<< Remove if not training for real.\n",
" restore_best_weights=False,\n",
" checkpoint_every_n_steps=\"never\",\n",
" summarize_every_n_steps=\"never\",\n",
Expand Down Expand Up @@ -976,7 +976,7 @@
{
"data": {
"text/plain": [
"RunResult(preprocess_model=\u003ckeras.src.engine.functional.Functional object at 0x7fed485eee90\u003e, base_model=\u003ckeras.src.engine.sequential.Sequential object at 0x7febfa0b3280\u003e, trained_model=\u003ckeras.src.engine.functional.Functional object at 0x7fec8811a9e0\u003e)"
"RunResult(preprocess_model=<keras.src.engine.functional.Functional object at 0x7fed485eee90>, base_model=<keras.src.engine.sequential.Sequential object at 0x7febfa0b3280>, trained_model=<keras.src.engine.functional.Functional object at 0x7fec8811a9e0>)"
]
},
"execution_count": 17,
Expand All @@ -989,8 +989,8 @@
" gtspec=example_input_graph_spec,\n",
" train_ds_provider=train_ds_provider,\n",
" train_padding=train_padding,\n",
" valid_ds_provider=valid_ds_provider, # \u003c\u003c\u003c Remove if not training for real.\n",
" valid_padding=valid_padding, # \u003c\u003c\u003c Remove if not training for real.\n",
" valid_ds_provider=valid_ds_provider, # <<< Remove if not training for real.\n",
" valid_padding=valid_padding, # <<< Remove if not training for real.\n",
" global_batch_size=global_batch_size,\n",
" epochs=epochs,\n",
" feature_processors=feature_processors,\n",
Expand Down
42 changes: 21 additions & 21 deletions examples/notebooks/ogbn_mag_indepth.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@
"id": "ev9vJpM94c3i"
},
"source": [
"\u003ctable class=\"tfo-notebook-buttons\" align=\"left\"\u003e\n",
" \u003ctd\u003e\n",
" \u003ctd\u003e\n",
" \u003ca target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/gnn/blob/master/examples/notebooks/ogbn_mag_indepth.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\u003c/a\u003e\n",
" \u003c/td\u003e\n",
" \u003ctd\u003e\n",
" \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/gnn/blob/main/examples/notebooks/ogbn_mag_indepth.ipynb\"\u003e\u003cimg src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView on GitHub\u003c/a\u003e\n",
" \u003c/td\u003e\n",
"\u003c/table\u003e"
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
" <td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/gnn/blob/master/examples/notebooks/ogbn_mag_indepth.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://github.com/tensorflow/gnn/blob/main/examples/notebooks/ogbn_mag_indepth.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View on GitHub</a>\n",
" </td>\n",
"</table>"
]
},
{
Expand Down Expand Up @@ -154,7 +154,7 @@
"\n",
"OGBN-MAG is [Open Graph Benchmark](https://ogb.stanford.edu)'s Node classification task on a subset of the [Microsoft Academic Graph](https://www.microsoft.com/en-us/research/publication/microsoft-academic-graph-when-experts-are-not-enough/). The [basic tutorial](https://colab.research.google.com/github/tensorflow/gnn/blob/master/examples/notebooks/ogbn_mag_e2e.ipynb) has explained the dataset and the task in detail. To recap, the OGBN-MAG dataset is one big graph with node sets \"paper\", \"field_of_study\", \"author\", and \"institution\" and edge sets \"cites\", \"has_topic\", \"writes\", and \"affiliated_with\", each connecting two particular node sets.\n",
"\n",
"The task is to **predict the venue** (journal or conference, not represented in the graph itself) at which each of the papers has been published. Based on their \"year\" feature, the \"paper\" nodes are split into \"train\" (`year\u003c=2017`), \"validation\" (`year==2018`), and \"test\"(`year==2019`).\n"
"The task is to **predict the venue** (journal or conference, not represented in the graph itself) at which each of the papers has been published. Based on their \"year\" feature, the \"paper\" nodes are split into \"train\" (`year<=2017`), \"validation\" (`year==2018`), and \"test\"(`year==2019`).\n"
]
},
{
Expand Down Expand Up @@ -447,7 +447,7 @@
" # For gs://download.tensorflow.org, we avoid listing it and do\n",
" filenames = _glob_sharded(file_pattern)\n",
" ds = tf.data.Dataset.from_tensor_slices(filenames)\n",
" if input_context and input_context.num_input_pipelines \u003e 1:\n",
" if input_context and input_context.num_input_pipelines > 1:\n",
" ds = ds.shard(input_context.num_input_pipelines,\n",
" input_context.input_pipeline_id)\n",
" if shuffle:\n",
Expand Down Expand Up @@ -680,7 +680,7 @@
" years = tf.io.parse_single_example(serialized_example, features)[\"years\"]\n",
" year = years[0] # By convention, the root node is the first node.\n",
" if split_name == \"train\": # 629,571\n",
" return year \u003c= 2017\n",
" return year <= 2017\n",
" elif split_name == \"validation\": # 64,879\n",
" return year == 2018\n",
" elif split_name == \"test\": # 41,939\n",
Expand Down Expand Up @@ -762,7 +762,7 @@
{
"data": {
"text/plain": [
"\u003ctf.Tensor: shape=(30, 1), dtype=int64, numpy=\n",
"<tf.Tensor: shape=(30, 1), dtype=int64, numpy=\n",
"array([[193],\n",
" [265],\n",
" [ 56],\n",
Expand Down Expand Up @@ -792,7 +792,7 @@
" [236],\n",
" [265],\n",
" [193],\n",
" [277]])\u003e"
" [277]])>"
]
},
"execution_count": 11,
Expand Down Expand Up @@ -847,12 +847,12 @@
"name": "stdout",
"output_type": "stream",
"text": [
"\u003ctf.RaggedTensor [[0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]\u003e\n",
"\u003ctf.RaggedTensor [[20],\n",
"<tf.RaggedTensor [[0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]>\n",
"<tf.RaggedTensor [[20],\n",
" [11, 90, 144, 167, 181, 196, 199, 201, 228, 264, 267, 287, 376, 401],\n",
" [32, 88, 111, 175, 211, 235, 301, 309, 311, 350, 377, 521, 631, 688, 693,\n",
" 723, 760, 770, 788, 795, 940, 1100, 1127] ]\u003e\n"
" 723, 760, 770, 788, 795, 940, 1100, 1127] ]>\n"
]
}
],
Expand Down Expand Up @@ -1355,8 +1355,8 @@
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING:tensorflow:Mapping types may not work well with tf.nest. Prefer using MutableMapping for \u003cclass 'tensorflow_gnn.graph.graph_tensor._ImmutableMapping'\u003e\n",
"WARNING:tensorflow:Mapping types may not work well with tf.nest. Prefer using MutableMapping for \u003cclass 'tensorflow_gnn.graph.graph_tensor._ImmutableMapping'\u003e\n"
"WARNING:tensorflow:Mapping types may not work well with tf.nest. Prefer using MutableMapping for <class 'tensorflow_gnn.graph.graph_tensor._ImmutableMapping'>\n",
"WARNING:tensorflow:Mapping types may not work well with tf.nest. Prefer using MutableMapping for <class 'tensorflow_gnn.graph.graph_tensor._ImmutableMapping'>\n"
]
}
],
Expand Down Expand Up @@ -1563,7 +1563,7 @@
"\n",
"Notice that the conventional names *source* and *target* for the endpoints of a directed edge do **not** prescribe the direction of information flow: each \"written\" edge logically goes from a paper to its author (so the \"author\" node is its `TARGET`), yet this model lets the data flow towards the paper (and the \"paper\" node is its `SOURCE`). In fact, sampled subgraphs have edges directed away from the root node, so data flow towards the root often goes from `TARGET` to `SOURCE`.\n",
"\n",
"\u003e Note on terminology: Convolutions are best known in deep learning for convolutional neural networks on image data, in which they aggregate information from a small, fixed, implicitly understood neighborhood of each element in a pixel grid. The term loosely carries over to graphs by interpreting edges as explicit, variable definitions of a node's neighborhood.\n",
"> Note on terminology: Convolutions are best known in deep learning for convolutional neural networks on image data, in which they aggregate information from a small, fixed, implicitly understood neighborhood of each element in a pixel grid. The term loosely carries over to graphs by interpreting edges as explicit, variable definitions of a node's neighborhood.\n",
"\n",
"The code above creates fresh Convolution and NextState layer objects for each edge set and node set, resp., and for each round of updates. This means they all have separate trainable weights. If desired, weight sharing is possible in the standard Keras way by sharing convolution and next-state layer objects, provided the input sizes match.\n",
"\n",
Expand Down
Loading