import{s as Ne,o as Ae,n as $e}from"../chunks/scheduler.cc52f4b9.js";import{S as Qe,i as Ze,e as d,s as i,c as b,h as Ve,a as J,d as p,b as u,f as Te,g as j,j as w,k as ke,l as ge,m as M,n as y,o as c,u as v,t as o,p as f,v as B}from"../chunks/index.9480bb2b.js";import{T as _e}from"../chunks/Tip.f7b80743.js";import{C as Ee,H as vl,E as ze}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.009dc98d.js";import{Y as Ce}from"../chunks/Youtube.7b12d9e8.js";import{C as h}from"../chunks/CodeBlock.184aadb6.js";import{C as Ie}from"../chunks/CourseFloatingBanner.6488bde3.js";import{F as We}from"../chunks/FrameworkSwitchCourse.42d12053.js";function Ge(m){let e,a;return e=new Ie({props:{chapter:2,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/chapter2/section5_tf.ipynb"},{label:"Aws Studio",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/zh-CN/chapter2/section5_tf.ipynb"}]}}),{c(){b(e.$$.fragment)},l(s){j(e.$$.fragment,s)},m(s,r){y(e,s,r),a=!0},i(s){a||(o(e.$$.fragment,s),a=!0)},o(s){c(e.$$.fragment,s),a=!1},d(s){f(e,s)}}}function Re(m){let e,a;return e=new Ie({props:{chapter:2,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/chapter2/section5_pt.ipynb"},{label:"Aws Studio",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/zh-CN/chapter2/section5_pt.ipynb"}]}}),{c(){b(e.$$.fragment)},l(s){j(e.$$.fragment,s)},m(s,r){y(e,s,r),a=!0},i(s){a||(o(e.$$.fragment,s),a=!0)},o(s){c(e.$$.fragment,s),a=!1},d(s){f(e,s)}}}function Xe(m){let e,a;return e=new Ce({props:{id:"ROxrFOEbsQE"}}),{c(){b(e.$$.fragment)},l(s){j(e.$$.fragment,s)},m(s,r){y(e,s,r),a=!0},i(s){a||(o(e.$$.fragment,s),a=!0)},o(s){c(e.$$.fragment,s),a=!1},d(s){f(e,s)}}}function ve(m){let e,a;return e=new Ce({props:{id:"M6adb1j2jPI"}}),{c(){b(e.$$.fragment)},l(s){j(e.$$.fragment,s)},m(s,r){y(e,s,r),a=!0},i(s){a||(o(e.$$.fragment,s),a=!0)},o(s){c(e.$$.fragment,s),a=!1},d(s){f(e,s)}}}function Be(m){let e,a,s,r;return e=new h({props:{code:"aW1wb3J0JTIwdGVuc29yZmxvdyUyMGFzJTIwdGYlMEFmcm9tJTIwdHJhbnNmb3JtZXJzJTIwaW1wb3J0JTIwQXV0b1Rva2VuaXplciUyQyUyMFRGQXV0b01vZGVsRm9yU2VxdWVuY2VDbGFzc2lmaWNhdGlvbiUwQSUwQWNoZWNrcG9pbnQlMjAlM0QlMjAlMjJkaXN0aWxiZXJ0LWJhc2UtdW5jYXNlZC1maW5ldHVuZWQtc3N0LTItZW5nbGlzaCUyMiUwQXRva2VuaXplciUyMCUzRCUyMEF1dG9Ub2tlbml6ZXIuZnJvbV9wcmV0cmFpbmVkKGNoZWNrcG9pbnQpJTBBbW9kZWwlMjAlM0QlMjBURkF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24uZnJvbV9wcmV0cmFpbmVkKGNoZWNrcG9pbnQpJTBBJTBBc2VxdWVuY2UlMjAlM0QlMjAlMjJJJ3ZlJTIwYmVlbiUyMHdhaXRpbmclMjBmb3IlMjBhJTIwSHVnZ2luZ0ZhY2UlMjBjb3Vyc2UlMjBteSUyMHdob2xlJTIwbGlmZS4lMjIlMEElMEF0b2tlbnMlMjAlM0QlMjB0b2tlbml6ZXIudG9rZW5pemUoc2VxdWVuY2UpJTBBaWRzJTIwJTNEJTIwdG9rZW5pemVyLmNvbnZlcnRfdG9rZW5zX3RvX2lkcyh0b2tlbnMpJTBBaW5wdXRfaWRzJTIwJTNEJTIwdGYuY29uc3RhbnQoaWRzKSUwQSUyMyUyMCVFOCVCRiU5OSVFNCVCOCU4MCVFOCVBMSU4QyVFNCVCQyU5QSVFOCVCRiU5MCVFOCVBMSU4QyVFNSVBNCVCMSVFOCVCNCVBNSUwQW1vZGVsKGlucHV0X2lkcyk=",highlighted:`<span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, TFAutoModelForSequenceClassification

checkpoint = <span class="hljs-string">&quot;distilbert-base-uncased-finetuned-sst-2-english&quot;</span>
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
model = TFAutoModelForSequenceClassification.from_pretrained(checkpoint)

sequence = <span class="hljs-string">&quot;I&#x27;ve been waiting for a HuggingFace course my whole life.&quot;</span>

tokens = tokenizer.tokenize(sequence)
ids = tokenizer.convert_tokens_to_ids(tokens)
input_ids = tf.constant(ids)
<span class="hljs-comment"># 这一行会运行失败</span>
model(input_ids)`,wrap:!1}}),s=new h({props:{code:"SW52YWxpZEFyZ3VtZW50RXJyb3IlM0ElMjBJbnB1dCUyMHRvJTIwcmVzaGFwZSUyMGlzJTIwYSUyMHRlbnNvciUyMHdpdGglMjAxNCUyMHZhbHVlcyUyQyUyMGJ1dCUyMHRoZSUyMHJlcXVlc3RlZCUyMHNoYXBlJTIwaGFzJTIwMTk2JTIwJTVCT3AlM0FSZXNoYXBlJTVE",highlighted:'InvalidArgumentError: Input to reshape <span class="hljs-keyword">is</span> a tensor <span class="hljs-keyword">with</span> <span class="hljs-number">14</span> values, but the requested shape has <span class="hljs-number">196</span> [Op:Reshape]',wrap:!1}}),{c(){b(e.$$.fragment),a=i(),b(s.$$.fragment)},l(t){j(e.$$.fragment,t),a=u(t),j(s.$$.fragment,t)},m(t,U){y(e,t,U),M(t,a,U),y(s,t,U),r=!0},i(t){r||(o(e.$$.fragment,t),o(s.$$.fragment,t),r=!0)},o(t){c(e.$$.fragment,t),c(s.$$.fragment,t),r=!1},d(t){t&&p(a),f(e,t),f(s,t)}}}function De(m){let e,a,s,r;return e=new h({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwdHJhbnNmb3JtZXJzJTIwaW1wb3J0JTIwQXV0b1Rva2VuaXplciUyQyUyMEF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24lMEElMEFjaGVja3BvaW50JTIwJTNEJTIwJTIyZGlzdGlsYmVydC1iYXNlLXVuY2FzZWQtZmluZXR1bmVkLXNzdC0yLWVuZ2xpc2glMjIlMEF0b2tlbml6ZXIlMjAlM0QlMjBBdXRvVG9rZW5pemVyLmZyb21fcHJldHJhaW5lZChjaGVja3BvaW50KSUwQW1vZGVsJTIwJTNEJTIwQXV0b01vZGVsRm9yU2VxdWVuY2VDbGFzc2lmaWNhdGlvbi5mcm9tX3ByZXRyYWluZWQoY2hlY2twb2ludCklMEElMEFzZXF1ZW5jZSUyMCUzRCUyMCUyMkkndmUlMjBiZWVuJTIwd2FpdGluZyUyMGZvciUyMGElMjBIdWdnaW5nRmFjZSUyMGNvdXJzZSUyMG15JTIwd2hvbGUlMjBsaWZlLiUyMiUwQSUwQXRva2VucyUyMCUzRCUyMHRva2VuaXplci50b2tlbml6ZShzZXF1ZW5jZSklMEFpZHMlMjAlM0QlMjB0b2tlbml6ZXIuY29udmVydF90b2tlbnNfdG9faWRzKHRva2VucyklMEFpbnB1dF9pZHMlMjAlM0QlMjB0b3JjaC50ZW5zb3IoaWRzKSUwQSUyMyUyMCVFOCVCRiU5OSVFNCVCOCU4MCVFOCVBMSU4QyVFNCVCQyU5QSVFOCVCRiU5MCVFOCVBMSU4QyVFNSVBNCVCMSVFOCVCNCVBNSUwQW1vZGVsKGlucHV0X2lkcyk=",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModelForSequenceClassification

checkpoint = <span class="hljs-string">&quot;distilbert-base-uncased-finetuned-sst-2-english&quot;</span>
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
model = AutoModelForSequenceClassification.from_pretrained(checkpoint)

sequence = <span class="hljs-string">&quot;I&#x27;ve been waiting for a HuggingFace course my whole life.&quot;</span>

tokens = tokenizer.tokenize(sequence)
ids = tokenizer.convert_tokens_to_ids(tokens)
input_ids = torch.tensor(ids)
<span class="hljs-comment"># 这一行会运行失败</span>
model(input_ids)`,wrap:!1}}),s=new h({props:{code:"SW5kZXhFcnJvciUzQSUyMERpbWVuc2lvbiUyMG91dCUyMG9mJTIwcmFuZ2UlMjAoZXhwZWN0ZWQlMjB0byUyMGJlJTIwaW4lMjByYW5nZSUyMG9mJTIwJTVCLTElMkMlMjAwJTVEJTJDJTIwYnV0JTIwZ290JTIwMSk=",highlighted:'IndexError: Dimension out of <span class="hljs-built_in">range</span> (expected to be <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span> of [-<span class="hljs-number">1</span>, <span class="hljs-number">0</span>], but got <span class="hljs-number">1</span>)',wrap:!1}}),{c(){b(e.$$.fragment),a=i(),b(s.$$.fragment)},l(t){j(e.$$.fragment,t),a=u(t),j(s.$$.fragment,t)},m(t,U){y(e,t,U),M(t,a,U),y(s,t,U),r=!0},i(t){r||(o(e.$$.fragment,t),o(s.$$.fragment,t),r=!0)},o(t){c(e.$$.fragment,t),c(s.$$.fragment,t),r=!1},d(t){t&&p(a),f(e,t),f(s,t)}}}function Fe(m){let e,a,s,r;return e=new h({props:{code:"dG9rZW5pemVkX2lucHV0cyUyMCUzRCUyMHRva2VuaXplcihzZXF1ZW5jZSUyQyUyMHJldHVybl90ZW5zb3JzJTNEJTIydGYlMjIpJTBBcHJpbnQodG9rZW5pemVkX2lucHV0cyU1QiUyMmlucHV0X2lkcyUyMiU1RCk=",highlighted:`tokenized_inputs = tokenizer(sequence, return_tensors=<span class="hljs-string">&quot;tf&quot;</span>)
<span class="hljs-built_in">print</span>(tokenized_inputs[<span class="hljs-string">&quot;input_ids&quot;</span>])`,wrap:!1}}),s=new h({props:{code:"dGYuVGVuc29yJTNBJTIwc2hhcGUlM0QoMSUyQyUyMDE2KSUyQyUyMGR0eXBlJTNEaW50MzIlMkMlMjBudW1weSUzRCUwQWFycmF5KCU1QiU1QiUyMCUyMDEwMSUyQyUyMCUyMDEwNDUlMkMlMjAlMjAxMDA1JTJDJTIwJTIwMjMxMCUyQyUyMCUyMDIwNDIlMkMlMjAlMjAzNDAzJTJDJTIwJTIwMjAwNSUyQyUyMCUyMDEwMzclMkMlMjAxNzY2MiUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMDEyMTcyJTJDJTIwJTIwMjYwNyUyQyUyMCUyMDIwMjYlMkMlMjAlMjAyODc4JTJDJTIwJTIwMjE2NiUyQyUyMCUyMDEwMTIlMkMlMjAlMjAlMjAxMDIlNUQlNUQlMkMlMjBkdHlwZSUzRGludDMyKSUzRQ==",highlighted:`tf.Tensor: shape=(<span class="hljs-number">1</span>, <span class="hljs-number">16</span>), dtype=int32, numpy=
array([[  <span class="hljs-number">101</span>,  <span class="hljs-number">1045</span>,  <span class="hljs-number">1005</span>,  <span class="hljs-number">2310</span>,  <span class="hljs-number">2042</span>,  <span class="hljs-number">3403</span>,  <span class="hljs-number">2005</span>,  <span class="hljs-number">1037</span>, <span class="hljs-number">17662</span>,
        <span class="hljs-number">12172</span>,  <span class="hljs-number">2607</span>,  <span class="hljs-number">2026</span>,  <span class="hljs-number">2878</span>,  <span class="hljs-number">2166</span>,  <span class="hljs-number">1012</span>,   <span class="hljs-number">102</span>]], dtype=int32)&gt;`,wrap:!1}}),{c(){b(e.$$.fragment),a=i(),b(s.$$.fragment)},l(t){j(e.$$.fragment,t),a=u(t),j(s.$$.fragment,t)},m(t,U){y(e,t,U),M(t,a,U),y(s,t,U),r=!0},i(t){r||(o(e.$$.fragment,t),o(s.$$.fragment,t),r=!0)},o(t){c(e.$$.fragment,t),c(s.$$.fragment,t),r=!1},d(t){t&&p(a),f(e,t),f(s,t)}}}function xe(m){let e,a,s,r;return e=new h({props:{code:"dG9rZW5pemVkX2lucHV0cyUyMCUzRCUyMHRva2VuaXplcihzZXF1ZW5jZSUyQyUyMHJldHVybl90ZW5zb3JzJTNEJTIycHQlMjIpJTBBcHJpbnQodG9rZW5pemVkX2lucHV0cyU1QiUyMmlucHV0X2lkcyUyMiU1RCk=",highlighted:`tokenized_inputs = tokenizer(sequence, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>)
<span class="hljs-built_in">print</span>(tokenized_inputs[<span class="hljs-string">&quot;input_ids&quot;</span>])`,wrap:!1}}),s=new h({props:{code:"dGVuc29yKCU1QiU1QiUyMCUyMDEwMSUyQyUyMCUyMDEwNDUlMkMlMjAlMjAxMDA1JTJDJTIwJTIwMjMxMCUyQyUyMCUyMDIwNDIlMkMlMjAlMjAzNDAzJTJDJTIwJTIwMjAwNSUyQyUyMCUyMDEwMzclMkMlMjAxNzY2MiUyQyUyMDEyMTcyJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwMjYwNyUyQyUyMCUyMDIwMjYlMkMlMjAlMjAyODc4JTJDJTIwJTIwMjE2NiUyQyUyMCUyMDEwMTIlMkMlMjAlMjAlMjAxMDIlNUQlNUQp",highlighted:`tensor([[  <span class="hljs-number">101</span>,  <span class="hljs-number">1045</span>,  <span class="hljs-number">1005</span>,  <span class="hljs-number">2310</span>,  <span class="hljs-number">2042</span>,  <span class="hljs-number">3403</span>,  <span class="hljs-number">2005</span>,  <span class="hljs-number">1037</span>, <span class="hljs-number">17662</span>, <span class="hljs-number">12172</span>,
          <span class="hljs-number">2607</span>,  <span class="hljs-number">2026</span>,  <span class="hljs-number">2878</span>,  <span class="hljs-number">2166</span>,  <span class="hljs-number">1012</span>,   <span class="hljs-number">102</span>]])`,wrap:!1}}),{c(){b(e.$$.fragment),a=i(),b(s.$$.fragment)},l(t){j(e.$$.fragment,t),a=u(t),j(s.$$.fragment,t)},m(t,U){y(e,t,U),M(t,a,U),y(s,t,U),r=!0},i(t){r||(o(e.$$.fragment,t),o(s.$$.fragment,t),r=!0)},o(t){c(e.$$.fragment,t),c(s.$$.fragment,t),r=!1},d(t){t&&p(a),f(e,t),f(s,t)}}}function Se(m){let e,a;return e=new h({props:{code:"aW1wb3J0JTIwdGVuc29yZmxvdyUyMGFzJTIwdGYlMEFmcm9tJTIwdHJhbnNmb3JtZXJzJTIwaW1wb3J0JTIwQXV0b1Rva2VuaXplciUyQyUyMFRGQXV0b01vZGVsRm9yU2VxdWVuY2VDbGFzc2lmaWNhdGlvbiUwQSUwQWNoZWNrcG9pbnQlMjAlM0QlMjAlMjJkaXN0aWxiZXJ0LWJhc2UtdW5jYXNlZC1maW5ldHVuZWQtc3N0LTItZW5nbGlzaCUyMiUwQXRva2VuaXplciUyMCUzRCUyMEF1dG9Ub2tlbml6ZXIuZnJvbV9wcmV0cmFpbmVkKGNoZWNrcG9pbnQpJTBBbW9kZWwlMjAlM0QlMjBURkF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24uZnJvbV9wcmV0cmFpbmVkKGNoZWNrcG9pbnQpJTBBJTBBc2VxdWVuY2UlMjAlM0QlMjAlMjJJJ3ZlJTIwYmVlbiUyMHdhaXRpbmclMjBmb3IlMjBhJTIwSHVnZ2luZ0ZhY2UlMjBjb3Vyc2UlMjBteSUyMHdob2xlJTIwbGlmZS4lMjIlMEElMEF0b2tlbnMlMjAlM0QlMjB0b2tlbml6ZXIudG9rZW5pemUoc2VxdWVuY2UpJTBBaWRzJTIwJTNEJTIwdG9rZW5pemVyLmNvbnZlcnRfdG9rZW5zX3RvX2lkcyh0b2tlbnMpJTBBJTBBaW5wdXRfaWRzJTIwJTNEJTIwdGYuY29uc3RhbnQoJTVCaWRzJTVEKSUwQXByaW50KCUyMklucHV0JTIwSURzJTNBJTIyJTJDJTIwaW5wdXRfaWRzKSUwQSUwQW91dHB1dCUyMCUzRCUyMG1vZGVsKGlucHV0X2lkcyklMEFwcmludCglMjJMb2dpdHMlM0ElMjIlMkMlMjBvdXRwdXQubG9naXRzKQ==",highlighted:`<span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, TFAutoModelForSequenceClassification

checkpoint = <span class="hljs-string">&quot;distilbert-base-uncased-finetuned-sst-2-english&quot;</span>
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
model = TFAutoModelForSequenceClassification.from_pretrained(checkpoint)

sequence = <span class="hljs-string">&quot;I&#x27;ve been waiting for a HuggingFace course my whole life.&quot;</span>

tokens = tokenizer.tokenize(sequence)
ids = tokenizer.convert_tokens_to_ids(tokens)

input_ids = tf.constant([ids])
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Input IDs:&quot;</span>, input_ids)

output = model(input_ids)
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Logits:&quot;</span>, output.logits)`,wrap:!1}}),{c(){b(e.$$.fragment)},l(s){j(e.$$.fragment,s)},m(s,r){y(e,s,r),a=!0},i(s){a||(o(e.$$.fragment,s),a=!0)},o(s){c(e.$$.fragment,s),a=!1},d(s){f(e,s)}}}function He(m){let e,a;return e=new h({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwdHJhbnNmb3JtZXJzJTIwaW1wb3J0JTIwQXV0b1Rva2VuaXplciUyQyUyMEF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24lMEElMEFjaGVja3BvaW50JTIwJTNEJTIwJTIyZGlzdGlsYmVydC1iYXNlLXVuY2FzZWQtZmluZXR1bmVkLXNzdC0yLWVuZ2xpc2glMjIlMEF0b2tlbml6ZXIlMjAlM0QlMjBBdXRvVG9rZW5pemVyLmZyb21fcHJldHJhaW5lZChjaGVja3BvaW50KSUwQW1vZGVsJTIwJTNEJTIwQXV0b01vZGVsRm9yU2VxdWVuY2VDbGFzc2lmaWNhdGlvbi5mcm9tX3ByZXRyYWluZWQoY2hlY2twb2ludCklMEElMEFzZXF1ZW5jZSUyMCUzRCUyMCUyMkkndmUlMjBiZWVuJTIwd2FpdGluZyUyMGZvciUyMGElMjBIdWdnaW5nRmFjZSUyMGNvdXJzZSUyMG15JTIwd2hvbGUlMjBsaWZlLiUyMiUwQSUwQXRva2VucyUyMCUzRCUyMHRva2VuaXplci50b2tlbml6ZShzZXF1ZW5jZSklMEFpZHMlMjAlM0QlMjB0b2tlbml6ZXIuY29udmVydF90b2tlbnNfdG9faWRzKHRva2VucyklMEElMEFpbnB1dF9pZHMlMjAlM0QlMjB0b3JjaC50ZW5zb3IoJTVCaWRzJTVEKSUwQXByaW50KCUyMklucHV0JTIwSURzJTNBJTIyJTJDJTIwaW5wdXRfaWRzKSUwQSUwQW91dHB1dCUyMCUzRCUyMG1vZGVsKGlucHV0X2lkcyklMEFwcmludCglMjJMb2dpdHMlM0ElMjIlMkMlMjBvdXRwdXQubG9naXRzKQ==",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, AutoModelForSequenceClassification

checkpoint = <span class="hljs-string">&quot;distilbert-base-uncased-finetuned-sst-2-english&quot;</span>
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
model = AutoModelForSequenceClassification.from_pretrained(checkpoint)

sequence = <span class="hljs-string">&quot;I&#x27;ve been waiting for a HuggingFace course my whole life.&quot;</span>

tokens = tokenizer.tokenize(sequence)
ids = tokenizer.convert_tokens_to_ids(tokens)

input_ids = torch.tensor([ids])
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Input IDs:&quot;</span>, input_ids)

output = model(input_ids)
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Logits:&quot;</span>, output.logits)`,wrap:!1}}),{c(){b(e.$$.fragment)},l(s){j(e.$$.fragment,s)},m(s,r){y(e,s,r),a=!0},i(s){a||(o(e.$$.fragment,s),a=!0)},o(s){c(e.$$.fragment,s),a=!1},d(s){f(e,s)}}}function Ye(m){let e,a;return e=new h({props:{code:"SW5wdXQlMjBJRHMlM0ElMjB0Zi5UZW5zb3IoJTBBJTVCJTVCJTIwMTA0NSUyMCUyMDEwMDUlMjAlMjAyMzEwJTIwJTIwMjA0MiUyMCUyMDM0MDMlMjAlMjAyMDA1JTIwJTIwMTAzNyUyMDE3NjYyJTIwMTIxNzIlMjAlMjAyNjA3JTIwJTIwMjAyNiUyMCUyMDI4NzglMEElMjAlMjAlMjAyMTY2JTIwJTIwMTAxMiU1RCU1RCUyQyUyMHNoYXBlJTNEKDElMkMlMjAxNCklMkMlMjBkdHlwZSUzRGludDMyKSUwQUxvZ2l0cyUzQSUyMHRmLlRlbnNvciglNUIlNUItMi43Mjc2MjA4JTIwJTIwMi44Nzg5Mzc3JTVEJTVEJTJDJTIwc2hhcGUlM0QoMSUyQyUyMDIpJTJDJTIwZHR5cGUlM0RmbG9hdDMyKQ==",highlighted:`Input IDs: tf.Tensor(
[[ <span class="hljs-number">1045</span>  <span class="hljs-number">1005</span>  <span class="hljs-number">2310</span>  <span class="hljs-number">2042</span>  <span class="hljs-number">3403</span>  <span class="hljs-number">2005</span>  <span class="hljs-number">1037</span> <span class="hljs-number">17662</span> <span class="hljs-number">12172</span>  <span class="hljs-number">2607</span>  <span class="hljs-number">2026</span>  <span class="hljs-number">2878</span>
   <span class="hljs-number">2166</span>  <span class="hljs-number">1012</span>]], shape=(<span class="hljs-number">1</span>, <span class="hljs-number">14</span>), dtype=int32)
Logits: tf.Tensor([[-<span class="hljs-number">2.7276208</span>  <span class="hljs-number">2.8789377</span>]], shape=(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>), dtype=float32)`,wrap:!1}}),{c(){b(e.$$.fragment)},l(s){j(e.$$.fragment,s)},m(s,r){y(e,s,r),a=!0},i(s){a||(o(e.$$.fragment,s),a=!0)},o(s){c(e.$$.fragment,s),a=!1},d(s){f(e,s)}}}function qe(m){let e,a;return e=new h({props:{code:"SW5wdXQlMjBJRHMlM0ElMjAlNUIlNUIlMjAxMDQ1JTJDJTIwJTIwMTAwNSUyQyUyMCUyMDIzMTAlMkMlMjAlMjAyMDQyJTJDJTIwJTIwMzQwMyUyQyUyMCUyMDIwMDUlMkMlMjAlMjAxMDM3JTJDJTIwMTc2NjIlMkMlMjAxMjE3MiUyQyUyMCUyMDI2MDclMkMlMjAyMDI2JTJDJTIwJTIwMjg3OCUyQyUyMCUyMDIxNjYlMkMlMjAlMjAxMDEyJTVEJTVEJTBBTG9naXRzJTNBJTIwJTVCJTVCLTIuNzI3NiUyQyUyMCUyMDIuODc4OSU1RCU1RA==",highlighted:`Input IDs: [[ <span class="hljs-number">1045</span>,  <span class="hljs-number">1005</span>,  <span class="hljs-number">2310</span>,  <span class="hljs-number">2042</span>,  <span class="hljs-number">3403</span>,  <span class="hljs-number">2005</span>,  <span class="hljs-number">1037</span>, <span class="hljs-number">17662</span>, <span class="hljs-number">12172</span>,  <span class="hljs-number">2607</span>, <span class="hljs-number">2026</span>,  <span class="hljs-number">2878</span>,  <span class="hljs-number">2166</span>,  <span class="hljs-number">1012</span>]]
Logits: [[-<span class="hljs-number">2.7276</span>,  <span class="hljs-number">2.8789</span>]]`,wrap:!1}}),{c(){b(e.$$.fragment)},l(s){j(e.$$.fragment,s)},m(s,r){y(e,s,r),a=!0},i(s){a||(o(e.$$.fragment,s),a=!0)},o(s){c(e.$$.fragment,s),a=!1},d(s){f(e,s)}}}function Le(m){let e,a="✏️   <strong>试试看！</strong>  将这个 <code>batched_ids</code> 列表转换为张量，并通过你的模型进行处理。检查你是否得到了与之前相同的 logits 值（但是重复了两次）！";return{c(){e=d("p"),e.innerHTML=a},l(s){e=J(s,"P",{"data-svelte-h":!0}),w(e)!=="svelte-10gqb74"&&(e.innerHTML=a)},m(s,r){M(s,e,r)},p:$e,d(s){s&&p(e)}}}function Ke(m){let e,a,s,r;return e=new h({props:{code:"bW9kZWwlMjAlM0QlMjBURkF1dG9Nb2RlbEZvclNlcXVlbmNlQ2xhc3NpZmljYXRpb24uZnJvbV9wcmV0cmFpbmVkKGNoZWNrcG9pbnQpJTBBJTBBc2VxdWVuY2UxX2lkcyUyMCUzRCUyMCU1QiU1QjIwMCUyQyUyMDIwMCUyQyUyMDIwMCU1RCU1RCUwQXNlcXVlbmNlMl9pZHMlMjAlM0QlMjAlNUIlNUIyMDAlMkMlMjAyMDAlNUQlNUQlMEFiYXRjaGVkX2lkcyUyMCUzRCUyMCU1QiUwQSUyMCUyMCUyMCUyMCU1QjIwMCUyQyUyMDIwMCUyQyUyMDIwMCU1RCUyQyUwQSUyMCUyMCUyMCUyMCU1QjIwMCUyQyUyMDIwMCUyQyUyMHRva2VuaXplci5wYWRfdG9rZW5faWQlNUQlMkMlMEElNUQlMEElMEFwcmludChtb2RlbCh0Zi5jb25zdGFudChzZXF1ZW5jZTFfaWRzKSkubG9naXRzKSUwQXByaW50KG1vZGVsKHRmLmNvbnN0YW50KHNlcXVlbmNlMl9pZHMpKS5sb2dpdHMpJTBBcHJpbnQobW9kZWwodGYuY29uc3RhbnQoYmF0Y2hlZF9pZHMpKS5sb2dpdHMp",highlighted:`model = TFAutoModelForSequenceClassification.from_pretrained(checkpoint)

sequence1_ids = [[<span class="hljs-number">200</span>, <span class="hljs-number">200</span>, <span class="hljs-number">200</span>]]
sequence2_ids = [[<span class="hljs-number">200</span>, <span class="hljs-number">200</span>]]
batched_ids = [
    [<span class="hljs-number">200</span>, <span class="hljs-number">200</span>, <span class="hljs-number">200</span>],
    [<span class="hljs-number">200</span>, <span class="hljs-number">200</span>, tokenizer.pad_token_id],
]

<span class="hljs-built_in">print</span>(model(tf.constant(sequence1_ids)).logits)
<span class="hljs-built_in">print</span>(model(tf.constant(sequence2_ids)).logits)
<span class="hljs-built_in">print</span>(model(tf.constant(batched_ids)).logits)`,wrap:!1}}),s=new h({props:{code:"dGYuVGVuc29yKCU1QiU1QiUyMDEuNTY5MzY3OCUyMC0xLjM4OTQ1ODElNUQlNUQlMkMlMjBzaGFwZSUzRCgxJTJDJTIwMiklMkMlMjBkdHlwZSUzRGZsb2F0MzIpJTBBdGYuVGVuc29yKCU1QiU1QiUyMDAuNTgwMzAwNSUyMCUyMC0wLjQxMjUyNDI4JTVEJTVEJTJDJTIwc2hhcGUlM0QoMSUyQyUyMDIpJTJDJTIwZHR5cGUlM0RmbG9hdDMyKSUwQXRmLlRlbnNvciglMEElNUIlNUIlMjAxLjU2OTM2ODElMjAtMS4zODk0NTgyJTVEJTBBJTIwJTVCJTIwMS4zMzczNDg2JTIwLTEuMjE2MzE5MyU1RCU1RCUyQyUyMHNoYXBlJTNEKDIlMkMlMjAyKSUyQyUyMGR0eXBlJTNEZmxvYXQzMik=",highlighted:`tf.Tensor([[ <span class="hljs-number">1.5693678</span> -<span class="hljs-number">1.3894581</span>]], shape=(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>), dtype=float32)
tf.Tensor([[ <span class="hljs-number">0.5803005</span>  -<span class="hljs-number">0.41252428</span>]], shape=(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>), dtype=float32)
tf.Tensor(
[[ <span class="hljs-number">1.5693681</span> -<span class="hljs-number">1.3894582</span>]
 [ <span class="hljs-number">1.3373486</span> -<span class="hljs-number">1.2163193</span>]], shape=(<span class="hljs-number">2</span>, <span class="hljs-number">2</span>), dtype=float32)`,wrap:!1}}),{c(){b(e.$$.fragment),a=i(),b(s.$$.fragment)},l(t){j(e.$$.fragment,t),a=u(t),j(s.$$.fragment,t)},m(t,U){y(e,t,U),M(t,a,U),y(s,t,U),r=!0},i(t){r||(o(e.$$.fragment,t),o(s.$$.fragment,t),r=!0)},o(t){c(e.$$.fragment,t),c(s.$$.fragment,t),r=!1},d(t){t&&p(a),f(e,t),f(s,t)}}}function Pe(m){let e,a,s,r;return e=new h({props:{code:"bW9kZWwlMjAlM0QlMjBBdXRvTW9kZWxGb3JTZXF1ZW5jZUNsYXNzaWZpY2F0aW9uLmZyb21fcHJldHJhaW5lZChjaGVja3BvaW50KSUwQSUwQXNlcXVlbmNlMV9pZHMlMjAlM0QlMjAlNUIlNUIyMDAlMkMlMjAyMDAlMkMlMjAyMDAlNUQlNUQlMEFzZXF1ZW5jZTJfaWRzJTIwJTNEJTIwJTVCJTVCMjAwJTJDJTIwMjAwJTVEJTVEJTBBYmF0Y2hlZF9pZHMlMjAlM0QlMjAlNUIlMEElMjAlMjAlMjAlMjAlNUIyMDAlMkMlMjAyMDAlMkMlMjAyMDAlNUQlMkMlMEElMjAlMjAlMjAlMjAlNUIyMDAlMkMlMjAyMDAlMkMlMjB0b2tlbml6ZXIucGFkX3Rva2VuX2lkJTVEJTJDJTBBJTVEJTBBJTBBcHJpbnQobW9kZWwodG9yY2gudGVuc29yKHNlcXVlbmNlMV9pZHMpKS5sb2dpdHMpJTBBcHJpbnQobW9kZWwodG9yY2gudGVuc29yKHNlcXVlbmNlMl9pZHMpKS5sb2dpdHMpJTBBcHJpbnQobW9kZWwodG9yY2gudGVuc29yKGJhdGNoZWRfaWRzKSkubG9naXRzKQ==",highlighted:`model = AutoModelForSequenceClassification.from_pretrained(checkpoint)

sequence1_ids = [[<span class="hljs-number">200</span>, <span class="hljs-number">200</span>, <span class="hljs-number">200</span>]]
sequence2_ids = [[<span class="hljs-number">200</span>, <span class="hljs-number">200</span>]]
batched_ids = [
    [<span class="hljs-number">200</span>, <span class="hljs-number">200</span>, <span class="hljs-number">200</span>],
    [<span class="hljs-number">200</span>, <span class="hljs-number">200</span>, tokenizer.pad_token_id],
]

<span class="hljs-built_in">print</span>(model(torch.tensor(sequence1_ids)).logits)
<span class="hljs-built_in">print</span>(model(torch.tensor(sequence2_ids)).logits)
<span class="hljs-built_in">print</span>(model(torch.tensor(batched_ids)).logits)`,wrap:!1}}),s=new h({props:{code:"dGVuc29yKCU1QiU1QiUyMDEuNTY5NCUyQyUyMC0xLjM4OTUlNUQlNUQlMkMlMjBncmFkX2ZuJTNEJTNDQWRkbW1CYWNrd2FyZCUzRSklMEF0ZW5zb3IoJTVCJTVCJTIwMC41ODAzJTJDJTIwLTAuNDEyNSU1RCU1RCUyQyUyMGdyYWRfZm4lM0QlM0NBZGRtbUJhY2t3YXJkJTNFKSUwQXRlbnNvciglNUIlNUIlMjAxLjU2OTQlMkMlMjAtMS4zODk1JTVEJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTVCJTIwMS4zMzczJTJDJTIwLTEuMjE2MyU1RCU1RCUyQyUyMGdyYWRfZm4lM0QlM0NBZGRtbUJhY2t3YXJkJTNFKQ==",highlighted:`tensor([[ <span class="hljs-number">1.5694</span>, -<span class="hljs-number">1.3895</span>]], grad_fn=&lt;AddmmBackward&gt;)
tensor([[ <span class="hljs-number">0.5803</span>, -<span class="hljs-number">0.4125</span>]], grad_fn=&lt;AddmmBackward&gt;)
tensor([[ <span class="hljs-number">1.5694</span>, -<span class="hljs-number">1.3895</span>],
        [ <span class="hljs-number">1.3373</span>, -<span class="hljs-number">1.2163</span>]], grad_fn=&lt;AddmmBackward&gt;)`,wrap:!1}}),{c(){b(e.$$.fragment),a=i(),b(s.$$.fragment)},l(t){j(e.$$.fragment,t),a=u(t),j(s.$$.fragment,t)},m(t,U){y(e,t,U),M(t,a,U),y(s,t,U),r=!0},i(t){r||(o(e.$$.fragment,t),o(s.$$.fragment,t),r=!0)},o(t){c(e.$$.fragment,t),c(s.$$.fragment,t),r=!1},d(t){t&&p(a),f(e,t),f(s,t)}}}function Oe(m){let e,a,s,r;return e=new h({props:{code:"YmF0Y2hlZF9pZHMlMjAlM0QlMjAlNUIlMEElMjAlMjAlMjAlMjAlNUIyMDAlMkMlMjAyMDAlMkMlMjAyMDAlNUQlMkMlMEElMjAlMjAlMjAlMjAlNUIyMDAlMkMlMjAyMDAlMkMlMjB0b2tlbml6ZXIucGFkX3Rva2VuX2lkJTVEJTJDJTBBJTVEJTBBJTBBYXR0ZW50aW9uX21hc2slMjAlM0QlMjAlNUIlMEElMjAlMjAlMjAlMjAlNUIxJTJDJTIwMSUyQyUyMDElNUQlMkMlMEElMjAlMjAlMjAlMjAlNUIxJTJDJTIwMSUyQyUyMDAlNUQlMkMlMEElNUQlMEElMEFvdXRwdXRzJTIwJTNEJTIwbW9kZWwodGYuY29uc3RhbnQoYmF0Y2hlZF9pZHMpJTJDJTIwYXR0ZW50aW9uX21hc2slM0R0Zi5jb25zdGFudChhdHRlbnRpb25fbWFzaykpJTBBcHJpbnQob3V0cHV0cy5sb2dpdHMp",highlighted:`batched_ids = [
    [<span class="hljs-number">200</span>, <span class="hljs-number">200</span>, <span class="hljs-number">200</span>],
    [<span class="hljs-number">200</span>, <span class="hljs-number">200</span>, tokenizer.pad_token_id],
]

attention_mask = [
    [<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">0</span>],
]

outputs = model(tf.constant(batched_ids), attention_mask=tf.constant(attention_mask))
<span class="hljs-built_in">print</span>(outputs.logits)`,wrap:!1}}),s=new h({props:{code:"dGYuVGVuc29yKCUwQSU1QiU1QiUyMDEuNTY5MzY4MSUyMCUyMC0xLjM4OTQ1ODIlMjAlNUQlMEElMjAlNUIlMjAwLjU4MDMwMjElMjAlMjAtMC40MTI1MjU4NiU1RCU1RCUyQyUyMHNoYXBlJTNEKDIlMkMlMjAyKSUyQyUyMGR0eXBlJTNEZmxvYXQzMik=",highlighted:`tf.Tensor(
[[ <span class="hljs-number">1.5693681</span>  -<span class="hljs-number">1.3894582</span> ]
 [ <span class="hljs-number">0.5803021</span>  -<span class="hljs-number">0.41252586</span>]], shape=(<span class="hljs-number">2</span>, <span class="hljs-number">2</span>), dtype=float32)`,wrap:!1}}),{c(){b(e.$$.fragment),a=i(),b(s.$$.fragment)},l(t){j(e.$$.fragment,t),a=u(t),j(s.$$.fragment,t)},m(t,U){y(e,t,U),M(t,a,U),y(s,t,U),r=!0},i(t){r||(o(e.$$.fragment,t),o(s.$$.fragment,t),r=!0)},o(t){c(e.$$.fragment,t),c(s.$$.fragment,t),r=!1},d(t){t&&p(a),f(e,t),f(s,t)}}}function lt(m){let e,a,s,r;return e=new h({props:{code:"YmF0Y2hlZF9pZHMlMjAlM0QlMjAlNUIlMEElMjAlMjAlMjAlMjAlNUIyMDAlMkMlMjAyMDAlMkMlMjAyMDAlNUQlMkMlMEElMjAlMjAlMjAlMjAlNUIyMDAlMkMlMjAyMDAlMkMlMjB0b2tlbml6ZXIucGFkX3Rva2VuX2lkJTVEJTJDJTBBJTVEJTBBJTBBYXR0ZW50aW9uX21hc2slMjAlM0QlMjAlNUIlMEElMjAlMjAlMjAlMjAlNUIxJTJDJTIwMSUyQyUyMDElNUQlMkMlMEElMjAlMjAlMjAlMjAlNUIxJTJDJTIwMSUyQyUyMDAlNUQlMkMlMEElNUQlMEElMEFvdXRwdXRzJTIwJTNEJTIwbW9kZWwodG9yY2gudGVuc29yKGJhdGNoZWRfaWRzKSUyQyUyMGF0dGVudGlvbl9tYXNrJTNEdG9yY2gudGVuc29yKGF0dGVudGlvbl9tYXNrKSklMEFwcmludChvdXRwdXRzLmxvZ2l0cyk=",highlighted:`batched_ids = [
    [<span class="hljs-number">200</span>, <span class="hljs-number">200</span>, <span class="hljs-number">200</span>],
    [<span class="hljs-number">200</span>, <span class="hljs-number">200</span>, tokenizer.pad_token_id],
]

attention_mask = [
    [<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">0</span>],
]

outputs = model(torch.tensor(batched_ids), attention_mask=torch.tensor(attention_mask))
<span class="hljs-built_in">print</span>(outputs.logits)`,wrap:!1}}),s=new h({props:{code:"dGVuc29yKCU1QiU1QiUyMDEuNTY5NCUyQyUyMC0xLjM4OTUlNUQlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlNUIlMjAwLjU4MDMlMkMlMjAtMC40MTI1JTVEJTVEJTJDJTIwZ3JhZF9mbiUzRCUzQ0FkZG1tQmFja3dhcmQlM0Up",highlighted:`tensor([[ <span class="hljs-number">1.5694</span>, -<span class="hljs-number">1.3895</span>],
        [ <span class="hljs-number">0.5803</span>, -<span class="hljs-number">0.4125</span>]], grad_fn=&lt;AddmmBackward&gt;)`,wrap:!1}}),{c(){b(e.$$.fragment),a=i(),b(s.$$.fragment)},l(t){j(e.$$.fragment,t),a=u(t),j(s.$$.fragment,t)},m(t,U){y(e,t,U),M(t,a,U),y(s,t,U),r=!0},i(t){r||(o(e.$$.fragment,t),o(s.$$.fragment,t),r=!0)},o(t){c(e.$$.fragment,t),c(s.$$.fragment,t),r=!1},d(t){t&&p(a),f(e,t),f(s,t)}}}function st(m){let e,a="✏️ <strong>试试看！</strong>在第二节使用的两个句子（“I’ve been waiting for a HuggingFace course my whole life.” 和 “I hate this so much!”）上手动进行 tokenize。将它们输入模型并检查你是否得到了与第二节相同的 logits 值。然后使用填充 token 将它们一起进行批处理，然后创建合适的注意力掩码。检查模型计算后是否得到了相同的结果！";return{c(){e=d("p"),e.innerHTML=a},l(s){e=J(s,"P",{"data-svelte-h":!0}),w(e)!=="svelte-rla7a6"&&(e.innerHTML=a)},m(s,r){M(s,e,r)},p:$e,d(s){s&&p(e)}}}function et(m){let e,a,s,r,t,U,D,Bl,F,Dl,T,k,Zl,_,$,Vl,x,gs="在上一节中，我们探讨了最简单的案例：对一个较短的句子进行推理。然而，一些问题已经出现：",Fl,S,Es="<li><p>我们如何处理多个句子？</p></li> <li><p>我们如何处理不同长度的多个句子？</p></li> <li><p>词汇索引是唯一可以让模型运行的输入吗？</p></li> <li><p>是否存在句子太长的问题？</p></li>",xl,H,zs="让我们看看这些问题会带来什么样的问题，以及如何使用🤗 Transformers API 解决它们",Sl,Y,Hl,q,Ws="在上一个练习中，你看到了句子如何转换为数字列表。让我们将此数字列表转换为张量，并将其发送到模型：",Yl,C,I,gl,L,Gs="哦，不！为什么失败了？“我们的确是按照第 2 节中管道的步骤一步步来做的。",ql,K,Rs="问题是我们向模型发送了一个单独的句子，而🤗 Transformers 模型默认情况下需要一个句子列表。在这里，当我们试图重现 tokenizer 在输入 <code>sequence</code> 后在其内部进行的所有操作。但如果你仔细观察，你会发现 tokenizer 不仅仅是将 inputs ID 的列表转换为张量，它还在其上添加了一个维度：",Ll,N,A,El,P,Xs="让我们再试一次并添加一个新的维度：",Kl,Q,Z,zl,O,vs="让我们打印 inputs ID 以及生成的 logits 值，以下是输出：",Pl,V,g,Wl,ll,Bs="批处理（Batching）是一次性通过模型发送多个句子的行为。如果你只有一句话，你可以构建一个只有一个句子的 batch：",Ol,sl,ls,el,Ds="这就是一个包含两个相同句子的 batch",ss,R,es,tl,Fs="批处理支持模型在输入多个句子时工作。使用多个句子就像使用单个句子构建批一样简单。不过，还有第二个问题。当你试图将两个（或更多）句子组合在一起时，它们的长度可能不同。如果你以前使用过张量，那么你知道它们必须是矩形，因此无法将 inputs ID 列表直接转换为张量。为了解决这个问题，我们通常填充输入（Padding）。",ts,nl,ns,al,xs="以下列表不能转换为张量：",as,pl,ps,Ml,Ss="为了解决这个问题，我们将使用填充使张量成为标准的矩形。Padding 通过在值较少的句子中添加一个名为 <code>padding_id</code> 的特殊单词来确保我们所有的句子长度相同。例如，如果你有 10 个包含 10 个单词的句子和 1 个包含 20 个单词的句子，填充能确保所有句子都包含 20 个单词。在我们的示例中，填充后的张量如下所示：",Ms,rl,rs,il,Hs="我们可以在 <code>tokenizer.pad_token_id</code> 中找到填充 token 的 ID。让我们使用它，分别用模型处理这两个句子，然后再尝试将这两个句子放在一起用模型批处理：",is,E,z,Gl,ul,Ys="咦，我们批处理预测中的 logits 值有点问题：第二行应该与第二句的 logits 相同，但我们得到了完全不同的值！",us,cl,qs="这是因为 Transformer 模型的关键特性：注意力层，它考虑了每个 token 的上下文信息。这具体来说，每个 token 的含义并非单独存在的，它的含义还取决于它在句子中的位置以及周围的其他 tokens。当我们使用填充（padding）来处理长度不同的句子时，我们会添加特殊的“填充 token”来使所有句子达到相同的长度。但是，注意力层会将这些填充 token 也纳入考虑，因为它们会关注序列中的所有 tokens。这就导致了一个问题：尽管填充 token 本身并没有实际的含义，但它们的存在会影响模型对句子的理解。我们需要告诉这些注意层忽略填充 token。这是通过使用注意力掩码（attention mask）层来实现的。",cs,ol,os,ml,Ls="注意力掩码（attention mask）是与 inputs ID 张量形状完全相同的张量，用 0 和 1 填充：1 表示应关注相应的 tokens，0 表示应忽略相应的 tokens（即，它们应被模型的注意力层忽视）。",ms,bl,Ks="让我们用 attention mask 修改上一个示例：",bs,W,G,Rl,jl,Ps="现在我们得到了批处理中第二句话相同的 logits 值。",js,yl,Os="注意第二序列的最后一个值是填充 ID，其在注意力掩码中的值为 0。",ys,X,fs,fl,Us,Ul,le="对于 Transformers 模型，我们可以通过模型的序列长度是有限的。大多数模型处理多达 512 或 1024 个 的 tokens 序列，当使用模型处理更长的序列时，会崩溃。此问题有两种解决方案：",ds,dl,se="<li>使用支持更长序列长度的模型。</li> <li>截断你的序列。</li>",Js,Jl,ee='不同的模型支持的序列长度各不相同，有些模型专门用于处理非常长的序列。例如 <a href="https://huggingface.co/transformers/model_doc/longformer" rel="nofollow">Longformer</a> 和 <a href="https://huggingface.co/transformers/model_doc/led" rel="nofollow">LED</a> 。如果你正在处理需要非常长序列的任务，我们建议你考虑使用这些模型。',ws,wl,te="另外，我们建议你通过设定 <code>max_sequence_length</code> 参数来截断序列：",hs,hl,Ts,Tl,ks,Xl,_s;t=new We({props:{fw:m[0]}}),D=new Ee({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),F=new vl({props:{title:"处理多个序列",local:"处理多个序列",headingTag:"h1"}});const ne=[Re,Ge],kl=[];function ae(l,n){return l[0]==="pt"?0:1}T=ae(m),k=kl[T]=ne[T](m);const pe=[ve,Xe],_l=[];function Me(l,n){return l[0]==="pt"?0:1}_=Me(m),$=_l[_]=pe[_](m),Y=new vl({props:{title:"模型需要一批输入",local:"模型需要一批输入",headingTag:"h2"}});const re=[De,Be],$l=[];function ie(l,n){return l[0]==="pt"?0:1}C=ie(m),I=$l[C]=re[C](m);const ue=[xe,Fe],Cl=[];function ce(l,n){return l[0]==="pt"?0:1}N=ce(m),A=Cl[N]=ue[N](m);const oe=[He,Se],Il=[];function me(l,n){return l[0]==="pt"?0:1}Q=me(m),Z=Il[Q]=oe[Q](m);const be=[qe,Ye],Nl=[];function je(l,n){return l[0]==="pt"?0:1}V=je(m),g=Nl[V]=be[V](m),sl=new h({props:{code:"YmF0Y2hlZF9pZHMlMjAlM0QlMjAlNUJpZHMlMkMlMjBpZHMlNUQ=",highlighted:'<span class="hljs-attr">batched_ids</span> = [ids, ids]',wrap:!1}}),R=new _e({props:{$$slots:{default:[Le]},$$scope:{ctx:m}}}),nl=new vl({props:{title:"填充输入（Padding）",local:"填充输入（Padding）",headingTag:"h2"}}),pl=new h({props:{code:"YmF0Y2hlZF9pZHMlMjAlM0QlMjAlNUIlMEElMjAlMjAlMjAlMjAlNUIyMDAlMkMlMjAyMDAlMkMlMjAyMDAlNUQlMkMlMEElMjAlMjAlMjAlMjAlNUIyMDAlMkMlMjAyMDAlNUQlMEElNUQ=",highlighted:`batched_ids = [
    [<span class="hljs-number">200</span>, <span class="hljs-number">200</span>, <span class="hljs-number">200</span>],
    [<span class="hljs-number">200</span>, <span class="hljs-number">200</span>]
]`,wrap:!1}}),rl=new h({props:{code:"cGFkZGluZ19pZCUyMCUzRCUyMDEwMCUwQSUwQWJhdGNoZWRfaWRzJTIwJTNEJTIwJTVCJTBBJTIwJTIwJTIwJTIwJTVCMjAwJTJDJTIwMjAwJTJDJTIwMjAwJTVEJTJDJTBBJTIwJTIwJTIwJTIwJTVCMjAwJTJDJTIwMjAwJTJDJTIwcGFkZGluZ19pZCU1RCUyQyUwQSU1RA==",highlighted:`padding_id = <span class="hljs-number">100</span>

batched_ids = [
    [<span class="hljs-number">200</span>, <span class="hljs-number">200</span>, <span class="hljs-number">200</span>],
    [<span class="hljs-number">200</span>, <span class="hljs-number">200</span>, padding_id],
]`,wrap:!1}});const ye=[Pe,Ke],Al=[];function fe(l,n){return l[0]==="pt"?0:1}E=fe(m),z=Al[E]=ye[E](m),ol=new vl({props:{title:"注意力掩码（attention mask）层",local:"注意力掩码（attention mask）层",headingTag:"h2"}});const Ue=[lt,Oe],Ql=[];function de(l,n){return l[0]==="pt"?0:1}return W=de(m),G=Ql[W]=Ue[W](m),X=new _e({props:{$$slots:{default:[st]},$$scope:{ctx:m}}}),fl=new vl({props:{title:"更长的句子",local:"更长的句子",headingTag:"h2"}}),hl=new h({props:{code:"c2VxdWVuY2UlMjAlM0QlMjBzZXF1ZW5jZSU1QiUzQW1heF9zZXF1ZW5jZV9sZW5ndGglNUQ=",highlighted:"sequence = sequence[:max_sequence_length]",wrap:!1}}),Tl=new ze({props:{source:"https://github.com/huggingface/course/blob/main/chapters/zh-CN/chapter2/5.mdx"}}),{c(){e=d("meta"),a=i(),s=d("p"),r=i(),b(t.$$.fragment),U=i(),b(D.$$.fragment),Bl=i(),b(F.$$.fragment),Dl=i(),k.c(),Zl=i(),$.c(),Vl=i(),x=d("p"),x.textContent=gs,Fl=i(),S=d("ul"),S.innerHTML=Es,xl=i(),H=d("p"),H.textContent=zs,Sl=i(),b(Y.$$.fragment),Hl=i(),q=d("p"),q.textContent=Ws,Yl=i(),I.c(),gl=i(),L=d("p"),L.textContent=Gs,ql=i(),K=d("p"),K.innerHTML=Rs,Ll=i(),A.c(),El=i(),P=d("p"),P.textContent=Xs,Kl=i(),Z.c(),zl=i(),O=d("p"),O.textContent=vs,Pl=i(),g.c(),Wl=i(),ll=d("p"),ll.textContent=Bs,Ol=i(),b(sl.$$.fragment),ls=i(),el=d("p"),el.textContent=Ds,ss=i(),b(R.$$.fragment),es=i(),tl=d("p"),tl.textContent=Fs,ts=i(),b(nl.$$.fragment),ns=i(),al=d("p"),al.textContent=xs,as=i(),b(pl.$$.fragment),ps=i(),Ml=d("p"),Ml.innerHTML=Ss,Ms=i(),b(rl.$$.fragment),rs=i(),il=d("p"),il.innerHTML=Hs,is=i(),z.c(),Gl=i(),ul=d("p"),ul.textContent=Ys,us=i(),cl=d("p"),cl.textContent=qs,cs=i(),b(ol.$$.fragment),os=i(),ml=d("p"),ml.textContent=Ls,ms=i(),bl=d("p"),bl.textContent=Ks,bs=i(),G.c(),Rl=i(),jl=d("p"),jl.textContent=Ps,js=i(),yl=d("p"),yl.textContent=Os,ys=i(),b(X.$$.fragment),fs=i(),b(fl.$$.fragment),Us=i(),Ul=d("p"),Ul.textContent=le,ds=i(),dl=d("ul"),dl.innerHTML=se,Js=i(),Jl=d("p"),Jl.innerHTML=ee,ws=i(),wl=d("p"),wl.innerHTML=te,hs=i(),b(hl.$$.fragment),Ts=i(),b(Tl.$$.fragment),ks=i(),Xl=d("p"),this.h()},l(l){const n=Ve("svelte-u9bgzb",document.head);e=J(n,"META",{name:!0,content:!0}),n.forEach(p),a=u(l),s=J(l,"P",{}),Te(s).forEach(p),r=u(l),j(t.$$.fragment,l),U=u(l),j(D.$$.fragment,l),Bl=u(l),j(F.$$.fragment,l),Dl=u(l),k.l(l),Zl=u(l),$.l(l),Vl=u(l),x=J(l,"P",{"data-svelte-h":!0}),w(x)!=="svelte-z82oxz"&&(x.textContent=gs),Fl=u(l),S=J(l,"UL",{"data-svelte-h":!0}),w(S)!=="svelte-lywb9a"&&(S.innerHTML=Es),xl=u(l),H=J(l,"P",{"data-svelte-h":!0}),w(H)!=="svelte-1svmy83"&&(H.textContent=zs),Sl=u(l),j(Y.$$.fragment,l),Hl=u(l),q=J(l,"P",{"data-svelte-h":!0}),w(q)!=="svelte-1p8m960"&&(q.textContent=Ws),Yl=u(l),I.l(l),gl=u(l),L=J(l,"P",{"data-svelte-h":!0}),w(L)!=="svelte-147bixk"&&(L.textContent=Gs),ql=u(l),K=J(l,"P",{"data-svelte-h":!0}),w(K)!=="svelte-16rjlml"&&(K.innerHTML=Rs),Ll=u(l),A.l(l),El=u(l),P=J(l,"P",{"data-svelte-h":!0}),w(P)!=="svelte-125erx6"&&(P.textContent=Xs),Kl=u(l),Z.l(l),zl=u(l),O=J(l,"P",{"data-svelte-h":!0}),w(O)!=="svelte-bqezwd"&&(O.textContent=vs),Pl=u(l),g.l(l),Wl=u(l),ll=J(l,"P",{"data-svelte-h":!0}),w(ll)!=="svelte-1fph75a"&&(ll.textContent=Bs),Ol=u(l),j(sl.$$.fragment,l),ls=u(l),el=J(l,"P",{"data-svelte-h":!0}),w(el)!=="svelte-1op94pm"&&(el.textContent=Ds),ss=u(l),j(R.$$.fragment,l),es=u(l),tl=J(l,"P",{"data-svelte-h":!0}),w(tl)!=="svelte-2oxj3v"&&(tl.textContent=Fs),ts=u(l),j(nl.$$.fragment,l),ns=u(l),al=J(l,"P",{"data-svelte-h":!0}),w(al)!=="svelte-1ui0yhg"&&(al.textContent=xs),as=u(l),j(pl.$$.fragment,l),ps=u(l),Ml=J(l,"P",{"data-svelte-h":!0}),w(Ml)!=="svelte-qm9ruv"&&(Ml.innerHTML=Ss),Ms=u(l),j(rl.$$.fragment,l),rs=u(l),il=J(l,"P",{"data-svelte-h":!0}),w(il)!=="svelte-1kpj2of"&&(il.innerHTML=Hs),is=u(l),z.l(l),Gl=u(l),ul=J(l,"P",{"data-svelte-h":!0}),w(ul)!=="svelte-gi3wua"&&(ul.textContent=Ys),us=u(l),cl=J(l,"P",{"data-svelte-h":!0}),w(cl)!=="svelte-1ce5nvp"&&(cl.textContent=qs),cs=u(l),j(ol.$$.fragment,l),os=u(l),ml=J(l,"P",{"data-svelte-h":!0}),w(ml)!=="svelte-1m1y3w2"&&(ml.textContent=Ls),ms=u(l),bl=J(l,"P",{"data-svelte-h":!0}),w(bl)!=="svelte-20zkaq"&&(bl.textContent=Ks),bs=u(l),G.l(l),Rl=u(l),jl=J(l,"P",{"data-svelte-h":!0}),w(jl)!=="svelte-1kfcc6k"&&(jl.textContent=Ps),js=u(l),yl=J(l,"P",{"data-svelte-h":!0}),w(yl)!=="svelte-757sp2"&&(yl.textContent=Os),ys=u(l),j(X.$$.fragment,l),fs=u(l),j(fl.$$.fragment,l),Us=u(l),Ul=J(l,"P",{"data-svelte-h":!0}),w(Ul)!=="svelte-1mpj738"&&(Ul.textContent=le),ds=u(l),dl=J(l,"UL",{"data-svelte-h":!0}),w(dl)!=="svelte-6suogo"&&(dl.innerHTML=se),Js=u(l),Jl=J(l,"P",{"data-svelte-h":!0}),w(Jl)!=="svelte-1r0yd7r"&&(Jl.innerHTML=ee),ws=u(l),wl=J(l,"P",{"data-svelte-h":!0}),w(wl)!=="svelte-23a2z6"&&(wl.innerHTML=te),hs=u(l),j(hl.$$.fragment,l),Ts=u(l),j(Tl.$$.fragment,l),ks=u(l),Xl=J(l,"P",{}),Te(Xl).forEach(p),this.h()},h(){ke(e,"name","hf:doc:metadata"),ke(e,"content",tt)},m(l,n){ge(document.head,e),M(l,a,n),M(l,s,n),M(l,r,n),y(t,l,n),M(l,U,n),y(D,l,n),M(l,Bl,n),y(F,l,n),M(l,Dl,n),kl[T].m(l,n),M(l,Zl,n),_l[_].m(l,n),M(l,Vl,n),M(l,x,n),M(l,Fl,n),M(l,S,n),M(l,xl,n),M(l,H,n),M(l,Sl,n),y(Y,l,n),M(l,Hl,n),M(l,q,n),M(l,Yl,n),$l[C].m(l,n),M(l,gl,n),M(l,L,n),M(l,ql,n),M(l,K,n),M(l,Ll,n),Cl[N].m(l,n),M(l,El,n),M(l,P,n),M(l,Kl,n),Il[Q].m(l,n),M(l,zl,n),M(l,O,n),M(l,Pl,n),Nl[V].m(l,n),M(l,Wl,n),M(l,ll,n),M(l,Ol,n),y(sl,l,n),M(l,ls,n),M(l,el,n),M(l,ss,n),y(R,l,n),M(l,es,n),M(l,tl,n),M(l,ts,n),y(nl,l,n),M(l,ns,n),M(l,al,n),M(l,as,n),y(pl,l,n),M(l,ps,n),M(l,Ml,n),M(l,Ms,n),y(rl,l,n),M(l,rs,n),M(l,il,n),M(l,is,n),Al[E].m(l,n),M(l,Gl,n),M(l,ul,n),M(l,us,n),M(l,cl,n),M(l,cs,n),y(ol,l,n),M(l,os,n),M(l,ml,n),M(l,ms,n),M(l,bl,n),M(l,bs,n),Ql[W].m(l,n),M(l,Rl,n),M(l,jl,n),M(l,js,n),M(l,yl,n),M(l,ys,n),y(X,l,n),M(l,fs,n),y(fl,l,n),M(l,Us,n),M(l,Ul,n),M(l,ds,n),M(l,dl,n),M(l,Js,n),M(l,Jl,n),M(l,ws,n),M(l,wl,n),M(l,hs,n),y(hl,l,n),M(l,Ts,n),y(Tl,l,n),M(l,ks,n),M(l,Xl,n),_s=!0},p(l,[n]){const Je={};n&1&&(Je.fw=l[0]),t.$set(Je);let $s=T;T=ae(l),T!==$s&&(B(),c(kl[$s],1,1,()=>{kl[$s]=null}),v(),k=kl[T],k||(k=kl[T]=ne[T](l),k.c()),o(k,1),k.m(Zl.parentNode,Zl));let Cs=_;_=Me(l),_!==Cs&&(B(),c(_l[Cs],1,1,()=>{_l[Cs]=null}),v(),$=_l[_],$||($=_l[_]=pe[_](l),$.c()),o($,1),$.m(Vl.parentNode,Vl));let Is=C;C=ie(l),C!==Is&&(B(),c($l[Is],1,1,()=>{$l[Is]=null}),v(),I=$l[C],I||(I=$l[C]=re[C](l),I.c()),o(I,1),I.m(gl.parentNode,gl));let Ns=N;N=ce(l),N!==Ns&&(B(),c(Cl[Ns],1,1,()=>{Cl[Ns]=null}),v(),A=Cl[N],A||(A=Cl[N]=ue[N](l),A.c()),o(A,1),A.m(El.parentNode,El));let As=Q;Q=me(l),Q!==As&&(B(),c(Il[As],1,1,()=>{Il[As]=null}),v(),Z=Il[Q],Z||(Z=Il[Q]=oe[Q](l),Z.c()),o(Z,1),Z.m(zl.parentNode,zl));let Qs=V;V=je(l),V!==Qs&&(B(),c(Nl[Qs],1,1,()=>{Nl[Qs]=null}),v(),g=Nl[V],g||(g=Nl[V]=be[V](l),g.c()),o(g,1),g.m(Wl.parentNode,Wl));const we={};n&2&&(we.$$scope={dirty:n,ctx:l}),R.$set(we);let Zs=E;E=fe(l),E!==Zs&&(B(),c(Al[Zs],1,1,()=>{Al[Zs]=null}),v(),z=Al[E],z||(z=Al[E]=ye[E](l),z.c()),o(z,1),z.m(Gl.parentNode,Gl));let Vs=W;W=de(l),W!==Vs&&(B(),c(Ql[Vs],1,1,()=>{Ql[Vs]=null}),v(),G=Ql[W],G||(G=Ql[W]=Ue[W](l),G.c()),o(G,1),G.m(Rl.parentNode,Rl));const he={};n&2&&(he.$$scope={dirty:n,ctx:l}),X.$set(he)},i(l){_s||(o(t.$$.fragment,l),o(D.$$.fragment,l),o(F.$$.fragment,l),o(k),o($),o(Y.$$.fragment,l),o(I),o(A),o(Z),o(g),o(sl.$$.fragment,l),o(R.$$.fragment,l),o(nl.$$.fragment,l),o(pl.$$.fragment,l),o(rl.$$.fragment,l),o(z),o(ol.$$.fragment,l),o(G),o(X.$$.fragment,l),o(fl.$$.fragment,l),o(hl.$$.fragment,l),o(Tl.$$.fragment,l),_s=!0)},o(l){c(t.$$.fragment,l),c(D.$$.fragment,l),c(F.$$.fragment,l),c(k),c($),c(Y.$$.fragment,l),c(I),c(A),c(Z),c(g),c(sl.$$.fragment,l),c(R.$$.fragment,l),c(nl.$$.fragment,l),c(pl.$$.fragment,l),c(rl.$$.fragment,l),c(z),c(ol.$$.fragment,l),c(G),c(X.$$.fragment,l),c(fl.$$.fragment,l),c(hl.$$.fragment,l),c(Tl.$$.fragment,l),_s=!1},d(l){l&&(p(a),p(s),p(r),p(U),p(Bl),p(Dl),p(Zl),p(Vl),p(x),p(Fl),p(S),p(xl),p(H),p(Sl),p(Hl),p(q),p(Yl),p(gl),p(L),p(ql),p(K),p(Ll),p(El),p(P),p(Kl),p(zl),p(O),p(Pl),p(Wl),p(ll),p(Ol),p(ls),p(el),p(ss),p(es),p(tl),p(ts),p(ns),p(al),p(as),p(ps),p(Ml),p(Ms),p(rs),p(il),p(is),p(Gl),p(ul),p(us),p(cl),p(cs),p(os),p(ml),p(ms),p(bl),p(bs),p(Rl),p(jl),p(js),p(yl),p(ys),p(fs),p(Us),p(Ul),p(ds),p(dl),p(Js),p(Jl),p(ws),p(wl),p(hs),p(Ts),p(ks),p(Xl)),p(e),f(t,l),f(D,l),f(F,l),kl[T].d(l),_l[_].d(l),f(Y,l),$l[C].d(l),Cl[N].d(l),Il[Q].d(l),Nl[V].d(l),f(sl,l),f(R,l),f(nl,l),f(pl,l),f(rl,l),Al[E].d(l),f(ol,l),Ql[W].d(l),f(X,l),f(fl,l),f(hl,l),f(Tl,l)}}}const tt='{"title":"处理多个序列","local":"处理多个序列","sections":[{"title":"模型需要一批输入","local":"模型需要一批输入","sections":[],"depth":2},{"title":"填充输入（Padding）","local":"填充输入（Padding）","sections":[],"depth":2},{"title":"注意力掩码（attention mask）层","local":"注意力掩码（attention mask）层","sections":[],"depth":2},{"title":"更长的句子","local":"更长的句子","sections":[],"depth":2}],"depth":1}';function nt(m,e,a){let s="pt";return Ae(()=>{const r=new URLSearchParams(window.location.search);a(0,s=r.get("fw")||"pt")}),[s]}class mt extends Qe{constructor(e){super(),Ze(this,e,nt,et,Ne,{})}}export{mt as component};
