Buckets:
| import{s as qt,n as Ht,o as Yt}from"../chunks/scheduler.7c59faff.js";import{S as St,i as Lt,e as i,s as a,c as p,h as Et,a as r,d as l,b as n,f as Qt,g as y,p as o,j as Ft,k as Nt,l as s,m as M,t as m,n as h,o as u}from"../chunks/index.09bb5655.js";import{C as Pt,H as gt,E as Dt}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.634927d2.js";import{C as H}from"../chunks/CodeBlock.7958fb3f.js";function Kt(Ct){let c,L,Y,E,d,N,w,P,f,kt=`In the <a href="quicktour">Quicktour</a>, we saw how to build and train a | |
| tokenizer using text files, but we can actually use any Python Iterator. | |
| In this section we’ll see a few different ways of training our | |
| tokenizer.`,D,U,$t=`For all the examples listed below, we’ll use the same <a href="/docs/tokenizers/pr_2129/en/api/tokenizer#tokenizers.Tokenizer">Tokenizer</a> and | |
| <code>Trainer</code>, built as | |
| following:`,K,j,O,J,It=`This tokenizer is based on the <a href="/docs/tokenizers/pr_2129/en/api/models#tokenizers.models.Unigram">Unigram</a> model. It | |
| takes care of normalizing the input using the NFKC Unicode normalization | |
| method, and uses a <a href="/docs/tokenizers/pr_2129/en/api/pre-tokenizers#tokenizers.pre_tokenizers.ByteLevel">ByteLevel</a> pre-tokenizer with the corresponding decoder.`,tt,b,zt=`For more information on the components used here, you can check | |
| <a href="components">here</a>.`,et,T,lt,g,Gt=`As you probably guessed already, the easiest way to train our tokenizer | |
| is by using a <code>List</code>{.interpreted-text role=“obj”}:`,st,C,at,k,Xt=`Easy, right? You can use anything working as an iterator here, be it a | |
| <code>List</code>{.interpreted-text role=“obj”}, <code>Tuple</code>{.interpreted-text | |
| role=“obj”}, or a <code>np.Array</code>{.interpreted-text role=“obj”}. Anything | |
| works as long as it provides strings.`,nt,$,it,I,Zt=`An awesome way to access one of the many datasets that exist out there | |
| is by using the 🤗 Datasets library. For more information about it, you | |
| should check <a href="https://huggingface.co/docs/datasets/" rel="nofollow">the official documentation | |
| here</a>.`,rt,z,vt="Let’s start by loading our dataset:",ot,G,pt,X,Bt=`The next step is to build an iterator over this dataset. The easiest way | |
| to do this is probably by using a generator:`,yt,Z,Mt,v,xt=`As you can see here, for improved efficiency we can actually provide a | |
| batch of examples used to train, instead of iterating over them one by | |
| one. By doing so, we can expect performances very similar to those we | |
| got while training directly from files.`,mt,B,Rt=`With our iterator ready, we just need to launch the training. In order | |
| to improve the look of our progress bars, we can specify the total | |
| length of the dataset:`,ht,x,ut,R,_t="And that’s it!",ct,_,dt,V,Vt=`Since gzip files in Python can be used as iterators, it is extremely | |
| simple to train on such files:`,wt,W,ft,A,Wt=`Now if we wanted to train from multiple gzip files, it wouldn’t be much | |
| harder:`,Ut,Q,jt,F,At="And voilà!",Jt,q,bt,S,Tt;return d=new Pt({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),w=new gt({props:{title:"Training from memory",local:"training-from-memory",headingTag:"h1"}}),j=new H({props:{code:"ZnJvbSUyMHRva2VuaXplcnMlMjBpbXBvcnQlMjBUb2tlbml6ZXIlMkMlMjBkZWNvZGVycyUyQyUyMG1vZGVscyUyQyUyMG5vcm1hbGl6ZXJzJTJDJTIwcHJlX3Rva2VuaXplcnMlMkMlMjB0cmFpbmVycyUwQXRva2VuaXplciUyMCUzRCUyMFRva2VuaXplcihtb2RlbHMuVW5pZ3JhbSgpKSUwQXRva2VuaXplci5ub3JtYWxpemVyJTIwJTNEJTIwbm9ybWFsaXplcnMuTkZLQygpJTBBdG9rZW5pemVyLnByZV90b2tlbml6ZXIlMjAlM0QlMjBwcmVfdG9rZW5pemVycy5CeXRlTGV2ZWwoKSUwQXRva2VuaXplci5kZWNvZGVyJTIwJTNEJTIwZGVjb2RlcnMuQnl0ZUxldmVsKCklMEF0cmFpbmVyJTIwJTNEJTIwdHJhaW5lcnMuVW5pZ3JhbVRyYWluZXIoJTBBJTIwJTIwJTIwJTIwdm9jYWJfc2l6ZSUzRDIwMDAwJTJDJTBBJTIwJTIwJTIwJTIwaW5pdGlhbF9hbHBoYWJldCUzRHByZV90b2tlbml6ZXJzLkJ5dGVMZXZlbC5hbHBoYWJldCgpJTJDJTBBJTIwJTIwJTIwJTIwc3BlY2lhbF90b2tlbnMlM0QlNUIlMjIlM0NQQUQlM0UlMjIlMkMlMjAlMjIlM0NCT1MlM0UlMjIlMkMlMjAlMjIlM0NFT1MlM0UlMjIlNUQlMkMlMEEp",highlighted:`<span class="hljs-keyword">from</span> tokenizers <span class="hljs-keyword">import</span> Tokenizer, decoders, models, normalizers, pre_tokenizers, trainers | |
| tokenizer = Tokenizer(models.Unigram()) | |
| tokenizer.normalizer = normalizers.NFKC() | |
| tokenizer.pre_tokenizer = pre_tokenizers.ByteLevel() | |
| tokenizer.decoder = decoders.ByteLevel() | |
| trainer = trainers.UnigramTrainer( | |
| vocab_size=<span class="hljs-number">20000</span>, | |
| initial_alphabet=pre_tokenizers.ByteLevel.alphabet(), | |
| special_tokens=[<span class="hljs-string">"<PAD>"</span>, <span class="hljs-string">"<BOS>"</span>, <span class="hljs-string">"<EOS>"</span>], | |
| )`,lang:"python",wrap:!1}}),T=new gt({props:{title:"The most basic way",local:"the-most-basic-way",headingTag:"h2"}}),C=new H({props:{code:"JTIzJTIwRmlyc3QlMjBmZXclMjBsaW5lcyUyMG9mJTIwdGhlJTIwJTIyWmVuJTIwb2YlMjBQeXRob24lMjIlMjBodHRwcyUzQSUyRiUyRnd3dy5weXRob24ub3JnJTJGZGV2JTJGcGVwcyUyRnBlcC0wMDIwJTJGJTBBZGF0YSUyMCUzRCUyMCU1QiUwQSUyMCUyMCUyMCUyMCUyMkJlYXV0aWZ1bCUyMGlzJTIwYmV0dGVyJTIwdGhhbiUyMHVnbHkuJTIyJTBBJTIwJTIwJTIwJTIwJTIyRXhwbGljaXQlMjBpcyUyMGJldHRlciUyMHRoYW4lMjBpbXBsaWNpdC4lMjIlMEElMjAlMjAlMjAlMjAlMjJTaW1wbGUlMjBpcyUyMGJldHRlciUyMHRoYW4lMjBjb21wbGV4LiUyMiUwQSUyMCUyMCUyMCUyMCUyMkNvbXBsZXglMjBpcyUyMGJldHRlciUyMHRoYW4lMjBjb21wbGljYXRlZC4lMjIlMEElMjAlMjAlMjAlMjAlMjJGbGF0JTIwaXMlMjBiZXR0ZXIlMjB0aGFuJTIwbmVzdGVkLiUyMiUwQSUyMCUyMCUyMCUyMCUyMlNwYXJzZSUyMGlzJTIwYmV0dGVyJTIwdGhhbiUyMGRlbnNlLiUyMiUwQSUyMCUyMCUyMCUyMCUyMlJlYWRhYmlsaXR5JTIwY291bnRzLiUyMiUwQSU1RCUwQXRva2VuaXplci50cmFpbl9mcm9tX2l0ZXJhdG9yKGRhdGElMkMlMjB0cmFpbmVyJTNEdHJhaW5lcik=",highlighted:`<span class="hljs-comment"># First few lines of the "Zen of Python" https://www.python.org/dev/peps/pep-0020/</span> | |
| data = [ | |
| <span class="hljs-string">"Beautiful is better than ugly."</span> | |
| <span class="hljs-string">"Explicit is better than implicit."</span> | |
| <span class="hljs-string">"Simple is better than complex."</span> | |
| <span class="hljs-string">"Complex is better than complicated."</span> | |
| <span class="hljs-string">"Flat is better than nested."</span> | |
| <span class="hljs-string">"Sparse is better than dense."</span> | |
| <span class="hljs-string">"Readability counts."</span> | |
| ] | |
| tokenizer.train_from_iterator(data, trainer=trainer)`,lang:"python",wrap:!1}}),$=new gt({props:{title:"Using the 🤗 Datasets library",local:"using-the--datasets-library",headingTag:"h2"}}),G=new H({props:{code:"aW1wb3J0JTIwZGF0YXNldHMlMjAlMjAlMjMlMjB0eXBlJTNBJTIwaWdub3JlJTVCaW1wb3J0LW5vdC1mb3VuZCU1RCUwQWRhdGFzZXQlMjAlM0QlMjBkYXRhc2V0cy5sb2FkX2RhdGFzZXQoJTIyU2FsZXNmb3JjZSUyRndpa2l0ZXh0JTIyJTJDJTIwJTIyd2lraXRleHQtMTAzLXJhdy12MSUyMiUyQyUyMHNwbGl0JTNEJTIydHJhaW4lMkJ0ZXN0JTJCdmFsaWRhdGlvbiUyMik=",highlighted:`<span class="hljs-keyword">import</span> datasets <span class="hljs-comment"># type: ignore[<span class="hljs-keyword">import</span>-<span class="hljs-keyword">not</span>-found]</span> | |
| dataset = datasets.load_dataset(<span class="hljs-string">"Salesforce/wikitext"</span>, <span class="hljs-string">"wikitext-103-raw-v1"</span>, split=<span class="hljs-string">"train+test+validation"</span>)`,lang:"python",wrap:!1}}),Z=new H({props:{code:"ZGVmJTIwYmF0Y2hfaXRlcmF0b3IoYmF0Y2hfc2l6ZSUzRDEwMDApJTNBJTBBJTIwJTIwJTIwJTIwJTIzJTIwT25seSUyMGtlZXAlMjB0aGUlMjB0ZXh0JTIwY29sdW1uJTIwdG8lMjBhdm9pZCUyMGRlY29kaW5nJTIwdGhlJTIwcmVzdCUyMG9mJTIwdGhlJTIwY29sdW1ucyUyMHVubmVjZXNzYXJpbHklMEElMjAlMjAlMjAlMjB0b2tfZGF0YXNldCUyMCUzRCUyMGRhdGFzZXQuc2VsZWN0X2NvbHVtbnMoJTIydGV4dCUyMiklMEElMjAlMjAlMjAlMjBmb3IlMjBiYXRjaCUyMGluJTIwdG9rX2RhdGFzZXQuaXRlcihiYXRjaF9zaXplKSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHlpZWxkJTIwYmF0Y2glNUIlMjJ0ZXh0JTIyJTVE",highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">batch_iterator</span>(<span class="hljs-params">batch_size=<span class="hljs-number">1000</span></span>): | |
| <span class="hljs-comment"># Only keep the text column to avoid decoding the rest of the columns unnecessarily</span> | |
| tok_dataset = dataset.select_columns(<span class="hljs-string">"text"</span>) | |
| <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> tok_dataset.<span class="hljs-built_in">iter</span>(batch_size): | |
| <span class="hljs-keyword">yield</span> batch[<span class="hljs-string">"text"</span>]`,lang:"python",wrap:!1}}),x=new H({props:{code:"dG9rZW5pemVyLnRyYWluX2Zyb21faXRlcmF0b3IoYmF0Y2hfaXRlcmF0b3IoKSUyQyUyMHRyYWluZXIlM0R0cmFpbmVyJTJDJTIwbGVuZ3RoJTNEbGVuKGRhdGFzZXQpKQ==",highlighted:'tokenizer.train_from_iterator(batch_iterator(), trainer=trainer, length=<span class="hljs-built_in">len</span>(dataset))',lang:"python",wrap:!1}}),_=new gt({props:{title:"Using gzip files",local:"using-gzip-files",headingTag:"h2"}}),W=new H({props:{code:"aW1wb3J0JTIwZ3ppcCUwQXdpdGglMjBnemlwLm9wZW4oJTIyZGF0YSUyRm15LWZpbGUuMC5neiUyMiUyQyUyMCUyMnJ0JTIyKSUyMGFzJTIwZiUzQSUwQSUyMCUyMCUyMCUyMHRva2VuaXplci50cmFpbl9mcm9tX2l0ZXJhdG9yKGYlMkMlMjB0cmFpbmVyJTNEdHJhaW5lcik=",highlighted:`<span class="hljs-keyword">import</span> gzip | |
| <span class="hljs-keyword">with</span> gzip.<span class="hljs-built_in">open</span>(<span class="hljs-string">"data/my-file.0.gz"</span>, <span class="hljs-string">"rt"</span>) <span class="hljs-keyword">as</span> f: | |
| tokenizer.train_from_iterator(f, trainer=trainer)`,lang:"python",wrap:!1}}),Q=new H({props:{code:"ZmlsZXMlMjAlM0QlMjAlNUIlMjJkYXRhJTJGbXktZmlsZS4wLmd6JTIyJTJDJTIwJTIyZGF0YSUyRm15LWZpbGUuMS5neiUyMiUyQyUyMCUyMmRhdGElMkZteS1maWxlLjIuZ3olMjIlNUQlMEFkZWYlMjBnemlwX2l0ZXJhdG9yKCklM0ElMEElMjAlMjAlMjAlMjBmb3IlMjBwYXRoJTIwaW4lMjBmaWxlcyUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHdpdGglMjBnemlwLm9wZW4ocGF0aCUyQyUyMCUyMnJ0JTIyKSUyMGFzJTIwZiUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZvciUyMGxpbmUlMjBpbiUyMGYlM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjB5aWVsZCUyMGxpbmUlMEF0b2tlbml6ZXIudHJhaW5fZnJvbV9pdGVyYXRvcihnemlwX2l0ZXJhdG9yKCklMkMlMjB0cmFpbmVyJTNEdHJhaW5lcik=",highlighted:`files = [<span class="hljs-string">"data/my-file.0.gz"</span>, <span class="hljs-string">"data/my-file.1.gz"</span>, <span class="hljs-string">"data/my-file.2.gz"</span>] | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">gzip_iterator</span>(): | |
| <span class="hljs-keyword">for</span> path <span class="hljs-keyword">in</span> files: | |
| <span class="hljs-keyword">with</span> gzip.<span class="hljs-built_in">open</span>(path, <span class="hljs-string">"rt"</span>) <span class="hljs-keyword">as</span> f: | |
| <span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> f: | |
| <span class="hljs-keyword">yield</span> line | |
| tokenizer.train_from_iterator(gzip_iterator(), trainer=trainer)`,lang:"python",wrap:!1}}),q=new Dt({props:{source:"https://github.com/huggingface/tokenizers/blob/main/docs/source-doc-builder/training_from_memory.mdx"}}),{c(){c=i("meta"),L=a(),Y=i("p"),E=a(),p(d.$$.fragment),N=a(),p(w.$$.fragment),P=a(),f=i("p"),f.innerHTML=kt,D=a(),U=i("p"),U.innerHTML=$t,K=a(),p(j.$$.fragment),O=a(),J=i("p"),J.innerHTML=It,tt=a(),b=i("p"),b.innerHTML=zt,et=a(),p(T.$$.fragment),lt=a(),g=i("p"),g.innerHTML=Gt,st=a(),p(C.$$.fragment),at=a(),k=i("p"),k.innerHTML=Xt,nt=a(),p($.$$.fragment),it=a(),I=i("p"),I.innerHTML=Zt,rt=a(),z=i("p"),z.textContent=vt,ot=a(),p(G.$$.fragment),pt=a(),X=i("p"),X.textContent=Bt,yt=a(),p(Z.$$.fragment),Mt=a(),v=i("p"),v.textContent=xt,mt=a(),B=i("p"),B.textContent=Rt,ht=a(),p(x.$$.fragment),ut=a(),R=i("p"),R.textContent=_t,ct=a(),p(_.$$.fragment),dt=a(),V=i("p"),V.textContent=Vt,wt=a(),p(W.$$.fragment),ft=a(),A=i("p"),A.textContent=Wt,Ut=a(),p(Q.$$.fragment),jt=a(),F=i("p"),F.textContent=At,Jt=a(),p(q.$$.fragment),bt=a(),S=i("p"),this.h()},l(t){const e=Et("svelte-u9bgzb",document.head);c=r(e,"META",{name:!0,content:!0}),e.forEach(l),L=n(t),Y=r(t,"P",{}),Qt(Y).forEach(l),E=n(t),y(d.$$.fragment,t),N=n(t),y(w.$$.fragment,t),P=n(t),f=r(t,"P",{"data-svelte-h":!0}),o(f)!=="svelte-zs4zcn"&&(f.innerHTML=kt),D=n(t),U=r(t,"P",{"data-svelte-h":!0}),o(U)!=="svelte-tkcjej"&&(U.innerHTML=$t),K=n(t),y(j.$$.fragment,t),O=n(t),J=r(t,"P",{"data-svelte-h":!0}),o(J)!=="svelte-14xwfcq"&&(J.innerHTML=It),tt=n(t),b=r(t,"P",{"data-svelte-h":!0}),o(b)!=="svelte-1cm91fx"&&(b.innerHTML=zt),et=n(t),y(T.$$.fragment,t),lt=n(t),g=r(t,"P",{"data-svelte-h":!0}),o(g)!=="svelte-800zef"&&(g.innerHTML=Gt),st=n(t),y(C.$$.fragment,t),at=n(t),k=r(t,"P",{"data-svelte-h":!0}),o(k)!=="svelte-2k2g93"&&(k.innerHTML=Xt),nt=n(t),y($.$$.fragment,t),it=n(t),I=r(t,"P",{"data-svelte-h":!0}),o(I)!=="svelte-tt7so"&&(I.innerHTML=Zt),rt=n(t),z=r(t,"P",{"data-svelte-h":!0}),o(z)!=="svelte-8j5br2"&&(z.textContent=vt),ot=n(t),y(G.$$.fragment,t),pt=n(t),X=r(t,"P",{"data-svelte-h":!0}),o(X)!=="svelte-134xe8u"&&(X.textContent=Bt),yt=n(t),y(Z.$$.fragment,t),Mt=n(t),v=r(t,"P",{"data-svelte-h":!0}),o(v)!=="svelte-18jpyth"&&(v.textContent=xt),mt=n(t),B=r(t,"P",{"data-svelte-h":!0}),o(B)!=="svelte-1ha2c1s"&&(B.textContent=Rt),ht=n(t),y(x.$$.fragment,t),ut=n(t),R=r(t,"P",{"data-svelte-h":!0}),o(R)!=="svelte-9wooxy"&&(R.textContent=_t),ct=n(t),y(_.$$.fragment,t),dt=n(t),V=r(t,"P",{"data-svelte-h":!0}),o(V)!=="svelte-rvsukz"&&(V.textContent=Vt),wt=n(t),y(W.$$.fragment,t),ft=n(t),A=r(t,"P",{"data-svelte-h":!0}),o(A)!=="svelte-1g7s20h"&&(A.textContent=Wt),Ut=n(t),y(Q.$$.fragment,t),jt=n(t),F=r(t,"P",{"data-svelte-h":!0}),o(F)!=="svelte-15ag4ju"&&(F.textContent=At),Jt=n(t),y(q.$$.fragment,t),bt=n(t),S=r(t,"P",{}),Qt(S).forEach(l),this.h()},h(){Ft(c,"name","hf:doc:metadata"),Ft(c,"content",Ot)},m(t,e){Nt(document.head,c),s(t,L,e),s(t,Y,e),s(t,E,e),M(d,t,e),s(t,N,e),M(w,t,e),s(t,P,e),s(t,f,e),s(t,D,e),s(t,U,e),s(t,K,e),M(j,t,e),s(t,O,e),s(t,J,e),s(t,tt,e),s(t,b,e),s(t,et,e),M(T,t,e),s(t,lt,e),s(t,g,e),s(t,st,e),M(C,t,e),s(t,at,e),s(t,k,e),s(t,nt,e),M($,t,e),s(t,it,e),s(t,I,e),s(t,rt,e),s(t,z,e),s(t,ot,e),M(G,t,e),s(t,pt,e),s(t,X,e),s(t,yt,e),M(Z,t,e),s(t,Mt,e),s(t,v,e),s(t,mt,e),s(t,B,e),s(t,ht,e),M(x,t,e),s(t,ut,e),s(t,R,e),s(t,ct,e),M(_,t,e),s(t,dt,e),s(t,V,e),s(t,wt,e),M(W,t,e),s(t,ft,e),s(t,A,e),s(t,Ut,e),M(Q,t,e),s(t,jt,e),s(t,F,e),s(t,Jt,e),M(q,t,e),s(t,bt,e),s(t,S,e),Tt=!0},p:Ht,i(t){Tt||(m(d.$$.fragment,t),m(w.$$.fragment,t),m(j.$$.fragment,t),m(T.$$.fragment,t),m(C.$$.fragment,t),m($.$$.fragment,t),m(G.$$.fragment,t),m(Z.$$.fragment,t),m(x.$$.fragment,t),m(_.$$.fragment,t),m(W.$$.fragment,t),m(Q.$$.fragment,t),m(q.$$.fragment,t),Tt=!0)},o(t){h(d.$$.fragment,t),h(w.$$.fragment,t),h(j.$$.fragment,t),h(T.$$.fragment,t),h(C.$$.fragment,t),h($.$$.fragment,t),h(G.$$.fragment,t),h(Z.$$.fragment,t),h(x.$$.fragment,t),h(_.$$.fragment,t),h(W.$$.fragment,t),h(Q.$$.fragment,t),h(q.$$.fragment,t),Tt=!1},d(t){t&&(l(L),l(Y),l(E),l(N),l(P),l(f),l(D),l(U),l(K),l(O),l(J),l(tt),l(b),l(et),l(lt),l(g),l(st),l(at),l(k),l(nt),l(it),l(I),l(rt),l(z),l(ot),l(pt),l(X),l(yt),l(Mt),l(v),l(mt),l(B),l(ht),l(ut),l(R),l(ct),l(dt),l(V),l(wt),l(ft),l(A),l(Ut),l(jt),l(F),l(Jt),l(bt),l(S)),l(c),u(d,t),u(w,t),u(j,t),u(T,t),u(C,t),u($,t),u(G,t),u(Z,t),u(x,t),u(_,t),u(W,t),u(Q,t),u(q,t)}}}const Ot='{"title":"Training from memory","local":"training-from-memory","sections":[{"title":"The most basic way","local":"the-most-basic-way","sections":[],"depth":2},{"title":"Using the 🤗 Datasets library","local":"using-the--datasets-library","sections":[],"depth":2},{"title":"Using gzip files","local":"using-gzip-files","sections":[],"depth":2}],"depth":1}';function te(Ct){return Yt(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class ne extends St{constructor(c){super(),Lt(this,c,te,Kt,qt,{})}}export{ne as component}; | |
Xet Storage Details
- Size:
- 15.8 kB
- Xet hash:
- 3a2bed4da8409e7d98b8d5e8a5d52fc551f8aeeb90ad487c43dec68e8c3feab6
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.