CINXE.COM

AdamW

<!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/optimizers/adamw/" /> <!-- Social --> <meta property="og:title" content="Keras documentation: AdamW"> <meta property="og:image" content="https://keras.io/img/logo-k-keras-wb.png"> <meta name="twitter:title" content="Keras documentation: AdamW"> <meta name="twitter:image" content="https://keras.io/img/k-keras-social.png"> <meta name="twitter:card" content="summary"> <title>AdamW</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" href="/examples/" role="tab" aria-selected="">Code examples</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" href="/api/layers/">Layers API</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 active" href="/api/optimizers/">Optimizers</a> <a class="nav-sublink2" href="/api/optimizers/sgd/">SGD</a> <a class="nav-sublink2" href="/api/optimizers/rmsprop/">RMSprop</a> <a class="nav-sublink2" href="/api/optimizers/adam/">Adam</a> <a class="nav-sublink2 active" href="/api/optimizers/adamw/">AdamW</a> <a class="nav-sublink2" href="/api/optimizers/adadelta/">Adadelta</a> <a class="nav-sublink2" href="/api/optimizers/adagrad/">Adagrad</a> <a class="nav-sublink2" href="/api/optimizers/adamax/">Adamax</a> <a class="nav-sublink2" href="/api/optimizers/adafactor/">Adafactor</a> <a class="nav-sublink2" href="/api/optimizers/Nadam/">Nadam</a> <a class="nav-sublink2" href="/api/optimizers/ftrl/">Ftrl</a> <a class="nav-sublink2" href="/api/optimizers/lion/">Lion</a> <a class="nav-sublink2" href="/api/optimizers/lamb/">Lamb</a> <a class="nav-sublink2" href="/api/optimizers/loss_scale_optimizer/">Loss Scale Optimizer</a> <a class="nav-sublink2" href="/api/optimizers/learning_rate_schedules/">Learning rate schedules API</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-link" href="/2.18/api/" role="tab" aria-selected="">Keras 2 API documentation</a> <a class="nav-link" href="/keras_tuner/" role="tab" aria-selected="">KerasTuner: Hyperparam Tuning</a> <a class="nav-link" href="/keras_hub/" role="tab" aria-selected="">KerasHub: Pretrained Models</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/optimizers/'>Optimizers</a> / AdamW </div> <div class='k-content'> <h1 id="adamw">AdamW</h1> <p><span style="float:right;"><a href="https://github.com/keras-team/keras/tree/v3.8.0/keras/src/optimizers/adamw.py#L6">[source]</a></span></p> <h3 id="adamw-class"><code>AdamW</code> class</h3> <div class="codehilite"><pre><span></span><code><span class="n">keras</span><span class="o">.</span><span class="n">optimizers</span><span class="o">.</span><span class="n">AdamW</span><span class="p">(</span> <span class="n">learning_rate</span><span class="o">=</span><span class="mf">0.001</span><span class="p">,</span> <span class="n">weight_decay</span><span class="o">=</span><span class="mf">0.004</span><span class="p">,</span> <span class="n">beta_1</span><span class="o">=</span><span class="mf">0.9</span><span class="p">,</span> <span class="n">beta_2</span><span class="o">=</span><span class="mf">0.999</span><span class="p">,</span> <span class="n">epsilon</span><span class="o">=</span><span class="mf">1e-07</span><span class="p">,</span> <span class="n">amsgrad</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">clipnorm</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">clipvalue</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">global_clipnorm</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">use_ema</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">ema_momentum</span><span class="o">=</span><span class="mf">0.99</span><span class="p">,</span> <span class="n">ema_overwrite_frequency</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">loss_scale_factor</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">gradient_accumulation_steps</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;adamw&quot;</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span> <span class="p">)</span> </code></pre></div> <p>Optimizer that implements the AdamW algorithm.</p> <p>AdamW optimization is a stochastic gradient descent method that is based on adaptive estimation of first-order and second-order moments with an added method to decay weights per the techniques discussed in the paper, 'Decoupled Weight Decay Regularization' by <a href="https://arxiv.org/abs/1711.05101">Loshchilov, Hutter et al., 2019</a>.</p> <p>According to <a href="http://arxiv.org/abs/1412.6980">Kingma et al., 2014</a>, the underlying Adam method is "<em>computationally efficient, has little memory requirement, invariant to diagonal rescaling of gradients, and is well suited for problems that are large in terms of data/parameters</em>".</p> <p><strong>Arguments</strong></p> <ul> <li><strong>learning_rate</strong>: A float, a <a href="/api/optimizers/learning_rate_schedules/learning_rate_schedule#learningrateschedule-class"><code>keras.optimizers.schedules.LearningRateSchedule</code></a> instance, or a callable that takes no arguments and returns the actual value to use. The learning rate. Defaults to <code>0.001</code>.</li> <li><strong>beta_1</strong>: A float value or a constant float tensor, or a callable that takes no arguments and returns the actual value to use. The exponential decay rate for the 1st moment estimates. Defaults to <code>0.9</code>.</li> <li><strong>beta_2</strong>: A float value or a constant float tensor, or a callable that takes no arguments and returns the actual value to use. The exponential decay rate for the 2nd moment estimates. Defaults to <code>0.999</code>.</li> <li><strong>epsilon</strong>: A small constant for numerical stability. This epsilon is "epsilon hat" in the Kingma and Ba paper (in the formula just before Section 2.1), not the epsilon in Algorithm 1 of the paper. Defaults to 1e-7.</li> <li><strong>amsgrad</strong>: Boolean. Whether to apply AMSGrad variant of this algorithm from the paper "On the Convergence of Adam and beyond". Defaults to <code>False</code>.</li> <li><strong>name</strong>: String. The name to use for momentum accumulator weights created by the optimizer.</li> <li><strong>weight_decay</strong>: Float. If set, weight decay is applied.</li> <li><strong>clipnorm</strong>: Float. If set, the gradient of each weight is individually clipped so that its norm is no higher than this value.</li> <li><strong>clipvalue</strong>: Float. If set, the gradient of each weight is clipped to be no higher than this value.</li> <li><strong>global_clipnorm</strong>: Float. If set, the gradient of all weights is clipped so that their global norm is no higher than this value.</li> <li><strong>use_ema</strong>: Boolean, defaults to <code>False</code>. If <code>True</code>, exponential moving average (EMA) is applied. EMA consists of computing an exponential moving average of the weights of the model (as the weight values change after each training batch), and periodically overwriting the weights with their moving average.</li> <li><strong>ema_momentum</strong>: Float, defaults to 0.99. Only used if <code>use_ema=True</code>. This is the momentum to use when computing the EMA of the model's weights: <code>new_average = ema_momentum * old_average + (1 - ema_momentum) * current_variable_value</code>.</li> <li><strong>ema_overwrite_frequency</strong>: Int or None, defaults to None. Only used if <code>use_ema=True</code>. Every <code>ema_overwrite_frequency</code> steps of iterations, we overwrite the model variable by its moving average. If None, the optimizer does not overwrite model variables in the middle of training, and you need to explicitly overwrite the variables at the end of training by calling <code>optimizer.finalize_variable_values()</code> (which updates the model variables in-place). When using the built-in <code>fit()</code> training loop, this happens automatically after the last epoch, and you don't need to do anything.</li> <li><strong>loss_scale_factor</strong>: Float or <code>None</code>. If a float, the scale factor will be multiplied the loss before computing gradients, and the inverse of the scale factor will be multiplied by the gradients before updating variables. Useful for preventing underflow during mixed precision training. Alternately, <a href="/api/optimizers/loss_scale_optimizer#lossscaleoptimizer-class"><code>keras.optimizers.LossScaleOptimizer</code></a> will automatically set a loss scale factor.</li> <li><strong>gradient_accumulation_steps</strong>: Int or <code>None</code>. If an int, model &amp; optimizer variables will not be updated at every step; instead they will be updated every <code>gradient_accumulation_steps</code> steps, using the average value of the gradients since the last update. This is known as "gradient accumulation". This can be useful when your batch size is very small, in order to reduce gradient noise at each update step. EMA frequency will look at "accumulated" iterations value (optimizer steps // gradient_accumulation_steps). Learning rate schedules will look at "real" iterations value (optimizer steps).</li> </ul> <p><strong>References</strong></p> <ul> <li><a href="https://arxiv.org/abs/1711.05101">Loshchilov et al., 2019</a></li> <li><a href="http://arxiv.org/abs/1412.6980">Kingma et al., 2014</a> for <code>adam</code></li> <li><a href="https://openreview.net/pdf?id=ryQu7f-RZ">Reddi et al., 2018</a> for <code>amsgrad</code>.</li> </ul> <hr /> </div> <div class='k-outline'> <div class='k-outline-depth-1'> <a href='#adamw'>AdamW</a> </div> <div class='k-outline-depth-3'> <a href='#adamw-class'><code>AdamW</code> class</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