Self-Attention Networks Can Process Bounded Hierarchical Languages
Shunyu Yao, Binghui Peng, Christos Papadimitriou, Karthik Narasimhan
Code Available — Be the first to reproduce this paper.
ReproduceCode
- github.com/princeton-nlp/dyck-transformerOfficialIn paperpytorch★ 13
Abstract
Despite their impressive performance in NLP, self-attention networks were recently proved to be limited for processing formal languages with hierarchical structure, such as Dyck_k, the language consisting of well-nested parentheses of k types. This suggested that natural language can be approximated well with models that are too weak for formal languages, or that the role of hierarchy and recursion in natural language might be limited. We qualify this implication by proving that self-attention networks can process Dyck_k, D, the subset of Dyck_k with depth bounded by D, which arguably better captures the bounded hierarchical structure of natural language. Specifically, we construct a hard-attention network with D+1 layers and O( k) memory size (per token per layer) that recognizes Dyck_k, D, and a soft-attention network with two layers and O( k) memory size that generates Dyck_k, D. Experiments show that self-attention networks trained on Dyck_k, D generalize to longer inputs with near-perfect accuracy, and also verify the theoretical memory advantage of self-attention networks over recurrent networks.