{"about":{"site":"https://codewithpapers.app","non_affiliation":"Code with Papers and Syntology are not affiliated with, endorsed by, or sponsored by Papers with Code, Meta, or the pwc-archive mirror.","licence":"CC BY-SA 4.0","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","attribution":"https://codewithpapers.app/attribution","modified":"archive material modified by Syntology; see the attribution page"},"url":"/paper/transformer-uncertainty-estimation-with","title":"Transformer Uncertainty Estimation with Hierarchical Stochastic Attention","arxiv_id":"2112.13776","date":"2021-12-27","proceeding":null,"authors":["Jiahuan Pei","Cheng Wang","György Szarvas"],"abstract":"Transformers are state-of-the-art in a wide range of NLP tasks and have also been applied to many real-world products. Understanding the reliability and certainty of transformer model predictions is crucial for building trustable machine learning applications, e.g., medical diagnosis. Although many recent transformer extensions have been proposed, the study of the uncertainty estimation of transformer models is under-explored. In this work, we propose a novel way to enable transformers to have the capability of uncertainty estimation and, meanwhile, retain the original predictive performance. This is achieved by learning a hierarchical stochastic self-attention that attends to values and a set of learnable centroids, respectively. Then new attention heads are formed with a mixture of sampled centroids using the Gumbel-Softmax trick. We theoretically show that the self-attention approximation by sampling from a Gumbel distribution is upper bounded. We empirically evaluate our model on two text classification tasks with both in-domain (ID) and out-of-domain (OOD) datasets. The experimental results demonstrate that our approach: (1) achieves the best predictive performance and uncertainty trade-off among compared methods; (2) exhibits very competitive (in most cases, improved) predictive performance on ID datasets; (3) is on par with Monte Carlo dropout and ensemble methods in uncertainty estimation on OOD datasets.","url_abs":"https://arxiv.org/abs/2112.13776v1","url_pdf":"https://arxiv.org/pdf/2112.13776v1.pdf","source":{"archive":"pwc-archive (Hugging Face), CC BY-SA 4.0","snapshot":"2025-07-28","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","row_kind":"abstracts"},"code_links":[{"paper_slug":"transformer-uncertainty-estimation-with","repo_url":"https://github.com/amzn/sto-transformer","is_official":1,"mentioned_in_paper":0,"mentioned_in_github":0,"framework":"pytorch","reach":{"status":"ok","spdx":"Apache-2.0"}}],"tasks":[{"task_slug":"medical-diagnosis","task_name":"Medical Diagnosis"},{"task_slug":"text-classification","task_name":"Text Classification"},{"task_slug":"text-classification-1","task_name":"text-classification"}],"methods":[{"method_slug":"dropout","method_name":"Dropout"},{"method_slug":"monte-carlo-dropout","method_name":"Monte Carlo Dropout"}],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"atlas_url":"https://app.syntology.ai/?focus=2112.13776","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2112.13776"}},"developers":"https://syntology.ai/developers","read_at":"2026-09-24T18:15:14+00:00","read_at_is":"when the build read Syntology's graph, not when any sample ran","claim":"Per-sample execution status on synthesized fixtures; not a correctness claim about the paper. Samples come from repositories linked to the paper, official or community; repo_kind says which.","repos":[{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/amzn/sto-transformer","reach":{"status":"ok","spdx":"Apache-2.0"}}],"summary":{"unverified":5},"by_repo_kind":{"official":{"samples":5,"ran":0,"repositories":1}},"repo_kind_vocabulary":{"official":"The archive marks this repository official for the paper","named_in_paper":"The archive records that the paper mentions this repository; it is not marked official","listed":"In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper","found_in_text":"Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted","community":"Not in the archive's code links for this paper; a community repository Syntology harvested"},"n_pointer_only_for_licence":0,"samples":[{"code_sha256_prefix":"ebb2db76f4500ae9","entry":"calculate_acc","repo":"amzn/sto-transformer","repo_kind":"official","path":"code/IMDB/Run_evaluation.py","file_url":"https://github.com/amzn/sto-transformer/blob/HEAD/code/IMDB/Run_evaluation.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"ebb2db76f4500ae9"}},{"code_sha256_prefix":"f85e8eae18e4e4a5","entry":"collate_fn","repo":"amzn/sto-transformer","repo_kind":"official","path":"code/IMDB/IMDBDataset.py","file_url":"https://github.com/amzn/sto-transformer/blob/HEAD/code/IMDB/IMDBDataset.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"f85e8eae18e4e4a5"}},{"code_sha256_prefix":"ee86ab65cf9c1c08","entry":"imdb_tokenize","repo":"amzn/sto-transformer","repo_kind":"official","path":"code/IMDB/Utils.py","file_url":"https://github.com/amzn/sto-transformer/blob/HEAD/code/IMDB/Utils.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"ee86ab65cf9c1c08"}},{"code_sha256_prefix":"7ac895ed49dd51da","entry":"load_n_result","repo":"amzn/sto-transformer","repo_kind":"official","path":"code/IMDB/Run_evaluation.py","file_url":"https://github.com/amzn/sto-transformer/blob/HEAD/code/IMDB/Run_evaluation.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"7ac895ed49dd51da"}},{"code_sha256_prefix":"69b332f3a6973b2d","entry":"show_result","repo":"amzn/sto-transformer","repo_kind":"official","path":"code/IMDB/Run_evaluation.py","file_url":"https://github.com/amzn/sto-transformer/blob/HEAD/code/IMDB/Run_evaluation.py","link_basis":"harvester_set","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"69b332f3a6973b2d"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}