CINXE.COM

Layer weight initializers

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content="Keras documentation"> <meta name="author" content="Keras Team"> <link rel="shortcut icon" href="https://keras.io/img/favicon.ico"> <link rel="canonical" href="https://keras.io/api/layers/initializers/" /> <!-- Social --> <meta property="og:title" content="Keras documentation: Layer weight initializers"> <meta property="og:image" content="https://keras.io/img/logo-k-keras-wb.png"> <meta name="twitter:title" content="Keras documentation: Layer weight initializers"> <meta name="twitter:image" content="https://keras.io/img/k-keras-social.png"> <meta name="twitter:card" content="summary"> <title>Layer weight initializers</title> <!-- Bootstrap core CSS --> <link href="/css/bootstrap.min.css" rel="stylesheet"> <!-- Custom fonts for this template --> <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600;700;800&display=swap" rel="stylesheet"> <!-- Custom styles for this template --> <link href="/css/docs.css" rel="stylesheet"> <link href="/css/monokai.css" rel="stylesheet"> <!-- Google Tag Manager --> <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-5DNGF4N'); </script> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-175165319-128', 'auto'); ga('send', 'pageview'); </script> <!-- End Google Tag Manager --> <script async defer src="https://buttons.github.io/buttons.js"></script> </head> <body> <!-- Google Tag Manager (noscript) --> <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-5DNGF4N" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> <!-- End Google Tag Manager (noscript) --> <div class='k-page'> <div class="k-nav" id="nav-menu"> <a href='/'><img src='/img/logo-small.png' class='logo-small' /></a> <div class="nav flex-column nav-pills" role="tablist" aria-orientation="vertical"> <a class="nav-link" href="/about/" role="tab" aria-selected="">About Keras</a> <a class="nav-link" href="/getting_started/" role="tab" aria-selected="">Getting started</a> <a class="nav-link" href="/guides/" role="tab" aria-selected="">Developer guides</a> <a class="nav-link active" href="/api/" role="tab" aria-selected="">Keras 3 API documentation</a> <a class="nav-sublink" href="/api/models/">Models API</a> <a class="nav-sublink active" href="/api/layers/">Layers API</a> <a class="nav-sublink2" href="/api/layers/base_layer/">The base Layer class</a> <a class="nav-sublink2" href="/api/layers/activations/">Layer activations</a> <a class="nav-sublink2 active" href="/api/layers/initializers/">Layer weight initializers</a> <a class="nav-sublink2" href="/api/layers/regularizers/">Layer weight regularizers</a> <a class="nav-sublink2" href="/api/layers/constraints/">Layer weight constraints</a> <a class="nav-sublink2" href="/api/layers/core_layers/">Core layers</a> <a class="nav-sublink2" href="/api/layers/convolution_layers/">Convolution layers</a> <a class="nav-sublink2" href="/api/layers/pooling_layers/">Pooling layers</a> <a class="nav-sublink2" href="/api/layers/recurrent_layers/">Recurrent layers</a> <a class="nav-sublink2" href="/api/layers/preprocessing_layers/">Preprocessing layers</a> <a class="nav-sublink2" href="/api/layers/normalization_layers/">Normalization layers</a> <a class="nav-sublink2" href="/api/layers/regularization_layers/">Regularization layers</a> <a class="nav-sublink2" href="/api/layers/attention_layers/">Attention layers</a> <a class="nav-sublink2" href="/api/layers/reshaping_layers/">Reshaping layers</a> <a class="nav-sublink2" href="/api/layers/merging_layers/">Merging layers</a> <a class="nav-sublink2" href="/api/layers/activation_layers/">Activation layers</a> <a class="nav-sublink2" href="/api/layers/backend_specific_layers/">Backend-specific layers</a> <a class="nav-sublink" href="/api/callbacks/">Callbacks API</a> <a class="nav-sublink" href="/api/ops/">Ops API</a> <a class="nav-sublink" href="/api/optimizers/">Optimizers</a> <a class="nav-sublink" href="/api/metrics/">Metrics</a> <a class="nav-sublink" href="/api/losses/">Losses</a> <a class="nav-sublink" href="/api/data_loading/">Data loading</a> <a class="nav-sublink" href="/api/datasets/">Built-in small datasets</a> <a class="nav-sublink" href="/api/applications/">Keras Applications</a> <a class="nav-sublink" href="/api/mixed_precision/">Mixed precision</a> <a class="nav-sublink" href="/api/distribution/">Multi-device distribution</a> <a class="nav-sublink" href="/api/random/">RNG API</a> <a class="nav-sublink" href="/api/utils/">Utilities</a> <a class="nav-sublink" href="/api/keras_tuner/">KerasTuner</a> <a class="nav-sublink" href="/api/keras_cv/">KerasCV</a> <a class="nav-sublink" href="/api/keras_nlp/">KerasNLP</a> <a class="nav-sublink" href="/api/keras_hub/">KerasHub</a> <a class="nav-link" href="/2.18/api/" role="tab" aria-selected="">Keras 2 API documentation</a> <a class="nav-link" href="/examples/" role="tab" aria-selected="">Code examples</a> <a class="nav-link" href="/keras_tuner/" role="tab" aria-selected="">KerasTuner: Hyperparameter Tuning</a> <a class="nav-link" href="/keras_hub/" role="tab" aria-selected="">KerasHub: Pretrained Models</a> <a class="nav-link" href="/keras_cv/" role="tab" aria-selected="">KerasCV: Computer Vision Workflows</a> <a class="nav-link" href="/keras_nlp/" role="tab" aria-selected="">KerasNLP: Natural Language Workflows</a> </div> </div> <div class='k-main'> <div class='k-main-top'> <script> function displayDropdownMenu() { e = document.getElementById("nav-menu"); if (e.style.display == "block") { e.style.display = "none"; } else { e.style.display = "block"; document.getElementById("dropdown-nav").style.display = "block"; } } function resetMobileUI() { if (window.innerWidth <= 840) { document.getElementById("nav-menu").style.display = "none"; document.getElementById("dropdown-nav").style.display = "block"; } else { document.getElementById("nav-menu").style.display = "block"; document.getElementById("dropdown-nav").style.display = "none"; } var navmenu = document.getElementById("nav-menu"); var menuheight = navmenu.clientHeight; var kmain = document.getElementById("k-main-id"); kmain.style.minHeight = (menuheight + 100) + 'px'; } window.onresize = resetMobileUI; window.addEventListener("load", (event) => { resetMobileUI() }); </script> <div id='dropdown-nav' onclick="displayDropdownMenu();"> <svg viewBox="-20 -20 120 120" width="60" height="60"> <rect width="100" height="20"></rect> <rect y="30" width="100" height="20"></rect> <rect y="60" width="100" height="20"></rect> </svg> </div> <form class="bd-search d-flex align-items-center k-search-form" id="search-form"> <input type="search" class="k-search-input" id="search-input" placeholder="Search Keras documentation..." aria-label="Search Keras documentation..." autocomplete="off"> <button class="k-search-btn"> <svg width="13" height="13" viewBox="0 0 13 13"><title>search</title><path d="m4.8495 7.8226c0.82666 0 1.5262-0.29146 2.0985-0.87438 0.57232-0.58292 0.86378-1.2877 0.87438-2.1144 0.010599-0.82666-0.28086-1.5262-0.87438-2.0985-0.59352-0.57232-1.293-0.86378-2.0985-0.87438-0.8055-0.010599-1.5103 0.28086-2.1144 0.87438-0.60414 0.59352-0.8956 1.293-0.87438 2.0985 0.021197 0.8055 0.31266 1.5103 0.87438 2.1144 0.56172 0.60414 1.2665 0.8956 2.1144 0.87438zm4.4695 0.2115 3.681 3.6819-1.259 1.284-3.6817-3.7 0.0019784-0.69479-0.090043-0.098846c-0.87973 0.76087-1.92 1.1413-3.1207 1.1413-1.3553 0-2.5025-0.46363-3.4417-1.3909s-1.4088-2.0686-1.4088-3.4239c0-1.3553 0.4696-2.4966 1.4088-3.4239 0.9392-0.92727 2.0864-1.3969 3.4417-1.4088 1.3553-0.011889 2.4906 0.45771 3.406 1.4088 0.9154 0.95107 1.379 2.0924 1.3909 3.4239 0 1.2126-0.38043 2.2588-1.1413 3.1385l0.098834 0.090049z"></path></svg> </button> </form> <script> var form = document.getElementById('search-form'); form.onsubmit = function(e) { e.preventDefault(); var query = document.getElementById('search-input').value; window.location.href = '/search.html?query=' + query; return False } </script> </div> <div class='k-main-inner' id='k-main-id'> <div class='k-location-slug'> <span class="k-location-slug-pointer">►</span> <a href='/api/'>Keras 3 API documentation</a> / <a href='/api/layers/'>Layers API</a> / Layer weight initializers </div> <div class='k-content'> <h1 id="layer-weight-initializers">Layer weight initializers</h1> <h2 id="usage-of-initializers">Usage of initializers</h2> <p>Initializers define the way to set the initial random weights of Keras layers.</p> <p>The keyword arguments used for passing initializers to layers depends on the layer. Usually, it is simply <code>kernel_initializer</code> and <code>bias_initializer</code>:</p> <div class="codehilite"><pre><span></span><code><span class="kn">from</span> <span class="nn">keras</span> <span class="kn">import</span> <span class="n">layers</span> <span class="kn">from</span> <span class="nn">keras</span> <span class="kn">import</span> <span class="n">initializers</span> <span class="n">layer</span> <span class="o">=</span> <span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span> <span class="n">units</span><span class="o">=</span><span class="mi">64</span><span class="p">,</span> <span class="n">kernel_initializer</span><span class="o">=</span><span class="n">initializers</span><span class="o">.</span><span class="n">RandomNormal</span><span class="p">(</span><span class="n">stddev</span><span class="o">=</span><span class="mf">0.01</span><span class="p">),</span> <span class="n">bias_initializer</span><span class="o">=</span><span class="n">initializers</span><span class="o">.</span><span class="n">Zeros</span><span class="p">()</span> <span class="p">)</span> </code></pre></div> <p>All built-in initializers can also be passed via their string identifier:</p> <div class="codehilite"><pre><span></span><code><span class="n">layer</span> <span class="o">=</span> <span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span> <span class="n">units</span><span class="o">=</span><span class="mi">64</span><span class="p">,</span> <span class="n">kernel_initializer</span><span class="o">=</span><span class="s1">&#39;random_normal&#39;</span><span class="p">,</span> <span class="n">bias_initializer</span><span class="o">=</span><span class="s1">&#39;zeros&#39;</span> <span class="p">)</span> </code></pre></div> <hr /> <h2 id="available-initializers">Available initializers</h2> <p>The following built-in initializers are available as part of the <code>keras.initializers</code> module:</p> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.6.0/keras/src/initializers/random_initializers.py#L30">[source]</a></span></p> <h3 id="randomnormal-class"><code>RandomNormal</code> class</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">initializers</span><span class="o">.</span><span class="n">RandomNormal</span><span class="p">(</span><span class="n">mean</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">stddev</span><span class="o">=</span><span class="mf">0.05</span><span class="p">,</span> <span class="n">seed</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> </code></pre></div> <p>Random normal initializer.</p> <p>Draws samples from a normal distribution for given parameters.</p> <p><strong>Examples</strong></p> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Standalone usage:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">RandomNormal</span><span class="p">(</span><span class="n">mean</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">stddev</span><span class="o">=</span><span class="mf">1.0</span><span class="p">)</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">values</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span> </code></pre></div> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Usage in a Keras layer:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">RandomNormal</span><span class="p">(</span><span class="n">mean</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">stddev</span><span class="o">=</span><span class="mf">1.0</span><span class="p">)</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">layer</span> <span class="o">=</span> <span class="n">Dense</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">kernel_initializer</span><span class="o">=</span><span class="n">initializer</span><span class="p">)</span> </code></pre></div> <p><strong>Arguments</strong></p> <ul> <li><strong>mean</strong>: A python scalar or a scalar keras tensor. Mean of the random values to generate.</li> <li><strong>stddev</strong>: A python scalar or a scalar keras tensor. Standard deviation of the random values to generate.</li> <li><strong>seed</strong>: A Python integer or instance of <code>keras.backend.SeedGenerator</code>. Used to make the behavior of the initializer deterministic. Note that an initializer seeded with an integer or <code>None</code> (unseeded) will produce the same random values across multiple calls. To get different random values across multiple calls, use as seed an instance of <code>keras.backend.SeedGenerator</code>.</li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.6.0/keras/src/initializers/random_initializers.py#L145">[source]</a></span></p> <h3 id="randomuniform-class"><code>RandomUniform</code> class</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">initializers</span><span class="o">.</span><span class="n">RandomUniform</span><span class="p">(</span><span class="n">minval</span><span class="o">=-</span><span class="mf">0.05</span><span class="p">,</span> <span class="n">maxval</span><span class="o">=</span><span class="mf">0.05</span><span class="p">,</span> <span class="n">seed</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> </code></pre></div> <p>Random uniform initializer.</p> <p>Draws samples from a uniform distribution for given parameters.</p> <p><strong>Examples</strong></p> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Standalone usage:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">RandomUniform</span><span class="p">(</span><span class="n">minval</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">maxval</span><span class="o">=</span><span class="mf">1.0</span><span class="p">)</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">values</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span> </code></pre></div> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Usage in a Keras layer:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">RandomUniform</span><span class="p">(</span><span class="n">minval</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">maxval</span><span class="o">=</span><span class="mf">1.0</span><span class="p">)</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">layer</span> <span class="o">=</span> <span class="n">Dense</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">kernel_initializer</span><span class="o">=</span><span class="n">initializer</span><span class="p">)</span> </code></pre></div> <p><strong>Arguments</strong></p> <ul> <li><strong>minval</strong>: A python scalar or a scalar keras tensor. Lower bound of the range of random values to generate (inclusive).</li> <li><strong>maxval</strong>: A python scalar or a scalar keras tensor. Upper bound of the range of random values to generate (exclusive).</li> <li><strong>seed</strong>: A Python integer or instance of <code>keras.backend.SeedGenerator</code>. Used to make the behavior of the initializer deterministic. Note that an initializer seeded with an integer or <code>None</code> (unseeded) will produce the same random values across multiple calls. To get different random values across multiple calls, use as seed an instance of <code>keras.backend.SeedGenerator</code>.</li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.6.0/keras/src/initializers/random_initializers.py#L86">[source]</a></span></p> <h3 id="truncatednormal-class"><code>TruncatedNormal</code> class</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">initializers</span><span class="o">.</span><span class="n">TruncatedNormal</span><span class="p">(</span><span class="n">mean</span><span class="o">=</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">stddev</span><span class="o">=</span><span class="mf">0.05</span><span class="p">,</span> <span class="n">seed</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> </code></pre></div> <p>Initializer that generates a truncated normal distribution.</p> <p>The values generated are similar to values from a <code>RandomNormal</code> initializer, except that values more than two standard deviations from the mean are discarded and re-drawn.</p> <p><strong>Examples</strong></p> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Standalone usage:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">TruncatedNormal</span><span class="p">(</span><span class="n">mean</span><span class="o">=</span><span class="mf">0.</span><span class="p">,</span> <span class="n">stddev</span><span class="o">=</span><span class="mf">1.</span><span class="p">)</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">values</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span> </code></pre></div> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Usage in a Keras layer:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">TruncatedNormal</span><span class="p">(</span><span class="n">mean</span><span class="o">=</span><span class="mf">0.</span><span class="p">,</span> <span class="n">stddev</span><span class="o">=</span><span class="mf">1.</span><span class="p">)</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">layer</span> <span class="o">=</span> <span class="n">Dense</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">kernel_initializer</span><span class="o">=</span><span class="n">initializer</span><span class="p">)</span> </code></pre></div> <p><strong>Arguments</strong></p> <ul> <li><strong>mean</strong>: A python scalar or a scalar keras tensor. Mean of the random values to generate.</li> <li><strong>stddev</strong>: A python scalar or a scalar keras tensor. Standard deviation of the random values to generate.</li> <li><strong>seed</strong>: A Python integer or instance of <code>keras.backend.SeedGenerator</code>. Used to make the behavior of the initializer deterministic. Note that an initializer seeded with an integer or <code>None</code> (unseeded) will produce the same random values across multiple calls. To get different random values across multiple calls, use as seed an instance of <code>keras.backend.SeedGenerator</code>.</li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.6.0/keras/src/initializers/constant_initializers.py#L48">[source]</a></span></p> <h3 id="zeros-class"><code>Zeros</code> class</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">initializers</span><span class="o">.</span><span class="n">Zeros</span><span class="p">()</span> </code></pre></div> <p>Initializer that generates tensors initialized to 0.</p> <p><strong>Examples</strong></p> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Standalone usage:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">Zeros</span><span class="p">()</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">values</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span> </code></pre></div> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Usage in a Keras layer:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">Zeros</span><span class="p">()</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">layer</span> <span class="o">=</span> <span class="n">Dense</span><span class="p">(</span><span class="n">units</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">kernel_initializer</span><span class="o">=</span><span class="n">initializer</span><span class="p">)</span> </code></pre></div> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.6.0/keras/src/initializers/constant_initializers.py#L77">[source]</a></span></p> <h3 id="ones-class"><code>Ones</code> class</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">initializers</span><span class="o">.</span><span class="n">Ones</span><span class="p">()</span> </code></pre></div> <p>Initializer that generates tensors initialized to 1.</p> <p>Also available via the shortcut function <code>ones</code>.</p> <p><strong>Examples</strong></p> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Standalone usage:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">Ones</span><span class="p">()</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">values</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span> </code></pre></div> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Usage in a Keras layer:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">Ones</span><span class="p">()</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">layer</span> <span class="o">=</span> <span class="n">Dense</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">kernel_initializer</span><span class="o">=</span><span class="n">initializer</span><span class="p">)</span> </code></pre></div> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.6.0/keras/src/initializers/random_initializers.py#L369">[source]</a></span></p> <h3 id="glorotnormal-class"><code>GlorotNormal</code> class</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">initializers</span><span class="o">.</span><span class="n">GlorotNormal</span><span class="p">(</span><span class="n">seed</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> </code></pre></div> <p>The Glorot normal initializer, also called Xavier normal initializer.</p> <p>Draws samples from a truncated normal distribution centered on 0 with <code>stddev = sqrt(2 / (fan_in + fan_out))</code> where <code>fan_in</code> is the number of input units in the weight tensor and <code>fan_out</code> is the number of output units in the weight tensor.</p> <p><strong>Examples</strong></p> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Standalone usage:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">GlorotNormal</span><span class="p">()</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">values</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span> </code></pre></div> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Usage in a Keras layer:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">GlorotNormal</span><span class="p">()</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">layer</span> <span class="o">=</span> <span class="n">Dense</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">kernel_initializer</span><span class="o">=</span><span class="n">initializer</span><span class="p">)</span> </code></pre></div> <p><strong>Arguments</strong></p> <ul> <li><strong>seed</strong>: A Python integer or instance of <code>keras.backend.SeedGenerator</code>. Used to make the behavior of the initializer deterministic. Note that an initializer seeded with an integer or <code>None</code> (unseeded) will produce the same random values across multiple calls. To get different random values across multiple calls, use as seed an instance of <code>keras.backend.SeedGenerator</code>.</li> </ul> <p><strong>Reference</strong></p> <ul> <li><a href="http://proceedings.mlr.press/v9/glorot10a.html">Glorot et al., 2010</a></li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.6.0/keras/src/initializers/random_initializers.py#L320">[source]</a></span></p> <h3 id="glorotuniform-class"><code>GlorotUniform</code> class</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">initializers</span><span class="o">.</span><span class="n">GlorotUniform</span><span class="p">(</span><span class="n">seed</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> </code></pre></div> <p>The Glorot uniform initializer, also called Xavier uniform initializer.</p> <p>Draws samples from a uniform distribution within <code>[-limit, limit]</code>, where <code>limit = sqrt(6 / (fan_in + fan_out))</code> (<code>fan_in</code> is the number of input units in the weight tensor and <code>fan_out</code> is the number of output units).</p> <p><strong>Examples</strong></p> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Standalone usage:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">GlorotUniform</span><span class="p">()</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">values</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span> </code></pre></div> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Usage in a Keras layer:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">GlorotUniform</span><span class="p">()</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">layer</span> <span class="o">=</span> <span class="n">Dense</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">kernel_initializer</span><span class="o">=</span><span class="n">initializer</span><span class="p">)</span> </code></pre></div> <p><strong>Arguments</strong></p> <ul> <li><strong>seed</strong>: A Python integer or instance of <code>keras.backend.SeedGenerator</code>. Used to make the behavior of the initializer deterministic. Note that an initializer seeded with an integer or <code>None</code> (unseeded) will produce the same random values across multiple calls. To get different random values across multiple calls, use as seed an instance of <code>keras.backend.SeedGenerator</code>.</li> </ul> <p><strong>Reference</strong></p> <ul> <li><a href="http://proceedings.mlr.press/v9/glorot10a.html">Glorot et al., 2010</a></li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.6.0/keras/src/initializers/random_initializers.py#L524">[source]</a></span></p> <h3 id="henormal-class"><code>HeNormal</code> class</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">initializers</span><span class="o">.</span><span class="n">HeNormal</span><span class="p">(</span><span class="n">seed</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> </code></pre></div> <p>He normal initializer.</p> <p>It draws samples from a truncated normal distribution centered on 0 with <code>stddev = sqrt(2 / fan_in)</code> where <code>fan_in</code> is the number of input units in the weight tensor.</p> <p><strong>Examples</strong></p> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Standalone usage:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">HeNormal</span><span class="p">()</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">values</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span> </code></pre></div> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Usage in a Keras layer:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">HeNormal</span><span class="p">()</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">layer</span> <span class="o">=</span> <span class="n">Dense</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">kernel_initializer</span><span class="o">=</span><span class="n">initializer</span><span class="p">)</span> </code></pre></div> <p><strong>Arguments</strong></p> <ul> <li><strong>seed</strong>: A Python integer or instance of <code>keras.backend.SeedGenerator</code>. Used to make the behavior of the initializer deterministic. Note that an initializer seeded with an integer or <code>None</code> (unseeded) will produce the same random values across multiple calls. To get different random values across multiple calls, use as seed an instance of <code>keras.backend.SeedGenerator</code>.</li> </ul> <p><strong>Reference</strong></p> <ul> <li><a href="https://arxiv.org/abs/1502.01852">He et al., 2015</a></li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.6.0/keras/src/initializers/random_initializers.py#L568">[source]</a></span></p> <h3 id="heuniform-class"><code>HeUniform</code> class</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">initializers</span><span class="o">.</span><span class="n">HeUniform</span><span class="p">(</span><span class="n">seed</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> </code></pre></div> <p>He uniform variance scaling initializer.</p> <p>Draws samples from a uniform distribution within <code>[-limit, limit]</code>, where <code>limit = sqrt(6 / fan_in)</code> (<code>fan_in</code> is the number of input units in the weight tensor).</p> <p><strong>Examples</strong></p> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Standalone usage:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">HeUniform</span><span class="p">()</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">values</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span> </code></pre></div> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Usage in a Keras layer:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">HeUniform</span><span class="p">()</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">layer</span> <span class="o">=</span> <span class="n">Dense</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">kernel_initializer</span><span class="o">=</span><span class="n">initializer</span><span class="p">)</span> </code></pre></div> <p><strong>Arguments</strong></p> <ul> <li><strong>seed</strong>: A Python integer or instance of <code>keras.backend.SeedGenerator</code>. Used to make the behavior of the initializer deterministic. Note that an initializer seeded with an integer or <code>None</code> (unseeded) will produce the same random values across multiple calls. To get different random values across multiple calls, use as seed an instance of <code>keras.backend.SeedGenerator</code>.</li> </ul> <p><strong>Reference</strong></p> <ul> <li><a href="https://arxiv.org/abs/1502.01852">He et al., 2015</a></li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.6.0/keras/src/initializers/random_initializers.py#L640">[source]</a></span></p> <h3 id="orthogonalinitializer-class"><code>OrthogonalInitializer</code> class</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">initializers</span><span class="o">.</span><span class="n">Orthogonal</span><span class="p">(</span><span class="n">gain</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">seed</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> </code></pre></div> <p>Initializer that generates an orthogonal matrix.</p> <p>If the shape of the tensor to initialize is two-dimensional, it is initialized with an orthogonal matrix obtained from the QR decomposition of a matrix of random numbers drawn from a normal distribution. If the matrix has fewer rows than columns then the output will have orthogonal rows. Otherwise, the output will have orthogonal columns.</p> <p>If the shape of the tensor to initialize is more than two-dimensional, a matrix of shape <code>(shape[0] * ... * shape[n - 2], shape[n - 1])</code> is initialized, where <code>n</code> is the length of the shape vector. The matrix is subsequently reshaped to give a tensor of the desired shape.</p> <p><strong>Examples</strong></p> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Standalone usage:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">initializers</span><span class="o">.</span><span class="n">Orthogonal</span><span class="p">()</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">values</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span> </code></pre></div> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Usage in a Keras layer:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">initializers</span><span class="o">.</span><span class="n">Orthogonal</span><span class="p">()</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">layer</span> <span class="o">=</span> <span class="n">keras</span><span class="o">.</span><span class="n">layers</span><span class="o">.</span><span class="n">Dense</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">kernel_initializer</span><span class="o">=</span><span class="n">initializer</span><span class="p">)</span> </code></pre></div> <p><strong>Arguments</strong></p> <ul> <li><strong>gain</strong>: Multiplicative factor to apply to the orthogonal matrix.</li> <li><strong>seed</strong>: A Python integer. Used to make the behavior of the initializer deterministic.</li> </ul> <p><strong>Reference</strong></p> <ul> <li><a href="https://openreview.net/forum?id=_wzZwKpTDF_9C">Saxe et al., 2014</a></li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.6.0/keras/src/initializers/constant_initializers.py#L8">[source]</a></span></p> <h3 id="constant-class"><code>Constant</code> class</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">initializers</span><span class="o">.</span><span class="n">Constant</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="mf">0.0</span><span class="p">)</span> </code></pre></div> <p>Initializer that generates tensors with constant values.</p> <p>Only scalar values are allowed. The constant value provided must be convertible to the dtype requested when calling the initializer.</p> <p><strong>Examples</strong></p> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Standalone usage:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">Constant</span><span class="p">(</span><span class="mf">10.</span><span class="p">)</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">values</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span> </code></pre></div> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Usage in a Keras layer:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">Constant</span><span class="p">(</span><span class="mf">10.</span><span class="p">)</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">layer</span> <span class="o">=</span> <span class="n">Dense</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">kernel_initializer</span><span class="o">=</span><span class="n">initializer</span><span class="p">)</span> </code></pre></div> <p><strong>Arguments</strong></p> <ul> <li><strong>value</strong>: A Python scalar.</li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.6.0/keras/src/initializers/random_initializers.py#L201">[source]</a></span></p> <h3 id="variancescaling-class"><code>VarianceScaling</code> class</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">initializers</span><span class="o">.</span><span class="n">VarianceScaling</span><span class="p">(</span> <span class="n">scale</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s2">&quot;fan_in&quot;</span><span class="p">,</span> <span class="n">distribution</span><span class="o">=</span><span class="s2">&quot;truncated_normal&quot;</span><span class="p">,</span> <span class="n">seed</span><span class="o">=</span><span class="kc">None</span> <span class="p">)</span> </code></pre></div> <p>Initializer that adapts its scale to the shape of its input tensors.</p> <p>With <code>distribution="truncated_normal" or "untruncated_normal"</code>, samples are drawn from a truncated/untruncated normal distribution with a mean of zero and a standard deviation (after truncation, if used) <code>stddev = sqrt(scale / n)</code>, where <code>n</code> is:</p> <ul> <li>number of input units in the weight tensor, if <code>mode="fan_in"</code></li> <li>number of output units, if <code>mode="fan_out"</code></li> <li>average of the numbers of input and output units, if <code>mode="fan_avg"</code></li> </ul> <p>With <code>distribution="uniform"</code>, samples are drawn from a uniform distribution within <code>[-limit, limit]</code>, where <code>limit = sqrt(3 * scale / n)</code>.</p> <p><strong>Examples</strong></p> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Standalone usage:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">VarianceScaling</span><span class="p">(</span> <span class="n">scale</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;fan_in&#39;</span><span class="p">,</span> <span class="n">distribution</span><span class="o">=</span><span class="s1">&#39;uniform&#39;</span><span class="p">)</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">values</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span> </code></pre></div> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Usage in a Keras layer:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">VarianceScaling</span><span class="p">(</span> <span class="n">scale</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;fan_in&#39;</span><span class="p">,</span> <span class="n">distribution</span><span class="o">=</span><span class="s1">&#39;uniform&#39;</span><span class="p">)</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">layer</span> <span class="o">=</span> <span class="n">Dense</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">kernel_initializer</span><span class="o">=</span><span class="n">initializer</span><span class="p">)</span> </code></pre></div> <p><strong>Arguments</strong></p> <ul> <li><strong>scale</strong>: Scaling factor (positive float).</li> <li><strong>mode</strong>: One of <code>"fan_in"</code>, <code>"fan_out"</code>, <code>"fan_avg"</code>.</li> <li><strong>distribution</strong>: Random distribution to use. One of <code>"truncated_normal"</code>, <code>"untruncated_normal"</code>, or <code>"uniform"</code>.</li> <li><strong>seed</strong>: A Python integer or instance of <code>keras.backend.SeedGenerator</code>. Used to make the behavior of the initializer deterministic. Note that an initializer seeded with an integer or <code>None</code> (unseeded) will produce the same random values across multiple calls. To get different random values across multiple calls, use as seed an instance of <code>keras.backend.SeedGenerator</code>.</li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.6.0/keras/src/initializers/random_initializers.py#L422">[source]</a></span></p> <h3 id="lecunnormal-class"><code>LecunNormal</code> class</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">initializers</span><span class="o">.</span><span class="n">LecunNormal</span><span class="p">(</span><span class="n">seed</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> </code></pre></div> <p>Lecun normal initializer.</p> <p>Initializers allow you to pre-specify an initialization strategy, encoded in the Initializer object, without knowing the shape and dtype of the variable being initialized.</p> <p>Draws samples from a truncated normal distribution centered on 0 with <code>stddev = sqrt(1 / fan_in)</code> where <code>fan_in</code> is the number of input units in the weight tensor.</p> <p><strong>Examples</strong></p> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Standalone usage:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">LecunNormal</span><span class="p">()</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">values</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span> </code></pre></div> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Usage in a Keras layer:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">LecunNormal</span><span class="p">()</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">layer</span> <span class="o">=</span> <span class="n">Dense</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">kernel_initializer</span><span class="o">=</span><span class="n">initializer</span><span class="p">)</span> </code></pre></div> <p><strong>Arguments</strong></p> <ul> <li><strong>seed</strong>: A Python integer or instance of <code>keras.backend.SeedGenerator</code>. Used to make the behavior of the initializer deterministic. Note that an initializer seeded with an integer or <code>None</code> (unseeded) will produce the same random values across multiple calls. To get different random values across multiple calls, use as seed an instance of <code>keras.backend.SeedGenerator</code>.</li> </ul> <p><strong>Reference</strong></p> <ul> <li><a href="https://arxiv.org/abs/1706.02515">Klambauer et al., 2017</a></li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.6.0/keras/src/initializers/random_initializers.py#L475">[source]</a></span></p> <h3 id="lecununiform-class"><code>LecunUniform</code> class</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">initializers</span><span class="o">.</span><span class="n">LecunUniform</span><span class="p">(</span><span class="n">seed</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> </code></pre></div> <p>Lecun uniform initializer.</p> <p>Draws samples from a uniform distribution within <code>[-limit, limit]</code>, where <code>limit = sqrt(3 / fan_in)</code> (<code>fan_in</code> is the number of input units in the weight tensor).</p> <p><strong>Examples</strong></p> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Standalone usage:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">LecunUniform</span><span class="p">()</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">values</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span> </code></pre></div> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Usage in a Keras layer:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">LecunUniform</span><span class="p">()</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">layer</span> <span class="o">=</span> <span class="n">Dense</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">kernel_initializer</span><span class="o">=</span><span class="n">initializer</span><span class="p">)</span> </code></pre></div> <p><strong>Arguments</strong></p> <ul> <li><strong>seed</strong>: A Python integer or instance of <code>keras.backend.SeedGenerator</code>. Used to make the behavior of the initializer deterministic. Note that an initializer seeded with an integer or <code>None</code> (unseeded) will produce the same random values across multiple calls. To get different random values across multiple calls, use as seed an instance of <code>keras.backend.SeedGenerator</code>.</li> </ul> <p><strong>Reference</strong></p> <ul> <li><a href="https://arxiv.org/abs/1706.02515">Klambauer et al., 2017</a></li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.6.0/keras/src/initializers/constant_initializers.py#L108">[source]</a></span></p> <h3 id="identity-class"><code>Identity</code> class</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">initializers</span><span class="o">.</span><span class="n">IdentityInitializer</span><span class="p">(</span><span class="n">gain</span><span class="o">=</span><span class="mf">1.0</span><span class="p">)</span> </code></pre></div> <p>Initializer that generates the identity matrix.</p> <p>Only usable for generating 2D matrices.</p> <p><strong>Examples</strong></p> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Standalone usage:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">Identity</span><span class="p">()</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">values</span> <span class="o">=</span> <span class="n">initializer</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span> </code></pre></div> <div class="codehilite"><pre><span></span><code><span class="o">&gt;&gt;&gt;</span> <span class="c1"># Usage in a Keras layer:</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">initializer</span> <span class="o">=</span> <span class="n">Identity</span><span class="p">()</span> <span class="o">&gt;&gt;&gt;</span> <span class="n">layer</span> <span class="o">=</span> <span class="n">Dense</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">kernel_initializer</span><span class="o">=</span><span class="n">initializer</span><span class="p">)</span> </code></pre></div> <p><strong>Arguments</strong></p> <ul> <li><strong>gain</strong>: Multiplicative factor to apply to the identity matrix.</li> </ul> <hr /> <h2 id="creating-custom-initializers">Creating custom initializers</h2> <h3 id="simple-callables">Simple callables</h3> <p>You can pass a custom callable as initializer. It must take the arguments <code>shape</code> (shape of the variable to initialize) and <code>dtype</code> (dtype of generated values):</p> <div class="codehilite"><pre><span></span><code><span class="k">def</span> <span class="nf">my_init</span><span class="p">(</span><span class="n">shape</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> <span class="k">return</span> <span class="n">keras</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="n">shape</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">dtype</span><span class="p">)</span> <span class="n">layer</span> <span class="o">=</span> <span class="n">Dense</span><span class="p">(</span><span class="mi">64</span><span class="p">,</span> <span class="n">kernel_initializer</span><span class="o">=</span><span class="n">my_init</span><span class="p">)</span> </code></pre></div> <h3 id="initializer-subclasses"><code>Initializer</code> subclasses</h3> <p>If you need to configure your initializer via various arguments (e.g. <code>stddev</code> argument in <code>RandomNormal</code>), you should implement it as a subclass of <code>keras.initializers.Initializer</code>.</p> <p>Initializers should implement a <code>__call__</code> method with the following signature:</p> <div class="codehilite"><pre><span></span><code><span class="k">def</span> <span class="fm">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">shape</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span><span class="err">`</span><span class="p">:</span> <span class="c1"># returns a tensor of shape `shape` and dtype `dtype`</span> <span class="c1"># containing values drawn from a distribution of your choice.</span> </code></pre></div> <p>Optionally, you an also implement the method <code>get_config</code> and the class method <code>from_config</code> in order to support serialization &ndash; just like with any Keras object.</p> <p>Here's a simple example: a random normal initializer.</p> <div class="codehilite"><pre><span></span><code><span class="k">class</span> <span class="nc">ExampleRandomNormal</span><span class="p">(</span><span class="n">keras</span><span class="o">.</span><span class="n">initializers</span><span class="o">.</span><span class="n">Initializer</span><span class="p">):</span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mean</span><span class="p">,</span> <span class="n">stddev</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">mean</span> <span class="o">=</span> <span class="n">mean</span> <span class="bp">self</span><span class="o">.</span><span class="n">stddev</span> <span class="o">=</span> <span class="n">stddev</span> <span class="k">def</span> <span class="fm">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">shape</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span><span class="err">`</span><span class="p">:</span> <span class="k">return</span> <span class="n">keras</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span> <span class="n">shape</span><span class="p">,</span> <span class="n">mean</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">mean</span><span class="p">,</span> <span class="n">stddev</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">stddev</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">dtype</span><span class="p">)</span> <span class="k">def</span> <span class="nf">get_config</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="c1"># To support serialization</span> <span class="k">return</span> <span class="p">{</span><span class="s1">&#39;mean&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">mean</span><span class="p">,</span> <span class="s1">&#39;stddev&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">stddev</span><span class="p">}</span> </code></pre></div> <p>Note that we don't have to implement <code>from_config</code> in the example above since the constructor arguments of the class the keys in the config returned by <code>get_config</code> are the same. In this case, the default <code>from_config</code> works fine.</p> </div> <div class='k-outline'> <div class='k-outline-depth-1'> <a href='#layer-weight-initializers'>Layer weight initializers</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#usage-of-initializers'>Usage of initializers</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#available-initializers'>Available initializers</a> </div> <div class='k-outline-depth-3'> <a href='#randomnormal-class'><code>RandomNormal</code> class</a> </div> <div class='k-outline-depth-3'> <a href='#randomuniform-class'><code>RandomUniform</code> class</a> </div> <div class='k-outline-depth-3'> <a href='#truncatednormal-class'><code>TruncatedNormal</code> class</a> </div> <div class='k-outline-depth-3'> <a href='#zeros-class'><code>Zeros</code> class</a> </div> <div class='k-outline-depth-3'> <a href='#ones-class'><code>Ones</code> class</a> </div> <div class='k-outline-depth-3'> <a href='#glorotnormal-class'><code>GlorotNormal</code> class</a> </div> <div class='k-outline-depth-3'> <a href='#glorotuniform-class'><code>GlorotUniform</code> class</a> </div> <div class='k-outline-depth-3'> <a href='#henormal-class'><code>HeNormal</code> class</a> </div> <div class='k-outline-depth-3'> <a href='#heuniform-class'><code>HeUniform</code> class</a> </div> <div class='k-outline-depth-3'> <a href='#orthogonalinitializer-class'><code>OrthogonalInitializer</code> class</a> </div> <div class='k-outline-depth-3'> <a href='#constant-class'><code>Constant</code> class</a> </div> <div class='k-outline-depth-3'> <a href='#variancescaling-class'><code>VarianceScaling</code> class</a> </div> <div class='k-outline-depth-3'> <a href='#lecunnormal-class'><code>LecunNormal</code> class</a> </div> <div class='k-outline-depth-3'> <a href='#lecununiform-class'><code>LecunUniform</code> class</a> </div> <div class='k-outline-depth-3'> <a href='#identity-class'><code>Identity</code> class</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#creating-custom-initializers'>Creating custom initializers</a> </div> <div class='k-outline-depth-3'> <a href='#simple-callables'>Simple callables</a> </div> <div class='k-outline-depth-3'> <a href='#initializer-subclasses'><code>Initializer</code> subclasses</a> </div> </div> </div> </div> </div> </body> <footer style="float: left; width: 100%; padding: 1em; border-top: solid 1px #bbb;"> <a href="https://policies.google.com/terms">Terms</a> | <a href="https://policies.google.com/privacy">Privacy</a> </footer> </html>

Pages: 1 2 3 4 5 6 7 8 9 10