1<!DOCTYPE html> 2<html lang="en"> 3 4<head> 5 6 <meta charset="utf-8"> 7 <meta name="viewport" content="width=device-width, initial-scale=1"> 8 <meta name="description" content="Keras documentation: Getting started with Keras"> 9 <meta name="author" content="Keras Team"> 10 <link rel="shortcut icon" href="https://keras.io/img/favicon.ico"> 11 12 <link rel="canonical" href="https://keras.io/getting_started/" /> 13 14 15 <!-- Social --> 16 <meta property="og:title" content="Keras documentation: Getting started with Keras"> 17 <meta property="og:image" content="https://keras.io/img/logo-k-keras-wb.png"> 18 <meta name="twitter:title" content="Keras documentation: Getting started with Keras"> 19 <meta name="twitter:image" content="https://keras.io/img/k-keras-social.png"> 20 <meta name="twitter:card" content="summary"> 21 22 <title>Getting started with Keras</title> 23 24 <!-- Custom fonts for this template --> 25 <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600;700;800&display=swap" rel="stylesheet"> 26 <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;600;700;800&display=swap" rel="stylesheet"> 27 <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400&display=swap" rel="stylesheet"> 28 29 <!-- Bootstrap core CSS --> 30 <link href="/css/bootstrap.min.css" rel="stylesheet"> 31 32 <!-- Custom styles for this template --> 33 <link href="/css/docs.css?v=3" rel="stylesheet"> 34 <link href="/css/monokai.css" rel="stylesheet"> 35 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css" 36 integrity="sha384-n8MVd4RsNIU0tAv4ct0nTaAbDJwPJzDEaqSD1odI+WdtXRGWt2kTvGFasHpSy3SV" crossorigin="anonymous"> 37 38
38<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js" 39 integrity="sha384-XjKyOOlGwcjNTAIQHIpgOno0Hl1YQqzUOEleOLALmuqehneUG+vnGctmUb0ZY0l8" crossorigin="anonymous"> 40 </script>
40 41 42
42<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js" 43 integrity="sha384-+VBxd3r6XgURycqtZ117nYw44OOcIax56Z4dCRWbxyPt0Koah1uHoK0o4+/RRE05" crossorigin="anonymous" 44 onload='renderMathInElement(document.body, { 45 delimiters: [ 46 {left: "\\(", right: "\\)", display: false}, 47 {left: "\\[", right: "\\]", display: true}, 48 {left: "$$", right: "$$", display: true}, 49 {left: "$", right: "$", display: false} 50 ], 51 throwOnError: false 52 });'> 53 </script>
53 54 55 <!-- Google Tag Manager --> 56
56<script>
vendor: 335 bytes, lines 56-60
56(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': 57 new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], 58 j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= 59 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); 60 })(window,document,'script','dataLayer','
60GTM-5DNGF4N
vendor: 6 bytes, lines 60-61
60'); 61
61</script>
61 62
62<script>
vendor: 344 bytes, lines 62-67
62 63 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ 64 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), 65 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) 66 })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); 67 ga('create', '
67UA-175165319-128
vendor: 40 bytes, lines 67-69
67', 'auto'); 68 ga('send', 'pageview'); 69
69</script>
69 70 <!-- End Google Tag Manager --> 71 72
72<script async defer src="https://buttons.github.io/buttons.js"></script>
72 73 <link rel="preconnect" href="https://fonts.googleapis.com"> 74 <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> 75 76</head> 77 78<body> 79 <!-- Google Tag Manager (noscript) -->
vendor: 70 bytes, lines 79-80
79 80 <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=
80GTM-5DNGF4N
vendor: 87 bytes, lines 80-82
80" 81 height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> 82
82<!-- End Google Tag Manager (noscript) --> 83 84 <div class="k-page"> 85 <div class="hidden"> 86 87 88 89 90 None 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 </div> 108 <nav class="nav__container"> 109 <div class="nav__wrapper"> 110 <div class="nav__controls--mobile"> 111 <button class="nav__menu--button"><i class="icon--menu"></i></button> 112 <button class="nav__menu--close"><i class="icon--close"></i></button> 113 <a href="/"> 114 <img class="nav__logo nav__logo--mobile" src="/img/k-logo.png" /> 115 </a> 116 <button class="nav__search--mobile"> 117 <i class="icon__search--mobile"></i> 118 </button> 119 </div> 120 <form class="nav__search nav__search-form--mobile"> 121 <input 122 class="nav__search--input" 123 type="search" 124 placeholder="SEARCH" 125 aria-label="Search" 126 /> 127 <button class="nav__search--button" type="submit"> 128 <i class="icon--search"></i> 129 </button> 130 </form> 131 <div class="k-nav nav__mobile-menu" id="nav-menu"> 132 <!-- version with just the active item visible --> 133 <div class="nav flex-column nav-pills" role="tablist" aria-orientation="vertical"> 134 135 <a class="nav-link active" href="/getting_started/" role="tab" aria-selected="">Getting started</a> 136 137 138 <a class="nav-sublink" href="/getting_started/about/">About Keras</a> 139 140 141 <a class="nav-sublink" href="/getting_started/intro_to_keras_for_engineers/">Introduction to Keras for engineers</a> 142 143 144 <a class="nav-sublink" href="/getting_started/benchmarks/">Keras 3 benchmarks</a> 145 146 147 <a class="nav-sublink" href="/getting_started/ecosystem/">The Keras ecosystem</a> 148 149 150 <a class="nav-sublink" href="/getting_started/faq/">Frequently Asked Questions</a> 151 152 153 154 155 <a class="nav-link" href="/guides/" role="tab" aria-selected="">Developer guides</a> 156 157 158 <a class="nav-link" href="/examples/" role="tab" aria-selected="">Code examples</a> 159 160 161 <a class="nav-link" href="/api/" role="tab" aria-selected="">Keras 3 API documentation</a> 162 163 164 <a class="nav-link" href="/2/api/" role="tab" aria-selected="">Keras 2 API documentation</a> 165 166 167 <a class="nav-link" href="/keras_tuner/" role="tab" aria-selected="">KerasTuner: Hyperparam Tuning</a> 168 169 170 <a class="nav-link" href="/keras_hub/" role="tab" aria-selected="">KerasHub: Pretrained Models</a> 171 172 173 <a class="nav-link" href="/keras_rs/" role="tab" aria-selected="">KerasRS</a> 174 175 176 </div> 177 </div> 178 179 <a href="/"> 180 <img class="nav__logo nav__logo--desktop" src="/img/logo.png" alt="keras.io logo" /> 181 </a> 182 <div class="nav__menu"> 183 184 <ul class="nav__item--container"> 185 186 <li class="nav__item"> 187 <a class="nav__link nav__link--active" href="/getting_started/">Get started</a> 188 </li> 189 190 <li class="nav__item"> 191 <a class="nav__link" href="/guides/">Guides</a> 192 </li> 193 194 <li class="nav__item"> 195 <a class="nav__link" href="/api/">API</a> 196 </li> 197 198 <li class="nav__item"> 199 <a class="nav__link" href="/examples/">Examples</a> 200 </li> 201 202 <li class="nav__item"> 203 <a class="nav__link" href="/keras_hub/">Keras Hub</a> 204 </li> 205 206 <li class="nav__item"> 207 <a class="nav__link" href="/keras_rs/">Keras RS</a> 208 </li> 209 210 <li class="nav__item"> 211 <a class="nav__link" href="/keras_tuner/">Keras Tuner</a> 212 </li> 213 214 </ul> 215 216 <form class="nav__search"> 217 <input 218 class="nav__search--input" 219 type="search" 220 placeholder="SEARCH" 221 aria-label="Search" 222 /> 223 <button class="nav__search--button" type="submit"> 224 <i class="icon--search"></i> 225 </button> 226 </form> 227 </div> 228 </div> 229 </nav> 230 231 <div class="page__container flex__container"> 232 <div class="nav__side-nav" id="nav-menu"> 233 <div class="nav flex-column nav-pills" role="tablist" aria-orientation="vertical"> 234 235 236 <a class="nav-link active" href="/getting_started/" role="tab" aria-selected=""> 237 Getting started 238 </a> 239 240 <div class="nav-expanded-panel"> 241 242 <a class="nav-sublink" href="/getting_started/about/">About Keras</a> 243 244 245 <a class="nav-sublink" href="/getting_started/intro_to_keras_for_engineers/">Introduction to Keras for engineers</a> 246 247 248 <a class="nav-sublink" href="/getting_started/benchmarks/">Keras 3 benchmarks</a> 249 250 251 <a class="nav-sublink" href="/getting_started/ecosystem/">The Keras ecosystem</a> 252 253 254 <a class="nav-sublink" href="/getting_started/faq/">Frequently Asked Questions</a> 255 256 257 </div> 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 </div> 276 </div> 277 <div class="k-main"> 278 <div class='k-main-inner' id='k-main-id'>
279 <div class='k-content'> 280 <div class='k-location-slug'> 281 <span class="k-location-slug-pointer">âº</span> Getting started with Keras 282 </div> 283 <h1 id="getting-started-with-keras">Getting started with Keras</h1> 284<h2 id="learning-resources">Learning resources</h2> 285<p>Are you a machine learning engineer looking for a Keras introduction one-pager? 286Read our guide <a href="/getting_started/intro_to_keras_for_engineers/">Introduction to Keras for engineers</a>.</p> 287<p>Want to learn more about Keras 3 and its capabilities? See the <a href="/keras_3/">Keras 3 launch announcement</a>.</p> 288<p>Are you looking for detailed guides covering in-depth usage of different parts of the Keras API? 289Read our <a href="/guides/">Keras developer guides</a>.</p> 290<p>Are you looking for tutorials showing Keras in action across a wide range of use cases? 291See the <a href="/examples/">Keras code examples</a>: over 150 well-explained notebooks demonstrating Keras best practices 292in computer vision, natural language processing, and generative AI.</p> 293<hr /> 294<h2 id="installing-keras-3">Installing Keras 3</h2> 295<p>You can install Keras from PyPI via:</p> 296<div class="codehilite"><pre><span></span><code>pip install --upgrade keras 297</code></pre></div> 298 299<p>You can check your local Keras version number via:</p> 300<div class="codehilite"><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">keras</span> 301<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> 302</code></pre></div> 303 304<p>To use Keras 3, you will also need to install a backend framework – either JAX, TensorFlow, or PyTorch:</p> 305<ul> 306<li><a href="https://jax.readthedocs.io/en/latest/installation.html">Installing JAX</a></li> 307<li><a href="https://www.tensorflow.org/install">Installing TensorFlow</a></li> 308<li><a href="https://pytorch.org/get-started/locally/">Installing PyTorch</a></li> 309</ul> 310<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>. 311This step is not necessary for TensorFlow versions 2.16 onwards as starting in TensorFlow 2.16, it will install Keras 3 by default.</p> 312<h3 id="installing-kerascv-and-kerashub">Installing KerasCV and KerasHub</h3> 313<p>KerasCV and KerasHub can be installed via pip:</p> 314<div class="codehilite"><pre><span></span><code>pip install --upgrade keras-cv 315pip install --upgrade keras-hub 316pip install --upgrade keras 317</code></pre></div> 318 319<hr /> 320<h2 id="configuring-your-backend">Configuring your backend</h2> 321<p>You can export the environment variable <code>KERAS_BACKEND</code> 322or you can edit your local config file at <code>~/.keras/keras.json</code> to configure your backend. 323Available backend options are: <code>"jax"</code>, <code>"tensorflow"</code>, <code>"torch"</code>. Example:</p> 324<div class="codehilite"><pre><span></span><code>export KERAS_BACKEND="jax" 325</code></pre></div> 326 327<p>In Colab, you can do:</p> 328<div class="codehilite"><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">os</span> 329<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> 330<span class="kn">import</span><span class="w"> </span><span class="nn">keras</span> 331</code></pre></div> 332 333<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> 334<h3 id="gpu-dependencies">GPU dependencies</h3> 335<h4 id="colab-or-kaggle">Colab or Kaggle</h4> 336<p>If you are running on Colab or Kaggle, the GPU should already be configured, with the correct CUDA version. 337Installing a newer version of CUDA on Colab or Kaggle is typically not possible. Even though pip installers exist, 338they rely on a pre-installed NVIDIA driver and there is no way to update the driver on Colab or Kaggle.</p> 339<h4 id="universal-gpu-environment">Universal GPU environment</h4> 340<p>If you want to attempt to create a "universal environment" where any backend can use the GPU, we recommend following 341<a href="https://colab.sandbox.google.com/drive/13cpd3wCwEHpsmypY9o6XB6rXgBm5oSxu">the dependency versions used by Colab</a> 342(which seeks to solve this exact problem). You can install the CUDA driver <a href="https://developer.nvidia.com/cuda-downloads">from here</a>, 343then pip install backends by following their respective CUDA installation instructions: 344<a href="https://jax.readthedocs.io/en/latest/installation.html">Installing JAX</a>, 345<a href="https://www.tensorflow.org/install">Installing TensorFlow</a>, 346<a href="https://pytorch.org/get-started/locally/">Installing PyTorch</a></p> 347<h4 id="most-stable-gpu-environment">Most stable GPU environment</h4> 348<p>This setup is recommended if you are a Keras contributor and are running Keras tests. It installs all backends but only 349gives GPU access to one backend at a time, avoiding potentially conflicting dependency requirements between backends. 350You can use the following backend-specific requirements files:</p> 351<ul> 352<li><a href="https://github.com/keras-team/keras/blob/master/requirements-jax-cuda.txt">requirements-jax-cuda.txt</a></li> 353<li><a href="https://github.com/keras-team/keras/blob/master/requirements-tensorflow-cuda.txt">requirements-tensorflow-cuda.txt</a></li> 354<li><a href="https://github.com/keras-team/keras/blob/master/requirements-torch-cuda.txt">requirements-torch-cuda.txt</a></li> 355</ul> 356<p>These install all CUDA-enabled dependencies via pip. They expect a NVIDIA driver to be preinstalled. 357We recommend a clean python environment for each backend to avoid CUDA version mismatches. 358As an example, here is how to create a JAX GPU environment with <a href="https://docs.conda.io/en/latest/">Conda</a>:</p> 359<div class="codehilite"><pre><span></span><code>conda create -y -n keras-jax python=3.11 360conda activate keras-jax 361pip install -r requirements-jax-cuda.txt 362pip install --upgrade keras 363</code></pre></div> 364 365<hr /> 366<h2 id="tensorflow--keras-2-backwards-compatibility">TensorFlow + Keras 2 backwards compatibility</h2> 367<p>From TensorFlow 2.0 to TensorFlow 2.15 (included), doing <code>pip install tensorflow</code> will also 368install the corresponding version of Keras 2 –
368 for instance, <code>pip install tensorflow==2.14.0</code> will 369install <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> 370(the <a href="https://www.tensorflow.org/api_docs/python/tf/keras"><code>tf.keras</code></a> namespace).</p> 371<p>Starting with TensorFlow 2.16, doing <code>pip install tensorflow</code> will install Keras 3. When you have TensorFlow >= 2.16 372and 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> 373<p>Meanwhile, the legacy Keras 2 package is still being released regularly and is available on PyPI as <code>tf_keras</code> 374(or equivalently <code>tf-keras</code> – note that <code>-</code> and <code>_</code> are equivalent in PyPI package names). 375To 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> 376<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 377so 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> 378<ol> 379<li>Make sure to install <code>tf_keras</code>. Note that TensorFlow does not install it by default.</li> 380<li>Export the environment variable <code>TF_USE_LEGACY_KERAS=1</code>.</li> 381</ol> 382<p>There are several ways to export the environment variable:</p> 383<ol> 384<li>You can simply run the shell command <code>export TF_USE_LEGACY_KERAS=1</code> before launching the Python interpreter.</li> 385<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> 386<li>You can start your Python script with:</li> 387</ol> 388<div class="codehilite"><pre><span></span><code><span class="kn">import</span><span class="w"> </span><span class="nn">os</span> 389<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> 390</code></pre></div> 391 392<p>These lines would need to be before any <code>import tensorflow</code> statement.</p> 393<hr /> 394<h2 id="compatibility-matrix">Compatibility matrix</h2> 395<h3 id="jax-compatibility">JAX compatibility</h3> 396<p>The following Keras + JAX versions are compatible with each other:</p> 397<ul> 398<li><code>jax==0.4.20</code> & <code>keras~=3.0</code></li> 399</ul> 400<h3 id="tensorflow-compatibility">TensorFlow compatibility</h3> 401<p>The following Keras + TensorFlow versions are compatible with each other:</p> 402<p>To use Keras 2:</p> 403<ul> 404<li><code>tensorflow~=2.13.0</code> & <code>keras~=2.13.0</code></li> 405<li><code>tensorflow~=2.14.0</code> & <code>keras~=2.14.0</code></li> 406<li><code>tensorflow~=2.15.0</code> & <code>keras~=2.15.0</code></li> 407</ul> 408<p>To use Keras 3:</p> 409<ul> 410<li><code>tensorflow~=2.16.1</code> & <code>keras~=3.0</code></li> 411</ul> 412<h3 id="pytorch-compatibility">PyTorch compatibility</h3> 413<p>The following Keras + PyTorch versions are compatible with each other:</p> 414<ul> 415<li><code>torch~=2.1.0</code> & <code>keras~=3.0</code></li> 416</ul> 417 </div> 418 419 <div class='k-outline'> 420 421 <div class='k-outline-depth-1'> 422 <a href='#getting-started-with-keras'>Getting started with Keras</a> 423 </div> 424 425 <div class='k-outline-depth-2'> 426 <a href='#learning-resources'>Learning resources</a> 427 </div> 428 429 <div class='k-outline-depth-2'> 430 <a href='#installing-keras-3'>Installing Keras 3</a> 431 </div> 432 433 <div class='k-outline-depth-3'> 434 <a href='#installing-kerascv-and-kerashub'>Installing KerasCV and KerasHub</a> 435 </div> 436 437 <div class='k-outline-depth-2'> 438 <a href='#configuring-your-backend'>Configuring your backend</a> 439 </div> 440 441 <div class='k-outline-depth-3'> 442 <a href='#gpu-dependencies'>
442GPU dependencies</a> 443 </div> 444 445 <div class='k-outline-depth-2'> 446 <a href='#tensorflow--keras-2-backwards-compatibility'>TensorFlow + Keras 2 backwards compatibility</a> 447 </div> 448 449 <div class='k-outline-depth-2'> 450 <a href='#compatibility-matrix'>Compatibility matrix</a> 451 </div> 452 453 <div class='k-outline-depth-3'> 454 <a href='#jax-compatibility'>JAX compatibility</a> 455 </div> 456 457 <div class='k-outline-depth-3'> 458 <a href='#tensorflow-compatibility'>TensorFlow compatibility</a> 459 </div> 460 461 <div class='k-outline-depth-3'> 462 <a href='#pytorch-compatibility'>PyTorch compatibility</a> 463 </div> 464 465 </div> 466 467</div> 468 </div> 469 </div> 470 </div> 471 <footer> 472 <div class="footer__container"> 473 <a href="https://policies.google.com/terms">Terms</a> 474 <div>|</div> 475 <a href="https://policies.google.com/privacy">Privacy</a> 476 </div> 477 </footer> 478
478<script src="/js/index.js"></script>
478 479</body> 480 481</html>
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.