.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/models/2_small_graph/3_tree-lstm.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorials_models_2_small_graph_3_tree-lstm.py: .. _model-tree-lstm: Tree-LSTM in DGL ========================== **Author**: Zihao Ye, Qipeng Guo, `Minjie Wang `_, `Jake Zhao `_, Zheng Zhang .. warning:: The tutorial aims at gaining insights into the paper, with code as a mean of explanation. The implementation thus is NOT optimized for running efficiency. For recommended implementation, please refer to the `official examples `_. .. GENERATED FROM PYTHON SOURCE LINES 19-22 .. code-block:: Python import os .. GENERATED FROM PYTHON SOURCE LINES 23-59 In this tutorial, you learn to use Tree-LSTM networks for sentiment analysis. The Tree-LSTM is a generalization of long short-term memory (LSTM) networks to tree-structured network topologies. The Tree-LSTM structure was first introduced by Kai et. al in an ACL 2015 paper: `Improved Semantic Representations From Tree-Structured Long Short-Term Memory Networks `__. The core idea is to introduce syntactic information for language tasks by extending the chain-structured LSTM to a tree-structured LSTM. The dependency tree and constituency tree techniques are leveraged to obtain a ''latent tree''. The challenge in training Tree-LSTMs is batching --- a standard technique in machine learning to accelerate optimization. However, since trees generally have different shapes by nature, parallization is non-trivial. DGL offers an alternative. Pool all the trees into one single graph then induce the message passing over them, guided by the structure of each tree. The task and the dataset ------------------------ The steps here use the `Stanford Sentiment Treebank `__ in ``dgl.data``. The dataset provides a fine-grained, tree-level sentiment annotation. There are five classes: Very negative, negative, neutral, positive, and very positive, which indicate the sentiment in the current subtree. Non-leaf nodes in a constituency tree do not contain words, so use a special ``PAD_WORD`` token to denote them. During training and inference their embeddings would be masked to all-zero. .. figure:: https://i.loli.net/2018/11/08/5be3d4bfe031b.png :alt: The figure displays one sample of the SST dataset, which is a constituency parse tree with their nodes labeled with sentiment. To speed up things, build a tiny set with five sentences and take a look at the first one. .. GENERATED FROM PYTHON SOURCE LINES 60-90 .. code-block:: Python from collections import namedtuple os.environ["DGLBACKEND"] = "pytorch" import dgl from dgl.data.tree import SSTDataset SSTBatch = namedtuple("SSTBatch", ["graph", "mask", "wordid", "label"]) # Each sample in the dataset is a constituency tree. The leaf nodes # represent words. The word is an int value stored in the "x" field. # The non-leaf nodes have a special word PAD_WORD. The sentiment # label is stored in the "y" feature field. trainset = SSTDataset(mode="tiny") # the "tiny" set has only five trees tiny_sst = [tr for tr in trainset] num_vocabs = trainset.vocab_size num_classes = trainset.num_classes vocab = trainset.vocab # vocabulary dict: key -> id inv_vocab = { v: k for k, v in vocab.items() } # inverted vocabulary dict: id -> word a_tree = tiny_sst[0] for token in a_tree.ndata["x"].tolist(): if token != trainset.PAD_WORD: print(inv_vocab[token], end=" ") import matplotlib.pyplot as plt .. rst-class:: sphx-glr-script-out .. code-block:: none Downloading /root/.dgl/sst.zip from https://data.dgl.ai/dataset/sst.zip... /root/.dgl/sst.zip: 0%| | 0.00/930k [00:00`__. There is also an implementation of the Child-Sum Tree-LSTM. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.893 seconds) .. _sphx_glr_download_tutorials_models_2_small_graph_3_tree-lstm.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 3_tree-lstm.ipynb <3_tree-lstm.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 3_tree-lstm.py <3_tree-lstm.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 3_tree-lstm.zip <3_tree-lstm.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_