CINXE.COM
Python & NumPy utilities
<!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/2.18/api/utils/python_utils/" /> <!-- Social --> <meta property="og:title" content="Keras documentation: Python & NumPy utilities"> <meta property="og:image" content="https://keras.io/img/logo-k-keras-wb.png"> <meta name="twitter:title" content="Keras documentation: Python & NumPy utilities"> <meta name="twitter:image" content="https://keras.io/img/k-keras-social.png"> <meta name="twitter:card" content="summary"> <title>Python & NumPy utilities</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="/api/" role="tab" aria-selected="">Keras 3 API documentation</a> <a class="nav-link active" href="/2.18/api/" role="tab" aria-selected="">Keras 2 API documentation</a> <a class="nav-sublink" href="/2.18/api/models/">Models API</a> <a class="nav-sublink" href="/2.18/api/layers/">Layers API</a> <a class="nav-sublink" href="/2.18/api/callbacks/">Callbacks API</a> <a class="nav-sublink" href="/2.18/api/optimizers/">Optimizers</a> <a class="nav-sublink" href="/2.18/api/metrics/">Metrics</a> <a class="nav-sublink" href="/2.18/api/losses/">Losses</a> <a class="nav-sublink" href="/2.18/api/data_loading/">Data loading</a> <a class="nav-sublink" href="/2.18/api/datasets/">Built-in small datasets</a> <a class="nav-sublink" href="/2.18/api/applications/">Keras Applications</a> <a class="nav-sublink" href="/2.18/api/mixed_precision/">Mixed precision</a> <a class="nav-sublink active" href="/2.18/api/utils/">Utilities</a> <a class="nav-sublink2" href="/2.18/api/utils/model_plotting_utils/">Model plotting utilities</a> <a class="nav-sublink2" href="/2.18/api/utils/feature_space/">Structured data preprocessing utilities</a> <a class="nav-sublink2 active" href="/2.18/api/utils/python_utils/">Python & NumPy utilities</a> <a class="nav-sublink2" href="/2.18/api/utils/backend_utils/">Backend utilities</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='/2.18/api/'>Keras 2 API documentation</a> / <a href='/2.18/api/utils/'>Utilities</a> / Python & NumPy utilities </div> <div class='k-content'> <h1 id="python-amp-numpy-utilities">Python & NumPy utilities</h1> <p><span style="float:right;"><a href="https://github.com/keras-team/tf-keras/tree/v2.18.0/tf_keras/utils/tf_utils.py#L39">[source]</a></span></p> <h3 id="setrandomseed-function"><code>set_random_seed</code> function</h3> <div class="codehilite"><pre><span></span><code><span class="n">tf_keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">set_random_seed</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span> </code></pre></div> <p>Sets all random seeds for the program (Python, NumPy, and TensorFlow).</p> <p>You can use this utility to make almost any TF-Keras program fully deterministic. Some limitations apply in cases where network communications are involved (e.g. parameter server distribution), which creates additional sources of randomness, or when certain non-deterministic cuDNN ops are involved.</p> <p>Calling this utility is equivalent to the following:</p> <div class="codehilite"><pre><span></span><code><span class="kn">import</span> <span class="nn">random</span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span> <span class="kn">import</span> <span class="nn">tensorflow</span> <span class="k">as</span> <span class="nn">tf</span> <span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span> <span class="n">tf</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">set_seed</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span> </code></pre></div> <p><strong>Arguments</strong></p> <ul> <li><strong>seed</strong>: Integer, the random seed to use.</li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/tf-keras/tree/v2.18.0/tf_keras/utils/dataset_utils.py#L32">[source]</a></span></p> <h3 id="splitdataset-function"><code>split_dataset</code> function</h3> <div class="codehilite"><pre><span></span><code><span class="n">tf_keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">split_dataset</span><span class="p">(</span> <span class="n">dataset</span><span class="p">,</span> <span class="n">left_size</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">right_size</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">shuffle</span><span class="o">=</span><span class="kc">False</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>Split a dataset into a left half and a right half (e.g. train / test).</p> <p><strong>Arguments</strong></p> <ul> <li><strong>dataset</strong>: A <a href="https://www.tensorflow.org/api_docs/python/tf/data/Dataset"><code>tf.data.Dataset</code></a> object, or a list/tuple of arrays with the same length.</li> <li><strong>left_size</strong>: If float (in the range <code>[0, 1]</code>), it signifies the fraction of the data to pack in the left dataset. If integer, it signifies the number of samples to pack in the left dataset. If <code>None</code>, it uses the complement to <code>right_size</code>. Defaults to <code>None</code>.</li> <li><strong>right_size</strong>: If float (in the range <code>[0, 1]</code>), it signifies the fraction of the data to pack in the right dataset. If integer, it signifies the number of samples to pack in the right dataset. If <code>None</code>, it uses the complement to <code>left_size</code>. Defaults to <code>None</code>.</li> <li><strong>shuffle</strong>: Boolean, whether to shuffle the data before splitting it.</li> <li><strong>seed</strong>: A random seed for shuffling.</li> </ul> <p><strong>Returns</strong></p> <ul> <li><strong>A tuple of two <a href="https://www.tensorflow.org/api_docs/python/tf/data/Dataset"><code>tf.data.Dataset</code></a> objects</strong>: the left and right splits.</li> </ul> <p><strong>Example</strong></p> <div class="codehilite"><pre><span></span><code><span class="o">>>></span> <span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">(</span><span class="n">size</span><span class="o">=</span><span class="p">(</span><span class="mi">1000</span><span class="p">,</span> <span class="mi">4</span><span class="p">))</span> <span class="o">>>></span> <span class="n">left_ds</span><span class="p">,</span> <span class="n">right_ds</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">split_dataset</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">left_size</span><span class="o">=</span><span class="mf">0.8</span><span class="p">)</span> <span class="o">>>></span> <span class="nb">int</span><span class="p">(</span><span class="n">left_ds</span><span class="o">.</span><span class="n">cardinality</span><span class="p">())</span> <span class="mi">800</span> <span class="o">>>></span> <span class="nb">int</span><span class="p">(</span><span class="n">right_ds</span><span class="o">.</span><span class="n">cardinality</span><span class="p">())</span> <span class="mi">200</span> </code></pre></div> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/tf-keras/tree/v2.18.0/tf_keras/utils/data_utils.py#L193">[source]</a></span></p> <h3 id="getfile-function"><code>get_file</code> function</h3> <div class="codehilite"><pre><span></span><code><span class="n">tf_keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">get_file</span><span class="p">(</span> <span class="n">fname</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">origin</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">untar</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">md5_hash</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">file_hash</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">cache_subdir</span><span class="o">=</span><span class="s2">"datasets"</span><span class="p">,</span> <span class="n">hash_algorithm</span><span class="o">=</span><span class="s2">"auto"</span><span class="p">,</span> <span class="n">extract</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">archive_format</span><span class="o">=</span><span class="s2">"auto"</span><span class="p">,</span> <span class="n">cache_dir</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="p">)</span> </code></pre></div> <p>Downloads a file from a URL if it not already in the cache.</p> <p>By default the file at the url <code>origin</code> is downloaded to the cache_dir <code>~/.keras</code>, placed in the cache_subdir <code>datasets</code>, and given the filename <code>fname</code>. The final location of a file <code>example.txt</code> would therefore be <code>~/.keras/datasets/example.txt</code>.</p> <p>Files in tar, tar.gz, tar.bz, and zip formats can also be extracted. Passing a hash will verify the file after download. The command line programs <code>shasum</code> and <code>sha256sum</code> can compute the hash.</p> <p><strong>Example</strong></p> <div class="codehilite"><pre><span></span><code><span class="n">path_to_downloaded_file</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">get_file</span><span class="p">(</span> <span class="n">origin</span><span class="o">=</span><span class="s2">"https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"</span><span class="p">,</span> <span class="n">extract</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="p">)</span> </code></pre></div> <p><strong>Arguments</strong></p> <ul> <li><strong>fname</strong>: Name of the file. If an absolute path <code>/path/to/file.txt</code> is specified the file will be saved at that location. If <code>None</code>, the name of the file at <code>origin</code> will be used.</li> <li><strong>origin</strong>: Original URL of the file.</li> <li><strong>untar</strong>: Deprecated in favor of <code>extract</code> argument. boolean, whether the file should be decompressed</li> <li><strong>md5_hash</strong>: Deprecated in favor of <code>file_hash</code> argument. md5 hash of the file for verification</li> <li><strong>file_hash</strong>: The expected hash string of the file after download. The sha256 and md5 hash algorithms are both supported.</li> <li><strong>cache_subdir</strong>: Subdirectory under the TF-Keras cache dir where the file is saved. If an absolute path <code>/path/to/folder</code> is specified the file will be saved at that location.</li> <li><strong>hash_algorithm</strong>: Select the hash algorithm to verify the file. options are <code>'md5'</code>, <code>'sha256'</code>, and <code>'auto'</code>. The default 'auto' detects the hash algorithm in use.</li> <li><strong>extract</strong>: True tries extracting the file as an Archive, like tar or zip.</li> <li><strong>archive_format</strong>: Archive format to try for extracting the file. Options are <code>'auto'</code>, <code>'tar'</code>, <code>'zip'</code>, and <code>None</code>. <code>'tar'</code> includes tar, tar.gz, and tar.bz files. The default <code>'auto'</code> corresponds to <code>['tar', 'zip']</code>. None or an empty list will return no matches found.</li> <li><strong>cache_dir</strong>: Location to store cached files, when None it defaults to <code>~/.keras/</code>.</li> </ul> <p><strong>Returns</strong></p> <p>Path to the downloaded file.</p> <p>⚠️ <strong>Warning on malicious downloads</strong> ⚠️</p> <p>Downloading something from the Internet carries a risk. NEVER download a file/archive if you do not trust the source. We recommend that you specify the <code>file_hash</code> argument (if the hash of the source file is known) to make sure that the file you are getting is the one you expect.</p> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/tf-keras/tree/v2.18.0/tf_keras/utils/generic_utils.py#L127">[source]</a></span></p> <h3 id="progbar-class"><code>Progbar</code> class</h3> <div class="codehilite"><pre><span></span><code><span class="n">tf_keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">Progbar</span><span class="p">(</span> <span class="n">target</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">30</span><span class="p">,</span> <span class="n">verbose</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">interval</span><span class="o">=</span><span class="mf">0.05</span><span class="p">,</span> <span class="n">stateful_metrics</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">unit_name</span><span class="o">=</span><span class="s2">"step"</span> <span class="p">)</span> </code></pre></div> <p>Displays a progress bar.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>target</strong>: Total number of steps expected, None if unknown.</li> <li><strong>width</strong>: Progress bar width on screen.</li> <li><strong>verbose</strong>: Verbosity mode, 0 (silent), 1 (verbose), 2 (semi-verbose)</li> <li><strong>stateful_metrics</strong>: Iterable of string names of metrics that should <em>not</em> be averaged over time. Metrics in this list will be displayed as-is. All others will be averaged by the progbar before display.</li> <li><strong>interval</strong>: Minimum visual progress update interval (in seconds).</li> <li><strong>unit_name</strong>: Display name for step counts (usually "step" or "sample").</li> </ul> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/tf-keras/tree/v2.18.0/tf_keras/utils/data_utils.py#L492">[source]</a></span></p> <h3 id="sequence-class"><code>Sequence</code> class</h3> <div class="codehilite"><pre><span></span><code><span class="n">tf_keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">Sequence</span><span class="p">()</span> </code></pre></div> <p>Base object for fitting to a sequence of data, such as a dataset.</p> <p>Every <code>Sequence</code> must implement the <code>__getitem__</code> and the <code>__len__</code> methods. If you want to modify your dataset between epochs, you may implement <code>on_epoch_end</code>. The method <code>__getitem__</code> should return a complete batch.</p> <p>Notes:</p> <p><code>Sequence</code> is a safer way to do multiprocessing. This structure guarantees that the network will only train once on each sample per epoch, which is not the case with generators.</p> <p><strong>Examples</strong></p> <div class="codehilite"><pre><span></span><code><span class="kn">from</span> <span class="nn">skimage.io</span> <span class="kn">import</span> <span class="n">imread</span> <span class="kn">from</span> <span class="nn">skimage.transform</span> <span class="kn">import</span> <span class="n">resize</span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span> <span class="kn">import</span> <span class="nn">math</span> <span class="c1"># Here, `x_set` is list of path to the images</span> <span class="c1"># and `y_set` are the associated classes.</span> <span class="k">class</span> <span class="nc">CIFAR10Sequence</span><span class="p">(</span><span class="n">tf</span><span class="o">.</span><span class="n">keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">Sequence</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">x_set</span><span class="p">,</span> <span class="n">y_set</span><span class="p">,</span> <span class="n">batch_size</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">x_set</span><span class="p">,</span> <span class="n">y_set</span> <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span> <span class="o">=</span> <span class="n">batch_size</span> <span class="k">def</span> <span class="fm">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="n">math</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">)</span> <span class="k">def</span> <span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">idx</span><span class="p">):</span> <span class="n">low</span> <span class="o">=</span> <span class="n">idx</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span> <span class="c1"># Cap upper bound at array length; the last batch may be smaller</span> <span class="c1"># if the total number of items is not a multiple of batch size.</span> <span class="n">high</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">low</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">batch_size</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="p">))</span> <span class="n">batch_x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="p">[</span><span class="n">low</span><span class="p">:</span><span class="n">high</span><span class="p">]</span> <span class="n">batch_y</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">[</span><span class="n">low</span><span class="p">:</span><span class="n">high</span><span class="p">]</span> <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span> <span class="n">resize</span><span class="p">(</span><span class="n">imread</span><span class="p">(</span><span class="n">file_name</span><span class="p">),</span> <span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="mi">200</span><span class="p">))</span> <span class="k">for</span> <span class="n">file_name</span> <span class="ow">in</span> <span class="n">batch_x</span><span class="p">]),</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">batch_y</span><span class="p">)</span> </code></pre></div> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/tf-keras/tree/v2.18.0/tf_keras/utils/np_utils.py#L23">[source]</a></span></p> <h3 id="tocategorical-function"><code>to_categorical</code> function</h3> <div class="codehilite"><pre><span></span><code><span class="n">tf_keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">to_categorical</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">num_classes</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="s2">"float32"</span><span class="p">)</span> </code></pre></div> <p>Converts a class vector (integers) to binary class matrix.</p> <p>E.g. for use with <code>categorical_crossentropy</code>.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>y</strong>: Array-like with class values to be converted into a matrix (integers from 0 to <code>num_classes - 1</code>).</li> <li><strong>num_classes</strong>: Total number of classes. If <code>None</code>, this would be inferred as <code>max(y) + 1</code>.</li> <li><strong>dtype</strong>: The data type expected by the input. Default: <code>'float32'</code>.</li> </ul> <p><strong>Returns</strong></p> <p>A binary matrix representation of the input as a NumPy array. The class axis is placed last.</p> <p><strong>Example</strong></p> <div class="codehilite"><pre><span></span><code><span class="o">>>></span> <span class="n">a</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">to_categorical</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">num_classes</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span> <span class="o">>>></span> <span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="p">[[</span><span class="mf">1.</span> <span class="mf">0.</span> <span class="mf">0.</span> <span class="mf">0.</span><span class="p">]</span> <span class="p">[</span><span class="mf">0.</span> <span class="mf">1.</span> <span class="mf">0.</span> <span class="mf">0.</span><span class="p">]</span> <span class="p">[</span><span class="mf">0.</span> <span class="mf">0.</span> <span class="mf">1.</span> <span class="mf">0.</span><span class="p">]</span> <span class="p">[</span><span class="mf">0.</span> <span class="mf">0.</span> <span class="mf">0.</span> <span class="mf">1.</span><span class="p">]]</span> </code></pre></div> <div class="codehilite"><pre><span></span><code><span class="o">>>></span> <span class="n">b</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">constant</span><span class="p">([</span><span class="mf">.9</span><span class="p">,</span> <span class="mf">.04</span><span class="p">,</span> <span class="mf">.03</span><span class="p">,</span> <span class="mf">.03</span><span class="p">,</span> <span class="o">...</span> <span class="mf">.3</span><span class="p">,</span> <span class="mf">.45</span><span class="p">,</span> <span class="mf">.15</span><span class="p">,</span> <span class="mf">.13</span><span class="p">,</span> <span class="o">...</span> <span class="mf">.04</span><span class="p">,</span> <span class="mf">.01</span><span class="p">,</span> <span class="mf">.94</span><span class="p">,</span> <span class="mf">.05</span><span class="p">,</span> <span class="o">...</span> <span class="mf">.12</span><span class="p">,</span> <span class="mf">.21</span><span class="p">,</span> <span class="mf">.5</span><span class="p">,</span> <span class="mf">.17</span><span class="p">],</span> <span class="o">...</span> <span class="n">shape</span><span class="o">=</span><span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">])</span> <span class="o">>>></span> <span class="n">loss</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">keras</span><span class="o">.</span><span class="n">backend</span><span class="o">.</span><span class="n">categorical_crossentropy</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="o">>>></span> <span class="nb">print</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">loss</span><span class="p">,</span> <span class="mi">5</span><span class="p">))</span> <span class="p">[</span><span class="mf">0.10536</span> <span class="mf">0.82807</span> <span class="mf">0.1011</span> <span class="mf">1.77196</span><span class="p">]</span> </code></pre></div> <div class="codehilite"><pre><span></span><code><span class="o">>>></span> <span class="n">loss</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">keras</span><span class="o">.</span><span class="n">backend</span><span class="o">.</span><span class="n">categorical_crossentropy</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span> <span class="o">>>></span> <span class="nb">print</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">around</span><span class="p">(</span><span class="n">loss</span><span class="p">,</span> <span class="mi">5</span><span class="p">))</span> <span class="p">[</span><span class="mf">0.</span> <span class="mf">0.</span> <span class="mf">0.</span> <span class="mf">0.</span><span class="p">]</span> </code></pre></div> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/tf-keras/tree/v2.18.0/tf_keras/utils/np_utils.py#L80">[source]</a></span></p> <h3 id="toordinal-function"><code>to_ordinal</code> function</h3> <div class="codehilite"><pre><span></span><code><span class="n">tf_keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">to_ordinal</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">num_classes</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="s2">"float32"</span><span class="p">)</span> </code></pre></div> <p>Converts a class vector (integers) to an ordinal regression matrix.</p> <p>This utility encodes class vector to ordinal regression/classification matrix where each sample is indicated by a row and rank of that sample is indicated by number of ones in that row.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>y</strong>: Array-like with class values to be converted into a matrix (integers from 0 to <code>num_classes - 1</code>).</li> <li><strong>num_classes</strong>: Total number of classes. If <code>None</code>, this would be inferred as <code>max(y) + 1</code>.</li> <li><strong>dtype</strong>: The data type expected by the input. Default: <code>'float32'</code>.</li> </ul> <p><strong>Returns</strong></p> <p>An ordinal regression matrix representation of the input as a NumPy array. The class axis is placed last.</p> <p><strong>Example</strong></p> <div class="codehilite"><pre><span></span><code><span class="o">>>></span> <span class="n">a</span> <span class="o">=</span> <span class="n">tf</span><span class="o">.</span><span class="n">keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">to_ordinal</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">num_classes</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span> <span class="o">>>></span> <span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="p">[[</span><span class="mf">0.</span> <span class="mf">0.</span> <span class="mf">0.</span><span class="p">]</span> <span class="p">[</span><span class="mf">1.</span> <span class="mf">0.</span> <span class="mf">0.</span><span class="p">]</span> <span class="p">[</span><span class="mf">1.</span> <span class="mf">1.</span> <span class="mf">0.</span><span class="p">]</span> <span class="p">[</span><span class="mf">1.</span> <span class="mf">1.</span> <span class="mf">1.</span><span class="p">]]</span> </code></pre></div> <hr /> <p><span style="float:right;"><a href="https://github.com/keras-team/tf-keras/tree/v2.18.0/tf_keras/utils/np_utils.py#L128">[source]</a></span></p> <h3 id="normalize-function"><code>normalize</code> function</h3> <div class="codehilite"><pre><span></span><code><span class="n">tf_keras</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">normalize</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">axis</span><span class="o">=-</span><span class="mi">1</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span> </code></pre></div> <p>Normalizes a Numpy array.</p> <p><strong>Arguments</strong></p> <ul> <li><strong>x</strong>: Numpy array to normalize.</li> <li><strong>axis</strong>: axis along which to normalize.</li> <li><strong>order</strong>: Normalization order (e.g. <code>order=2</code> for L2 norm).</li> </ul> <p><strong>Returns</strong></p> <p>A normalized copy of the array.</p> <hr /> </div> <div class='k-outline'> <div class='k-outline-depth-1'> <a href='#python-amp-numpy-utilities'>Python & NumPy utilities</a> </div> <div class='k-outline-depth-3'> <a href='#setrandomseed-function'><code>set_random_seed</code> function</a> </div> <div class='k-outline-depth-3'> <a href='#splitdataset-function'><code>split_dataset</code> function</a> </div> <div class='k-outline-depth-3'> <a href='#getfile-function'><code>get_file</code> function</a> </div> <div class='k-outline-depth-3'> <a href='#progbar-class'><code>Progbar</code> class</a> </div> <div class='k-outline-depth-3'> <a href='#sequence-class'><code>Sequence</code> class</a> </div> <div class='k-outline-depth-3'> <a href='#tocategorical-function'><code>to_categorical</code> function</a> </div> <div class='k-outline-depth-3'> <a href='#toordinal-function'><code>to_ordinal</code> function</a> </div> <div class='k-outline-depth-3'> <a href='#normalize-function'><code>normalize</code> function</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>