import{s as Qt,o as Rt,n as he}from"../chunks/scheduler.37c15a92.js";import{S as Zt,i as Vt,g as u,s as M,r as j,A as Nt,h as T,f as n,c as i,j as $t,u as U,x as y,k as gt,y as Et,a,v as m,t as J,b as Os,d as r,w as d,p as sl}from"../chunks/index.2bf4358c.js";import{T as be}from"../chunks/Tip.363c041f.js";import{Y as rl}from"../chunks/Youtube.1e50a667.js";import{C as h}from"../chunks/CodeBlock.4e987730.js";import{C as Bt}from"../chunks/CourseFloatingBanner.9ff4c771.js";import{F as qt}from"../chunks/FrameworkSwitchCourse.8d4d4ab6.js";import{H as fe,E as zt}from"../chunks/getInferenceSnippets.1837c472.js";function vt(w){let e,p;return e=new Bt({props:{chapter:3,classNames:"absolute z-10 right-0 top-0",notebooks:[{label:"Google Colab",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/zh-CN/chapter3/section2_tf.ipynb"},{label:"Aws Studio",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/zh-CN/chapter3/section2_tf.ipynb"}]}}),{c(){j(e.$$.fragment)},l(t){U(e.$$.fragment,t)},m(t,c){m(e,t,c),p=!0},i(t){p||(r(e.$$.fragment,t),p=!0)},o(t){J(e.$$.fragment,t),p=!1},d(t){d(e,t)}}}function Gt(w){let e,p;return e=new Bt({props:{chapter:3,classNames:"absolute z-10 right-0 top-0",notebooks:[{label:"Google Colab",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/zh-CN/chapter3/section2_pt.ipynb"},{label:"Aws Studio",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/zh-CN/chapter3/section2_pt.ipynb"}]}}),{c(){j(e.$$.fragment)},l(t){U(e.$$.fragment,t)},m(t,c){m(e,t,c),p=!0},i(t){p||(r(e.$$.fragment,t),p=!0)},o(t){J(e.$$.fragment,t),p=!1},d(t){d(e,t)}}}function xt(w){let e,p='在这一小节你将学习 <a href="/course/chapter2">第一小节</a> 中提到的“如何使用模型中心（hub）加载大型数据集”，下面是用模型中心的数据在 TensorFlow 上训练句子分类器的一个例子：',t,c,f;return c=new h({props:{code:"aW1wb3J0JTIwdGVuc29yZmxvdyUyMGFzJTIwdGYlMEFpbXBvcnQlMjBudW1weSUyMGFzJTIwbnAlMEFmcm9tJTIwdHJhbnNmb3JtZXJzJTIwaW1wb3J0JTIwQXV0b1Rva2VuaXplciUyQyUyMFRGQXV0b01vZGVsRm9yU2VxdWVuY2VDbGFzc2lmaWNhdGlvbiUwQSUwQSUyMyUyMCVFNSU5MiU4QyVFNCVCOSU4QiVFNSU4OSU4RCVFNCVCOCU4MCVFNiVBMCVCNyUwQWNoZWNrcG9pbnQlMjAlM0QlMjAlMjJiZXJ0LWJhc2UtdW5jYXNlZCUyMiUwQXRva2VuaXplciUyMCUzRCUyMEF1dG9Ub2tlbml6ZXIuZnJvbV9wcmV0cmFpbmVkKGNoZWNrcG9pbnQpJTBBbW9kZWwlMjAlM0QlMjBURkF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24uZnJvbV9wcmV0cmFpbmVkKGNoZWNrcG9pbnQpJTBBc2VxdWVuY2VzJTIwJTNEJTIwJTVCJTBBJTIwJTIwJTIwJTIwJTIySSd2ZSUyMGJlZW4lMjB3YWl0aW5nJTIwZm9yJTIwYSUyMEh1Z2dpbmdGYWNlJTIwY291cnNlJTIwbXklMjB3aG9sZSUyMGxpZmUuJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIyVGhpcyUyMGNvdXJzZSUyMGlzJTIwYW1hemluZyElMjIlMkMlMEElNUQlMEFiYXRjaCUyMCUzRCUyMGRpY3QodG9rZW5pemVyKHNlcXVlbmNlcyUyQyUyMHBhZGRpbmclM0RUcnVlJTJDJTIwdHJ1bmNhdGlvbiUzRFRydWUlMkMlMjByZXR1cm5fdGVuc29ycyUzRCUyMnRmJTIyKSklMEElMEElMjMlMjAlRTYlOTYlQjAlRTUlQTIlOUUlRTklODMlQTglRTUlODglODYlMEFtb2RlbC5jb21waWxlKG9wdGltaXplciUzRCUyMmFkYW0lMjIlMkMlMjBsb3NzJTNEJTIyc3BhcnNlX2NhdGVnb3JpY2FsX2Nyb3NzZW50cm9weSUyMiklMEFsYWJlbHMlMjAlM0QlMjB0Zi5jb252ZXJ0X3RvX3RlbnNvciglNUIxJTJDJTIwMSU1RCklMEFtb2RlbC50cmFpbl9vbl9iYXRjaChiYXRjaCUyQyUyMGxhYmVscyk=",highlighted:`<span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, TFAutoModelForSequenceClassification

<span class="hljs-comment"># 和之前一样</span>
checkpoint = <span class="hljs-string">&quot;bert-base-uncased&quot;</span>
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
model = TFAutoModelForSequenceClassification.from_pretrained(checkpoint)
sequences = [
    <span class="hljs-string">&quot;I&#x27;ve been waiting for a HuggingFace course my whole life.&quot;</span>,
    <span class="hljs-string">&quot;This course is amazing!&quot;</span>,
]
batch = <span class="hljs-built_in">dict</span>(tokenizer(sequences, padding=<span class="hljs-literal">True</span>, truncation=<span class="hljs-literal">True</span>, return_tensors=<span class="hljs-string">&quot;tf&quot;</span>))

<span class="hljs-comment"># 新增部分</span>
model.<span class="hljs-built_in">compile</span>(optimizer=<span class="hljs-string">&quot;adam&quot;</span>, loss=<span class="hljs-string">&quot;sparse_categorical_crossentropy&quot;</span>)
labels = tf.convert_to_tensor([<span class="hljs-number">1</span>, <span class="hljs-number">1</span>])
model.train_on_batch(batch, labels)`,wrap:!1}}),{c(){e=u("p"),e.innerHTML=p,t=M(),j(c.$$.fragment)},l(o){e=T(o,"P",{"data-svelte-h":!0}),y(e)!=="svelte-coydjb"&&(e.innerHTML=p),t=i(o),U(c.$$.fragment,o)},m(o,I){a(o,e,I),a(o,t,I),m(c,o,I),f=!0},i(o){f||(r(c.$$.fragment,o),f=!0)},o(o){J(c.$$.fragment,o),f=!1},d(o){o&&(n(e),n(t)),d(c,o)}}}function Xt(w){let e,p='在这一小节你将学习 <a href="/course/chapter2">第一小节</a> 中提到的“如何使用模型中心（hub）加载大型数据集”，下面是用模型中心的数据在 PyTorch 上训练句子分类器的一个例子：',t,c,f;return c=new h({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwdG9yY2gub3B0aW0lMjBpbXBvcnQlMjBBZGFtVyUwQWZyb20lMjB0cmFuc2Zvcm1lcnMlMjBpbXBvcnQlMjBBdXRvVG9rZW5pemVyJTJDJTIwQXV0b01vZGVsRm9yU2VxdWVuY2VDbGFzc2lmaWNhdGlvbiUwQSUwQSUyMyUyMCVFNSU5MiU4QyVFNCVCOSU4QiVFNSU4OSU4RCVFNCVCOCU4MCVFNiVBMCVCNyUwQWNoZWNrcG9pbnQlMjAlM0QlMjAlMjJiZXJ0LWJhc2UtdW5jYXNlZCUyMiUwQXRva2VuaXplciUyMCUzRCUyMEF1dG9Ub2tlbml6ZXIuZnJvbV9wcmV0cmFpbmVkKGNoZWNrcG9pbnQpJTBBbW9kZWwlMjAlM0QlMjBBdXRvTW9kZWxGb3JTZXF1ZW5jZUNsYXNzaWZpY2F0aW9uLmZyb21fcHJldHJhaW5lZChjaGVja3BvaW50KSUwQXNlcXVlbmNlcyUyMCUzRCUyMCU1QiUwQSUyMCUyMCUyMCUyMCUyMkkndmUlMjBiZWVuJTIwd2FpdGluZyUyMGZvciUyMGElMjBIdWdnaW5nRmFjZSUyMGNvdXJzZSUyMG15JTIwd2hvbGUlMjBsaWZlLiUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMlRoaXMlMjBjb3Vyc2UlMjBpcyUyMGFtYXppbmchJTIyJTJDJTBBJTVEJTBBYmF0Y2glMjAlM0QlMjB0b2tlbml6ZXIoc2VxdWVuY2VzJTJDJTIwcGFkZGluZyUzRFRydWUlMkMlMjB0cnVuY2F0aW9uJTNEVHJ1ZSUyQyUyMHJldHVybl90ZW5zb3JzJTNEJTIycHQlMjIpJTBBJTBBJTIzJTIwJUU2JTk2JUIwJUU1JUEyJTlFJUU5JTgzJUE4JUU1JTg4JTg2JTBBYmF0Y2glNUIlMjJsYWJlbHMlMjIlNUQlMjAlM0QlMjB0b3JjaC50ZW5zb3IoJTVCMSUyQyUyMDElNUQpJTBBJTBBb3B0aW1pemVyJTIwJTNEJTIwQWRhbVcobW9kZWwucGFyYW1ldGVycygpKSUwQWxvc3MlMjAlM0QlMjBtb2RlbCgqKmJhdGNoKS5sb3NzJTBBbG9zcy5iYWNrd2FyZCgpJTBBb3B0aW1pemVyLnN0ZXAoKQ==",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> torch.optim <span class="hljs-keyword">import</span> AdamW
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModelForSequenceClassification

<span class="hljs-comment"># 和之前一样</span>
checkpoint = <span class="hljs-string">&quot;bert-base-uncased&quot;</span>
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
model = AutoModelForSequenceClassification.from_pretrained(checkpoint)
sequences = [
    <span class="hljs-string">&quot;I&#x27;ve been waiting for a HuggingFace course my whole life.&quot;</span>,
    <span class="hljs-string">&quot;This course is amazing!&quot;</span>,
]
batch = tokenizer(sequences, padding=<span class="hljs-literal">True</span>, truncation=<span class="hljs-literal">True</span>, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>)

<span class="hljs-comment"># 新增部分</span>
batch[<span class="hljs-string">&quot;labels&quot;</span>] = torch.tensor([<span class="hljs-number">1</span>, <span class="hljs-number">1</span>])

optimizer = AdamW(model.parameters())
loss = model(**batch).loss
loss.backward()
optimizer.step()`,wrap:!1}}),{c(){e=u("p"),e.innerHTML=p,t=M(),j(c.$$.fragment)},l(o){e=T(o,"P",{"data-svelte-h":!0}),y(e)!=="svelte-1762k8p"&&(e.innerHTML=p),t=i(o),U(c.$$.fragment,o)},m(o,I){a(o,e,I),a(o,t,I),m(c,o,I),f=!0},i(o){f||(r(c.$$.fragment,o),f=!0)},o(o){J(c.$$.fragment,o),f=!1},d(o){o&&(n(e),n(t)),d(c,o)}}}function Wt(w){let e,p;return e=new rl({props:{id:"W_gMJF0xomE"}}),{c(){j(e.$$.fragment)},l(t){U(e.$$.fragment,t)},m(t,c){m(e,t,c),p=!0},i(t){p||(r(e.$$.fragment,t),p=!0)},o(t){J(e.$$.fragment,t),p=!1},d(t){d(e,t)}}}function Ht(w){let e,p;return e=new rl({props:{id:"_BZearw7f0w"}}),{c(){j(e.$$.fragment)},l(t){U(e.$$.fragment,t)},m(t,c){m(e,t,c),p=!0},i(t){p||(r(e.$$.fragment,t),p=!0)},o(t){J(e.$$.fragment,t),p=!1},d(t){d(e,t)}}}function Ft(w){let e,p="⚠️ <strong>警告</strong> 确保你已经运行 <code>pip install datasets</code> 安装了 <code>datasets</code>。然后，再继续下面的加载 MRPC 数据集和打印出来查看其内容。";return{c(){e=u("p"),e.innerHTML=p},l(t){e=T(t,"P",{"data-svelte-h":!0}),y(e)!=="svelte-i8h2qh"&&(e.innerHTML=p)},m(t,c){a(t,e,c)},p:he,d(t){t&&n(e)}}}function St(w){let e,p="✏️ <strong>试试看！</strong> 查看训练集的第 15 行元素和验证集的 87 行元素。他们的标签是什么？";return{c(){e=u("p"),e.innerHTML=p},l(t){e=T(t,"P",{"data-svelte-h":!0}),y(e)!=="svelte-uixrea"&&(e.innerHTML=p)},m(t,c){a(t,e,c)},p:he,d(t){t&&n(e)}}}function Yt(w){let e,p;return e=new rl({props:{id:"P-rZWqcB6CE"}}),{c(){j(e.$$.fragment)},l(t){U(e.$$.fragment,t)},m(t,c){m(e,t,c),p=!0},i(t){p||(r(e.$$.fragment,t),p=!0)},o(t){J(e.$$.fragment,t),p=!1},d(t){d(e,t)}}}function Dt(w){let e,p;return e=new rl({props:{id:"0u3ioSwev3s"}}),{c(){j(e.$$.fragment)},l(t){U(e.$$.fragment,t)},m(t,c){m(e,t,c),p=!0},i(t){p||(r(e.$$.fragment,t),p=!0)},o(t){J(e.$$.fragment,t),p=!1},d(t){d(e,t)}}}function Lt(w){let e,p="✏️ <strong>试试看！</strong> 选取训练集中的第 15 个元素，将两句话分别进行tokenization。结果和上方的例子有什么不同？";return{c(){e=u("p"),e.innerHTML=p},l(t){e=T(t,"P",{"data-svelte-h":!0}),y(e)!=="svelte-bnppb5"&&(e.innerHTML=p)},m(t,c){a(t,e,c)},p:he,d(t){t&&n(e)}}}function Pt(w){let e,p="负责在批处理中将数据整理为一个 batch 的函数称为 <code>collate 函数</code> 。默认的拼合函数只会将你的样本转换为 <code>tf.Tensor</code> 并将它们拼接起来（如果你的元素是列表、元组或字典，则会使用递归进行拼接）。这在本例中是不可行的，因为我们的输入不是都是相同大小的。我们特意推迟了填充时间，只在每个 batch 上进行填充，以避免有太多填充的过长的输入。这将大大加快训练速度，但请注意，如果你在 TPU 上训练，需要注意一个问题——TPU 喜欢固定的形状，即使这需要额外填充很多无用的 token。";return{c(){e=u("p"),e.innerHTML=p},l(t){e=T(t,"P",{"data-svelte-h":!0}),y(e)!=="svelte-1odwpu8"&&(e.innerHTML=p)},m(t,c){a(t,e,c)},d(t){t&&n(e)}}}function Kt(w){let e,p="负责在批处理中将数据整理为一个 batch 的函数称为 <code>collate 函数</code> 。这是一个可以在构建 <code>DataLoader</code> 时传递的一个参数，默认是一个将你的数据集转换为 PyTorch 张量并将它们拼接起来的函数（如果你的元素是列表、元组或字典，则会使用递归进行拼接）。这在本例子中下是不可行的，因为我们的输入的大小可能是不相同的。我们特意推迟了填充的时间，只在每个 batch 上进行必要的填充，以避免出现有大量填充的过长输入。这将大大加快训练速度，但请注意，如果你在 TPU 上训练，需要注意一个问题——TPU 喜欢固定的形状，即使这需要额外填充很多无用的 token。";return{c(){e=u("p"),e.innerHTML=p},l(t){e=T(t,"P",{"data-svelte-h":!0}),y(e)!=="svelte-1ohb3u7"&&(e.innerHTML=p)},m(t,c){a(t,e,c)},d(t){t&&n(e)}}}function Ot(w){let e,p;return e=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMERhdGFDb2xsYXRvcldpdGhQYWRkaW5nJTBBJTBBZGF0YV9jb2xsYXRvciUyMCUzRCUyMERhdGFDb2xsYXRvcldpdGhQYWRkaW5nKHRva2VuaXplciUzRHRva2VuaXplciUyQyUyMHJldHVybl90ZW5zb3JzJTNEJTIydGYlMjIp",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DataCollatorWithPadding

data_collator = DataCollatorWithPadding(tokenizer=tokenizer, return_tensors=<span class="hljs-string">&quot;tf&quot;</span>)`,wrap:!1}}),{c(){j(e.$$.fragment)},l(t){U(e.$$.fragment,t)},m(t,c){m(e,t,c),p=!0},i(t){p||(r(e.$$.fragment,t),p=!0)},o(t){J(e.$$.fragment,t),p=!1},d(t){d(e,t)}}}function sn(w){let e,p;return e=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMERhdGFDb2xsYXRvcldpdGhQYWRkaW5nJTBBJTBBZGF0YV9jb2xsYXRvciUyMCUzRCUyMERhdGFDb2xsYXRvcldpdGhQYWRkaW5nKHRva2VuaXplciUzRHRva2VuaXplcik=",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DataCollatorWithPadding

data_collator = DataCollatorWithPadding(tokenizer=tokenizer)`,wrap:!1}}),{c(){j(e.$$.fragment)},l(t){U(e.$$.fragment,t)},m(t,c){m(e,t,c),p=!0},i(t){p||(r(e.$$.fragment,t),p=!0)},o(t){J(e.$$.fragment,t),p=!1},d(t){d(e,t)}}}function ln(w){let e,p,t,c="看起来不错！现在，我们已经从原始文本转化为了模型可以处理的数据，我们准备好对其进行微调。",f;return e=new h({props:{code:"JTdCJTBBJTIwJTIwJTIwJTIwJTIyYXR0ZW50aW9uX21hc2slMjIlM0ElMjB0b3JjaC5TaXplKCU1QjglMkMlMjA2NyU1RCklMkMlMEElMjAlMjAlMjAlMjAlMjJpbnB1dF9pZHMlMjIlM0ElMjB0b3JjaC5TaXplKCU1QjglMkMlMjA2NyU1RCklMkMlMEElMjAlMjAlMjAlMjAlMjJ0b2tlbl90eXBlX2lkcyUyMiUzQSUyMHRvcmNoLlNpemUoJTVCOCUyQyUyMDY3JTVEKSUyQyUwQSUyMCUyMCUyMCUyMCUyMmxhYmVscyUyMiUzQSUyMHRvcmNoLlNpemUoJTVCOCU1RCklMkMlMEElN0Q=",highlighted:`{
    <span class="hljs-string">&quot;attention_mask&quot;</span>: torch.Size([<span class="hljs-number">8</span>, <span class="hljs-number">67</span>]),
    <span class="hljs-string">&quot;input_ids&quot;</span>: torch.Size([<span class="hljs-number">8</span>, <span class="hljs-number">67</span>]),
    <span class="hljs-string">&quot;token_type_ids&quot;</span>: torch.Size([<span class="hljs-number">8</span>, <span class="hljs-number">67</span>]),
    <span class="hljs-string">&quot;labels&quot;</span>: torch.Size([<span class="hljs-number">8</span>]),
}`,wrap:!1}}),{c(){j(e.$$.fragment),p=M(),t=u("p"),t.textContent=c},l(o){U(e.$$.fragment,o),p=i(o),t=T(o,"P",{"data-svelte-h":!0}),y(t)!=="svelte-jptunb"&&(t.textContent=c)},m(o,I){m(e,o,I),a(o,p,I),a(o,t,I),f=!0},i(o){f||(r(e.$$.fragment,o),f=!0)},o(o){J(e.$$.fragment,o),f=!1},d(o){o&&(n(p),n(t)),d(e,o)}}}function en(w){let e,p;return e=new h({props:{code:"JTdCJTBBJTIwJTIwJTIwJTIwJTIyYXR0ZW50aW9uX21hc2slMjIlM0ElMjBUZW5zb3JTaGFwZSglNUI4JTJDJTIwNjclNUQpJTJDJTBBJTIwJTIwJTIwJTIwJTIyaW5wdXRfaWRzJTIyJTNBJTIwVGVuc29yU2hhcGUoJTVCOCUyQyUyMDY3JTVEKSUyQyUwQSUyMCUyMCUyMCUyMCUyMnRva2VuX3R5cGVfaWRzJTIyJTNBJTIwVGVuc29yU2hhcGUoJTVCOCUyQyUyMDY3JTVEKSUyQyUwQSUyMCUyMCUyMCUyMCUyMmxhYmVscyUyMiUzQSUyMFRlbnNvclNoYXBlKCU1QjglNUQpJTJDJTBBJTdE",highlighted:`{
    <span class="hljs-string">&quot;attention_mask&quot;</span>: TensorShape([<span class="hljs-number">8</span>, <span class="hljs-number">67</span>]),
    <span class="hljs-string">&quot;input_ids&quot;</span>: TensorShape([<span class="hljs-number">8</span>, <span class="hljs-number">67</span>]),
    <span class="hljs-string">&quot;token_type_ids&quot;</span>: TensorShape([<span class="hljs-number">8</span>, <span class="hljs-number">67</span>]),
    <span class="hljs-string">&quot;labels&quot;</span>: TensorShape([<span class="hljs-number">8</span>]),
}`,wrap:!1}}),{c(){j(e.$$.fragment)},l(t){U(e.$$.fragment,t)},m(t,c){m(e,t,c),p=!0},i(t){p||(r(e.$$.fragment,t),p=!0)},o(t){J(e.$$.fragment,t),p=!1},d(t){d(e,t)}}}function tn(w){let e,p="✏️ <strong>试试看！</strong> 在 GLUE SST-2 数据集上复刻上述预处理。它有点不同，因为它是由单句而不是成对的句子组成的，但是我们所做的其他事情看起来应该是一样的。另一个进阶的挑战是尝试编写一个可用于任何 GLUE 任务的预处理函数。";return{c(){e=u("p"),e.innerHTML=p},l(t){e=T(t,"P",{"data-svelte-h":!0}),y(e)!=="svelte-ktaex5"&&(e.innerHTML=p)},m(t,c){a(t,e,c)},p:he,d(t){t&&n(e)}}}function At(w){let e,p="现在我们有了 dataset 和 data collator，我们需要使用 data collator 批量地处理 dataset。我们可以手动加载批次并进行整合，但这需要大量工作，性能也有可能不好。相反，有一个简单的方法为这个问题提供高效的解决方案： <code>to_tf_dataset()</code> 。它将把你的数据集包装一个 <code>tf.data.Dataset</code> 类中，这个方法带有一个可选的 data collator 功能。 <code>tf.data.Dataset</code> 是 TensorFlow 的本地格式，Keras 可以直接用它来进行 <code>model.fit()</code> ，因此这种方法会立即将🤗 Dataset 转换为可用于训练的格式。让我们用我们的数据集演示一下这个方法！",t,c,f,o,I="就是这样！我们可以把这些数据集带入下一节，在经过所有艰苦的数据预处理工作之后，训练将变得非常简单和愉快。",E;return c=new h({props:{code:"dGZfdHJhaW5fZGF0YXNldCUyMCUzRCUyMHRva2VuaXplZF9kYXRhc2V0cyU1QiUyMnRyYWluJTIyJTVELnRvX3RmX2RhdGFzZXQoJTBBJTIwJTIwJTIwJTIwY29sdW1ucyUzRCU1QiUyMmF0dGVudGlvbl9tYXNrJTIyJTJDJTIwJTIyaW5wdXRfaWRzJTIyJTJDJTIwJTIydG9rZW5fdHlwZV9pZHMlMjIlNUQlMkMlMEElMjAlMjAlMjAlMjBsYWJlbF9jb2xzJTNEJTVCJTIybGFiZWxzJTIyJTVEJTJDJTBBJTIwJTIwJTIwJTIwc2h1ZmZsZSUzRFRydWUlMkMlMEElMjAlMjAlMjAlMjBjb2xsYXRlX2ZuJTNEZGF0YV9jb2xsYXRvciUyQyUwQSUyMCUyMCUyMCUyMGJhdGNoX3NpemUlM0Q4JTJDJTBBKSUwQSUwQXRmX3ZhbGlkYXRpb25fZGF0YXNldCUyMCUzRCUyMHRva2VuaXplZF9kYXRhc2V0cyU1QiUyMnZhbGlkYXRpb24lMjIlNUQudG9fdGZfZGF0YXNldCglMEElMjAlMjAlMjAlMjBjb2x1bW5zJTNEJTVCJTIyYXR0ZW50aW9uX21hc2slMjIlMkMlMjAlMjJpbnB1dF9pZHMlMjIlMkMlMjAlMjJ0b2tlbl90eXBlX2lkcyUyMiU1RCUyQyUwQSUyMCUyMCUyMCUyMGxhYmVsX2NvbHMlM0QlNUIlMjJsYWJlbHMlMjIlNUQlMkMlMEElMjAlMjAlMjAlMjBzaHVmZmxlJTNERmFsc2UlMkMlMEElMjAlMjAlMjAlMjBjb2xsYXRlX2ZuJTNEZGF0YV9jb2xsYXRvciUyQyUwQSUyMCUyMCUyMCUyMGJhdGNoX3NpemUlM0Q4JTJDJTBBKQ==",highlighted:`tf_train_dataset = tokenized_datasets[<span class="hljs-string">&quot;train&quot;</span>].to_tf_dataset(
    columns=[<span class="hljs-string">&quot;attention_mask&quot;</span>, <span class="hljs-string">&quot;input_ids&quot;</span>, <span class="hljs-string">&quot;token_type_ids&quot;</span>],
    label_cols=[<span class="hljs-string">&quot;labels&quot;</span>],
    shuffle=<span class="hljs-literal">True</span>,
    collate_fn=data_collator,
    batch_size=<span class="hljs-number">8</span>,
)

tf_validation_dataset = tokenized_datasets[<span class="hljs-string">&quot;validation&quot;</span>].to_tf_dataset(
    columns=[<span class="hljs-string">&quot;attention_mask&quot;</span>, <span class="hljs-string">&quot;input_ids&quot;</span>, <span class="hljs-string">&quot;token_type_ids&quot;</span>],
    label_cols=[<span class="hljs-string">&quot;labels&quot;</span>],
    shuffle=<span class="hljs-literal">False</span>,
    collate_fn=data_collator,
    batch_size=<span class="hljs-number">8</span>,
)`,wrap:!1}}),{c(){e=u("p"),e.innerHTML=p,t=M(),j(c.$$.fragment),f=M(),o=u("p"),o.textContent=I},l(b){e=T(b,"P",{"data-svelte-h":!0}),y(e)!=="svelte-rc4442"&&(e.innerHTML=p),t=i(b),U(c.$$.fragment,b),f=i(b),o=T(b,"P",{"data-svelte-h":!0}),y(o)!=="svelte-18ygwq1"&&(o.textContent=I)},m(b,k){a(b,e,k),a(b,t,k),m(c,b,k),a(b,f,k),a(b,o,k),E=!0},i(b){E||(r(c.$$.fragment,b),E=!0)},o(b){J(c.$$.fragment,b),E=!1},d(b){b&&(n(e),n(t),n(f),n(o)),d(c,b)}}}function nn(w){let e,p,t,c,f,o,I,E,b,k,ll,_,$,el,X,Ae="然而仅用两句话训练模型不会产生很好的效果，你需要准备一个更大的数据集才能得到更好的训练结果。",ol,W,Be='在本节中，我们以 MRPC（微软研究院释义语料库）数据集为例，该数据集由威廉·多兰和克里斯·布罗克特在 <a href="https://www.aclweb.org/anthology/I05-5002.pdf" rel="nofollow">这篇文章</a> 发布，由 5801 对句子组成，每个句子对带有一个标签来指示它们是否为同义（即两个句子的意思相同）。在本章选择该数据集的原因是它的数据体量小，容易对其进行训练。',Jl,H,ul,g,A,tl,F,Qe='模型中心（hub）不仅仅包含模型，还有许多别的语言的数据集。访问 <a href="https://huggingface.co/datasets" rel="nofollow">Datasets</a> 的链接即可进行浏览。我们建议你在完成本节的学习后阅读一下 <a href="https://huggingface.co/docs/datasets/loading" rel="nofollow">加载和处理新的数据集</a> 这篇文章，这会让你对 huggingface 的数据集理解更加清晰。现在让我们使用 MRPC 数据集中的 <a href="https://gluebenchmark.com" rel="nofollow">GLUE 基准测试数据集</a> 作为我们训练所使用的数据集，它是构成 MRPC 数据集的 10 个数据集之一，作为一个用于衡量机器学习模型在 10 个不同文本分类任务中性能的学术基准。',Tl,S,Re="🤗 Datasets 库提供了一条非常便捷的命令，可以在模型中心（hub）上下载和缓存数据集。你可以以下代码下载 MRPC 数据集：",yl,z,jl,Y,Ul,D,ml,L,Ze="现在我们获得了一个 <code>DatasetDict</code> 对象，这个对象包含训练集、验证集和测试集。每一个集合都包含 4 个列（ <code>sentence1</code> ， <code>sentence2</code> ， <code>label</code> 和 <code>idx</code> ）以及一个代表行数的变量（每个集合中的行的个数）。运行结果显示该训练集中有 3668 对句子，验证集中有 408 对，测试集中有 1725 对。",dl,P,Ve="默认情况下，该命令会下载数据集并缓存到 <code>~/.cache/huggingface/datasets</code> 。回想在第 2 章中我们学到过，可以通过设置 <code>HF_HOME</code> 环境变量来自定义缓存的文件夹。",wl,K,Ne="我们可以访问该数据集中的每一个 <code>raw_train_dataset</code> 对象，例如使用字典：",bl,O,fl,ss,hl,ls,Ee="现在可以看到标签已经是整数了，因此不需要对标签做任何预处理。如果想要知道不同数字对应标签的实际含义，我们可以查看 <code>raw_train_dataset</code> 的 <code>features</code> 。这告诉我们每列的类型：",Il,es,Cl,ts,kl,ns,qe="上面的例子中的 <code>Label（标签）</code> 是一种 <code>ClassLabel（分类标签）</code> ，也就是使用整数建立起类别标签的映射关系。 <code>0</code> 对应于 <code>not_equivalent（非同义）</code> ， <code>1</code> 对应于 <code>equivalent（同义）</code> 。",_l,v,$l,as,gl,B,Q,nl,ps,ze='为了预处理数据集，我们需要将文本转换为模型能够理解的数字。在 <a href="/course/chapter2">第二章</a> 我们已经学习过。这是通过一个 Tokenizer 完成的，我们可以向 Tokenizer 输入一个句子或一个句子列表。以下代码表示对每对句子中的所有第一句和所有第二句进行 tokenize：',Al,Ms,Bl,is,ve="不过在将两句话传递给模型，预测这两句话是否是同义之前，我们需要给这两句话依次进行适当的预处理。Tokenizer 不仅仅可以输入单个句子，还可以输入一组句子，并按照 BERT 模型所需要的输入进行处理：",Ql,cs,Rl,rs,Zl,os,Ge='我们在 <a href="/course/chapter2">第二章</a> 讨论了 <code>输入词id(input_ids)</code> 和 <code>注意力遮罩(attention_mask)</code> ，但尚未讨论 <code>token类型ID(token_type_ids)</code> 。在本例中， <code>token类型ID(token_type_ids)</code> 的作用就是告诉模型输入的哪一部分是第一句，哪一部分是第二句。',Vl,G,Nl,Js,xe="如果将 <code>input_ids</code> 中的 id 转换回文字：",El,us,ql,Ts,Xe="将得到：",zl,ys,vl,js,We="所以我们看到模型需要输入的形式是 <code>[CLS] sentence1 [SEP] sentence2 [SEP]</code> 。所以当有两句话的时候， <code>token类型ID(token_type_ids)</code> 的值是：",Gl,Us,xl,ms,He="现在输入中 <code>[CLS] sentence1 [SEP]</code> 它们的 <code>token_type_ids</code> 均为 <code>0</code> ，而其他部分例如 <code>sentence2 [SEP]</code> ，所有的 <code>token_type_ids</code> 均为 <code>1</code> 。",Xl,ds,Fe="请注意，如果选择其他的 checkpoint，不一定具有 <code>token_type_ids</code> ，比如，DistilBERT 模型就不会返回。只有当 tokenizer 在预训练期间使用过这一层，也就是模型在构建时需要它们时，才会返回 <code>token_type_ids</code> 。",Wl,ws,Se='在这里，BERT 使用了带有 <code>token_type_ids</code> 的预训练 tokenizer，除了我们在 <a href="/course/chapter1">第一章</a> 中讨论的掩码语言建模，还有一个额外的应用类型称为“下一句预测”。这个任务的目标是对句子对之间的关系进行建模。',Hl,bs,Ye="在下一句预测任务中，会给模型输入成对的句子（带有随机遮罩的 token），并要求预测第二个句子是否紧跟第一个句子。为了使任务具有挑战性，提高模型的泛化能力，数据集中一有一半句子对中的句子在原始文档中顺序排列，另一半句子对中的两个句子来自两个不同的文档。",Fl,fs,De="一般来说无需要担心在你的输入中是否需要有 <code>token_type_ids</code> 。只要你使用相同的 checkpoint 的 Tokenizer 和模型，Tokenizer 就会知道向模型提供什么，一切都会顺利进行。",Sl,hs,Le='现在我们已经了解了 Tokenizer 如何处理一对句子，我们可以用它来处理整个数据集：就像在 <a href="/course/chapter2">第二章</a> 中一样，我们可以给 Tokenizer 提供一对句子，第一个参数是它第一个句子的列表，第二个参数是第二个句子的列表。这也与我们在 <a href="/course/chapter2">第二章</a> 中看到的填充和截断选项兼容。因此预处理训练数据集的一种方法是：',Yl,Is,Dl,Cs,Pe='这种方法虽然有效，但有一个缺点是它返回的是一个字典（字典的键是 <code>输入词id(input_ids)</code> ， <code>注意力遮罩(attention_mask)</code> 和 <code>token类型ID(token_type_ids)</code> ，字典的值是键所对应值的列表）。这意味着在转换过程中要有足够的内存来存储整个数据集才不会出错。不过来自🤗 Datasets 库中的数据集是以 <a href="https://arrow.apache.org" rel="nofollow">Apache Arrow</a> 格式存储在磁盘上的，因此你只需将接下来要用的数据加载在内存中，而不是加载整个数据集，这对内存容量的需求比较友好。',Ll,ks,Ke='我们将使用 <a href="https://huggingface.co/docs/datasets/package_reference/main_classes#datasets.Dataset.map" rel="nofollow">Dataset.map()</a> 方法将数据保存为 dataset 格式，如果我们需要做更多的预处理而不仅仅是 tokenization 它还支持了一些额外的自定义的方法。 <code>map()</code> 方法的工作原理是使用一个函数处理数据集的每个元素。让我们定义一个对输入进行 tokenize 的函数：',Pl,_s,Kl,$s,Oe='该函数接收一个字典（与 dataset 的项类似）并返回一个包含 <code>输入词id(input_ids)</code> ， <code>注意力遮罩(attention_mask)</code> 和 <code>token_type_ids</code> 键的新字典。请注意，如果 <code>example</code> 字典所对应的值包含多个句子（每个键作为一个句子列表），那么它依然可以运行，就像前面的例子一样， <code>tokenizer</code> 可以处理成对的句子列表，这样的话我们可以在调用 <code>map()</code> 时使用该选项 <code>batched=True</code> ，这将显著加快处理的速度。 <code>tokenizer</code> 来自 <a href="https://github.com/huggingface/tokenizers" rel="nofollow">🤗 Tokenizers</a> 库，由 Rust 编写而成。当一次给它很多输入时，这个 <code>tokenizer</code> 可以处理地非常快。',Ol,gs,st="请注意，我们暂时在 <code>tokenize_function</code> 中省略了 padding 参数。这是因为将所有的样本填充到最大长度有些浪费。一个更好的做法是：在构建 batch 的时候。这样我们只需要填充到每个 batch 中的最大长度，而不是整个数据集的最大长度。当输入长度不稳定时，这可以节省大量时间和处理能力！",se,As,lt="下面是我们如何使用一次性 <code>tokenize_function</code> 处理整个数据集。我们在调用 <code>map</code> 时使用了 <code>batch =True</code> ，这样函数就可以同时处理数据集的多个元素，而不是分别处理每个元素，这样可以更快进行预处理。",le,Bs,et="以下是如何使用 tokenization 函数处理我们的整个数据集的方法。我们在调用 map 时使用了 <code>batched=True</code> ，因此该函数会一次性处理数据集的多个元素，而不是单独处理每个元素。这样可以实现更快的预处理。",ee,Qs,te,Rs,tt="🤗Datasets 库进行这种处理的方式是向数据集添加新的字段，每个字段对应预处理函数返回的字典中的每个键：",ne,Zs,ae,Vs,nt="在使用预处理函数 <code>map()</code> 时，甚至可以通过传递 <code>num_proc</code> 参数并行处理。我们在这里没有这样做，因为在这个例子中🤗 Tokenizers 库已经使用多线程来更快地对样本 tokenize，但是如果没有使用该库支持的快速 tokenizer，使用 <code>num_proc</code> 可能会加快预处理。",pe,Ns,at="我们的 <code>tokenize_function</code> 返回包含 <code>输入词id(input_ids)</code> ， <code>注意力遮罩(attention_mask)</code> 和 <code>token_type_ids</code> 键的字典，这三个字段被添加到数据集的三个集合里（训练集、验证集和测试集）。请注意，如果预处理函数 <code>map()</code> 为现有键返回一个新值，我们可以通过使用 <code>map()</code> 函数返回的新值修改现有的字段。",Me,Es,pt="我们最后需要做的是将所有示例填充到该 batch 中最长元素的长度，这种技术被称为动态填充。",ie,qs,ce,zs,re,al,vs,Mt="为了解决句子长度不统一的问题，我们必须定义一个 collate 函数，该函数会将每个 batch 句子填充到正确的长度。幸运的是，🤗transformer 库通过 <code>DataCollatorWithPadding</code> 为我们提供了这样一个函数。当你实例化它时，它需要一个 tokenizer （用来知道使用哪种填充 token 以及模型期望在输入的左边填充还是右边填充），然后它会自动完成所有需要的操作：",oe,R,Z,pl,Gs,it="为了测试这个新东西，让我们从我们的训练集中抽取几个样本，在这里，我们删除列 <code>idx</code> ， <code>sentence1</code> 和 <code>sentence2</code> ，因为不需要它们，而且删除包含字符串的列（我们不能用字符串创建张量），然后查看一个 batch 中每个条目的长度：",Je,xs,ue,Xs,Te,Ws,ct="不出所料，我们得到了不同长度的样本，从 32 到 67。动态填充意味着这个 batch 都应该填充到长度为 67，这是这个 batch 中的最大长度。如果没有动态填充，所有的样本都必须填充到整个数据集中的最大长度，或者模型可以接受的最大长度。让我们再次检查 <code>data_collator</code> 是否正确地动态填充了这批样本：",ye,Hs,je,V,N,Ml,x,Ue,il,Fs,me,cl,de;f=new qt({props:{fw:w[0]}}),I=new fe({props:{title:"处理数据",local:"处理数据",headingTag:"h1"}});const rt=[Gt,vt],Ss=[];function ot(s,l){return s[0]==="pt"?0:1}b=ot(w),k=Ss[b]=rt[b](w);const Jt=[Xt,xt],Ys=[];function ut(s,l){return s[0]==="pt"?0:1}_=ut(w),$=Ys[_]=Jt[_](w),H=new fe({props:{title:"从模型中心（Hub）加载数据集",local:"从模型中心（Hub）加载数据集",headingTag:"h2"}});const Tt=[Ht,Wt],Ds=[];function yt(s,l){return s[0]==="pt"?0:1}g=yt(w),A=Ds[g]=Tt[g](w),z=new be({props:{$$slots:{default:[Ft]},$$scope:{ctx:w}}}),Y=new h({props:{code:"ZnJvbSUyMGRhdGFzZXRzJTIwaW1wb3J0JTIwbG9hZF9kYXRhc2V0JTBBJTBBcmF3X2RhdGFzZXRzJTIwJTNEJTIwbG9hZF9kYXRhc2V0KCUyMmdsdWUlMjIlMkMlMjAlMjJtcnBjJTIyKSUwQXJhd19kYXRhc2V0cw==",highlighted:`<span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset

raw_datasets = load_dataset(<span class="hljs-string">&quot;glue&quot;</span>, <span class="hljs-string">&quot;mrpc&quot;</span>)
raw_datasets`,wrap:!1}}),D=new h({props:{code:"RGF0YXNldERpY3QoJTdCJTBBJTIwJTIwJTIwJTIwdHJhaW4lM0ElMjBEYXRhc2V0KCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZlYXR1cmVzJTNBJTIwJTVCJ3NlbnRlbmNlMSclMkMlMjAnc2VudGVuY2UyJyUyQyUyMCdsYWJlbCclMkMlMjAnaWR4JyU1RCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG51bV9yb3dzJTNBJTIwMzY2OCUwQSUyMCUyMCUyMCUyMCU3RCklMEElMjAlMjAlMjAlMjB2YWxpZGF0aW9uJTNBJTIwRGF0YXNldCglN0IlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBmZWF0dXJlcyUzQSUyMCU1QidzZW50ZW5jZTEnJTJDJTIwJ3NlbnRlbmNlMiclMkMlMjAnbGFiZWwnJTJDJTIwJ2lkeCclNUQlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBudW1fcm93cyUzQSUyMDQwOCUwQSUyMCUyMCUyMCUyMCU3RCklMEElMjAlMjAlMjAlMjB0ZXN0JTNBJTIwRGF0YXNldCglN0IlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBmZWF0dXJlcyUzQSUyMCU1QidzZW50ZW5jZTEnJTJDJTIwJ3NlbnRlbmNlMiclMkMlMjAnbGFiZWwnJTJDJTIwJ2lkeCclNUQlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBudW1fcm93cyUzQSUyMDE3MjUlMEElMjAlMjAlMjAlMjAlN0QpJTBBJTdEKQ==",highlighted:`DatasetDict({
    train: Dataset({
        features: [<span class="hljs-string">&#x27;sentence1&#x27;</span>, <span class="hljs-string">&#x27;sentence2&#x27;</span>, <span class="hljs-string">&#x27;label&#x27;</span>, <span class="hljs-string">&#x27;idx&#x27;</span>],
        num_rows: <span class="hljs-number">3668</span>
    })
    validation: Dataset({
        features: [<span class="hljs-string">&#x27;sentence1&#x27;</span>, <span class="hljs-string">&#x27;sentence2&#x27;</span>, <span class="hljs-string">&#x27;label&#x27;</span>, <span class="hljs-string">&#x27;idx&#x27;</span>],
        num_rows: <span class="hljs-number">408</span>
    })
    test: Dataset({
        features: [<span class="hljs-string">&#x27;sentence1&#x27;</span>, <span class="hljs-string">&#x27;sentence2&#x27;</span>, <span class="hljs-string">&#x27;label&#x27;</span>, <span class="hljs-string">&#x27;idx&#x27;</span>],
        num_rows: <span class="hljs-number">1725</span>
    })
})`,wrap:!1}}),O=new h({props:{code:"cmF3X3RyYWluX2RhdGFzZXQlMjAlM0QlMjByYXdfZGF0YXNldHMlNUIlMjJ0cmFpbiUyMiU1RCUwQXJhd190cmFpbl9kYXRhc2V0JTVCMCU1RA==",highlighted:`raw_train_dataset = raw_datasets[<span class="hljs-string">&quot;train&quot;</span>]
raw_train_dataset[<span class="hljs-number">0</span>]`,wrap:!1}}),ss=new h({props:{code:"JTdCJTBBJTIwJTIwJTIwJTIwJTIyaWR4JTIyJTNBJTIwMCUyQyUwQSUyMCUyMCUyMCUyMCUyMmxhYmVsJTIyJTNBJTIwMSUyQyUwQSUyMCUyMCUyMCUyMCUyMnNlbnRlbmNlMSUyMiUzQSUyMCdBbXJvemklMjBhY2N1c2VkJTIwaGlzJTIwYnJvdGhlciUyMCUyQyUyMHdob20lMjBoZSUyMGNhbGxlZCUyMCUyMiUyMHRoZSUyMHdpdG5lc3MlMjAlMjIlMjAlMkMlMjBvZiUyMGRlbGliZXJhdGVseSUyMGRpc3RvcnRpbmclMjBoaXMlMjBldmlkZW5jZSUyMC4nJTJDJTBBJTIwJTIwJTIwJTIwJTIyc2VudGVuY2UyJTIyJTNBJTIwJ1JlZmVycmluZyUyMHRvJTIwaGltJTIwYXMlMjBvbmx5JTIwJTIyJTIwdGhlJTIwd2l0bmVzcyUyMCUyMiUyMCUyQyUyMEFtcm96aSUyMGFjY3VzZWQlMjBoaXMlMjBicm90aGVyJTIwb2YlMjBkZWxpYmVyYXRlbHklMjBkaXN0b3J0aW5nJTIwaGlzJTIwZXZpZGVuY2UlMjAuJyUyQyUwQSU3RA==",highlighted:`{
    <span class="hljs-string">&quot;idx&quot;</span>: <span class="hljs-number">0</span>,
    <span class="hljs-string">&quot;label&quot;</span>: <span class="hljs-number">1</span>,
    <span class="hljs-string">&quot;sentence1&quot;</span>: <span class="hljs-string">&#x27;Amrozi accused his brother , whom he called &quot; the witness &quot; , of deliberately distorting his evidence .&#x27;</span>,
    <span class="hljs-string">&quot;sentence2&quot;</span>: <span class="hljs-string">&#x27;Referring to him as only &quot; the witness &quot; , Amrozi accused his brother of deliberately distorting his evidence .&#x27;</span>,
}`,wrap:!1}}),es=new h({props:{code:"cmF3X3RyYWluX2RhdGFzZXQuZmVhdHVyZXM=",highlighted:"raw_train_dataset.features",wrap:!1}}),ts=new h({props:{code:"JTdCJTBBJTIwJTIwJTIwJTIwJTIyc2VudGVuY2UxJTIyJTNBJTIwVmFsdWUoZHR5cGUlM0QlMjJzdHJpbmclMjIlMkMlMjBpZCUzRE5vbmUpJTJDJTBBJTIwJTIwJTIwJTIwJTIyc2VudGVuY2UyJTIyJTNBJTIwVmFsdWUoZHR5cGUlM0QlMjJzdHJpbmclMjIlMkMlMjBpZCUzRE5vbmUpJTJDJTBBJTIwJTIwJTIwJTIwJTIybGFiZWwlMjIlM0ElMjBDbGFzc0xhYmVsKCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG51bV9jbGFzc2VzJTNEMiUyQyUyMG5hbWVzJTNEJTVCJTIybm90X2VxdWl2YWxlbnQlMjIlMkMlMjAlMjJlcXVpdmFsZW50JTIyJTVEJTJDJTIwbmFtZXNfZmlsZSUzRE5vbmUlMkMlMjBpZCUzRE5vbmUlMEElMjAlMjAlMjAlMjApJTJDJTBBJTIwJTIwJTIwJTIwJTIyaWR4JTIyJTNBJTIwVmFsdWUoZHR5cGUlM0QlMjJpbnQzMiUyMiUyQyUyMGlkJTNETm9uZSklMkMlMEElN0Q=",highlighted:`{
    <span class="hljs-string">&quot;sentence1&quot;</span>: Value(dtype=<span class="hljs-string">&quot;string&quot;</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>),
    <span class="hljs-string">&quot;sentence2&quot;</span>: Value(dtype=<span class="hljs-string">&quot;string&quot;</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>),
    <span class="hljs-string">&quot;label&quot;</span>: ClassLabel(
        num_classes=<span class="hljs-number">2</span>, names=[<span class="hljs-string">&quot;not_equivalent&quot;</span>, <span class="hljs-string">&quot;equivalent&quot;</span>], names_file=<span class="hljs-literal">None</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>
    ),
    <span class="hljs-string">&quot;idx&quot;</span>: Value(dtype=<span class="hljs-string">&quot;int32&quot;</span>, <span class="hljs-built_in">id</span>=<span class="hljs-literal">None</span>),
}`,wrap:!1}}),v=new be({props:{$$slots:{default:[St]},$$scope:{ctx:w}}}),as=new fe({props:{title:"预处理数据集",local:"预处理数据集",headingTag:"h2"}});const jt=[Dt,Yt],Ls=[];function Ut(s,l){return s[0]==="pt"?0:1}B=Ut(w),Q=Ls[B]=jt[B](w),Ms=new h({props:{code:"ZnJvbSUyMHRyYW5zZm9ybWVycyUyMGltcG9ydCUyMEF1dG9Ub2tlbml6ZXIlMEElMEFjaGVja3BvaW50JTIwJTNEJTIwJTIyYmVydC1iYXNlLXVuY2FzZWQlMjIlMEF0b2tlbml6ZXIlMjAlM0QlMjBBdXRvVG9rZW5pemVyLmZyb21fcHJldHJhaW5lZChjaGVja3BvaW50KSUwQXRva2VuaXplZF9zZW50ZW5jZXNfMSUyMCUzRCUyMHRva2VuaXplcihyYXdfZGF0YXNldHMlNUIlMjJ0cmFpbiUyMiU1RCU1QiUyMnNlbnRlbmNlMSUyMiU1RCklMEF0b2tlbml6ZWRfc2VudGVuY2VzXzIlMjAlM0QlMjB0b2tlbml6ZXIocmF3X2RhdGFzZXRzJTVCJTIydHJhaW4lMjIlNUQlNUIlMjJzZW50ZW5jZTIlMjIlNUQp",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer

checkpoint = <span class="hljs-string">&quot;bert-base-uncased&quot;</span>
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
tokenized_sentences_1 = tokenizer(raw_datasets[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-string">&quot;sentence1&quot;</span>])
tokenized_sentences_2 = tokenizer(raw_datasets[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-string">&quot;sentence2&quot;</span>])`,wrap:!1}}),cs=new h({props:{code:"aW5wdXRzJTIwJTNEJTIwdG9rZW5pemVyKCUyMlRoaXMlMjBpcyUyMHRoZSUyMGZpcnN0JTIwc2VudGVuY2UuJTIyJTJDJTIwJTIyVGhpcyUyMGlzJTIwdGhlJTIwc2Vjb25kJTIwb25lLiUyMiklMEFpbnB1dHM=",highlighted:`inputs = tokenizer(<span class="hljs-string">&quot;This is the first sentence.&quot;</span>, <span class="hljs-string">&quot;This is the second one.&quot;</span>)
inputs`,wrap:!1}}),rs=new h({props:{code:"JTdCJTBBJTIwJTIwJTIwJTIwJTIyaW5wdXRfaWRzJTIyJTNBJTIwJTVCJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwMTAxJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwMjAyMyUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMDIwMDMlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAxOTk2JTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwMjAzNCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMDYyNTElMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAxMDEyJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwMTAyJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwMjAyMyUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMDIwMDMlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAxOTk2JTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwMjExNyUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMDIwMjglMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAxMDEyJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwMTAyJTJDJTBBJTIwJTIwJTIwJTIwJTVEJTJDJTBBJTIwJTIwJTIwJTIwJTIydG9rZW5fdHlwZV9pZHMlMjIlM0ElMjAlNUIwJTJDJTIwMCUyQyUyMDAlMkMlMjAwJTJDJTIwMCUyQyUyMDAlMkMlMjAwJTJDJTIwMCUyQyUyMDElMkMlMjAxJTJDJTIwMSUyQyUyMDElMkMlMjAxJTJDJTIwMSUyQyUyMDElNUQlMkMlMEElMjAlMjAlMjAlMjAlMjJhdHRlbnRpb25fbWFzayUyMiUzQSUyMCU1QjElMkMlMjAxJTJDJTIwMSUyQyUyMDElMkMlMjAxJTJDJTIwMSUyQyUyMDElMkMlMjAxJTJDJTIwMSUyQyUyMDElMkMlMjAxJTJDJTIwMSUyQyUyMDElMkMlMjAxJTJDJTIwMSU1RCUyQyUwQSU3RA==",highlighted:`{
    <span class="hljs-string">&quot;input_ids&quot;</span>: [
        <span class="hljs-number">101</span>,
        <span class="hljs-number">2023</span>,
        <span class="hljs-number">2003</span>,
        <span class="hljs-number">1996</span>,
        <span class="hljs-number">2034</span>,
        <span class="hljs-number">6251</span>,
        <span class="hljs-number">1012</span>,
        <span class="hljs-number">102</span>,
        <span class="hljs-number">2023</span>,
        <span class="hljs-number">2003</span>,
        <span class="hljs-number">1996</span>,
        <span class="hljs-number">2117</span>,
        <span class="hljs-number">2028</span>,
        <span class="hljs-number">1012</span>,
        <span class="hljs-number">102</span>,
    ],
    <span class="hljs-string">&quot;token_type_ids&quot;</span>: [<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>],
    <span class="hljs-string">&quot;attention_mask&quot;</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>],
}`,wrap:!1}}),G=new be({props:{$$slots:{default:[Lt]},$$scope:{ctx:w}}}),us=new h({props:{code:"dG9rZW5pemVyLmNvbnZlcnRfaWRzX3RvX3Rva2VucyhpbnB1dHMlNUIlMjJpbnB1dF9pZHMlMjIlNUQp",highlighted:'tokenizer.convert_ids_to_tokens(inputs[<span class="hljs-string">&quot;input_ids&quot;</span>])',wrap:!1}}),ys=new h({props:{code:"JTVCJTBBJTIwJTIwJTIwJTIwJTIyJTVCQ0xTJTVEJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIydGhpcyUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMmlzJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIydGhlJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIyZmlyc3QlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjJzZW50ZW5jZSUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMi4lMjIlMkMlMEElMjAlMjAlMjAlMjAlMjIlNUJTRVAlNUQlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjJ0aGlzJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIyaXMlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjJ0aGUlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjJzZWNvbmQlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjJvbmUlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjIuJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIyJTVCU0VQJTVEJTIyJTJDJTBBJTVE",highlighted:`[
    <span class="hljs-string">&quot;[CLS]&quot;</span>,
    <span class="hljs-string">&quot;this&quot;</span>,
    <span class="hljs-string">&quot;is&quot;</span>,
    <span class="hljs-string">&quot;the&quot;</span>,
    <span class="hljs-string">&quot;first&quot;</span>,
    <span class="hljs-string">&quot;sentence&quot;</span>,
    <span class="hljs-string">&quot;.&quot;</span>,
    <span class="hljs-string">&quot;[SEP]&quot;</span>,
    <span class="hljs-string">&quot;this&quot;</span>,
    <span class="hljs-string">&quot;is&quot;</span>,
    <span class="hljs-string">&quot;the&quot;</span>,
    <span class="hljs-string">&quot;second&quot;</span>,
    <span class="hljs-string">&quot;one&quot;</span>,
    <span class="hljs-string">&quot;.&quot;</span>,
    <span class="hljs-string">&quot;[SEP]&quot;</span>,
]`,wrap:!1}}),Us=new h({props:{code:"JTVCJTBBJTIwJTIwJTIwJTIwJTIyJTVCQ0xTJTVEJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIydGhpcyUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMmlzJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIydGhlJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIyZmlyc3QlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjJzZW50ZW5jZSUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMi4lMjIlMkMlMEElMjAlMjAlMjAlMjAlMjIlNUJTRVAlNUQlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjJ0aGlzJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIyaXMlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjJ0aGUlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjJzZWNvbmQlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjJvbmUlMjIlMkMlMEElMjAlMjAlMjAlMjAlMjIuJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIyJTVCU0VQJTVEJTIyJTJDJTBBJTVEJTBBJTVCMCUyQyUyMDAlMkMlMjAwJTJDJTIwMCUyQyUyMDAlMkMlMjAwJTJDJTIwMCUyQyUyMDAlMkMlMjAxJTJDJTIwMSUyQyUyMDElMkMlMjAxJTJDJTIwMSUyQyUyMDElMkMlMjAxJTVE",highlighted:`[
    <span class="hljs-string">&quot;[CLS]&quot;</span>,
    <span class="hljs-string">&quot;this&quot;</span>,
    <span class="hljs-string">&quot;is&quot;</span>,
    <span class="hljs-string">&quot;the&quot;</span>,
    <span class="hljs-string">&quot;first&quot;</span>,
    <span class="hljs-string">&quot;sentence&quot;</span>,
    <span class="hljs-string">&quot;.&quot;</span>,
    <span class="hljs-string">&quot;[SEP]&quot;</span>,
    <span class="hljs-string">&quot;this&quot;</span>,
    <span class="hljs-string">&quot;is&quot;</span>,
    <span class="hljs-string">&quot;the&quot;</span>,
    <span class="hljs-string">&quot;second&quot;</span>,
    <span class="hljs-string">&quot;one&quot;</span>,
    <span class="hljs-string">&quot;.&quot;</span>,
    <span class="hljs-string">&quot;[SEP]&quot;</span>,
]
[<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>]`,wrap:!1}}),Is=new h({props:{code:"dG9rZW5pemVkX2RhdGFzZXQlMjAlM0QlMjB0b2tlbml6ZXIoJTBBJTIwJTIwJTIwJTIwcmF3X2RhdGFzZXRzJTVCJTIydHJhaW4lMjIlNUQlNUIlMjJzZW50ZW5jZTElMjIlNUQlMkMlMEElMjAlMjAlMjAlMjByYXdfZGF0YXNldHMlNUIlMjJ0cmFpbiUyMiU1RCU1QiUyMnNlbnRlbmNlMiUyMiU1RCUyQyUwQSUyMCUyMCUyMCUyMHBhZGRpbmclM0RUcnVlJTJDJTBBJTIwJTIwJTIwJTIwdHJ1bmNhdGlvbiUzRFRydWUlMkMlMEEp",highlighted:`tokenized_dataset = tokenizer(
    raw_datasets[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-string">&quot;sentence1&quot;</span>],
    raw_datasets[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-string">&quot;sentence2&quot;</span>],
    padding=<span class="hljs-literal">True</span>,
    truncation=<span class="hljs-literal">True</span>,
)`,wrap:!1}}),_s=new h({props:{code:"ZGVmJTIwdG9rZW5pemVfZnVuY3Rpb24oZXhhbXBsZSklM0ElMEElMjAlMjAlMjAlMjByZXR1cm4lMjB0b2tlbml6ZXIoZXhhbXBsZSU1QiUyMnNlbnRlbmNlMSUyMiU1RCUyQyUyMGV4YW1wbGUlNUIlMjJzZW50ZW5jZTIlMjIlNUQlMkMlMjB0cnVuY2F0aW9uJTNEVHJ1ZSk=",highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize_function</span>(<span class="hljs-params">example</span>):
    <span class="hljs-keyword">return</span> tokenizer(example[<span class="hljs-string">&quot;sentence1&quot;</span>], example[<span class="hljs-string">&quot;sentence2&quot;</span>], truncation=<span class="hljs-literal">True</span>)`,wrap:!1}}),Qs=new h({props:{code:"dG9rZW5pemVkX2RhdGFzZXRzJTIwJTNEJTIwcmF3X2RhdGFzZXRzLm1hcCh0b2tlbml6ZV9mdW5jdGlvbiUyQyUyMGJhdGNoZWQlM0RUcnVlKSUwQXRva2VuaXplZF9kYXRhc2V0cw==",highlighted:`tokenized_datasets = raw_datasets.<span class="hljs-built_in">map</span>(tokenize_function, batched=<span class="hljs-literal">True</span>)
tokenized_datasets`,wrap:!1}}),Zs=new h({props:{code:"RGF0YXNldERpY3QoJTdCJTBBJTIwJTIwJTIwJTIwdHJhaW4lM0ElMjBEYXRhc2V0KCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZlYXR1cmVzJTNBJTIwJTVCJ2F0dGVudGlvbl9tYXNrJyUyQyUyMCdpZHgnJTJDJTIwJ2lucHV0X2lkcyclMkMlMjAnbGFiZWwnJTJDJTIwJ3NlbnRlbmNlMSclMkMlMjAnc2VudGVuY2UyJyUyQyUyMCd0b2tlbl90eXBlX2lkcyclNUQlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBudW1fcm93cyUzQSUyMDM2NjglMEElMjAlMjAlMjAlMjAlN0QpJTBBJTIwJTIwJTIwJTIwdmFsaWRhdGlvbiUzQSUyMERhdGFzZXQoJTdCJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwZmVhdHVyZXMlM0ElMjAlNUInYXR0ZW50aW9uX21hc2snJTJDJTIwJ2lkeCclMkMlMjAnaW5wdXRfaWRzJyUyQyUyMCdsYWJlbCclMkMlMjAnc2VudGVuY2UxJyUyQyUyMCdzZW50ZW5jZTInJTJDJTIwJ3Rva2VuX3R5cGVfaWRzJyU1RCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG51bV9yb3dzJTNBJTIwNDA4JTBBJTIwJTIwJTIwJTIwJTdEKSUwQSUyMCUyMCUyMCUyMHRlc3QlM0ElMjBEYXRhc2V0KCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGZlYXR1cmVzJTNBJTIwJTVCJ2F0dGVudGlvbl9tYXNrJyUyQyUyMCdpZHgnJTJDJTIwJ2lucHV0X2lkcyclMkMlMjAnbGFiZWwnJTJDJTIwJ3NlbnRlbmNlMSclMkMlMjAnc2VudGVuY2UyJyUyQyUyMCd0b2tlbl90eXBlX2lkcyclNUQlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBudW1fcm93cyUzQSUyMDE3MjUlMEElMjAlMjAlMjAlMjAlN0QpJTBBJTdEKQ==",highlighted:`DatasetDict({
    train: Dataset({
        features: [<span class="hljs-string">&#x27;attention_mask&#x27;</span>, <span class="hljs-string">&#x27;idx&#x27;</span>, <span class="hljs-string">&#x27;input_ids&#x27;</span>, <span class="hljs-string">&#x27;label&#x27;</span>, <span class="hljs-string">&#x27;sentence1&#x27;</span>, <span class="hljs-string">&#x27;sentence2&#x27;</span>, <span class="hljs-string">&#x27;token_type_ids&#x27;</span>],
        num_rows: <span class="hljs-number">3668</span>
    })
    validation: Dataset({
        features: [<span class="hljs-string">&#x27;attention_mask&#x27;</span>, <span class="hljs-string">&#x27;idx&#x27;</span>, <span class="hljs-string">&#x27;input_ids&#x27;</span>, <span class="hljs-string">&#x27;label&#x27;</span>, <span class="hljs-string">&#x27;sentence1&#x27;</span>, <span class="hljs-string">&#x27;sentence2&#x27;</span>, <span class="hljs-string">&#x27;token_type_ids&#x27;</span>],
        num_rows: <span class="hljs-number">408</span>
    })
    test: Dataset({
        features: [<span class="hljs-string">&#x27;attention_mask&#x27;</span>, <span class="hljs-string">&#x27;idx&#x27;</span>, <span class="hljs-string">&#x27;input_ids&#x27;</span>, <span class="hljs-string">&#x27;label&#x27;</span>, <span class="hljs-string">&#x27;sentence1&#x27;</span>, <span class="hljs-string">&#x27;sentence2&#x27;</span>, <span class="hljs-string">&#x27;token_type_ids&#x27;</span>],
        num_rows: <span class="hljs-number">1725</span>
    })
})`,wrap:!1}}),qs=new fe({props:{title:"动态填充",local:"动态填充",headingTag:"h2"}}),zs=new rl({props:{id:"7q5NyFT8REg"}});function mt(s,l){return s[0]==="pt"?Kt:Pt}let we=mt(w),q=we(w);const dt=[sn,Ot],Ps=[];function wt(s,l){return s[0]==="pt"?0:1}R=wt(w),Z=Ps[R]=dt[R](w),xs=new h({props:{code:"c2FtcGxlcyUyMCUzRCUyMHRva2VuaXplZF9kYXRhc2V0cyU1QiUyMnRyYWluJTIyJTVEJTVCJTNBOCU1RCUwQXNhbXBsZXMlMjAlM0QlMjAlN0JrJTNBJTIwdiUyMGZvciUyMGslMkMlMjB2JTIwaW4lMjBzYW1wbGVzLml0ZW1zKCklMjBpZiUyMGslMjBub3QlMjBpbiUyMCU1QiUyMmlkeCUyMiUyQyUyMCUyMnNlbnRlbmNlMSUyMiUyQyUyMCUyMnNlbnRlbmNlMiUyMiU1RCU3RCUwQSU1Qmxlbih4KSUyMGZvciUyMHglMjBpbiUyMHNhbXBsZXMlNUIlMjJpbnB1dF9pZHMlMjIlNUQlNUQ=",highlighted:`samples = tokenized_datasets[<span class="hljs-string">&quot;train&quot;</span>][:<span class="hljs-number">8</span>]
samples = {k: v <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> samples.items() <span class="hljs-keyword">if</span> k <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> [<span class="hljs-string">&quot;idx&quot;</span>, <span class="hljs-string">&quot;sentence1&quot;</span>, <span class="hljs-string">&quot;sentence2&quot;</span>]}
[<span class="hljs-built_in">len</span>(x) <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> samples[<span class="hljs-string">&quot;input_ids&quot;</span>]]`,wrap:!1}}),Xs=new h({props:{code:"JTVCNTAlMkMlMjA1OSUyQyUyMDQ3JTJDJTIwNjclMkMlMjA1OSUyQyUyMDUwJTJDJTIwNjIlMkMlMjAzMiU1RA==",highlighted:'[<span class="hljs-number">50</span>, <span class="hljs-number">59</span>, <span class="hljs-number">47</span>, <span class="hljs-number">67</span>, <span class="hljs-number">59</span>, <span class="hljs-number">50</span>, <span class="hljs-number">62</span>, <span class="hljs-number">32</span>]',wrap:!1}}),Hs=new h({props:{code:"JTBBJTYwJTYwJTYwcHklMEFiYXRjaCUyMCUzRCUyMGRhdGFfY29sbGF0b3Ioc2FtcGxlcyklMEElN0JrJTNBJTIwdi5zaGFwZSUyMGZvciUyMGslMkMlMjB2JTIwaW4lMjBiYXRjaC5pdGVtcygpJTdE",highlighted:'\n```py\nbatch = dat<span class="hljs-built_in">a_collator</span>(samples)\n<span class="hljs-title">{k:</span> v.shape for k, v in batch.items()}',wrap:!1}});const bt=[en,ln],Ks=[];function ft(s,l){return s[0]==="tf"?0:1}V=ft(w),N=Ks[V]=bt[V](w),x=new be({props:{$$slots:{default:[tn]},$$scope:{ctx:w}}});let C=w[0]==="tf"&&At();return Fs=new zt({props:{source:"https://github.com/huggingface/course/blob/main/chapters/zh-CN/chapter3/2.mdx"}}),{c(){e=u("meta"),p=M(),t=u("p"),c=M(),j(f.$$.fragment),o=M(),j(I.$$.fragment),E=M(),k.c(),ll=M(),$.c(),el=M(),X=u("p"),X.textContent=Ae,ol=M(),W=u("p"),W.innerHTML=Be,Jl=M(),j(H.$$.fragment),ul=M(),A.c(),tl=M(),F=u("p"),F.innerHTML=Qe,Tl=M(),S=u("p"),S.textContent=Re,yl=M(),j(z.$$.fragment),jl=M(),j(Y.$$.fragment),Ul=M(),j(D.$$.fragment),ml=M(),L=u("p"),L.innerHTML=Ze,dl=M(),P=u("p"),P.innerHTML=Ve,wl=M(),K=u("p"),K.innerHTML=Ne,bl=M(),j(O.$$.fragment),fl=M(),j(ss.$$.fragment),hl=M(),ls=u("p"),ls.innerHTML=Ee,Il=M(),j(es.$$.fragment),Cl=M(),j(ts.$$.fragment),kl=M(),ns=u("p"),ns.innerHTML=qe,_l=M(),j(v.$$.fragment),$l=M(),j(as.$$.fragment),gl=M(),Q.c(),nl=M(),ps=u("p"),ps.innerHTML=ze,Al=M(),j(Ms.$$.fragment),Bl=M(),is=u("p"),is.textContent=ve,Ql=M(),j(cs.$$.fragment),Rl=M(),j(rs.$$.fragment),Zl=M(),os=u("p"),os.innerHTML=Ge,Vl=M(),j(G.$$.fragment),Nl=M(),Js=u("p"),Js.innerHTML=xe,El=M(),j(us.$$.fragment),ql=M(),Ts=u("p"),Ts.textContent=Xe,zl=M(),j(ys.$$.fragment),vl=M(),js=u("p"),js.innerHTML=We,Gl=M(),j(Us.$$.fragment),xl=M(),ms=u("p"),ms.innerHTML=He,Xl=M(),ds=u("p"),ds.innerHTML=Fe,Wl=M(),ws=u("p"),ws.innerHTML=Se,Hl=M(),bs=u("p"),bs.textContent=Ye,Fl=M(),fs=u("p"),fs.innerHTML=De,Sl=M(),hs=u("p"),hs.innerHTML=Le,Yl=M(),j(Is.$$.fragment),Dl=M(),Cs=u("p"),Cs.innerHTML=Pe,Ll=M(),ks=u("p"),ks.innerHTML=Ke,Pl=M(),j(_s.$$.fragment),Kl=M(),$s=u("p"),$s.innerHTML=Oe,Ol=M(),gs=u("p"),gs.innerHTML=st,se=M(),As=u("p"),As.innerHTML=lt,le=M(),Bs=u("p"),Bs.innerHTML=et,ee=M(),j(Qs.$$.fragment),te=M(),Rs=u("p"),Rs.textContent=tt,ne=M(),j(Zs.$$.fragment),ae=M(),Vs=u("p"),Vs.innerHTML=nt,pe=M(),Ns=u("p"),Ns.innerHTML=at,Me=M(),Es=u("p"),Es.textContent=pt,ie=M(),j(qs.$$.fragment),ce=M(),j(zs.$$.fragment),re=M(),q.c(),al=M(),vs=u("p"),vs.innerHTML=Mt,oe=M(),Z.c(),pl=M(),Gs=u("p"),Gs.innerHTML=it,Je=M(),j(xs.$$.fragment),ue=M(),j(Xs.$$.fragment),Te=M(),Ws=u("p"),Ws.innerHTML=ct,ye=M(),j(Hs.$$.fragment),je=M(),N.c(),Ml=M(),j(x.$$.fragment),Ue=M(),C&&C.c(),il=M(),j(Fs.$$.fragment),me=M(),cl=u("p"),this.h()},l(s){const l=Nt("svelte-u9bgzb",document.head);e=T(l,"META",{name:!0,content:!0}),l.forEach(n),p=i(s),t=T(s,"P",{}),$t(t).forEach(n),c=i(s),U(f.$$.fragment,s),o=i(s),U(I.$$.fragment,s),E=i(s),k.l(s),ll=i(s),$.l(s),el=i(s),X=T(s,"P",{"data-svelte-h":!0}),y(X)!=="svelte-15gugyr"&&(X.textContent=Ae),ol=i(s),W=T(s,"P",{"data-svelte-h":!0}),y(W)!=="svelte-5qb5be"&&(W.innerHTML=Be),Jl=i(s),U(H.$$.fragment,s),ul=i(s),A.l(s),tl=i(s),F=T(s,"P",{"data-svelte-h":!0}),y(F)!=="svelte-1hcs3zf"&&(F.innerHTML=Qe),Tl=i(s),S=T(s,"P",{"data-svelte-h":!0}),y(S)!=="svelte-1uu5t0h"&&(S.textContent=Re),yl=i(s),U(z.$$.fragment,s),jl=i(s),U(Y.$$.fragment,s),Ul=i(s),U(D.$$.fragment,s),ml=i(s),L=T(s,"P",{"data-svelte-h":!0}),y(L)!=="svelte-wjga9o"&&(L.innerHTML=Ze),dl=i(s),P=T(s,"P",{"data-svelte-h":!0}),y(P)!=="svelte-h7tbmv"&&(P.innerHTML=Ve),wl=i(s),K=T(s,"P",{"data-svelte-h":!0}),y(K)!=="svelte-9z9egh"&&(K.innerHTML=Ne),bl=i(s),U(O.$$.fragment,s),fl=i(s),U(ss.$$.fragment,s),hl=i(s),ls=T(s,"P",{"data-svelte-h":!0}),y(ls)!=="svelte-11i6zuy"&&(ls.innerHTML=Ee),Il=i(s),U(es.$$.fragment,s),Cl=i(s),U(ts.$$.fragment,s),kl=i(s),ns=T(s,"P",{"data-svelte-h":!0}),y(ns)!=="svelte-1baj0cv"&&(ns.innerHTML=qe),_l=i(s),U(v.$$.fragment,s),$l=i(s),U(as.$$.fragment,s),gl=i(s),Q.l(s),nl=i(s),ps=T(s,"P",{"data-svelte-h":!0}),y(ps)!=="svelte-115n8io"&&(ps.innerHTML=ze),Al=i(s),U(Ms.$$.fragment,s),Bl=i(s),is=T(s,"P",{"data-svelte-h":!0}),y(is)!=="svelte-1w0cu3c"&&(is.textContent=ve),Ql=i(s),U(cs.$$.fragment,s),Rl=i(s),U(rs.$$.fragment,s),Zl=i(s),os=T(s,"P",{"data-svelte-h":!0}),y(os)!=="svelte-1huv486"&&(os.innerHTML=Ge),Vl=i(s),U(G.$$.fragment,s),Nl=i(s),Js=T(s,"P",{"data-svelte-h":!0}),y(Js)!=="svelte-1alev3e"&&(Js.innerHTML=xe),El=i(s),U(us.$$.fragment,s),ql=i(s),Ts=T(s,"P",{"data-svelte-h":!0}),y(Ts)!=="svelte-1kyzytr"&&(Ts.textContent=Xe),zl=i(s),U(ys.$$.fragment,s),vl=i(s),js=T(s,"P",{"data-svelte-h":!0}),y(js)!=="svelte-67gq1e"&&(js.innerHTML=We),Gl=i(s),U(Us.$$.fragment,s),xl=i(s),ms=T(s,"P",{"data-svelte-h":!0}),y(ms)!=="svelte-o8zghu"&&(ms.innerHTML=He),Xl=i(s),ds=T(s,"P",{"data-svelte-h":!0}),y(ds)!=="svelte-1yiq9us"&&(ds.innerHTML=Fe),Wl=i(s),ws=T(s,"P",{"data-svelte-h":!0}),y(ws)!=="svelte-1fsn2je"&&(ws.innerHTML=Se),Hl=i(s),bs=T(s,"P",{"data-svelte-h":!0}),y(bs)!=="svelte-2ceenm"&&(bs.textContent=Ye),Fl=i(s),fs=T(s,"P",{"data-svelte-h":!0}),y(fs)!=="svelte-zrrcqd"&&(fs.innerHTML=De),Sl=i(s),hs=T(s,"P",{"data-svelte-h":!0}),y(hs)!=="svelte-192t0x0"&&(hs.innerHTML=Le),Yl=i(s),U(Is.$$.fragment,s),Dl=i(s),Cs=T(s,"P",{"data-svelte-h":!0}),y(Cs)!=="svelte-gl7yo1"&&(Cs.innerHTML=Pe),Ll=i(s),ks=T(s,"P",{"data-svelte-h":!0}),y(ks)!=="svelte-1vkb6jg"&&(ks.innerHTML=Ke),Pl=i(s),U(_s.$$.fragment,s),Kl=i(s),$s=T(s,"P",{"data-svelte-h":!0}),y($s)!=="svelte-1wptzj5"&&($s.innerHTML=Oe),Ol=i(s),gs=T(s,"P",{"data-svelte-h":!0}),y(gs)!=="svelte-1v4peow"&&(gs.innerHTML=st),se=i(s),As=T(s,"P",{"data-svelte-h":!0}),y(As)!=="svelte-1xpqfo8"&&(As.innerHTML=lt),le=i(s),Bs=T(s,"P",{"data-svelte-h":!0}),y(Bs)!=="svelte-3q8611"&&(Bs.innerHTML=et),ee=i(s),U(Qs.$$.fragment,s),te=i(s),Rs=T(s,"P",{"data-svelte-h":!0}),y(Rs)!=="svelte-19wv9od"&&(Rs.textContent=tt),ne=i(s),U(Zs.$$.fragment,s),ae=i(s),Vs=T(s,"P",{"data-svelte-h":!0}),y(Vs)!=="svelte-155n6ur"&&(Vs.innerHTML=nt),pe=i(s),Ns=T(s,"P",{"data-svelte-h":!0}),y(Ns)!=="svelte-utasre"&&(Ns.innerHTML=at),Me=i(s),Es=T(s,"P",{"data-svelte-h":!0}),y(Es)!=="svelte-fttlv5"&&(Es.textContent=pt),ie=i(s),U(qs.$$.fragment,s),ce=i(s),U(zs.$$.fragment,s),re=i(s),q.l(s),al=i(s),vs=T(s,"P",{"data-svelte-h":!0}),y(vs)!=="svelte-iyzvbp"&&(vs.innerHTML=Mt),oe=i(s),Z.l(s),pl=i(s),Gs=T(s,"P",{"data-svelte-h":!0}),y(Gs)!=="svelte-vovtt8"&&(Gs.innerHTML=it),Je=i(s),U(xs.$$.fragment,s),ue=i(s),U(Xs.$$.fragment,s),Te=i(s),Ws=T(s,"P",{"data-svelte-h":!0}),y(Ws)!=="svelte-1obyezs"&&(Ws.innerHTML=ct),ye=i(s),U(Hs.$$.fragment,s),je=i(s),N.l(s),Ml=i(s),U(x.$$.fragment,s),Ue=i(s),C&&C.l(s),il=i(s),U(Fs.$$.fragment,s),me=i(s),cl=T(s,"P",{}),$t(cl).forEach(n),this.h()},h(){gt(e,"name","hf:doc:metadata"),gt(e,"content",an)},m(s,l){Et(document.head,e),a(s,p,l),a(s,t,l),a(s,c,l),m(f,s,l),a(s,o,l),m(I,s,l),a(s,E,l),Ss[b].m(s,l),a(s,ll,l),Ys[_].m(s,l),a(s,el,l),a(s,X,l),a(s,ol,l),a(s,W,l),a(s,Jl,l),m(H,s,l),a(s,ul,l),Ds[g].m(s,l),a(s,tl,l),a(s,F,l),a(s,Tl,l),a(s,S,l),a(s,yl,l),m(z,s,l),a(s,jl,l),m(Y,s,l),a(s,Ul,l),m(D,s,l),a(s,ml,l),a(s,L,l),a(s,dl,l),a(s,P,l),a(s,wl,l),a(s,K,l),a(s,bl,l),m(O,s,l),a(s,fl,l),m(ss,s,l),a(s,hl,l),a(s,ls,l),a(s,Il,l),m(es,s,l),a(s,Cl,l),m(ts,s,l),a(s,kl,l),a(s,ns,l),a(s,_l,l),m(v,s,l),a(s,$l,l),m(as,s,l),a(s,gl,l),Ls[B].m(s,l),a(s,nl,l),a(s,ps,l),a(s,Al,l),m(Ms,s,l),a(s,Bl,l),a(s,is,l),a(s,Ql,l),m(cs,s,l),a(s,Rl,l),m(rs,s,l),a(s,Zl,l),a(s,os,l),a(s,Vl,l),m(G,s,l),a(s,Nl,l),a(s,Js,l),a(s,El,l),m(us,s,l),a(s,ql,l),a(s,Ts,l),a(s,zl,l),m(ys,s,l),a(s,vl,l),a(s,js,l),a(s,Gl,l),m(Us,s,l),a(s,xl,l),a(s,ms,l),a(s,Xl,l),a(s,ds,l),a(s,Wl,l),a(s,ws,l),a(s,Hl,l),a(s,bs,l),a(s,Fl,l),a(s,fs,l),a(s,Sl,l),a(s,hs,l),a(s,Yl,l),m(Is,s,l),a(s,Dl,l),a(s,Cs,l),a(s,Ll,l),a(s,ks,l),a(s,Pl,l),m(_s,s,l),a(s,Kl,l),a(s,$s,l),a(s,Ol,l),a(s,gs,l),a(s,se,l),a(s,As,l),a(s,le,l),a(s,Bs,l),a(s,ee,l),m(Qs,s,l),a(s,te,l),a(s,Rs,l),a(s,ne,l),m(Zs,s,l),a(s,ae,l),a(s,Vs,l),a(s,pe,l),a(s,Ns,l),a(s,Me,l),a(s,Es,l),a(s,ie,l),m(qs,s,l),a(s,ce,l),m(zs,s,l),a(s,re,l),q.m(s,l),a(s,al,l),a(s,vs,l),a(s,oe,l),Ps[R].m(s,l),a(s,pl,l),a(s,Gs,l),a(s,Je,l),m(xs,s,l),a(s,ue,l),m(Xs,s,l),a(s,Te,l),a(s,Ws,l),a(s,ye,l),m(Hs,s,l),a(s,je,l),Ks[V].m(s,l),a(s,Ml,l),m(x,s,l),a(s,Ue,l),C&&C.m(s,l),a(s,il,l),m(Fs,s,l),a(s,me,l),a(s,cl,l),de=!0},p(s,[l]){const ht={};l&1&&(ht.fw=s[0]),f.$set(ht);let Ie=b;b=ot(s),b!==Ie&&(sl(),J(Ss[Ie],1,1,()=>{Ss[Ie]=null}),Os(),k=Ss[b],k||(k=Ss[b]=rt[b](s),k.c()),r(k,1),k.m(ll.parentNode,ll));let Ce=_;_=ut(s),_!==Ce&&(sl(),J(Ys[Ce],1,1,()=>{Ys[Ce]=null}),Os(),$=Ys[_],$||($=Ys[_]=Jt[_](s),$.c()),r($,1),$.m(el.parentNode,el));let ke=g;g=yt(s),g!==ke&&(sl(),J(Ds[ke],1,1,()=>{Ds[ke]=null}),Os(),A=Ds[g],A||(A=Ds[g]=Tt[g](s),A.c()),r(A,1),A.m(tl.parentNode,tl));const It={};l&2&&(It.$$scope={dirty:l,ctx:s}),z.$set(It);const Ct={};l&2&&(Ct.$$scope={dirty:l,ctx:s}),v.$set(Ct);let _e=B;B=Ut(s),B!==_e&&(sl(),J(Ls[_e],1,1,()=>{Ls[_e]=null}),Os(),Q=Ls[B],Q||(Q=Ls[B]=jt[B](s),Q.c()),r(Q,1),Q.m(nl.parentNode,nl));const kt={};l&2&&(kt.$$scope={dirty:l,ctx:s}),G.$set(kt),we!==(we=mt(s))&&(q.d(1),q=we(s),q&&(q.c(),q.m(al.parentNode,al)));let $e=R;R=wt(s),R!==$e&&(sl(),J(Ps[$e],1,1,()=>{Ps[$e]=null}),Os(),Z=Ps[R],Z||(Z=Ps[R]=dt[R](s),Z.c()),r(Z,1),Z.m(pl.parentNode,pl));let ge=V;V=ft(s),V!==ge&&(sl(),J(Ks[ge],1,1,()=>{Ks[ge]=null}),Os(),N=Ks[V],N||(N=Ks[V]=bt[V](s),N.c()),r(N,1),N.m(Ml.parentNode,Ml));const _t={};l&2&&(_t.$$scope={dirty:l,ctx:s}),x.$set(_t),s[0]==="tf"?C?l&1&&r(C,1):(C=At(),C.c(),r(C,1),C.m(il.parentNode,il)):C&&(sl(),J(C,1,1,()=>{C=null}),Os())},i(s){de||(r(f.$$.fragment,s),r(I.$$.fragment,s),r(k),r($),r(H.$$.fragment,s),r(A),r(z.$$.fragment,s),r(Y.$$.fragment,s),r(D.$$.fragment,s),r(O.$$.fragment,s),r(ss.$$.fragment,s),r(es.$$.fragment,s),r(ts.$$.fragment,s),r(v.$$.fragment,s),r(as.$$.fragment,s),r(Q),r(Ms.$$.fragment,s),r(cs.$$.fragment,s),r(rs.$$.fragment,s),r(G.$$.fragment,s),r(us.$$.fragment,s),r(ys.$$.fragment,s),r(Us.$$.fragment,s),r(Is.$$.fragment,s),r(_s.$$.fragment,s),r(Qs.$$.fragment,s),r(Zs.$$.fragment,s),r(qs.$$.fragment,s),r(zs.$$.fragment,s),r(Z),r(xs.$$.fragment,s),r(Xs.$$.fragment,s),r(Hs.$$.fragment,s),r(N),r(x.$$.fragment,s),r(C),r(Fs.$$.fragment,s),de=!0)},o(s){J(f.$$.fragment,s),J(I.$$.fragment,s),J(k),J($),J(H.$$.fragment,s),J(A),J(z.$$.fragment,s),J(Y.$$.fragment,s),J(D.$$.fragment,s),J(O.$$.fragment,s),J(ss.$$.fragment,s),J(es.$$.fragment,s),J(ts.$$.fragment,s),J(v.$$.fragment,s),J(as.$$.fragment,s),J(Q),J(Ms.$$.fragment,s),J(cs.$$.fragment,s),J(rs.$$.fragment,s),J(G.$$.fragment,s),J(us.$$.fragment,s),J(ys.$$.fragment,s),J(Us.$$.fragment,s),J(Is.$$.fragment,s),J(_s.$$.fragment,s),J(Qs.$$.fragment,s),J(Zs.$$.fragment,s),J(qs.$$.fragment,s),J(zs.$$.fragment,s),J(Z),J(xs.$$.fragment,s),J(Xs.$$.fragment,s),J(Hs.$$.fragment,s),J(N),J(x.$$.fragment,s),J(C),J(Fs.$$.fragment,s),de=!1},d(s){s&&(n(p),n(t),n(c),n(o),n(E),n(ll),n(el),n(X),n(ol),n(W),n(Jl),n(ul),n(tl),n(F),n(Tl),n(S),n(yl),n(jl),n(Ul),n(ml),n(L),n(dl),n(P),n(wl),n(K),n(bl),n(fl),n(hl),n(ls),n(Il),n(Cl),n(kl),n(ns),n(_l),n($l),n(gl),n(nl),n(ps),n(Al),n(Bl),n(is),n(Ql),n(Rl),n(Zl),n(os),n(Vl),n(Nl),n(Js),n(El),n(ql),n(Ts),n(zl),n(vl),n(js),n(Gl),n(xl),n(ms),n(Xl),n(ds),n(Wl),n(ws),n(Hl),n(bs),n(Fl),n(fs),n(Sl),n(hs),n(Yl),n(Dl),n(Cs),n(Ll),n(ks),n(Pl),n(Kl),n($s),n(Ol),n(gs),n(se),n(As),n(le),n(Bs),n(ee),n(te),n(Rs),n(ne),n(ae),n(Vs),n(pe),n(Ns),n(Me),n(Es),n(ie),n(ce),n(re),n(al),n(vs),n(oe),n(pl),n(Gs),n(Je),n(ue),n(Te),n(Ws),n(ye),n(je),n(Ml),n(Ue),n(il),n(me),n(cl)),n(e),d(f,s),d(I,s),Ss[b].d(s),Ys[_].d(s),d(H,s),Ds[g].d(s),d(z,s),d(Y,s),d(D,s),d(O,s),d(ss,s),d(es,s),d(ts,s),d(v,s),d(as,s),Ls[B].d(s),d(Ms,s),d(cs,s),d(rs,s),d(G,s),d(us,s),d(ys,s),d(Us,s),d(Is,s),d(_s,s),d(Qs,s),d(Zs,s),d(qs,s),d(zs,s),q.d(s),Ps[R].d(s),d(xs,s),d(Xs,s),d(Hs,s),Ks[V].d(s),d(x,s),C&&C.d(s),d(Fs,s)}}}const an='{"title":"处理数据","local":"处理数据","sections":[{"title":"从模型中心（Hub）加载数据集","local":"从模型中心（Hub）加载数据集","sections":[],"depth":2},{"title":"预处理数据集","local":"预处理数据集","sections":[],"depth":2},{"title":"动态填充","local":"动态填充","sections":[],"depth":2}],"depth":1}';function pn(w,e,p){let t="pt";return Rt(()=>{const c=new URLSearchParams(window.location.search);p(0,t=c.get("fw")||"pt")}),[t]}class jn extends Zt{constructor(e){super(),Vt(this,e,pn,nn,Qt,{})}}export{jn as component};
