mirror of
				https://github.com/labmlai/annotated_deep_learning_paper_implementations.git
				synced 2025-10-31 10:48:49 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			153 lines
		
	
	
		
			7.1 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			153 lines
		
	
	
		
			7.1 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
| <!DOCTYPE html>
 | |
| <html>
 | |
| <head>
 | |
|     <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
 | |
|     <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
 | |
|     <meta name="description" content="A bunch of utility functions and classes for transformers."/>
 | |
| 
 | |
|     <meta name="twitter:card" content="summary"/>
 | |
|     <meta name="twitter:image:src" content="https://avatars1.githubusercontent.com/u/64068543?s=400&v=4"/>
 | |
|     <meta name="twitter:title" content="Utilities for Transformer"/>
 | |
|     <meta name="twitter:description" content="A bunch of utility functions and classes for transformers."/>
 | |
|     <meta name="twitter:site" content="@labmlai"/>
 | |
|     <meta name="twitter:creator" content="@labmlai"/>
 | |
| 
 | |
|     <meta property="og:url" content="https://nn.labml.ai/transformers/utils.html"/>
 | |
|     <meta property="og:title" content="Utilities for Transformer"/>
 | |
|     <meta property="og:image" content="https://avatars1.githubusercontent.com/u/64068543?s=400&v=4"/>
 | |
|     <meta property="og:site_name" content="LabML Neural Networks"/>
 | |
|     <meta property="og:type" content="object"/>
 | |
|     <meta property="og:title" content="Utilities for Transformer"/>
 | |
|     <meta property="og:description" content="A bunch of utility functions and classes for transformers."/>
 | |
| 
 | |
|     <title>Utilities for Transformer</title>
 | |
|     <link rel="shortcut icon" href="/icon.png"/>
 | |
|     <link rel="stylesheet" href="../pylit.css">
 | |
|     <link rel="canonical" href="https://nn.labml.ai/transformers/utils.html"/>
 | |
|     <!-- Global site tag (gtag.js) - Google Analytics -->
 | |
|     <script async src="https://www.googletagmanager.com/gtag/js?id=G-4V3HC8HBLH"></script>
 | |
|     <script>
 | |
|         window.dataLayer = window.dataLayer || [];
 | |
| 
 | |
|         function gtag() {
 | |
|             dataLayer.push(arguments);
 | |
|         }
 | |
| 
 | |
|         gtag('js', new Date());
 | |
| 
 | |
|         gtag('config', 'G-4V3HC8HBLH');
 | |
|     </script>
 | |
| </head>
 | |
| <body>
 | |
| <div id='container'>
 | |
|     <div id="background"></div>
 | |
|     <div class='section'>
 | |
|         <div class='docs'>
 | |
|             <p>
 | |
|                 <a class="parent" href="/">home</a>
 | |
|                 <a class="parent" href="index.html">transformers</a>
 | |
|             </p>
 | |
|             <p>
 | |
| 
 | |
|                 <a href="https://github.com/lab-ml/labml_nn/tree/master/labml_nn/transformers/utils.py">
 | |
|                     <img alt="Github"
 | |
|                          src="https://img.shields.io/github/stars/lab-ml/nn?style=social"
 | |
|                          style="max-width:100%;"/></a>
 | |
|                 <a href="https://join.slack.com/t/labforml/shared_invite/zt-egj9zvq9-Dl3hhZqobexgT7aVKnD14g/"
 | |
|                    rel="nofollow">
 | |
|                     <img alt="Join Slact"
 | |
|                          src="https://img.shields.io/badge/slack-chat-green.svg?logo=slack"
 | |
|                          style="max-width:100%;"/></a>
 | |
|                 <a href="https://twitter.com/labmlai"
 | |
|                    rel="nofollow">
 | |
|                     <img alt="Twitter"
 | |
|                          src="https://img.shields.io/twitter/follow/labmlai?style=social"
 | |
|                          style="max-width:100%;"/></a>
 | |
|             </p>
 | |
|         </div>
 | |
|     </div>
 | |
|     <div class='section' id='section-0'>
 | |
