import{s as Ce,o as Ie,n as ke}from"../chunks/scheduler.37c15a92.js";import{S as Ne,i as Ae,g as d,s as i,r as b,A as Qe,h as J,f as p,c,j as we,u as j,x as w,k as he,y as Ze,a as M,v as y,t as u,b as B,d as o,w as f,p as v}from"../chunks/index.2bf4358c.js";import{T as Te}from"../chunks/Tip.363c041f.js";import{Y as _e}from"../chunks/Youtube.1e50a667.js";import{C as h}from"../chunks/CodeBlock.4e987730.js";import{C as $e}from"../chunks/CourseFloatingBanner.9ff4c771.js";import{F as Ve}from"../chunks/FrameworkSwitchCourse.8d4d4ab6.js";import{H as Xl,E as ge}from"../chunks/index.b8688d36.js";function Ee(m){let e,a;return e=new $e({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){u(e.$$.fragment,s),a=!1},d(s){f(e,s)}}}function We(m){let e,a;return e=new $e({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){u(e.$$.fragment,s),a=!1},d(s){f(e,s)}}}function ze(m){let e,a;return e=new _e({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){u(e.$$.fragment,s),a=!1},d(s){f(e,s)}}}function Ge(m){let e,a;return e=new _e({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){u(e.$$.fragment,s),a=!1},d(s){f(e,s)}}}function Re(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=c(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){u(e.$$.fragment,t),u(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:"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=c(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){u(e.$$.fragment,t),u(s.$$.fragment,t),r=!1},d(t){t&&p(a),f(e,t),f(s,t)}}}function Be(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=c(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){u(e.$$.fragment,t),u(s.$$.fragment,t),r=!1},d(t){t&&p(a),f(e,t),f(s,t)}}}function ve(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=c(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){u(e.$$.fragment,t),u(s.$$.fragment,t),r=!1},d(t){t&&p(a),f(e,t),f(s,t)}}}function De(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){u(e.$$.fragment,s),a=!1},d(s){f(e,s)}}}function Fe(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){u(e.$$.fragment,s),a=!1},d(s){f(e,s)}}}function xe(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){u(e.$$.fragment,s),a=!1},d(s){f(e,s)}}}function Se(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){u(e.$$.fragment,s),a=!1},d(s){f(e,s)}}}function He(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:ke,d(s){s&&p(e)}}}function Ye(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=c(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){u(e.$$.fragment,t),u(s.$$.fragment,t),r=!1},d(t){t&&p(a),f(e,t),f(s,t)}}}function qe(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=c(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){u(e.$$.fragment,t),u(s.$$.fragment,t),r=!1},d(t){t&&p(a),f(e,t),f(s,t)}}}function Le(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=c(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){u(e.$$.fragment,t),u(s.$$.fragment,t),r=!1},d(t){t&&p(a),f(e,t),f(s,t)}}}function Ke(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=c(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){u(e.$$.fragment,t),u(s.$$.fragment,t),r=!1},d(t){t&&p(a),f(e,t),f(s,t)}}}function Pe(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:ke,d(s){s&&p(e)}}}function Oe(m){let e,a,s,r,t,U,D,Bl,T,k,Ql,_,$,Zl,F,Zs="在上一节中，我们探讨了最简单的案例：对一个较短的句子进行推理。然而，一些问题已经出现：",vl,x,Vs="<li><p>我们如何处理多个句子？</p></li> <li><p>我们如何处理不同长度的多个句子？</p></li> <li><p>词汇索引是唯一可以让模型运行的输入吗？</p></li> <li><p>是否存在句子太长的问题？</p></li>",Dl,S,gs="让我们看看这些问题会带来什么样的问题，以及如何使用🤗 Transformers API 解决它们",Fl,H,xl,Y,Es="在上一个练习中，你看到了句子如何转换为数字列表。让我们将此数字列表转换为张量，并将其发送到模型：",Sl,C,I,Vl,q,Ws="哦，不！为什么失败了？“我们的确是按照第 2 节中管道的步骤一步步来做的。",Hl,L,zs="问题是我们向模型发送了一个单独的句子，而🤗 Transformers 模型默认情况下需要一个句子列表。在这里，当我们试图重现 tokenizer 在输入 <code>sequence</code> 后在其内部进行的所有操作。但如果你仔细观察，你会发现 tokenizer 不仅仅是将 inputs ID 的列表转换为张量，它还在其上添加了一个维度：",Yl,N,A,gl,K,Gs="让我们再试一次并添加一个新的维度：",ql,Q,Z,El,P,Rs="让我们打印 inputs ID 以及生成的 logits 值，以下是输出：",Ll,V,g,Wl,O,Xs="批处理（Batching）是一次性通过模型发送多个句子的行为。如果你只有一句话，你可以构建一个只有一个句子的 batch：",Kl,ll,Pl,sl,Bs="这就是一个包含两个相同句子的 batch",Ol,R,ls,el,vs="批处理支持模型在输入多个句子时工作。使用多个句子就像使用单个句子构建批一样简单。不过，还有第二个问题。当你试图将两个（或更多）句子组合在一起时，它们的长度可能不同。如果你以前使用过张量，那么你知道它们必须是矩形，因此无法将 inputs ID 列表直接转换为张量。为了解决这个问题，我们通常填充输入（Padding）。",ss,tl,es,nl,Ds="以下列表不能转换为张量：",ts,al,ns,pl,Fs="为了解决这个问题，我们将使用填充使张量成为标准的矩形。Padding 通过在值较少的句子中添加一个名为 <code>padding_id</code> 的特殊单词来确保我们所有的句子长度相同。例如，如果你有 10 个包含 10 个单词的句子和 1 个包含 20 个单词的句子，填充能确保所有句子都包含 20 个单词。在我们的示例中，填充后的张量如下所示：",as,Ml,ps,rl,xs="我们可以在 <code>tokenizer.pad_token_id</code> 中找到填充 token 的 ID。让我们使用它，分别用模型处理这两个句子，然后再尝试将这两个句子放在一起用模型批处理：",Ms,E,W,zl,il,Ss="咦，我们批处理预测中的 logits 值有点问题：第二行应该与第二句的 logits 相同，但我们得到了完全不同的值！",rs,cl,Hs="这是因为 Transformer 模型的关键特性：注意力层，它考虑了每个 token 的上下文信息。这具体来说，每个 token 的含义并非单独存在的，它的含义还取决于它在句子中的位置以及周围的其他 tokens。当我们使用填充（padding）来处理长度不同的句子时，我们会添加特殊的“填充 token”来使所有句子达到相同的长度。但是，注意力层会将这些填充 token 也纳入考虑，因为它们会关注序列中的所有 tokens。这就导致了一个问题：尽管填充 token 本身并没有实际的含义，但它们的存在会影响模型对句子的理解。我们需要告诉这些注意层忽略填充 token。这是通过使用注意力掩码（attention mask）层来实现的。",is,ul,cs,ol,Ys="注意力掩码（attention mask）是与 inputs ID 张量形状完全相同的张量，用 0 和 1 填充：1 表示应关注相应的 tokens，0 表示应忽略相应的 tokens（即，它们应被模型的注意力层忽视）。",us,ml,qs="让我们用 attention mask 修改上一个示例：",os,z,G,Gl,bl,Ls="现在我们得到了批处理中第二句话相同的 logits 值。",ms,jl,Ks="注意第二序列的最后一个值是填充 ID，其在注意力掩码中的值为 0。",bs,X,js,yl,ys,fl,Ps="对于 Transformers 模型，我们可以通过模型的序列长度是有限的。大多数模型处理多达 512 或 1024 个 的 tokens 序列，当使用模型处理更长的序列时，会崩溃。此问题有两种解决方案：",fs,Ul,Os="<li>使用支持更长序列长度的模型。</li> <li>截断你的序列。</li>",Us,dl,le='不同的模型支持的序列长度各不相同，有些模型专门用于处理非常长的序列。例如 <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> 。如果你正在处理需要非常长序列的任务，我们建议你考虑使用这些模型。',ds,Jl,se="另外，我们建议你通过设定 <code>max_sequence_length</code> 参数来截断序列：",Js,wl,ws,hl,hs,Rl,Ts;t=new Ve({props:{fw:m[0]}}),D=new Xl({props:{title:"处理多个序列",local:"处理多个序列",headingTag:"h1"}});const ee=[We,Ee],Tl=[];function te(l,n){return l[0]==="pt"?0:1}T=te(m),k=Tl[T]=ee[T](m);const ne=[Ge,ze],kl=[];function ae(l,n){return l[0]==="pt"?0:1}_=ae(m),$=kl[_]=ne[_](m),H=new Xl({props:{title:"模型需要一批输入",local:"模型需要一批输入",headingTag:"h2"}});const pe=[Xe,Re],_l=[];function Me(l,n){return l[0]==="pt"?0:1}C=Me(m),I=_l[C]=pe[C](m);const re=[ve,Be],$l=[];function ie(l,n){return l[0]==="pt"?0:1}N=ie(m),A=$l[N]=re[N](m);const ce=[Fe,De],Cl=[];function ue(l,n){return l[0]==="pt"?0:1}Q=ue(m),Z=Cl[Q]=ce[Q](m);const oe=[Se,xe],Il=[];function me(l,n){return l[0]==="pt"?0:1}V=me(m),g=Il[V]=oe[V](m),ll=new h({props:{code:"YmF0Y2hlZF9pZHMlMjAlM0QlMjAlNUJpZHMlMkMlMjBpZHMlNUQ=",highlighted:'<span class="hljs-attr">batched_ids</span> = [ids, ids]',wrap:!1}}),R=new Te({props:{$$slots:{default:[He]},$$scope:{ctx:m}}}),tl=new Xl({props:{title:"填充输入（Padding）",local:"填充输入（Padding）",headingTag:"h2"}}),al=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}}),Ml=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 be=[qe,Ye],Nl=[];function je(l,n){return l[0]==="pt"?0:1}E=je(m),W=Nl[E]=be[E](m),ul=new Xl({props:{title:"注意力掩码（attention mask）层",local:"注意力掩码（attention mask）层",headingTag:"h2"}});const ye=[Ke,Le],Al=[];function fe(l,n){return l[0]==="pt"?0:1}return z=fe(m),G=Al[z]=ye[z](m),X=new Te({props:{$$slots:{default:[Pe]},$$scope:{ctx:m}}}),yl=new Xl({props:{title:"更长的句子",local:"更长的句子",headingTag:"h2"}}),wl=new h({props:{code:"c2VxdWVuY2UlMjAlM0QlMjBzZXF1ZW5jZSU1QiUzQW1heF9zZXF1ZW5jZV9sZW5ndGglNUQ=",highlighted:"sequence = sequence[:max_sequence_length]",wrap:!1}}),hl=new ge({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(),k.c(),Ql=i(),$.c(),Zl=i(),F=d("p"),F.textContent=Zs,vl=i(),x=d("ul"),x.innerHTML=Vs,Dl=i(),S=d("p"),S.textContent=gs,Fl=i(),b(H.$$.fragment),xl=i(),Y=d("p"),Y.textContent=Es,Sl=i(),I.c(),Vl=i(),q=d("p"),q.textContent=Ws,Hl=i(),L=d("p"),L.innerHTML=zs,Yl=i(),A.c(),gl=i(),K=d("p"),K.textContent=Gs,ql=i(),Z.c(),El=i(),P=d("p"),P.textContent=Rs,Ll=i(),g.c(),Wl=i(),O=d("p"),O.textContent=Xs,Kl=i(),b(ll.$$.fragment),Pl=i(),sl=d("p"),sl.textContent=Bs,Ol=i(),b(R.$$.fragment),ls=i(),el=d("p"),el.textContent=vs,ss=i(),b(tl.$$.fragment),es=i(),nl=d("p"),nl.textContent=Ds,ts=i(),b(al.$$.fragment),ns=i(),pl=d("p"),pl.innerHTML=Fs,as=i(),b(Ml.$$.fragment),ps=i(),rl=d("p"),rl.innerHTML=xs,Ms=i(),W.c(),zl=i(),il=d("p"),il.textContent=Ss,rs=i(),cl=d("p"),cl.textContent=Hs,is=i(),b(ul.$$.fragment),cs=i(),ol=d("p"),ol.textContent=Ys,us=i(),ml=d("p"),ml.textContent=qs,os=i(),G.c(),Gl=i(),bl=d("p"),bl.textContent=Ls,ms=i(),jl=d("p"),jl.textContent=Ks,bs=i(),b(X.$$.fragment),js=i(),b(yl.$$.fragment),ys=i(),fl=d("p"),fl.textContent=Ps,fs=i(),Ul=d("ul"),Ul.innerHTML=Os,Us=i(),dl=d("p"),dl.innerHTML=le,ds=i(),Jl=d("p"),Jl.innerHTML=se,Js=i(),b(wl.$$.fragment),ws=i(),b(hl.$$.fragment),hs=i(),Rl=d("p"),this.h()},l(l){const n=Qe("svelte-u9bgzb",document.head);e=J(n,"META",{name:!0,content:!0}),n.forEach(p),a=c(l),s=J(l,"P",{}),we(s).forEach(p),r=c(l),j(t.$$.fragment,l),U=c(l),j(D.$$.fragment,l),Bl=c(l),k.l(l),Ql=c(l),$.l(l),Zl=c(l),F=J(l,"P",{"data-svelte-h":!0}),w(F)!=="svelte-z82oxz"&&(F.textContent=Zs),vl=c(l),x=J(l,"UL",{"data-svelte-h":!0}),w(x)!=="svelte-lywb9a"&&(x.innerHTML=Vs),Dl=c(l),S=J(l,"P",{"data-svelte-h":!0}),w(S)!=="svelte-1svmy83"&&(S.textContent=gs),Fl=c(l),j(H.$$.fragment,l),xl=c(l),Y=J(l,"P",{"data-svelte-h":!0}),w(Y)!=="svelte-1p8m960"&&(Y.textContent=Es),Sl=c(l),I.l(l),Vl=c(l),q=J(l,"P",{"data-svelte-h":!0}),w(q)!=="svelte-147bixk"&&(q.textContent=Ws),Hl=c(l),L=J(l,"P",{"data-svelte-h":!0}),w(L)!=="svelte-16rjlml"&&(L.innerHTML=zs),Yl=c(l),A.l(l),gl=c(l),K=J(l,"P",{"data-svelte-h":!0}),w(K)!=="svelte-125erx6"&&(K.textContent=Gs),ql=c(l),Z.l(l),El=c(l),P=J(l,"P",{"data-svelte-h":!0}),w(P)!=="svelte-bqezwd"&&(P.textContent=Rs),Ll=c(l),g.l(l),Wl=c(l),O=J(l,"P",{"data-svelte-h":!0}),w(O)!=="svelte-1fph75a"&&(O.textContent=Xs),Kl=c(l),j(ll.$$.fragment,l),Pl=c(l),sl=J(l,"P",{"data-svelte-h":!0}),w(sl)!=="svelte-1op94pm"&&(sl.textContent=Bs),Ol=c(l),j(R.$$.fragment,l),ls=c(l),el=J(l,"P",{"data-svelte-h":!0}),w(el)!=="svelte-2oxj3v"&&(el.textContent=vs),ss=c(l),j(tl.$$.fragment,l),es=c(l),nl=J(l,"P",{"data-svelte-h":!0}),w(nl)!=="svelte-1ui0yhg"&&(nl.textContent=Ds),ts=c(l),j(al.$$.fragment,l),ns=c(l),pl=J(l,"P",{"data-svelte-h":!0}),w(pl)!=="svelte-qm9ruv"&&(pl.innerHTML=Fs),as=c(l),j(Ml.$$.fragment,l),ps=c(l),rl=J(l,"P",{"data-svelte-h":!0}),w(rl)!=="svelte-1kpj2of"&&(rl.innerHTML=xs),Ms=c(l),W.l(l),zl=c(l),il=J(l,"P",{"data-svelte-h":!0}),w(il)!=="svelte-gi3wua"&&(il.textContent=Ss),rs=c(l),cl=J(l,"P",{"data-svelte-h":!0}),w(cl)!=="svelte-1ce5nvp"&&(cl.textContent=Hs),is=c(l),j(ul.$$.fragment,l),cs=c(l),ol=J(l,"P",{"data-svelte-h":!0}),w(ol)!=="svelte-1m1y3w2"&&(ol.textContent=Ys),us=c(l),ml=J(l,"P",{"data-svelte-h":!0}),w(ml)!=="svelte-20zkaq"&&(ml.textContent=qs),os=c(l),G.l(l),Gl=c(l),bl=J(l,"P",{"data-svelte-h":!0}),w(bl)!=="svelte-1kfcc6k"&&(bl.textContent=Ls),ms=c(l),jl=J(l,"P",{"data-svelte-h":!0}),w(jl)!=="svelte-757sp2"&&(jl.textContent=Ks),bs=c(l),j(X.$$.fragment,l),js=c(l),j(yl.$$.fragment,l),ys=c(l),fl=J(l,"P",{"data-svelte-h":!0}),w(fl)!=="svelte-1mpj738"&&(fl.textContent=Ps),fs=c(l),Ul=J(l,"UL",{"data-svelte-h":!0}),w(Ul)!=="svelte-6suogo"&&(Ul.innerHTML=Os),Us=c(l),dl=J(l,"P",{"data-svelte-h":!0}),w(dl)!=="svelte-1r0yd7r"&&(dl.innerHTML=le),ds=c(l),Jl=J(l,"P",{"data-svelte-h":!0}),w(Jl)!=="svelte-23a2z6"&&(Jl.innerHTML=se),Js=c(l),j(wl.$$.fragment,l),ws=c(l),j(hl.$$.fragment,l),hs=c(l),Rl=J(l,"P",{}),we(Rl).forEach(p),this.h()},h(){he(e,"name","hf:doc:metadata"),he(e,"content",lt)},m(l,n){Ze(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),Tl[T].m(l,n),M(l,Ql,n),kl[_].m(l,n),M(l,Zl,n),M(l,F,n),M(l,vl,n),M(l,x,n),M(l,Dl,n),M(l,S,n),M(l,Fl,n),y(H,l,n),M(l,xl,n),M(l,Y,n),M(l,Sl,n),_l[C].m(l,n),M(l,Vl,n),M(l,q,n),M(l,Hl,n),M(l,L,n),M(l,Yl,n),$l[N].m(l,n),M(l,gl,n),M(l,K,n),M(l,ql,n),Cl[Q].m(l,n),M(l,El,n),M(l,P,n),M(l,Ll,n),Il[V].m(l,n),M(l,Wl,n),M(l,O,n),M(l,Kl,n),y(ll,l,n),M(l,Pl,n),M(l,sl,n),M(l,Ol,n),y(R,l,n),M(l,ls,n),M(l,el,n),M(l,ss,n),y(tl,l,n),M(l,es,n),M(l,nl,n),M(l,ts,n),y(al,l,n),M(l,ns,n),M(l,pl,n),M(l,as,n),y(Ml,l,n),M(l,ps,n),M(l,rl,n),M(l,Ms,n),Nl[E].m(l,n),M(l,zl,n),M(l,il,n),M(l,rs,n),M(l,cl,n),M(l,is,n),y(ul,l,n),M(l,cs,n),M(l,ol,n),M(l,us,n),M(l,ml,n),M(l,os,n),Al[z].m(l,n),M(l,Gl,n),M(l,bl,n),M(l,ms,n),M(l,jl,n),M(l,bs,n),y(X,l,n),M(l,js,n),y(yl,l,n),M(l,ys,n),M(l,fl,n),M(l,fs,n),M(l,Ul,n),M(l,Us,n),M(l,dl,n),M(l,ds,n),M(l,Jl,n),M(l,Js,n),y(wl,l,n),M(l,ws,n),y(hl,l,n),M(l,hs,n),M(l,Rl,n),Ts=!0},p(l,[n]){const Ue={};n&1&&(Ue.fw=l[0]),t.$set(Ue);let ks=T;T=te(l),T!==ks&&(v(),u(Tl[ks],1,1,()=>{Tl[ks]=null}),B(),k=Tl[T],k||(k=Tl[T]=ee[T](l),k.c()),o(k,1),k.m(Ql.parentNode,Ql));let _s=_;_=ae(l),_!==_s&&(v(),u(kl[_s],1,1,()=>{kl[_s]=null}),B(),$=kl[_],$||($=kl[_]=ne[_](l),$.c()),o($,1),$.m(Zl.parentNode,Zl));let $s=C;C=Me(l),C!==$s&&(v(),u(_l[$s],1,1,()=>{_l[$s]=null}),B(),I=_l[C],I||(I=_l[C]=pe[C](l),I.c()),o(I,1),I.m(Vl.parentNode,Vl));let Cs=N;N=ie(l),N!==Cs&&(v(),u($l[Cs],1,1,()=>{$l[Cs]=null}),B(),A=$l[N],A||(A=$l[N]=re[N](l),A.c()),o(A,1),A.m(gl.parentNode,gl));let Is=Q;Q=ue(l),Q!==Is&&(v(),u(Cl[Is],1,1,()=>{Cl[Is]=null}),B(),Z=Cl[Q],Z||(Z=Cl[Q]=ce[Q](l),Z.c()),o(Z,1),Z.m(El.parentNode,El));let Ns=V;V=me(l),V!==Ns&&(v(),u(Il[Ns],1,1,()=>{Il[Ns]=null}),B(),g=Il[V],g||(g=Il[V]=oe[V](l),g.c()),o(g,1),g.m(Wl.parentNode,Wl));const de={};n&2&&(de.$$scope={dirty:n,ctx:l}),R.$set(de);let As=E;E=je(l),E!==As&&(v(),u(Nl[As],1,1,()=>{Nl[As]=null}),B(),W=Nl[E],W||(W=Nl[E]=be[E](l),W.c()),o(W,1),W.m(zl.parentNode,zl));let Qs=z;z=fe(l),z!==Qs&&(v(),u(Al[Qs],1,1,()=>{Al[Qs]=null}),B(),G=Al[z],G||(G=Al[z]=ye[z](l),G.c()),o(G,1),G.m(Gl.parentNode,Gl));const Je={};n&2&&(Je.$$scope={dirty:n,ctx:l}),X.$set(Je)},i(l){Ts||(o(t.$$.fragment,l),o(D.$$.fragment,l),o(k),o($),o(H.$$.fragment,l),o(I),o(A),o(Z),o(g),o(ll.$$.fragment,l),o(R.$$.fragment,l),o(tl.$$.fragment,l),o(al.$$.fragment,l),o(Ml.$$.fragment,l),o(W),o(ul.$$.fragment,l),o(G),o(X.$$.fragment,l),o(yl.$$.fragment,l),o(wl.$$.fragment,l),o(hl.$$.fragment,l),Ts=!0)},o(l){u(t.$$.fragment,l),u(D.$$.fragment,l),u(k),u($),u(H.$$.fragment,l),u(I),u(A),u(Z),u(g),u(ll.$$.fragment,l),u(R.$$.fragment,l),u(tl.$$.fragment,l),u(al.$$.fragment,l),u(Ml.$$.fragment,l),u(W),u(ul.$$.fragment,l),u(G),u(X.$$.fragment,l),u(yl.$$.fragment,l),u(wl.$$.fragment,l),u(hl.$$.fragment,l),Ts=!1},d(l){l&&(p(a),p(s),p(r),p(U),p(Bl),p(Ql),p(Zl),p(F),p(vl),p(x),p(Dl),p(S),p(Fl),p(xl),p(Y),p(Sl),p(Vl),p(q),p(Hl),p(L),p(Yl),p(gl),p(K),p(ql),p(El),p(P),p(Ll),p(Wl),p(O),p(Kl),p(Pl),p(sl),p(Ol),p(ls),p(el),p(ss),p(es),p(nl),p(ts),p(ns),p(pl),p(as),p(ps),p(rl),p(Ms),p(zl),p(il),p(rs),p(cl),p(is),p(cs),p(ol),p(us),p(ml),p(os),p(Gl),p(bl),p(ms),p(jl),p(bs),p(js),p(ys),p(fl),p(fs),p(Ul),p(Us),p(dl),p(ds),p(Jl),p(Js),p(ws),p(hs),p(Rl)),p(e),f(t,l),f(D,l),Tl[T].d(l),kl[_].d(l),f(H,l),_l[C].d(l),$l[N].d(l),Cl[Q].d(l),Il[V].d(l),f(ll,l),f(R,l),f(tl,l),f(al,l),f(Ml,l),Nl[E].d(l),f(ul,l),Al[z].d(l),f(X,l),f(yl,l),f(wl,l),f(hl,l)}}}const lt='{"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 st(m,e,a){let s="pt";return Ie(()=>{const r=new URLSearchParams(window.location.search);a(0,s=r.get("fw")||"pt")}),[s]}class ct extends Ne{constructor(e){super(),Ae(this,e,st,Oe,Ce,{})}}export{ct as component};
