Buckets:
| import"../chunks/DsnmJJEf.js";import{i as v,h as N,C as G,H as t,a,E as F,s as q}from"../chunks/Y07aUP_m.js";import{p as x,o as X,s,f as z,a as C,b as E,c as _,n as V}from"../chunks/DEl054mf.js";const W='{"title":"Using Datasets with TensorFlow","local":"using-datasets-with-tensorflow","sections":[{"title":"Dataset format","local":"dataset-format","sections":[{"title":"N-dimensional arrays","local":"n-dimensional-arrays","sections":[],"depth":3},{"title":"Other feature types","local":"other-feature-types","sections":[],"depth":3}],"depth":2},{"title":"Data loading","local":"data-loading","sections":[{"title":"Using to_tf_dataset()","local":"using-totfdataset","sections":[],"depth":3},{"title":"When to use to_tf_dataset","local":"when-to-use-totfdataset","sections":[],"depth":3},{"title":"Caveats and limitations","local":"caveats-and-limitations","sections":[],"depth":3}],"depth":2}],"depth":1}';var D=_('<meta name="hf:doc:metadata"/>'),Y=_(`<p></p> <!> <!> <p>This document is a quick introduction to using <code>datasets</code> with TensorFlow, with a particular focus on how to get <code>tf.Tensor</code> objects out of our datasets, and how to stream data from Hugging Face <code>Dataset</code> objects to Keras methods | |
| like <code>model.fit()</code>.</p> <!> <p>By default, datasets return regular Python objects: integers, floats, strings, lists, etc.</p> <p>To get TensorFlow tensors instead, you can set the format of the dataset to <code>tf</code>:</p> <!> <blockquote class="tip"><p>A <a href="/docs/datasets/pr_8414/en/package_reference/main_classes#datasets.Dataset">Dataset</a> object is a wrapper of an Arrow table, which allows fast reads from arrays in the dataset to TensorFlow tensors.</p></blockquote> <p>This can be useful for converting your dataset to a dict of <code>Tensor</code> objects, or for writing a generator to load TF | |
| samples from it. If you wish to convert the entire dataset to <code>Tensor</code>, simply query the full dataset:</p> <!> <!> <p>If your dataset consists of N-dimensional arrays, you will see that by default they are considered as the same tensor if the shape is fixed:</p> <!> <p>Otherwise, a TensorFlow formatted dataset outputs a <code>RaggedTensor</code> instead of a single tensor:</p> <!> <p>However this logic often requires slow shape comparisons and data copies. | |
| To avoid this, you must explicitly use the <code>Array</code> feature type and specify the shape of your tensors:</p> <!> <!> <p><a href="/docs/datasets/pr_8414/en/package_reference/main_classes#datasets.ClassLabel">ClassLabel</a> data are properly converted to tensors:</p> <!> <p>Strings and binary objects are also supported:</p> <!> <p>You can also explicitly format certain columns and leave the other columns unformatted:</p> <!> <p>String and binary objects are unchanged, since PyTorch only supports numbers.</p> <p>The <a href="/docs/datasets/pr_8414/en/package_reference/main_classes#datasets.Image">Image</a> and <a href="/docs/datasets/pr_8414/en/package_reference/main_classes#datasets.Audio">Audio</a> feature types are also supported.</p> <blockquote class="tip"><p>To use the <a href="/docs/datasets/pr_8414/en/package_reference/main_classes#datasets.Image">Image</a> feature type, you’ll need to install the <code>vision</code> extra as <code>pip install datasets[vision]</code>.</p></blockquote> <!> <blockquote class="tip"><p>To use the <a href="/docs/datasets/pr_8414/en/package_reference/main_classes#datasets.Audio">Audio</a> feature type, you’ll need to install the <code>audio</code> extra as <code>pip install datasets[audio]</code>.</p></blockquote> <!> <!> <p>Although you can load individual samples and batches just by indexing into your dataset, this won’t work if you want | |
| to use Keras methods like <code>fit()</code> and <code>predict()</code>. You could write a generator function that shuffles and loads batches | |
| from your dataset and <code>fit()</code> on that, but that sounds like a lot of unnecessary work. Instead, if you want to stream | |
| data from your dataset on-the-fly, we recommend converting your dataset to a <code>tf.data.Dataset</code> using the <code>to_tf_dataset()</code> method.</p> <p>The <code>tf.data.Dataset</code> class covers a wide range of use-cases - it is often created from Tensors in memory, or using a load function to read files on disc | |
| or external storage. The dataset can be transformed arbitrarily with the <code>map()</code> method, or methods like <code>batch()</code> and <code>shuffle()</code> can be used to create a dataset that’s ready for training. These methods do not modify the stored data | |
| in any way - instead, the methods build a data pipeline graph that will be executed when the dataset is iterated over, | |
| usually during model training or inference. This is different from the <code>map()</code> method of Hugging Face <code>Dataset</code> objects, | |
| which runs the map function immediately and saves the new or changed columns.</p> <p>Since the entire data preprocessing pipeline can be compiled in a <code>tf.data.Dataset</code>, this approach allows for massively | |
| parallel, asynchronous data loading and training. However, the requirement for graph compilation can be a limitation, | |
| particularly for Hugging Face tokenizers, which are usually not (yet!) compilable as part of a TF graph. As a result, | |
| we usually advise pre-processing the dataset as a Hugging Face dataset, where arbitrary Python functions can be | |
| used, and then converting to <code>tf.data.Dataset</code> afterwards using <code>to_tf_dataset()</code> to get a batched dataset ready for | |
| training. To see examples of this approach, please see the <a href="https://github.com/huggingface/transformers/tree/main/examples" rel="nofollow">examples</a> or <a href="https://huggingface.co/docs/transformers/notebooks" rel="nofollow">notebooks</a> for <code>transformers</code>.</p> <!> <p>Using <code>to_tf_dataset()</code> is straightforward. Once your dataset is preprocessed and ready, simply call it like so:</p> <!> <p>The returned <code>tf_ds</code> object here is now fully ready to train on, and can be passed directly to <code>model.fit()</code>. Note | |
| that you set the batch size when creating the dataset, and so you don’t need to specify it when calling <code>fit()</code>:</p> <!> <p>For a full description of the arguments, please see the <a href="/docs/datasets/pr_8414/en/package_reference/main_classes#datasets.Dataset.to_tf_dataset">to_tf_dataset()</a> documentation. In many cases, | |
| you will also need to add a <code>collate_fn</code> to your call. This is a function that takes multiple elements of the dataset | |
| and combines them into a single batch. When all elements have the same length, the built-in default collator will | |
| suffice, but for more complex tasks a custom collator may be necessary. In particular, many tasks have samples | |
| with varying sequence lengths which will require a <a href="https://huggingface.co/docs/transformers/main/en/main_classes/data_collator" rel="nofollow">data collator</a> that can pad batches correctly. You can see examples | |
| of this in the <code>transformers</code> NLP <a href="https://github.com/huggingface/transformers/tree/main/examples" rel="nofollow">examples</a> and <a href="https://huggingface.co/docs/transformers/notebooks" rel="nofollow">notebooks</a>, where variable sequence lengths are very common.</p> <p>If you find that loading with <code>to_tf_dataset</code> is slow, you can also use the <code>num_workers</code> argument. This spins | |
| up multiple subprocesses to load data in parallel. This feature is recent and still somewhat experimental - please file | |
| an issue if you encounter any bugs while using it!</p> <!> <p>The astute reader may have noticed at this point that we have offered two approaches to achieve the same goal - if you | |
| want to pass your dataset to a TensorFlow model, you can either convert the dataset to a <code>Tensor</code> or <code>dict</code> of <code>Tensors</code> using <code>.with_format('tf')</code>, or you can convert the dataset to a <code>tf.data.Dataset</code> with <code>to_tf_dataset()</code>. Either of these | |
| can be passed to <code>model.fit()</code>, so which should you choose?</p> <p>The key thing to recognize is that when you convert the whole dataset to <code>Tensor</code>s, it is static and fully loaded into | |
| RAM. This is simple and convenient, but if any of the following apply, you should probably use <code>to_tf_dataset()</code> instead:</p> <ul><li>Your dataset is too large to fit in RAM. <code>to_tf_dataset()</code> streams only one batch at a time, so even very large | |
| datasets can be handled with this method.</li> <li>You want to apply random transformations using <code>dataset.with_transform()</code> or the <code>collate_fn</code>. This is | |
| common in several modalities, such as image augmentations when training vision models, or random masking when training | |
| masked language models. Using <code>to_tf_dataset()</code> will apply those transformations | |
| at the moment when a batch is loaded, which means the same samples will get different augmentations each time | |
| they are loaded. This is usually what you want.</li> <li>Your data has a variable dimension, such as input texts in NLP that consist of varying | |
| numbers of tokens. When you create a batch with samples with a variable dimension, the standard solution is to | |
| pad the shorter samples to the length of the longest one. When you stream samples from a dataset with <code>to_tf_dataset</code>, | |
| you can apply this padding to each batch via your <code>collate_fn</code>. However, if you want to convert | |
| such a dataset to dense <code>Tensor</code>s, then you will have to pad samples to the length of the longest sample in <em>the | |
| entire dataset!</em> This can result in huge amounts of padding, which wastes memory and reduces your model’s speed.</li></ul> <!> <p>Right now, <code>to_tf_dataset()</code> always returns a batched dataset - we will add support for unbatched datasets soon!</p> <!> <p></p>`,1);function H(Q,k){x(k,!1),X(()=>{new URLSearchParams(window.location.search).get("fw")}),v();var e=Y();N("1t92rm3",R=>{var I=D();q(I,"content",W),C(R,I)});var n=s(z(e),2);G(n,{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"});var l=s(n,2);t(l,{title:"Using Datasets with TensorFlow",local:"using-datasets-with-tensorflow",headingTag:"h1"});var p=s(l,4);t(p,{title:"Dataset format",local:"dataset-format",headingTag:"h2"});var o=s(p,6);a(o,{code:"ZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwRGF0YXNldCUwQWRhdGElMjAlM0QlMjAlNUIlNUIxJTJDJTIwMiU1RCUyQyU1QjMlMkMlMjA0JTVEJTVEJTBBZHMlMjAlM0QlMjBEYXRhc2V0LmZyb21fZGljdCglN0IlMjJkYXRhJTIyJTNBJTIwZGF0YSU3RCklMEFkcyUyMCUzRCUyMGRzLndpdGhfZm9ybWF0KCUyMnRmJTIyKSUwQWRzJTVCMCU1RCUwQWRzJTVCJTNBMiU1RA==",highlighted:`<span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> Dataset | |
| <span class="hljs-meta">>>> </span>data = [[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>],[<span class="hljs-number">3</span>, <span class="hljs-number">4</span>]] | |
| <span class="hljs-meta">>>> </span>ds = Dataset.from_dict({<span class="hljs-string">"data"</span>: data}) | |
| <span class="hljs-meta">>>> </span>ds = ds.with_format(<span class="hljs-string">"tf"</span>) | |
| <span class="hljs-meta">>>> </span>ds[<span class="hljs-number">0</span>] | |
| {<span class="hljs-string">'data'</span>: <tf.Tensor: shape=(<span class="hljs-number">2</span>,), dtype=int64, numpy=array([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>])>} | |
| <span class="hljs-meta">>>> </span>ds[:<span class="hljs-number">2</span>] | |
| {<span class="hljs-string">'data'</span>: <tf.Tensor: shape=(<span class="hljs-number">2</span>, <span class="hljs-number">2</span>), dtype=int64, numpy= | |
| array([[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>], | |
| [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>]])>}`,lang:"py",wrap:!1});var r=s(o,6);a(r,{code:"ZHMlNUIlM0ElNUQ=",highlighted:`<span class="hljs-meta">>>> </span>ds[:] | |
| {<span class="hljs-string">'data'</span>: <tf.Tensor: shape=(<span class="hljs-number">2</span>, <span class="hljs-number">2</span>), dtype=int64, numpy= | |
| array([[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>], | |
| [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>]])>}`,lang:"py",wrap:!1});var c=s(r,2);t(c,{title:"N-dimensional arrays",local:"n-dimensional-arrays",headingTag:"h3"});var h=s(c,4);a(h,{code:"ZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwRGF0YXNldCUwQWRhdGElMjAlM0QlMjAlNUIlNUIlNUIxJTJDJTIwMiU1RCUyQyU1QjMlMkMlMjA0JTVEJTVEJTJDJTVCJTVCNSUyQyUyMDYlNUQlMkMlNUI3JTJDJTIwOCU1RCU1RCU1RCUyMCUyMCUyMyUyMGZpeGVkJTIwc2hhcGUlMEFkcyUyMCUzRCUyMERhdGFzZXQuZnJvbV9kaWN0KCU3QiUyMmRhdGElMjIlM0ElMjBkYXRhJTdEKSUwQWRzJTIwJTNEJTIwZHMud2l0aF9mb3JtYXQoJTIydGYlMjIpJTBBZHMlNUIwJTVE",highlighted:`<span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> Dataset | |
| <span class="hljs-meta">>>> </span>data = [[[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>],[<span class="hljs-number">3</span>, <span class="hljs-number">4</span>]],[[<span class="hljs-number">5</span>, <span class="hljs-number">6</span>],[<span class="hljs-number">7</span>, <span class="hljs-number">8</span>]]] <span class="hljs-comment"># fixed shape</span> | |
| <span class="hljs-meta">>>> </span>ds = Dataset.from_dict({<span class="hljs-string">"data"</span>: data}) | |
| <span class="hljs-meta">>>> </span>ds = ds.with_format(<span class="hljs-string">"tf"</span>) | |
| <span class="hljs-meta">>>> </span>ds[<span class="hljs-number">0</span>] | |
| {<span class="hljs-string">'data'</span>: <tf.Tensor: shape=(<span class="hljs-number">2</span>, <span class="hljs-number">2</span>), dtype=int64, numpy= | |
| array([[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>], | |
| [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>]])>}`,lang:"py",wrap:!1});var d=s(h,4);a(d,{code:"ZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwRGF0YXNldCUwQWRhdGElMjAlM0QlMjAlNUIlNUIlNUIxJTJDJTIwMiU1RCUyQyU1QjMlNUQlNUQlMkMlNUIlNUI0JTJDJTIwNSUyQyUyMDYlNUQlMkMlNUI3JTJDJTIwOCU1RCU1RCU1RCUyMCUyMCUyMyUyMHZhcnlpbmclMjBzaGFwZSUwQWRzJTIwJTNEJTIwRGF0YXNldC5mcm9tX2RpY3QoJTdCJTIyZGF0YSUyMiUzQSUyMGRhdGElN0QpJTBBZHMlMjAlM0QlMjBkcy53aXRoX2Zvcm1hdCglMjJ0b3JjaCUyMiklMEFkcyU1QjAlNUQ=",highlighted:`<span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> Dataset | |
| <span class="hljs-meta">>>> </span>data = [[[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>],[<span class="hljs-number">3</span>]],[[<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>],[<span class="hljs-number">7</span>, <span class="hljs-number">8</span>]]] <span class="hljs-comment"># varying shape</span> | |
| <span class="hljs-meta">>>> </span>ds = Dataset.from_dict({<span class="hljs-string">"data"</span>: data}) | |
| <span class="hljs-meta">>>> </span>ds = ds.with_format(<span class="hljs-string">"torch"</span>) | |
| <span class="hljs-meta">>>> </span>ds[<span class="hljs-number">0</span>] | |
| {<span class="hljs-string">'data'</span>: <tf.RaggedTensor [[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>], [<span class="hljs-number">3</span>]]>}`,lang:"py",wrap:!1});var i=s(d,4);a(i,{code:"ZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwRGF0YXNldCUyQyUyMEZlYXR1cmVzJTJDJTIwQXJyYXkyRCUwQWRhdGElMjAlM0QlMjAlNUIlNUIlNUIxJTJDJTIwMiU1RCUyQyU1QjMlMkMlMjA0JTVEJTVEJTJDJTVCJTVCNSUyQyUyMDYlNUQlMkMlNUI3JTJDJTIwOCU1RCU1RCU1RCUwQWZlYXR1cmVzJTIwJTNEJTIwRmVhdHVyZXMoJTdCJTIyZGF0YSUyMiUzQSUyMEFycmF5MkQoc2hhcGUlM0QoMiUyQyUyMDIpJTJDJTIwZHR5cGUlM0QnaW50MzInKSU3RCklMEFkcyUyMCUzRCUyMERhdGFzZXQuZnJvbV9kaWN0KCU3QiUyMmRhdGElMjIlM0ElMjBkYXRhJTdEJTJDJTIwZmVhdHVyZXMlM0RmZWF0dXJlcyklMEFkcyUyMCUzRCUyMGRzLndpdGhfZm9ybWF0KCUyMnRmJTIyKSUwQWRzJTVCMCU1RCUwQWRzJTVCJTNBMiU1RA==",highlighted:`<span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> Dataset, Features, Array2D | |
| <span class="hljs-meta">>>> </span>data = [[[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>],[<span class="hljs-number">3</span>, <span class="hljs-number">4</span>]],[[<span class="hljs-number">5</span>, <span class="hljs-number">6</span>],[<span class="hljs-number">7</span>, <span class="hljs-number">8</span>]]] | |
| <span class="hljs-meta">>>> </span>features = Features({<span class="hljs-string">"data"</span>: Array2D(shape=(<span class="hljs-number">2</span>, <span class="hljs-number">2</span>), dtype=<span class="hljs-string">'int32'</span>)}) | |
| <span class="hljs-meta">>>> </span>ds = Dataset.from_dict({<span class="hljs-string">"data"</span>: data}, features=features) | |
| <span class="hljs-meta">>>> </span>ds = ds.with_format(<span class="hljs-string">"tf"</span>) | |
| <span class="hljs-meta">>>> </span>ds[<span class="hljs-number">0</span>] | |
| {<span class="hljs-string">'data'</span>: <tf.Tensor: shape=(<span class="hljs-number">2</span>, <span class="hljs-number">2</span>), dtype=int64, numpy= | |
| array([[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>], | |
| [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>]])>} | |
| <span class="hljs-meta">>>> </span>ds[:<span class="hljs-number">2</span>] | |
| {<span class="hljs-string">'data'</span>: <tf.Tensor: shape=(<span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>), dtype=int64, numpy= | |
| array([[[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>], | |
| [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>]], | |
| [[<span class="hljs-number">5</span>, <span class="hljs-number">6</span>], | |
| [<span class="hljs-number">7</span>, <span class="hljs-number">8</span>]]])>}`,lang:"py",wrap:!1});var m=s(i,2);t(m,{title:"Other feature types",local:"other-feature-types",headingTag:"h3"});var u=s(m,4);a(u,{code:"ZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwRGF0YXNldCUyQyUyMEZlYXR1cmVzJTJDJTIwQ2xhc3NMYWJlbCUwQWxhYmVscyUyMCUzRCUyMCU1QjAlMkMlMjAwJTJDJTIwMSU1RCUwQWZlYXR1cmVzJTIwJTNEJTIwRmVhdHVyZXMoJTdCJTIybGFiZWwlMjIlM0ElMjBDbGFzc0xhYmVsKG5hbWVzJTNEJTVCJTIybmVnYXRpdmUlMjIlMkMlMjAlMjJwb3NpdGl2ZSUyMiU1RCklN0QpJTBBZHMlMjAlM0QlMjBEYXRhc2V0LmZyb21fZGljdCglN0IlMjJsYWJlbCUyMiUzQSUyMGxhYmVscyU3RCUyQyUyMGZlYXR1cmVzJTNEZmVhdHVyZXMpJTIwJTBBZHMlMjAlM0QlMjBkcy53aXRoX2Zvcm1hdCglMjJ0ZiUyMiklMjAlMjAlMEFkcyU1QiUzQTMlNUQ=",highlighted:`<span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> Dataset, Features, ClassLabel | |
| <span class="hljs-meta">>>> </span>labels = [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>] | |
| <span class="hljs-meta">>>> </span>features = Features({<span class="hljs-string">"label"</span>: ClassLabel(names=[<span class="hljs-string">"negative"</span>, <span class="hljs-string">"positive"</span>])}) | |
| <span class="hljs-meta">>>> </span>ds = Dataset.from_dict({<span class="hljs-string">"label"</span>: labels}, features=features) | |
| <span class="hljs-meta">>>> </span>ds = ds.with_format(<span class="hljs-string">"tf"</span>) | |
| <span class="hljs-meta">>>> </span>ds[:<span class="hljs-number">3</span>] | |
| {<span class="hljs-string">'label'</span>: <tf.Tensor: shape=(<span class="hljs-number">3</span>,), dtype=int64, numpy=array([<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>])>}`,lang:"py",wrap:!1});var g=s(u,4);a(g,{code:"ZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwRGF0YXNldCUyQyUyMEZlYXR1cmVzJTIwJTBBdGV4dCUyMCUzRCUyMCU1QiUyMmZvbyUyMiUyQyUyMCUyMmJhciUyMiU1RCUwQWRhdGElMjAlM0QlMjAlNUIwJTJDJTIwMSU1RCUyMCUwQWRzJTIwJTNEJTIwRGF0YXNldC5mcm9tX2RpY3QoJTdCJTIydGV4dCUyMiUzQSUyMHRleHQlMkMlMjAlMjJkYXRhJTIyJTNBJTIwZGF0YSU3RCklMjAlMjAlMEFkcyUyMCUzRCUyMGRzLndpdGhfZm9ybWF0KCUyMnRmJTIyKSUyMCUwQWRzJTVCJTNBMiU1RA==",highlighted:`<span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> Dataset, Features | |
| <span class="hljs-meta">>>> </span>text = [<span class="hljs-string">"foo"</span>, <span class="hljs-string">"bar"</span>] | |
| <span class="hljs-meta">>>> </span>data = [<span class="hljs-number">0</span>, <span class="hljs-number">1</span>] | |
| <span class="hljs-meta">>>> </span>ds = Dataset.from_dict({<span class="hljs-string">"text"</span>: text, <span class="hljs-string">"data"</span>: data}) | |
| <span class="hljs-meta">>>> </span>ds = ds.with_format(<span class="hljs-string">"tf"</span>) | |
| <span class="hljs-meta">>>> </span>ds[:<span class="hljs-number">2</span>] | |
| {<span class="hljs-string">'text'</span>: <tf.Tensor: shape=(<span class="hljs-number">2</span>,), dtype=string, numpy=array([<span class="hljs-string">b'foo'</span>, <span class="hljs-string">b'bar'</span>], dtype=<span class="hljs-built_in">object</span>)>, | |
| <span class="hljs-string">'data'</span>: <tf.Tensor: shape=(<span class="hljs-number">2</span>,), dtype=int64, numpy=array([<span class="hljs-number">0</span>, <span class="hljs-number">1</span>])>}`,lang:"py",wrap:!1});var y=s(g,4);a(y,{code:"ZHMlMjAlM0QlMjBkcy53aXRoX2Zvcm1hdCglMjJ0ZiUyMiUyQyUyMGNvbHVtbnMlM0QlNUIlMjJkYXRhJTIyJTVEJTJDJTIwb3V0cHV0X2FsbF9jb2x1bW5zJTNEVHJ1ZSklMEFkcyU1QiUzQTIlNUQ=",highlighted:`<span class="hljs-meta">>>> </span>ds = ds.with_format(<span class="hljs-string">"tf"</span>, columns=[<span class="hljs-string">"data"</span>], output_all_columns=<span class="hljs-literal">True</span>) | |
| <span class="hljs-meta">>>> </span>ds[:<span class="hljs-number">2</span>] | |
| {<span class="hljs-string">'data'</span>: <tf.Tensor: shape=(<span class="hljs-number">2</span>,), dtype=int64, numpy=array([<span class="hljs-number">0</span>, <span class="hljs-number">1</span>])>, | |
| <span class="hljs-string">'text'</span>: [<span class="hljs-string">'foo'</span>, <span class="hljs-string">'bar'</span>]}`,lang:"py",wrap:!1});var j=s(y,8);a(j,{code:"ZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwRGF0YXNldCUyQyUyMEZlYXR1cmVzJTJDJTIwQXVkaW8lMkMlMjBJbWFnZSUwQWltYWdlcyUyMCUzRCUyMCU1QiUyMnBhdGglMkZ0byUyRmltYWdlLnBuZyUyMiU1RCUyMColMjAxMCUwQWZlYXR1cmVzJTIwJTNEJTIwRmVhdHVyZXMoJTdCJTIyaW1hZ2UlMjIlM0ElMjBJbWFnZSgpJTdEKSUwQWRzJTIwJTNEJTIwRGF0YXNldC5mcm9tX2RpY3QoJTdCJTIyaW1hZ2UlMjIlM0ElMjBpbWFnZXMlN0QlMkMlMjBmZWF0dXJlcyUzRGZlYXR1cmVzKSUyMCUwQWRzJTIwJTNEJTIwZHMud2l0aF9mb3JtYXQoJTIydGYlMjIpJTIwJTIwJTBBZHMlNUIwJTVEJTBBZHMlNUIlM0EyJTVE",highlighted:`<span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> Dataset, Features, Audio, Image | |
| <span class="hljs-meta">>>> </span>images = [<span class="hljs-string">"path/to/image.png"</span>] * <span class="hljs-number">10</span> | |
| <span class="hljs-meta">>>> </span>features = Features({<span class="hljs-string">"image"</span>: Image()}) | |
| <span class="hljs-meta">>>> </span>ds = Dataset.from_dict({<span class="hljs-string">"image"</span>: images}, features=features) | |
| <span class="hljs-meta">>>> </span>ds = ds.with_format(<span class="hljs-string">"tf"</span>) | |
| <span class="hljs-meta">>>> </span>ds[<span class="hljs-number">0</span>] | |
| {<span class="hljs-string">'image'</span>: <tf.Tensor: shape=(<span class="hljs-number">512</span>, <span class="hljs-number">512</span>, <span class="hljs-number">4</span>), dtype=uint8, numpy= | |
| array([[[<span class="hljs-number">255</span>, <span class="hljs-number">215</span>, <span class="hljs-number">106</span>, <span class="hljs-number">255</span>], | |
| [<span class="hljs-number">255</span>, <span class="hljs-number">215</span>, <span class="hljs-number">106</span>, <span class="hljs-number">255</span>], | |
| ..., | |
| [<span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>], | |
| [<span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>]]], dtype=uint8)>} | |
| <span class="hljs-meta">>>> </span>ds[:<span class="hljs-number">2</span>] | |
| {<span class="hljs-string">'image'</span>: <tf.Tensor: shape=(<span class="hljs-number">2</span>, <span class="hljs-number">512</span>, <span class="hljs-number">512</span>, <span class="hljs-number">4</span>), dtype=uint8, numpy= | |
| array([[[[<span class="hljs-number">255</span>, <span class="hljs-number">215</span>, <span class="hljs-number">106</span>, <span class="hljs-number">255</span>], | |
| [<span class="hljs-number">255</span>, <span class="hljs-number">215</span>, <span class="hljs-number">106</span>, <span class="hljs-number">255</span>], | |
| ..., | |
| [<span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>], | |
| [<span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>, <span class="hljs-number">255</span>]]]], dtype=uint8)>}`,lang:"py",wrap:!1});var b=s(j,4);a(b,{code:"ZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwRGF0YXNldCUyQyUyMEZlYXR1cmVzJTJDJTIwQXVkaW8lMkMlMjBJbWFnZSUwQWF1ZGlvJTIwJTNEJTIwJTVCJTIycGF0aCUyRnRvJTJGYXVkaW8ud2F2JTIyJTVEJTIwKiUyMDEwJTBBZmVhdHVyZXMlMjAlM0QlMjBGZWF0dXJlcyglN0IlMjJhdWRpbyUyMiUzQSUyMEF1ZGlvKCklN0QpJTBBZHMlMjAlM0QlMjBEYXRhc2V0LmZyb21fZGljdCglN0IlMjJhdWRpbyUyMiUzQSUyMGF1ZGlvJTdEJTJDJTIwZmVhdHVyZXMlM0RmZWF0dXJlcyklMjAlMEFkcyUyMCUzRCUyMGRzLndpdGhfZm9ybWF0KCUyMnRmJTIyKSUyMCUyMCUwQWRzJTVCMCU1RCU1QiUyMmF1ZGlvJTIyJTVEJTVCJTIyYXJyYXklMjIlNUQlMEFkcyU1QjAlNUQlNUIlMjJhdWRpbyUyMiU1RCU1QiUyMnNhbXBsaW5nX3JhdGUlMjIlNUQ=",highlighted:`<span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> Dataset, Features, Audio, Image | |
| <span class="hljs-meta">>>> </span>audio = [<span class="hljs-string">"path/to/audio.wav"</span>] * <span class="hljs-number">10</span> | |
| <span class="hljs-meta">>>> </span>features = Features({<span class="hljs-string">"audio"</span>: Audio()}) | |
| <span class="hljs-meta">>>> </span>ds = Dataset.from_dict({<span class="hljs-string">"audio"</span>: audio}, features=features) | |
| <span class="hljs-meta">>>> </span>ds = ds.with_format(<span class="hljs-string">"tf"</span>) | |
| <span class="hljs-meta">>>> </span>ds[<span class="hljs-number">0</span>][<span class="hljs-string">"audio"</span>][<span class="hljs-string">"array"</span>] | |
| <tf.Tensor: shape=(<span class="hljs-number">202311</span>,), dtype=float32, numpy= | |
| array([ <span class="hljs-number">6.1035156e-05</span>, <span class="hljs-number">1.5258789e-05</span>, <span class="hljs-number">1.6784668e-04</span>, ..., | |
| -<span class="hljs-number">1.5258789e-05</span>, -<span class="hljs-number">1.5258789e-05</span>, <span class="hljs-number">1.5258789e-05</span>], dtype=float32)> | |
| <span class="hljs-meta">>>> </span>ds[<span class="hljs-number">0</span>][<span class="hljs-string">"audio"</span>][<span class="hljs-string">"sampling_rate"</span>] | |
| <tf.Tensor: shape=(), dtype=int32, numpy=<span class="hljs-number">44100</span>>`,lang:"py",wrap:!1});var f=s(b,2);t(f,{title:"Data loading",local:"data-loading",headingTag:"h2"});var M=s(f,8);t(M,{title:"Using to_tf_dataset()",local:"using-totfdataset",headingTag:"h3"});var w=s(M,4);a(w,{code:"ZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwRGF0YXNldCUwQWRhdGElMjAlM0QlMjAlN0IlMjJpbnB1dHMlMjIlM0ElMjAlNUIlNUIxJTJDJTIwMiU1RCUyQyU1QjMlMkMlMjA0JTVEJTVEJTJDJTIwJTIybGFiZWxzJTIyJTNBJTIwJTVCMCUyQyUyMDElNUQlN0QlMEFkcyUyMCUzRCUyMERhdGFzZXQuZnJvbV9kaWN0KGRhdGEpJTBBdGZfZHMlMjAlM0QlMjBkcy50b190Zl9kYXRhc2V0KA==",highlighted:`<span class="hljs-meta">>>> </span><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> Dataset | |
| <span class="hljs-meta">>>> </span>data = {<span class="hljs-string">"inputs"</span>: [[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>],[<span class="hljs-number">3</span>, <span class="hljs-number">4</span>]], <span class="hljs-string">"labels"</span>: [<span class="hljs-number">0</span>, <span class="hljs-number">1</span>]} | |
| <span class="hljs-meta">>>> </span>ds = Dataset.from_dict(data) | |
| <span class="hljs-meta">>>> </span>tf_ds = ds.to_tf_dataset( | |
| columns=[<span class="hljs-string">"inputs"</span>], | |
| label_cols=[<span class="hljs-string">"labels"</span>], | |
| batch_size=<span class="hljs-number">2</span>, | |
| shuffle=<span class="hljs-literal">True</span> | |
| )`,lang:"py",wrap:!1});var J=s(w,4);a(J,{code:"bW9kZWwuZml0KHRmX2RzJTJDJTIwZXBvY2hzJTNEMik=",highlighted:'<span class="hljs-meta">>>> </span>model.fit(tf_ds, epochs=<span class="hljs-number">2</span>)',lang:"py",wrap:!1});var T=s(J,6);t(T,{title:"When to use to_tf_dataset",local:"when-to-use-totfdataset",headingTag:"h3"});var U=s(T,8);t(U,{title:"Caveats and limitations",local:"caveats-and-limitations",headingTag:"h3"});var Z=s(U,4);F(Z,{source:"https://github.com/huggingface/datasets/blob/main/docs/source/use_with_tensorflow.mdx"}),V(2),C(Q,e),E()}export{H as component}; | |
Xet Storage Details
- Size:
- 30.4 kB
- Xet hash:
- 4ca42dc84ced19f4b5c1b61d980223aa60df3ed0c660d1d406fd04bf6bcb37e2
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.