CINXE.COM
Getting started with Keras
<!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/getting_started/" /> <!-- Social --> <meta property="og:title" content="Keras documentation: Getting started with Keras"> <meta property="og:image" content="https://keras.io/img/logo-k-keras-wb.png"> <meta name="twitter:title" content="Keras documentation: Getting started with Keras"> <meta name="twitter:image" content="https://keras.io/img/k-keras-social.png"> <meta name="twitter:card" content="summary"> <title>Getting started with Keras</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 active" href="/getting_started/" role="tab" aria-selected="">Getting started</a> <a class="nav-sublink" href="/getting_started/intro_to_keras_for_engineers/">Introduction to Keras for engineers</a> <a class="nav-sublink" href="/getting_started/benchmarks/">Keras 3 benchmarks</a> <a class="nav-sublink" href="/getting_started/ecosystem/">The Keras ecosystem</a> <a class="nav-sublink" href="/getting_started/faq/">Frequently Asked Questions</a> <a class="nav-link" href="/guides/" role="tab" aria-selected="">Developer guides</a> <a class="nav-link" href="/api/" role="tab" aria-selected="">Keras 3 API documentation</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> Getting started with Keras </div> <div class='k-content'> <h1 id="getting-started-with-keras">Getting started with Keras</h1> <h2 id="learning-resources">Learning resources</h2> <p>Are you a machine learning engineer looking for a Keras introduction one-pager? Read our guide <a href="/getting_started/intro_to_keras_for_engineers/">Introduction to Keras for engineers</a>.</p> <p>Want to learn more about Keras 3 and its capabilities? See the <a href="/keras_3/">Keras 3 launch announcement</a>.</p> <p>Are you looking for detailed guides covering in-depth usage of different parts of the Keras API? Read our <a href="/guides/">Keras developer guides</a>.</p> <p>Are you looking for tutorials showing Keras in action across a wide range of use cases? See the <a href="/examples/">Keras code examples</a>: over 150 well-explained notebooks demonstrating Keras best practices in computer vision, natural language processing, and generative AI.</p> <hr /> <h2 id="installing-keras-3">Installing Keras 3</h2> <p>You can install Keras from PyPI via:</p> <div class="codehilite"><pre><span></span><code>pip install --upgrade keras </code></pre></div> <p>You can check your local Keras version number via:</p> <div class="codehilite"><pre><span></span><code><span class="kn">import</span> <span class="nn">keras</span> <span class="nb">print</span><span class="p">(</span><span class="n">keras</span><span class="o">.</span><span class="n">__version__</span><span class="p">)</span> </code></pre></div> <p>To use Keras 3, you will also need to install a backend framework – either JAX, TensorFlow, or PyTorch:</p> <ul> <li><a href="https://jax.readthedocs.io/en/latest/installation.html">Installing JAX</a></li> <li><a href="https://www.tensorflow.org/install">Installing TensorFlow</a></li> <li><a href="https://pytorch.org/get-started/locally/">Installing PyTorch</a></li> </ul> <p>If you install TensorFlow 2.15, you should reinstall Keras 3 afterwards. The cause is that <code>tensorflow==2.15</code> will overwrite your Keras installation with <code>keras==2.15</code>. This step is not necessary for TensorFlow versions 2.16 onwards as starting in TensorFlow 2.16, it will install Keras 3 by default.</p> <h3 id="installing-kerascv-and-kerashub">Installing KerasCV and KerasHub</h3> <p>KerasCV and KerasHub can be installed via pip:</p> <div class="codehilite"><pre><span></span><code>pip install --upgrade keras-cv pip install --upgrade keras-hub pip install --upgrade keras </code></pre></div> <hr /> <h2 id="configuring-your-backend">Configuring your backend</h2> <p>You can export the environment variable <code>KERAS_BACKEND</code> or you can edit your local config file at <code>~/.keras/keras.json</code> to configure your backend. Available backend options are: <code>"jax"</code>, <code>"tensorflow"</code>, <code>"torch"</code>. Example:</p> <div class="codehilite"><pre><span></span><code>export KERAS_BACKEND="jax" </code></pre></div> <p>In Colab, you can do:</p> <div class="codehilite"><pre><span></span><code><span class="kn">import</span> <span class="nn">os</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">"KERAS_BACKEND"</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"jax"</span> <span class="kn">import</span> <span class="nn">keras</span> </code></pre></div> <p><strong>Note:</strong> The backend must be configured before importing Keras, and the backend cannot be changed after the package has been imported.</p> <h3 id="gpu-dependencies">GPU dependencies</h3> <h4 id="colab-or-kaggle">Colab or Kaggle</h4> <p>If you are running on Colab or Kaggle, the GPU should already be configured, with the correct CUDA version. Installing a newer version of CUDA on Colab or Kaggle is typically not possible. Even though pip installers exist, they rely on a pre-installed NVIDIA driver and there is no way to update the driver on Colab or Kaggle.</p> <h4 id="universal-gpu-environment">Universal GPU environment</h4> <p>If you want to attempt to create a "universal environment" where any backend can use the GPU, we recommend following <a href="https://colab.sandbox.google.com/drive/13cpd3wCwEHpsmypY9o6XB6rXgBm5oSxu">the dependency versions used by Colab</a> (which seeks to solve this exact problem). You can install the CUDA driver <a href="https://developer.nvidia.com/cuda-downloads">from here</a>, then pip install backends by following their respective CUDA installation instructions: <a href="https://jax.readthedocs.io/en/latest/installation.html">Installing JAX</a>, <a href="https://www.tensorflow.org/install">Installing TensorFlow</a>, <a href="https://pytorch.org/get-started/locally/">Installing PyTorch</a></p> <h4 id="most-stable-gpu-environment">Most stable GPU environment</h4> <p>This setup is recommended if you are a Keras contributor and are running Keras tests. It installs all backends but only gives GPU access to one backend at a time, avoiding potentially conflicting dependency requirements between backends. You can use the following backend-specific requirements files:</p> <ul> <li><a href="https://github.com/keras-team/keras/blob/master/requirements-jax-cuda.txt">requirements-jax-cuda.txt</a></li> <li><a href="https://github.com/keras-team/keras/blob/master/requirements-tensorflow-cuda.txt">requirements-tensorflow-cuda.txt</a></li> <li><a href="https://github.com/keras-team/keras/blob/master/requirements-torch-cuda.txt">requirements-torch-cuda.txt</a></li> </ul> <p>These install all CUDA-enabled dependencies via pip. They expect a NVIDIA driver to be preinstalled. We recommend a clean python environment for each backend to avoid CUDA version mismatches. As an example, here is how to create a JAX GPU environment with <a href="https://docs.conda.io/en/latest/">Conda</a>:</p> <div class="codehilite"><pre><span></span><code>conda create -y -n keras-jax python=3.10 conda activate keras-jax pip install -r requirements-jax-cuda.txt pip install --upgrade keras </code></pre></div> <hr /> <h2 id="tensorflow--keras-2-backwards-compatibility">TensorFlow + Keras 2 backwards compatibility</h2> <p>From TensorFlow 2.0 to TensorFlow 2.15 (included), doing <code>pip install tensorflow</code> will also install the corresponding version of Keras 2 – for instance, <code>pip install tensorflow==2.14.0</code> will install <code>keras==2.14.0</code>. That version of Keras is then available via both <code>import keras</code> and <code>from tensorflow import keras</code> (the <a href="https://www.tensorflow.org/api_docs/python/tf/keras"><code>tf.keras</code></a> namespace).</p> <p>Starting with TensorFlow 2.16, doing <code>pip install tensorflow</code> will install Keras 3. When you have TensorFlow >= 2.16 and Keras 3, then by default <code>from tensorflow import keras</code> (<a href="https://www.tensorflow.org/api_docs/python/tf/keras"><code>tf.keras</code></a>) will be Keras 3.</p> <p>Meanwhile, the legacy Keras 2 package is still being released regularly and is available on PyPI as <code>tf_keras</code> (or equivalently <code>tf-keras</code> – note that <code>-</code> and <code>_</code> are equivalent in PyPI package names). To use it, you can install it via <code>pip install tf_keras</code> then import it via <code>import tf_keras as keras</code>.</p> <p>Should you want <a href="https://www.tensorflow.org/api_docs/python/tf/keras"><code>tf.keras</code></a> to stay on Keras 2 after upgrading to TensorFlow 2.16+, you can configure your TensorFlow installation so that <a href="https://www.tensorflow.org/api_docs/python/tf/keras"><code>tf.keras</code></a> points to <code>tf_keras</code>. To achieve this:</p> <ol> <li>Make sure to install <code>tf_keras</code>. Note that TensorFlow does not install it by default.</li> <li>Export the environment variable <code>TF_USE_LEGACY_KERAS=1</code>.</li> </ol> <p>There are several ways to export the environment variable:</p> <ol> <li>You can simply run the shell command <code>export TF_USE_LEGACY_KERAS=1</code> before launching the Python interpreter.</li> <li>You can add <code>export TF_USE_LEGACY_KERAS=1</code> to your <code>.bashrc</code> file. That way the variable will still be exported when you restart your shell.</li> <li>You can start your Python script with:</li> </ol> <div class="codehilite"><pre><span></span><code><span class="kn">import</span> <span class="nn">os</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">"TF_USE_LEGACY_KERAS"</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"1"</span> </code></pre></div> <p>These lines would need to be before any <code>import tensorflow</code> statement.</p> <hr /> <h2 id="compatibility-matrix">Compatibility matrix</h2> <h3 id="jax-compatibility">JAX compatibility</h3> <p>The following Keras + JAX versions are compatible with each other:</p> <ul> <li><code>jax==0.4.20</code> & <code>keras~=3.0</code></li> </ul> <h3 id="tensorflow-compatibility">TensorFlow compatibility</h3> <p>The following Keras + TensorFlow versions are compatible with each other:</p> <p>To use Keras 2:</p> <ul> <li><code>tensorflow~=2.13.0</code> & <code>keras~=2.13.0</code></li> <li><code>tensorflow~=2.14.0</code> & <code>keras~=2.14.0</code></li> <li><code>tensorflow~=2.15.0</code> & <code>keras~=2.15.0</code></li> </ul> <p>To use Keras 3:</p> <ul> <li><code>tensorflow~=2.16.1</code> & <code>keras~=3.0</code></li> </ul> <h3 id="pytorch-compatibility">PyTorch compatibility</h3> <p>The following Keras + PyTorch versions are compatible with each other:</p> <ul> <li><code>torch~=2.1.0</code> & <code>keras~=3.0</code></li> </ul> </div> <div class='k-outline'> <div class='k-outline-depth-1'> <a href='#getting-started-with-keras'>Getting started with Keras</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#learning-resources'>Learning resources</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#installing-keras-3'>Installing Keras 3</a> </div> <div class='k-outline-depth-3'> <a href='#installing-kerascv-and-kerashub'>Installing KerasCV and KerasHub</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#configuring-your-backend'>Configuring your backend</a> </div> <div class='k-outline-depth-3'> <a href='#gpu-dependencies'>GPU dependencies</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#tensorflow--keras-2-backwards-compatibility'>TensorFlow + Keras 2 backwards compatibility</a> </div> <div class='k-outline-depth-2'> ◆ <a href='#compatibility-matrix'>Compatibility matrix</a> </div> <div class='k-outline-depth-3'> <a href='#jax-compatibility'>JAX compatibility</a> </div> <div class='k-outline-depth-3'> <a href='#tensorflow-compatibility'>TensorFlow compatibility</a> </div> <div class='k-outline-depth-3'> <a href='#pytorch-compatibility'>PyTorch compatibility</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>