|         <div class='docs doc-strings'>
 | |
|                 <div class='section-link'>
 | |
|                     <a href='#section-0'>#</a>
 | |
|                 </div>
 | |
|                 <h1>Utilities for Transformer</h1>
 | |
|             </div>
 | |
|             <div class='code'>
 | |
|                 <div class="highlight"><pre><span class="lineno">10</span><span></span><span class="kn">import</span> <span class="nn">torch</span></pre></div>
 | |
|             </div>
 | |
|         </div>
 | |
|     <div class='section' id='section-1'>
 | |
|         <div class='docs doc-strings'>
 | |
|                 <div class='section-link'>
 | |
|                     <a href='#section-1'>#</a>
 | |
|                 </div>
 | |
|                 <h2>Subsequent mask to mask out data from future (subsequent) time steps</h2>
 | |
|             </div>
 | |
|             <div class='code'>
 | |
|                 <div class="highlight"><pre><span class="lineno">13</span><span class="k">def</span> <span class="nf">subsequent_mask</span><span class="p">(</span><span class="n">seq_len</span><span class="p">):</span></pre></div>
 | |
|             </div>
 | |
|         </div>
 | |
|     <div class='section' id='section-2'>
 | |
|             <div class='docs'>
 | |
|                 <div class='section-link'>
 | |
|                     <a href='#section-2'>#</a>
 | |
|                 </div>
 | |
|                 
 | |
|             </div>
 | |
|             <div class='code'>
 | |
|                 <div class="highlight"><pre><span class="lineno">17</span>    <span class="n">mask</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">tril</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">seq_len</span><span class="p">,</span> <span class="n">seq_len</span><span class="p">))</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">torch</span><span class="o">.</span><span class="n">bool</span><span class="p">)</span><span class="o">.</span><span class="n">unsqueeze</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
 | |
| <span class="lineno">18</span>    <span class="k">return</span> <span class="n">mask</span></pre></div>
 | |
|             </div>
 | |
|         </div>
 | |
|     <div class='section' id='section-3'>
 | |
|             <div class='docs'>
 | |
|                 <div class='section-link'>
 | |
|                     <a href='#section-3'>#</a>
 | |
|                 </div>
 | |
|                 
 | |
|             </div>
 | |
|             <div class='code'>
 | |
|                 <div class="highlight"><pre><span class="lineno">21</span><span class="k">def</span> <span class="nf">_subsequent_mask</span><span class="p">():</span>
 | |
| <span class="lineno">22</span>    <span class="kn">from</span> <span class="nn">labml.logger</span> <span class="kn">import</span> <span class="n">inspect</span>
 | |
| <span class="lineno">23</span>    <span class="n">inspect</span><span class="p">(</span><span class="n">subsequent_mask</span><span class="p">(</span><span class="mi">10</span><span class="p">)[:,</span> <span class="p">:,</span> <span class="mi">0</span><span class="p">])</span>
 | |
| <span class="lineno">24</span>
 | |
| <span class="lineno">25</span>
 | |
| <span class="lineno">26</span><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span>
 | |
| <span class="lineno">27</span>    <span class="n">_subsequent_mask</span><span class="p">()</span></pre></div>
 | |
|             </div>
 | |
|         </div>
 | |
|     </div>
 | |
| </div>
 | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-AMS_HTML">
 | |
| </script>
 | |
| <!-- MathJax configuration -->
 | |
| <script type="text/x-mathjax-config">
 | |
|     MathJax.Hub.Config({
 | |
|         tex2jax: {
 | |
|             inlineMath: [ ['$','$'] ],
 | |
|             displayMath: [ ['$$','$$'] ],
 | |
|             processEscapes: true,
 | |
|             processEnvironments: true
 | |
|         },
 | |
|         // Center justify equations in code and markdown cells. Elsewhere
 | |
|         // we use CSS to left justify single line equations in code cells.
 | |
|         displayAlign: 'center',
 | |
|         "HTML-CSS": { fonts: ["TeX"] }
 | |
|     });
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| </script>
 | |
| </body>
 | |
| </html> | 
