import{S as No,i as Ho,s as Io,e as n,k as c,t as a,w as m,M as Bo,c as p,d as s,m as d,a as i,h as l,x as u,b as g,G as t,g as o,y as f,q as _,o as h,B as $,v as Ro}from"../../chunks/vendor-hf-doc-builder.js";import{T as Lo}from"../../chunks/Tip-hf-doc-builder.js";import{Y as Go}from"../../chunks/Youtube-hf-doc-builder.js";import{I as Jo}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as v}from"../../chunks/CodeBlock-hf-doc-builder.js";import{C as Yo}from"../../chunks/CourseFloatingBanner-hf-doc-builder.js";import"../../chunks/DocNotebookDropdown-hf-doc-builder.js";function Qo(We){let b,q,z,le,I;return{c(){b=n("p"),q=a("\u270F\uFE0F "),z=n("strong"),le=a("\u8BD5\u8BD5\u770B\uFF01"),I=a(" \u4FEE\u6539\u4E4B\u524D\u7684\u8BAD\u7EC3\u5FAA\u73AF\u4EE5\u5728 SST-2 \u6570\u636E\u96C6\u4E0A\u5FAE\u8C03\u60A8\u7684\u6A21\u578B\u3002")},l(A){b=p(A,"P",{});var x=i(b);q=l(x,"\u270F\uFE0F "),z=p(x,"STRONG",{});var U=i(z);le=l(U,"\u8BD5\u8BD5\u770B\uFF01"),U.forEach(s),I=l(x," \u4FEE\u6539\u4E4B\u524D\u7684\u8BAD\u7EC3\u5FAA\u73AF\u4EE5\u5728 SST-2 \u6570\u636E\u96C6\u4E0A\u5FAE\u8C03\u60A8\u7684\u6A21\u578B\u3002"),x.forEach(s)},m(A,x){o(A,b,x),t(b,q),t(b,z),t(z,le),t(b,I)},d(A){A&&s(b)}}}function Ko(We){let b;return{c(){b=a("\u26A0\uFE0F \u4E3A\u4E86\u4F7F\u4E91\u7AEF TPU \u63D0\u4F9B\u7684\u52A0\u901F\u53D1\u6325\u6700\u5927\u7684\u6548\u76CA\uFF0C\u6211\u4EEC\u5EFA\u8BAE\u4F7F\u7528\u6807\u8BB0\u5668(tokenizer)\u7684 `padding=max_length` \u548C `max_length` \u53C2\u6570\u5C06\u60A8\u7684\u6837\u672C\u586B\u5145\u5230\u56FA\u5B9A\u957F\u5EA6\u3002")},l(q){b=l(q,"\u26A0\uFE0F \u4E3A\u4E86\u4F7F\u4E91\u7AEF TPU \u63D0\u4F9B\u7684\u52A0\u901F\u53D1\u6325\u6700\u5927\u7684\u6548\u76CA\uFF0C\u6211\u4EEC\u5EFA\u8BAE\u4F7F\u7528\u6807\u8BB0\u5668(tokenizer)\u7684 `padding=max_length` \u548C `max_length` \u53C2\u6570\u5C06\u60A8\u7684\u6837\u672C\u586B\u5145\u5230\u56FA\u5B9A\u957F\u5EA6\u3002")},m(q,z){o(q,b,z)},d(q){q&&s(b)}}}function Vo(We){let b,q,z,le,I,A,x,U,Jt,B,oa,Ze,na,pa,Yt,re,Qt,oe,ia,Kt,W,ca,et,da,ma,tt,ua,fa,Vt,M,N,_a,st,ha,$a,at,ba,va,ka,T,ja,lt,Ea,wa,rt,ga,za,ot,qa,ya,Ca,nt,Aa,Xt,R,Pa,pt,Da,Oa,Zt,ne,es,Me,Sa,ts,pe,ss,Fe,xa,as,ie,ls,Le,Ta,rs,ce,os,de,ns,F,Ua,it,Wa,Ma,ct,Fa,La,ps,Ge,Ga,is,me,cs,J,Na,dt,Ha,Ia,ds,ue,ms,fe,us,j,Ba,mt,Ra,Ja,ut,Ya,Qa,ft,Ka,Va,_t,Xa,Za,ht,el,tl,fs,E,sl,$t,al,ll,bt,rl,ol,vt,nl,pl,kt,il,cl,_e,dl,ml,_s,he,hs,L,ul,jt,fl,_l,Et,hl,$l,$s,$e,bs,be,vs,ve,bl,ks,P,vl,wt,kl,jl,gt,El,wl,zt,gl,zl,js,ke,Es,je,ws,Y,ql,qt,yl,Cl,gs,Ee,zs,Ne,Al,qs,we,Pl,ys,w,Dl,yt,Ol,Sl,Ct,xl,Tl,At,Ul,Wl,Pt,Ml,Fl,Dt,Ll,Gl,Cs,ge,As,ze,Ps,He,Nl,Ds,Q,Os,H,K,Ot,qe,Hl,St,Il,Ss,ye,xs,V,Bl,Ce,Rl,Jl,Ts,Ae,Us,Ie,Yl,Ws,Pe,Ms,y,Ql,xt,Kl,Vl,Tt,Xl,Zl,Ut,er,tr,Wt,sr,ar,Fs,k,lr,Mt,rr,or,Ft,nr,pr,Lt,ir,cr,Gt,dr,mr,Nt,ur,fr,Ht,_r,hr,Ls,X,Gs,Be,$r,Ns,De,Hs,Z,br,It,vr,kr,Is,Oe,Bs,Re,jr,Rs,Se,Js,Je,Er,Ys,ee,wr,Bt,gr,zr,Qs,xe,Ks,te,qr,Te,yr,Cr,Vs;return A=new Yo({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/section4.ipynb"},{label:"Aws Studio",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/zh-CN/chapter3/section4.ipynb"}]}}),U=new Go({props:{id:"Dh9CL8fyG80"}}),re=new v({props:{code:`from datasets import load_dataset
from transformers import AutoTokenizer, DataCollatorWithPadding

raw_datasets = load_dataset("glue", "mrpc")
checkpoint = "bert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)


def tokenize_function(example):
    return tokenizer(example["sentence1"], example["sentence2"], truncation=True)


tokenized_datasets = raw_datasets.map(tokenize_function, batched=True)
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)`,highlighted:`<span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, DataCollatorWithPadding

raw_datasets = load_dataset(<span class="hljs-string">&quot;glue&quot;</span>, <span class="hljs-string">&quot;mrpc&quot;</span>)
checkpoint = <span class="hljs-string">&quot;bert-base-uncased&quot;</span>
tokenizer = AutoTokenizer.from_pretrained(checkpoint)


<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>)


tokenized_datasets = raw_datasets.<span class="hljs-built_in">map</span>(tokenize_function, batched=<span class="hljs-literal">True</span>)
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)`}}),ne=new v({props:{code:`tokenized_datasets = tokenized_datasets.remove_columns(["sentence1", "sentence2", "idx"])
tokenized_datasets = tokenized_datasets.rename_column("label", "labels")
tokenized_datasets.set_format("torch")
tokenized_datasets["train"].column_names`,highlighted:`tokenized_datasets = tokenized_datasets.remove_columns([<span class="hljs-string">&quot;sentence1&quot;</span>, <span class="hljs-string">&quot;sentence2&quot;</span>, <span class="hljs-string">&quot;idx&quot;</span>])
tokenized_datasets = tokenized_datasets.rename_column(<span class="hljs-string">&quot;label&quot;</span>, <span class="hljs-string">&quot;labels&quot;</span>)
tokenized_datasets.set_format(<span class="hljs-string">&quot;torch&quot;</span>)
tokenized_datasets[<span class="hljs-string">&quot;train&quot;</span>].column_names`}}),pe=new v({props:{code:'["attention_mask", "input_ids", "labels", "token_type_ids"]',highlighted:'[<span class="hljs-string">&quot;attention_mask&quot;</span>, <span class="hljs-string">&quot;input_ids&quot;</span>, <span class="hljs-string">&quot;labels&quot;</span>, <span class="hljs-string">&quot;token_type_ids&quot;</span>]'}}),ie=new v({props:{code:`from torch.utils.data import DataLoader

train_dataloader = DataLoader(
    tokenized_datasets["train"], shuffle=True, batch_size=8, collate_fn=data_collator
)
eval_dataloader = DataLoader(
    tokenized_datasets["validation"], batch_size=8, collate_fn=data_collator
)`,highlighted:`<span class="hljs-keyword">from</span> torch.utils.data <span class="hljs-keyword">import</span> DataLoader

train_dataloader = DataLoader(
    tokenized_datasets[<span class="hljs-string">&quot;train&quot;</span>], shuffle=<span class="hljs-literal">True</span>, batch_size=<span class="hljs-number">8</span>, collate_fn=data_collator
)
eval_dataloader = DataLoader(
    tokenized_datasets[<span class="hljs-string">&quot;validation&quot;</span>], batch_size=<span class="hljs-number">8</span>, collate_fn=data_collator
)`}}),ce=new v({props:{code:`for batch in train_dataloader:
    break
{k: v.shape for k, v in batch.items()}`,highlighted:`<span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> train_dataloader:
    <span class="hljs-keyword">break</span>
{k: v.shape <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> batch.items()}`}}),de=new v({props:{code:`{'attention_mask': torch.Size([8, 65]),
 'input_ids': torch.Size([8, 65]),
 'labels': torch.Size([8]),
 'token_type_ids': torch.Size([8, 65])}`,highlighted:`{<span class="hljs-string">&#x27;attention_mask&#x27;</span>: torch.Size([<span class="hljs-number">8</span>, <span class="hljs-number">65</span>]),
 <span class="hljs-string">&#x27;input_ids&#x27;</span>: torch.Size([<span class="hljs-number">8</span>, <span class="hljs-number">65</span>]),
 <span class="hljs-string">&#x27;labels&#x27;</span>: torch.Size([<span class="hljs-number">8</span>]),
 <span class="hljs-string">&#x27;token_type_ids&#x27;</span>: torch.Size([<span class="hljs-number">8</span>, <span class="hljs-number">65</span>])}`}}),me=new v({props:{code:`from transformers import AutoModelForSequenceClassification

model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2)`,highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForSequenceClassification

model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=<span class="hljs-number">2</span>)`}}),ue=new v({props:{code:`outputs = model(**batch)
print(outputs.loss, outputs.logits.shape)`,highlighted:`outputs = model(**batch)
<span class="hljs-built_in">print</span>(outputs.loss, outputs.logits.shape)`}}),fe=new v({props:{code:"tensor(0.5441, grad_fn=<NllLossBackward>) torch.Size([8, 2])",highlighted:'tensor(<span class="hljs-number">0.5441</span>, grad_fn=&lt;NllLossBackward&gt;) torch.Size([<span class="hljs-number">8</span>, <span class="hljs-number">2</span>])'}}),he=new v({props:{code:`from transformers import AdamW

optimizer = AdamW(model.parameters(), lr=5e-5)`,highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AdamW

optimizer = AdamW(model.parameters(), lr=<span class="hljs-number">5e-5</span>)`}}),$e=new v({props:{code:`from transformers import get_scheduler

num_epochs = 3
num_training_steps = num_epochs * len(train_dataloader)
lr_scheduler = get_scheduler(
    "linear",
    optimizer=optimizer,
    num_warmup_steps=0,
    num_training_steps=num_training_steps,
)
print(num_training_steps)`,highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> get_scheduler

num_epochs = <span class="hljs-number">3</span>
num_training_steps = num_epochs * <span class="hljs-built_in">len</span>(train_dataloader)
lr_scheduler = get_scheduler(
    <span class="hljs-string">&quot;linear&quot;</span>,
    optimizer=optimizer,
    num_warmup_steps=<span class="hljs-number">0</span>,
    num_training_steps=num_training_steps,
)
<span class="hljs-built_in">print</span>(num_training_steps)`}}),be=new v({props:{code:"1377",highlighted:'<span class="hljs-number">1377</span>'}}),ke=new v({props:{code:`import torch

device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
model.to(device)
device`,highlighted:`<span class="hljs-keyword">import</span> torch

device = torch.device(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-keyword">if</span> torch.cuda.is_available() <span class="hljs-keyword">else</span> torch.device(<span class="hljs-string">&quot;cpu&quot;</span>)
model.to(device)
device`}}),je=new v({props:{code:"device(type='cuda')",highlighted:'device(<span class="hljs-built_in">type</span>=<span class="hljs-string">&#x27;cuda&#x27;</span>)'}}),Ee=new v({props:{code:`from tqdm.auto import tqdm

progress_bar = tqdm(range(num_training_steps))

model.train()
for epoch in range(num_epochs):
    for batch in train_dataloader:
        batch = {k: v.to(device) for k, v in batch.items()}
        outputs = model(**batch)
        loss = outputs.loss
        loss.backward()

        optimizer.step()
        lr_scheduler.step()
        optimizer.zero_grad()
        progress_bar.update(1)`,highlighted:`<span class="hljs-keyword">from</span> tqdm.auto <span class="hljs-keyword">import</span> tqdm

progress_bar = tqdm(<span class="hljs-built_in">range</span>(num_training_steps))

model.train()
<span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(num_epochs):
    <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> train_dataloader:
        batch = {k: v.to(device) <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> batch.items()}
        outputs = model(**batch)
        loss = outputs.loss
        loss.backward()

        optimizer.step()
        lr_scheduler.step()
        optimizer.zero_grad()
        progress_bar.update(<span class="hljs-number">1</span>)`}}),ge=new v({props:{code:`import evaluate

metric = evaluate.load("glue", "mrpc")
model.eval()
for batch in eval_dataloader:
    batch = {k: v.to(device) for k, v in batch.items()}
    with torch.no_grad():
        outputs = model(**batch)

    logits = outputs.logits
    predictions = torch.argmax(logits, dim=-1)
    metric.add_batch(predictions=predictions, references=batch["labels"])

metric.compute()`,highlighted:`<span class="hljs-keyword">import</span> evaluate

metric = evaluate.load(<span class="hljs-string">&quot;glue&quot;</span>, <span class="hljs-string">&quot;mrpc&quot;</span>)
model.<span class="hljs-built_in">eval</span>()
<span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> eval_dataloader:
    batch = {k: v.to(device) <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> batch.items()}
    <span class="hljs-keyword">with</span> torch.no_grad():
        outputs = model(**batch)

    logits = outputs.logits
    predictions = torch.argmax(logits, dim=-<span class="hljs-number">1</span>)
    metric.add_batch(predictions=predictions, references=batch[<span class="hljs-string">&quot;labels&quot;</span>])

metric.compute()`}}),ze=new v({props:{code:"{'accuracy': 0.8431372549019608, 'f1': 0.8907849829351535}",highlighted:'{<span class="hljs-string">&#x27;accuracy&#x27;</span>: <span class="hljs-number">0.8431372549019608</span>, <span class="hljs-string">&#x27;f1&#x27;</span>: <span class="hljs-number">0.8907849829351535</span>}'}}),Q=new Lo({props:{$$slots:{default:[Qo]},$$scope:{ctx:We}}}),qe=new Jo({}),ye=new Go({props:{id:"s7dy8QRgjJ0"}}),Ae=new v({props:{code:`from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler

model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2)
optimizer = AdamW(model.parameters(), lr=3e-5)

device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
model.to(device)

num_epochs = 3
num_training_steps = num_epochs * len(train_dataloader)
lr_scheduler = get_scheduler(
    "linear",
    optimizer=optimizer,
    num_warmup_steps=0,
    num_training_steps=num_training_steps,
)

progress_bar = tqdm(range(num_training_steps))

model.train()
for epoch in range(num_epochs):
    for batch in train_dataloader:
        batch = {k: v.to(device) for k, v in batch.items()}
        outputs = model(**batch)
        loss = outputs.loss
        loss.backward()

        optimizer.step()
        lr_scheduler.step()
        optimizer.zero_grad()
        progress_bar.update(1)`,highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AdamW, AutoModelForSequenceClassification, get_scheduler

model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=<span class="hljs-number">2</span>)
optimizer = AdamW(model.parameters(), lr=<span class="hljs-number">3e-5</span>)

device = torch.device(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-keyword">if</span> torch.cuda.is_available() <span class="hljs-keyword">else</span> torch.device(<span class="hljs-string">&quot;cpu&quot;</span>)
model.to(device)

num_epochs = <span class="hljs-number">3</span>
num_training_steps = num_epochs * <span class="hljs-built_in">len</span>(train_dataloader)
lr_scheduler = get_scheduler(
    <span class="hljs-string">&quot;linear&quot;</span>,
    optimizer=optimizer,
    num_warmup_steps=<span class="hljs-number">0</span>,
    num_training_steps=num_training_steps,
)

progress_bar = tqdm(<span class="hljs-built_in">range</span>(num_training_steps))

model.train()
<span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(num_epochs):
    <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> train_dataloader:
        batch = {k: v.to(device) <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> batch.items()}
        outputs = model(**batch)
        loss = outputs.loss
        loss.backward()

        optimizer.step()
        lr_scheduler.step()
        optimizer.zero_grad()
        progress_bar.update(<span class="hljs-number">1</span>)`}}),Pe=new v({props:{code:`+ from accelerate import Accelerator
  from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler

+ accelerator = Accelerator()

  model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2)
  optimizer = AdamW(model.parameters(), lr=3e-5)

- device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
- model.to(device)

+ train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare(
+     train_dataloader, eval_dataloader, model, optimizer
+ )

  num_epochs = 3
  num_training_steps = num_epochs * len(train_dataloader)
  lr_scheduler = get_scheduler(
      "linear",
      optimizer=optimizer,
      num_warmup_steps=0,
      num_training_steps=num_training_steps
  )

  progress_bar = tqdm(range(num_training_steps))

  model.train()
  for epoch in range(num_epochs):
      for batch in train_dataloader:
-         batch = {k: v.to(device) for k, v in batch.items()}
          outputs = model(**batch)
          loss = outputs.loss
-         loss.backward()
+         accelerator.backward(loss)

          optimizer.step()
          lr_scheduler.step()
          optimizer.zero_grad()
          progress_bar.update(1)`,highlighted:`<span class="hljs-addition">+ from accelerate import Accelerator</span>
  from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler

<span class="hljs-addition">+ accelerator = Accelerator()</span>

  model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2)
  optimizer = AdamW(model.parameters(), lr=3e-5)

<span class="hljs-deletion">- device = torch.device(&quot;cuda&quot;) if torch.cuda.is_available() else torch.device(&quot;cpu&quot;)</span>
<span class="hljs-deletion">- model.to(device)</span>

<span class="hljs-addition">+ train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare(</span>
<span class="hljs-addition">+     train_dataloader, eval_dataloader, model, optimizer</span>
<span class="hljs-addition">+ )</span>

  num_epochs = 3
  num_training_steps = num_epochs * len(train_dataloader)
  lr_scheduler = get_scheduler(
      &quot;linear&quot;,
      optimizer=optimizer,
      num_warmup_steps=0,
      num_training_steps=num_training_steps
  )

  progress_bar = tqdm(range(num_training_steps))

  model.train()
  for epoch in range(num_epochs):
      for batch in train_dataloader:
<span class="hljs-deletion">-         batch = {k: v.to(device) for k, v in batch.items()}</span>
          outputs = model(**batch)
          loss = outputs.loss
<span class="hljs-deletion">-         loss.backward()</span>
<span class="hljs-addition">+         accelerator.backward(loss)</span>

          optimizer.step()
          lr_scheduler.step()
          optimizer.zero_grad()
          progress_bar.update(1)`}}),X=new Lo({props:{$$slots:{default:[Ko]},$$scope:{ctx:We}}}),De=new v({props:{code:`from accelerate import Accelerator
from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler

accelerator = Accelerator()

model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2)
optimizer = AdamW(model.parameters(), lr=3e-5)

train_dl, eval_dl, model, optimizer = accelerator.prepare(
    train_dataloader, eval_dataloader, model, optimizer
)

num_epochs = 3
num_training_steps = num_epochs * len(train_dl)
lr_scheduler = get_scheduler(
    "linear",
    optimizer=optimizer,
    num_warmup_steps=0,
    num_training_steps=num_training_steps,
)

progress_bar = tqdm(range(num_training_steps))

model.train()
for epoch in range(num_epochs):
    for batch in train_dl:
        outputs = model(**batch)
        loss = outputs.loss
        accelerator.backward(loss)

        optimizer.step()
        lr_scheduler.step()
        optimizer.zero_grad()
        progress_bar.update(1)`,highlighted:`<span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> Accelerator
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AdamW, AutoModelForSequenceClassification, get_scheduler

accelerator = Accelerator()

model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=<span class="hljs-number">2</span>)
optimizer = AdamW(model.parameters(), lr=<span class="hljs-number">3e-5</span>)

train_dl, eval_dl, model, optimizer = accelerator.prepare(
    train_dataloader, eval_dataloader, model, optimizer
)

num_epochs = <span class="hljs-number">3</span>
num_training_steps = num_epochs * <span class="hljs-built_in">len</span>(train_dl)
lr_scheduler = get_scheduler(
    <span class="hljs-string">&quot;linear&quot;</span>,
    optimizer=optimizer,
    num_warmup_steps=<span class="hljs-number">0</span>,
    num_training_steps=num_training_steps,
)

progress_bar = tqdm(<span class="hljs-built_in">range</span>(num_training_steps))

model.train()
<span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(num_epochs):
    <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> train_dl:
        outputs = model(**batch)
        loss = outputs.loss
        accelerator.backward(loss)

        optimizer.step()
        lr_scheduler.step()
        optimizer.zero_grad()
        progress_bar.update(<span class="hljs-number">1</span>)`}}),Oe=new v({props:{code:"accelerate config",highlighted:"accelerate config"}}),Se=new v({props:{code:"accelerate launch train.py",highlighted:'accelerate <span class="hljs-built_in">launch</span> train.py'}}),xe=new v({props:{code:`from accelerate import notebook_launcher

notebook_launcher(training_function)`,highlighted:`<span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> notebook_launcher

notebook_launcher(training_function)`}}),{c(){b=n("meta"),q=c(),z=n("h1"),le=a("\u4E00\u4E2A\u5B8C\u6574\u7684\u8BAD\u7EC3"),I=c(),m(A.$$.fragment),x=c(),m(U.$$.fragment),Jt=c(),B=n("p"),oa=a("\u73B0\u5728\uFF0C\u6211\u4EEC\u5C06\u4E86\u89E3\u5982\u4F55\u5728\u4E0D\u4F7F\u7528"),Ze=n("code"),na=a("Trainer"),pa=a("\u7C7B\u7684\u60C5\u51B5\u4E0B\u83B7\u5F97\u4E0E\u4E0A\u4E00\u8282\u76F8\u540C\u7684\u7ED3\u679C\u3002\u540C\u6837\uFF0C\u6211\u4EEC\u5047\u8BBE\u60A8\u5DF2\u7ECF\u5B66\u4E60\u4E86\u7B2C 2 \u8282\u4E2D\u7684\u6570\u636E\u5904\u7406\u3002\u4E0B\u9762\u662F\u4E00\u4E2A\u7B80\u77ED\u7684\u603B\u7ED3\uFF0C\u6DB5\u76D6\u4E86\u60A8\u9700\u8981\u7684\u6240\u6709\u5185\u5BB9:"),Yt=c(),m(re.$$.fragment),Qt=c(),oe=n("h3"),ia=a("\u8BAD\u7EC3\u524D\u7684\u51C6\u5907"),Kt=c(),W=n("p"),ca=a("\u5728\u5B9E\u9645\u7F16\u5199\u6211\u4EEC\u7684\u8BAD\u7EC3\u5FAA\u73AF\u4E4B\u524D\uFF0C\u6211\u4EEC\u9700\u8981\u5B9A\u4E49\u4E00\u4E9B\u5BF9\u8C61\u3002\u7B2C\u4E00\u4E2A\u662F\u6211\u4EEC\u5C06\u7528\u4E8E\u8FED\u4EE3\u6279\u6B21\u7684\u6570\u636E\u52A0\u8F7D\u5668\u3002\u6211\u4EEC\u9700\u8981\u5BF9\u6211\u4EEC\u7684"),et=n("code"),da=a("tokenized_datasets"),ma=a("\u505A\u4E00\u4E9B\u5904\u7406\uFF0C\u6765\u5904\u7406"),tt=n("code"),ua=a("Trainer"),fa=a("\u81EA\u52A8\u4E3A\u6211\u4EEC\u505A\u7684\u4E00\u4E9B\u4E8B\u60C5\u3002\u5177\u4F53\u6765\u8BF4\uFF0C\u6211\u4EEC\u9700\u8981:"),Vt=c(),M=n("ul"),N=n("li"),_a=a("\u5220\u9664\u4E0E\u6A21\u578B\u4E0D\u671F\u671B\u7684\u503C\u76F8\u5BF9\u5E94\u7684\u5217\uFF08\u5982"),st=n("code"),ha=a("sentence1"),$a=a("\u548C"),at=n("code"),ba=a("sentence2"),va=a("\u5217\uFF09\u3002"),ka=c(),T=n("li"),ja=a("\u5C06\u5217\u540D"),lt=n("code"),Ea=a("label"),wa=a("\u91CD\u547D\u540D\u4E3A"),rt=n("code"),ga=a("labels"),za=a("\uFF08\u56E0\u4E3A\u6A21\u578B\u671F\u671B\u53C2\u6570\u662F"),ot=n("code"),qa=a("labels"),ya=a("\uFF09\u3002"),Ca=c(),nt=n("li"),Aa=a("\u8BBE\u7F6E\u6570\u636E\u96C6\u7684\u683C\u5F0F\uFF0C\u4F7F\u5176\u8FD4\u56DE PyTorch \u5F20\u91CF\u800C\u4E0D\u662F\u5217\u8868\u3002"),Xt=c(),R=n("p"),Pa=a("\u9488\u5BF9\u4E0A\u9762\u7684\u6BCF\u4E2A\u6B65\u9AA4\uFF0C\u6211\u4EEC\u7684 "),pt=n("code"),Da=a("tokenized_datasets"),Oa=a(" \u90FD\u6709\u4E00\u4E2A\u65B9\u6CD5:"),Zt=c(),m(ne.$$.fragment),es=c(),Me=n("p"),Sa=a("\u7136\u540E\uFF0C\u6211\u4EEC\u53EF\u4EE5\u68C0\u67E5\u7ED3\u679C\u4E2D\u662F\u5426\u53EA\u6709\u6A21\u578B\u80FD\u591F\u63A5\u53D7\u7684\u5217:"),ts=c(),m(pe.$$.fragment),ss=c(),Fe=n("p"),xa=a("\u81F3\u6B64\uFF0C\u6211\u4EEC\u53EF\u4EE5\u8F7B\u677E\u5B9A\u4E49\u6570\u636E\u52A0\u8F7D\u5668:"),as=c(),m(ie.$$.fragment),ls=c(),Le=n("p"),Ta=a("\u4E3A\u4E86\u5FEB\u901F\u68C0\u9A8C\u6570\u636E\u5904\u7406\u4E2D\u6CA1\u6709\u9519\u8BEF\uFF0C\u6211\u4EEC\u53EF\u4EE5\u8FD9\u6837\u68C0\u9A8C\u5176\u4E2D\u7684\u4E00\u4E2A\u6279\u6B21:"),rs=c(),m(ce.$$.fragment),os=c(),m(de.$$.fragment),ns=c(),F=n("p"),Ua=a("\u8BF7\u6CE8\u610F\uFF0C\u5B9E\u9645\u7684\u5F62\u72B6\u53EF\u80FD\u4E0E\u60A8\u7565\u6709\u4E0D\u540C\uFF0C\u56E0\u4E3A\u6211\u4EEC\u4E3A\u8BAD\u7EC3\u6570\u636E\u52A0\u8F7D\u5668\u8BBE\u7F6E\u4E86"),it=n("code"),Wa=a("shuffle=True"),Ma=a("\uFF0C\u5E76\u4E14\u6A21\u578B\u4F1A\u5C06\u53E5\u5B50\u586B\u5145\u5230"),ct=n("code"),Fa=a("batch"),La=a("\u4E2D\u7684\u6700\u5927\u957F\u5EA6\u3002"),ps=c(),Ge=n("p"),Ga=a("\u73B0\u5728\u6211\u4EEC\u5DF2\u7ECF\u5B8C\u5168\u5B8C\u6210\u4E86\u6570\u636E\u9884\u5904\u7406\uFF08\u5BF9\u4E8E\u4EFB\u4F55 ML \u4ECE\u4E1A\u8005\u6765\u8BF4\u90FD\u662F\u4E00\u4E2A\u4EE4\u4EBA\u6EE1\u610F\u4F46\u96BE\u4EE5\u5B9E\u73B0\u7684\u76EE\u6807\uFF09\uFF0C\u8BA9\u6211\u4EEC\u5C06\u6CE8\u610F\u529B\u8F6C\u5411\u6A21\u578B\u3002\u6211\u4EEC\u5B8C\u5168\u50CF\u5728\u4E0A\u4E00\u8282\u4E2D\u6240\u505A\u7684\u90A3\u6837\u5B9E\u4F8B\u5316\u5B83:"),is=c(),m(me.$$.fragment),cs=c(),J=n("p"),Na=a("\u4E3A\u4E86\u786E\u4FDD\u8BAD\u7EC3\u8FC7\u7A0B\u4E2D\u4E00\u5207\u987A\u5229\uFF0C\u6211\u4EEC\u5C06"),dt=n("code"),Ha=a("batch"),Ia=a("\u4F20\u9012\u7ED9\u8FD9\u4E2A\u6A21\u578B:"),ds=c(),m(ue.$$.fragment),ms=c(),m(fe.$$.fragment),us=c(),j=n("p"),Ba=a("\u5F53\u6211\u4EEC\u63D0\u4F9B "),mt=n("code"),Ra=a("labels"),Ja=a(" \u65F6\uFF0C \u{1F917} Transformers \u6A21\u578B\u90FD\u5C06\u8FD4\u56DE\u8FD9\u4E2A"),ut=n("code"),Ya=a("batch"),Qa=a("\u7684"),ft=n("code"),Ka=a("loss"),Va=a("\uFF0C\u6211\u4EEC\u8FD8\u5F97\u5230\u4E86 "),_t=n("code"),Xa=a("logits"),Za=a("("),ht=n("code"),el=a("batch"),tl=a("\u4E2D\u7684\u6BCF\u4E2A\u8F93\u5165\u6709\u4E24\u4E2A\uFF0C\u6240\u4EE5\u5F20\u91CF\u5927\u5C0F\u4E3A 8 x 2)\u3002"),fs=c(),E=n("p"),sl=a("\u6211\u4EEC\u51E0\u4E4E\u51C6\u5907\u597D\u7F16\u5199\u6211\u4EEC\u7684\u8BAD\u7EC3\u5FAA\u73AF\u4E86\uFF01\u6211\u4EEC\u53EA\u662F\u7F3A\u5C11\u4E24\u4EF6\u4E8B\uFF1A\u4F18\u5316\u5668\u548C\u5B66\u4E60\u7387\u8C03\u5EA6\u5668\u3002\u7531\u4E8E\u6211\u4EEC\u8BD5\u56FE\u81EA\u884C\u5B9E\u73B0 "),$t=n("code"),al=a("Trainer"),ll=a("\u7684\u529F\u80FD\uFF0C\u6211\u4EEC\u5C06\u4F7F\u7528\u76F8\u540C\u7684\u4F18\u5316\u5668\u548C\u5B66\u4E60\u7387\u8C03\u5EA6\u5668\u3002"),bt=n("code"),rl=a("Trainer"),ol=a(" \u4F7F\u7528\u7684\u4F18\u5316\u5668\u662F "),vt=n("code"),nl=a("AdamW"),pl=a(" , \u4E0E "),kt=n("code"),il=a("Adam"),cl=a(" \u76F8\u540C\uFF0C\u4F46\u5728\u6743\u91CD\u8870\u51CF\u6B63\u5219\u5316\u65B9\u9762\u6709\u6240\u4E0D\u540C(\u53C2\u89C1"),_e=n("a"),dl=a("\u201CDecoupled Weight Decay Regularization\u201D"),ml=a("\u4F5C\u8005:Ilya Loshchilov \u548C Frank Hutter):"),_s=c(),m(he.$$.fragment),hs=c(),L=n("p"),ul=a("\u6700\u540E\uFF0C\u9ED8\u8BA4\u4F7F\u7528\u7684\u5B66\u4E60\u7387\u8C03\u5EA6\u5668\u53EA\u662F\u4ECE\u6700\u5927\u503C (5e-5) \u5230 0 \u7684\u7EBF\u6027\u8870\u51CF\u3002 \u4E3A\u4E86\u5B9A\u4E49\u5B83\uFF0C\u6211\u4EEC\u9700\u8981\u77E5\u9053\u6211\u4EEC\u8BAD\u7EC3\u7684\u6B21\u6570\uFF0C\u5373\u6240\u6709\u6570\u636E\u8BAD\u7EC3\u7684\u6B21\u6570(epochs)\u4E58\u4EE5\u7684\u6570\u636E\u91CF\uFF08\u8FD9\u662F\u6211\u4EEC\u6240\u6709\u8BAD\u7EC3\u6570\u636E\u7684\u6570\u91CF\uFF09\u3002"),jt=n("code"),fl=a("Trainer"),_l=a("\u9ED8\u8BA4\u60C5\u51B5\u4E0B\u4F7F\u7528\u4E09\u4E2A"),Et=n("code"),hl=a("epochs"),$l=a("\uFF0C\u56E0\u6B64\u6211\u4EEC\u5B9A\u4E49\u8BAD\u7EC3\u8FC7\u7A0B\u5982\u4E0B:"),$s=c(),m($e.$$.fragment),bs=c(),m(be.$$.fragment),vs=c(),ve=n("h3"),bl=a("\u8BAD\u7EC3\u5FAA\u73AF"),ks=c(),P=n("p"),vl=a("\u6700\u540E\u4E00\u4EF6\u4E8B\uFF1A\u5982\u679C\u6211\u4EEC\u53EF\u4EE5\u8BBF\u95EE GPU,\u6211\u4EEC\u5C06\u5E0C\u671B\u4F7F\u7528 GPU(\u5728 CPU \u4E0A\uFF0C\u8BAD\u7EC3\u53EF\u80FD\u9700\u8981\u51E0\u4E2A\u5C0F\u65F6\u800C\u4E0D\u662F\u51E0\u5206\u949F)\u3002\u4E3A\u6B64\uFF0C\u6211\u4EEC\u5B9A\u4E49\u4E86\u4E00\u4E2A "),wt=n("code"),kl=a("device"),jl=a(",\u5B83\u5728GPU\u53EF\u7528\u7684\u60C5\u51B5\u4E0B\u6307\u5411GPU \u6211\u4EEC\u5C06\u628A\u6211\u4EEC\u7684\u6A21\u578B\u548C"),gt=n("code"),El=a("batche"),wl=a("\u653E\u5728"),zt=n("code"),gl=a("device"),zl=a("\u4E0A:"),js=c(),m(ke.$$.fragment),Es=c(),m(je.$$.fragment),ws=c(),Y=n("p"),ql=a("\u6211\u4EEC\u73B0\u5728\u51C6\u5907\u597D\u8BAD\u7EC3\u4E86\uFF01\u4E3A\u4E86\u4E86\u89E3\u8BAD\u7EC3\u4F55\u65F6\u7ED3\u675F\uFF0C\u6211\u4EEC\u4F7F\u7528 "),qt=n("code"),yl=a("tqdm"),Cl=a(" \u5E93,\u5728\u8BAD\u7EC3\u6B65\u9AA4\u6570\u4E0A\u6DFB\u52A0\u4E86\u4E00\u4E2A\u8FDB\u5EA6\u6761:"),gs=c(),m(Ee.$$.fragment),zs=c(),Ne=n("p"),Al=a("\u60A8\u53EF\u4EE5\u770B\u5230\u8BAD\u7EC3\u5FAA\u73AF\u7684\u6838\u5FC3\u4E0E\u4ECB\u7ECD\u4E2D\u7684\u975E\u5E38\u76F8\u4F3C\u3002\u6211\u4EEC\u6CA1\u6709\u8981\u6C42\u4EFB\u4F55\u68C0\u9A8C\uFF0C\u6240\u4EE5\u8FD9\u4E2A\u8BAD\u7EC3\u5FAA\u73AF\u4E0D\u4F1A\u544A\u8BC9\u6211\u4EEC\u4EFB\u4F55\u5173\u4E8E\u6A21\u578B\u76EE\u524D\u7684\u72B6\u6001\u3002\u6211\u4EEC\u9700\u8981\u4E3A\u6B64\u6DFB\u52A0\u4E00\u4E2A\u8BC4\u4F30\u5FAA\u73AF\u3002"),qs=c(),we=n("h3"),Pl=a("\u8BC4\u4F30\u5FAA\u73AF"),ys=c(),w=n("p"),Dl=a("\u6B63\u5982\u6211\u4EEC\u4E4B\u524D\u6240\u505A\u7684\u90A3\u6837\uFF0C\u6211\u4EEC\u5C06\u4F7F\u7528 \u{1F917} Evaluate \u5E93\u63D0\u4F9B\u7684\u6307\u6807\u3002\u6211\u4EEC\u5DF2\u7ECF\u4E86\u89E3\u4E86 "),yt=n("code"),Ol=a("metric.compute()"),Sl=a(" \u65B9\u6CD5\uFF0C\u5F53\u6211\u4EEC\u4F7F\u7528 "),Ct=n("code"),xl=a("add_batch()"),Tl=a("\u65B9\u6CD5\u8FDB\u884C\u9884\u6D4B\u5FAA\u73AF\u65F6\uFF0C\u5B9E\u9645\u4E0A\u8BE5\u6307\u6807\u53EF\u4EE5\u4E3A\u6211\u4EEC\u7D2F\u79EF\u6240\u6709 "),At=n("code"),Ul=a("batch"),Wl=a(" \u7684\u7ED3\u679C\u3002\u4E00\u65E6\u6211\u4EEC\u7D2F\u79EF\u4E86\u6240\u6709 "),Pt=n("code"),Ml=a("batch"),Fl=a(" \uFF0C\u6211\u4EEC\u5C31\u53EF\u4EE5\u4F7F\u7528 "),Dt=n("code"),Ll=a("metric.compute()"),Gl=a(" \u5F97\u5230\u6700\u7EC8\u7ED3\u679C .\u4EE5\u4E0B\u662F\u5728\u8BC4\u4F30\u5FAA\u73AF\u4E2D\u5B9E\u73B0\u6240\u6709\u8FD9\u4E9B\u7684\u65B9\u6CD5:"),Cs=c(),m(ge.$$.fragment),As=c(),m(ze.$$.fragment),Ps=c(),He=n("p"),Nl=a("\u540C\u6837\uFF0C\u7531\u4E8E\u6A21\u578B\u5934\u90E8\u521D\u59CB\u5316\u548C\u6570\u636E\u6539\u7EC4\u7684\u968F\u673A\u6027\uFF0C\u60A8\u7684\u7ED3\u679C\u4F1A\u7565\u6709\u4E0D\u540C\uFF0C\u4F46\u5B83\u4EEC\u5E94\u8BE5\u5728\u540C\u4E00\u4E2A\u8303\u56F4\u5185\u3002"),Ds=c(),m(Q.$$.fragment),Os=c(),H=n("h3"),K=n("a"),Ot=n("span"),m(qe.$$.fragment),Hl=c(),St=n("span"),Il=a("S\u4F7F\u7528\u{1F917} Accelerate\u52A0\u901F\u60A8\u7684\u8BAD\u7EC3\u5FAA\u73AF"),Ss=c(),m(ye.$$.fragment),xs=c(),V=n("p"),Bl=a("\u6211\u4EEC\u4E4B\u524D\u5B9A\u4E49\u7684\u8BAD\u7EC3\u5FAA\u73AF\u5728\u5355\u4E2A CPU \u6216 GPU \u4E0A\u8FD0\u884C\u826F\u597D\u3002\u4F46\u662F\u4F7F\u7528"),Ce=n("a"),Rl=a("\u{1F917} Accelerate"),Jl=a("\u5E93\uFF0C\u53EA\u9700\u8FDB\u884C\u4E00\u4E9B\u8C03\u6574\uFF0C\u6211\u4EEC\u5C31\u53EF\u4EE5\u5728\u591A\u4E2A GPU \u6216 TPU \u4E0A\u542F\u7528\u5206\u5E03\u5F0F\u8BAD\u7EC3\u3002\u4ECE\u521B\u5EFA\u8BAD\u7EC3\u548C\u9A8C\u8BC1\u6570\u636E\u52A0\u8F7D\u5668\u5F00\u59CB\uFF0C\u6211\u4EEC\u7684\u624B\u52A8\u8BAD\u7EC3\u5FAA\u73AF\u5982\u4E0B\u6240\u793A\uFF1A"),Ts=c(),m(Ae.$$.fragment),Us=c(),Ie=n("p"),Yl=a("\u4EE5\u4E0B\u662F\u53D8\u5316:"),Ws=c(),m(Pe.$$.fragment),Ms=c(),y=n("p"),Ql=a("\u8981\u6DFB\u52A0\u7684\u7B2C\u4E00\u884C\u662F\u5BFC\u5165"),xt=n("code"),Kl=a("Accelerator"),Vl=a("\u3002\u7B2C\u4E8C\u884C\u5B9E\u4F8B\u5316\u4E00\u4E2A "),Tt=n("code"),Xl=a("Accelerator"),Zl=a("\u5BF9\u8C61 \uFF0C\u5B83\u5C06\u67E5\u770B\u73AF\u5883\u5E76\u521D\u59CB\u5316\u9002\u5F53\u7684\u5206\u5E03\u5F0F\u8BBE\u7F6E\u3002 \u{1F917} Accelerate \u4E3A\u60A8\u5904\u7406\u6570\u636E\u5728\u8BBE\u5907\u95F4\u7684\u4F20\u9012\uFF0C\u56E0\u6B64\u60A8\u53EF\u4EE5\u5220\u9664\u5C06\u6A21\u578B\u653E\u5728\u8BBE\u5907\u4E0A\u7684\u90A3\u884C\u4EE3\u7801\uFF08\u6216\u8005\uFF0C\u5982\u679C\u60A8\u613F\u610F\uFF0C\u53EF\u4F7F\u7528 "),Ut=n("code"),er=a("accelerator.device"),tr=a(" \u4EE3\u66FF "),Wt=n("code"),sr=a("device"),ar=a(" \uFF09\u3002"),Fs=c(),k=n("p"),lr=a("\u7136\u540E\u5927\u90E8\u5206\u5DE5\u4F5C\u4F1A\u5728\u5C06\u6570\u636E\u52A0\u8F7D\u5668\u3001\u6A21\u578B\u548C\u4F18\u5316\u5668\u53D1\u9001\u5230\u7684"),Mt=n("code"),rr=a("accelerator.prepare()"),or=a("\u4E2D\u5B8C\u6210\u3002\u8FD9\u5C06\u4F1A\u628A\u8FD9\u4E9B\u5BF9\u8C61\u5305\u88C5\u5728\u9002\u5F53\u7684\u5BB9\u5668\u4E2D\uFF0C\u4EE5\u786E\u4FDD\u60A8\u7684\u5206\u5E03\u5F0F\u8BAD\u7EC3\u6309\u9884\u671F\u5DE5\u4F5C\u3002\u8981\u8FDB\u884C\u7684\u5176\u4F59\u66F4\u6539\u662F\u5220\u9664\u5C06"),Ft=n("code"),nr=a("batch"),pr=a("\u653E\u5728 "),Lt=n("code"),ir=a("device"),cr=a(" \u7684\u90A3\u884C\u4EE3\u7801\uFF08\u540C\u6837\uFF0C\u5982\u679C\u60A8\u60F3\u4FDD\u7559\u5B83\uFF0C\u60A8\u53EF\u4EE5\u5C06\u5176\u66F4\u6539\u4E3A\u4F7F\u7528 "),Gt=n("code"),dr=a("accelerator.device"),mr=a(" ) \u5E76\u5C06 "),Nt=n("code"),ur=a("loss.backward()"),fr=a(" \u66FF\u6362\u4E3A"),Ht=n("code"),_r=a("accelerator.backward(loss)"),hr=a("\u3002"),Ls=c(),m(X.$$.fragment),Gs=c(),Be=n("p"),$r=a("\u5982\u679C\u60A8\u60F3\u590D\u5236\u5E76\u7C98\u8D34\u6765\u76F4\u63A5\u8FD0\u884C\uFF0C\u4EE5\u4E0B\u662F \u{1F917} Accelerate \u7684\u5B8C\u6574\u8BAD\u7EC3\u5FAA\u73AF:"),Ns=c(),m(De.$$.fragment),Hs=c(),Z=n("p"),br=a("\u628A\u8FD9\u4E2A\u653E\u5728 "),It=n("code"),vr=a("train.py"),kr=a(" \u6587\u4EF6\u4E2D\uFF0C\u53EF\u4EE5\u8BA9\u5B83\u5728\u4EFB\u4F55\u7C7B\u578B\u7684\u5206\u5E03\u5F0F\u8BBE\u7F6E\u4E0A\u8FD0\u884C\u3002\u8981\u5728\u5206\u5E03\u5F0F\u8BBE\u7F6E\u4E2D\u8BD5\u7528\u5B83\uFF0C\u8BF7\u8FD0\u884C\u4EE5\u4E0B\u547D\u4EE4:"),Is=c(),m(Oe.$$.fragment),Bs=c(),Re=n("p"),jr=a("\u8FD9\u5C06\u8BE2\u95EE\u60A8\u51E0\u4E2A\u914D\u7F6E\u7684\u95EE\u9898\u5E76\u5C06\u60A8\u7684\u56DE\u7B54\u8F6C\u50A8\u5230\u6B64\u547D\u4EE4\u4F7F\u7528\u7684\u914D\u7F6E\u6587\u4EF6\u4E2D:"),Rs=c(),m(Se.$$.fragment),Js=c(),Je=n("p"),Er=a("\u8FD9\u5C06\u542F\u52A8\u5206\u5E03\u5F0F\u8BAD\u7EC3"),Ys=c(),ee=n("p"),wr=a("\u8FD9\u5C06\u542F\u52A8\u5206\u5E03\u5F0F\u8BAD\u7EC3\u3002\u5982\u679C\u60A8\u60F3\u5728 Notebook \u4E2D\u5C1D\u8BD5\u6B64\u64CD\u4F5C\uFF08\u4F8B\u5982\uFF0C\u5728 Colab \u4E0A\u4F7F\u7528 TPU \u8FDB\u884C\u6D4B\u8BD5\uFF09\uFF0C\u53EA\u9700\u5C06\u4EE3\u7801\u7C98\u8D34\u5230 "),Bt=n("code"),gr=a("training_function()"),zr=a(" \u5E76\u4F7F\u7528\u4EE5\u4E0B\u547D\u4EE4\u8FD0\u884C\u6700\u540E\u4E00\u4E2A\u5355\u5143\u683C:"),Qs=c(),m(xe.$$.fragment),Ks=c(),te=n("p"),qr=a("\u60A8\u53EF\u4EE5\u5728"),Te=n("a"),yr=a("\u{1F917} Accelerate repo"),Cr=a("\u627E\u5230\u66F4\u591A\u7684\u793A\u4F8B\u3002"),this.h()},l(e){const r=Bo('[data-svelte="svelte-1phssyn"]',document.head);b=p(r,"META",{name:!0,content:!0}),r.forEach(s),q=d(e),z=p(e,"H1",{id:!0});var Rt=i(z);le=l(Rt,"\u4E00\u4E2A\u5B8C\u6574\u7684\u8BAD\u7EC3"),Rt.forEach(s),I=d(e),u(A.$$.fragment,e),x=d(e),u(U.$$.fragment,e),Jt=d(e),B=p(e,"P",{});var Ue=i(B);oa=l(Ue,"\u73B0\u5728\uFF0C\u6211\u4EEC\u5C06\u4E86\u89E3\u5982\u4F55\u5728\u4E0D\u4F7F\u7528"),Ze=p(Ue,"CODE",{});var Ar=i(Ze);na=l(Ar,"Trainer"),Ar.forEach(s),pa=l(Ue,"\u7C7B\u7684\u60C5\u51B5\u4E0B\u83B7\u5F97\u4E0E\u4E0A\u4E00\u8282\u76F8\u540C\u7684\u7ED3\u679C\u3002\u540C\u6837\uFF0C\u6211\u4EEC\u5047\u8BBE\u60A8\u5DF2\u7ECF\u5B66\u4E60\u4E86\u7B2C 2 \u8282\u4E2D\u7684\u6570\u636E\u5904\u7406\u3002\u4E0B\u9762\u662F\u4E00\u4E2A\u7B80\u77ED\u7684\u603B\u7ED3\uFF0C\u6DB5\u76D6\u4E86\u60A8\u9700\u8981\u7684\u6240\u6709\u5185\u5BB9:"),Ue.forEach(s),Yt=d(e),u(re.$$.fragment,e),Qt=d(e),oe=p(e,"H3",{id:!0});var Pr=i(oe);ia=l(Pr,"\u8BAD\u7EC3\u524D\u7684\u51C6\u5907"),Pr.forEach(s),Kt=d(e),W=p(e,"P",{});var Ye=i(W);ca=l(Ye,"\u5728\u5B9E\u9645\u7F16\u5199\u6211\u4EEC\u7684\u8BAD\u7EC3\u5FAA\u73AF\u4E4B\u524D\uFF0C\u6211\u4EEC\u9700\u8981\u5B9A\u4E49\u4E00\u4E9B\u5BF9\u8C61\u3002\u7B2C\u4E00\u4E2A\u662F\u6211\u4EEC\u5C06\u7528\u4E8E\u8FED\u4EE3\u6279\u6B21\u7684\u6570\u636E\u52A0\u8F7D\u5668\u3002\u6211\u4EEC\u9700\u8981\u5BF9\u6211\u4EEC\u7684"),et=p(Ye,"CODE",{});var Dr=i(et);da=l(Dr,"tokenized_datasets"),Dr.forEach(s),ma=l(Ye,"\u505A\u4E00\u4E9B\u5904\u7406\uFF0C\u6765\u5904\u7406"),tt=p(Ye,"CODE",{});var Or=i(tt);ua=l(Or,"Trainer"),Or.forEach(s),fa=l(Ye,"\u81EA\u52A8\u4E3A\u6211\u4EEC\u505A\u7684\u4E00\u4E9B\u4E8B\u60C5\u3002\u5177\u4F53\u6765\u8BF4\uFF0C\u6211\u4EEC\u9700\u8981:"),Ye.forEach(s),Vt=d(e),M=p(e,"UL",{});var Qe=i(M);N=p(Qe,"LI",{});var Ke=i(N);_a=l(Ke,"\u5220\u9664\u4E0E\u6A21\u578B\u4E0D\u671F\u671B\u7684\u503C\u76F8\u5BF9\u5E94\u7684\u5217\uFF08\u5982"),st=p(Ke,"CODE",{});var Sr=i(st);ha=l(Sr,"sentence1"),Sr.forEach(s),$a=l(Ke,"\u548C"),at=p(Ke,"CODE",{});var xr=i(at);ba=l(xr,"sentence2"),xr.forEach(s),va=l(Ke,"\u5217\uFF09\u3002"),Ke.forEach(s),ka=d(Qe),T=p(Qe,"LI",{});var se=i(T);ja=l(se,"\u5C06\u5217\u540D"),lt=p(se,"CODE",{});var Tr=i(lt);Ea=l(Tr,"label"),Tr.forEach(s),wa=l(se,"\u91CD\u547D\u540D\u4E3A"),rt=p(se,"CODE",{});var Ur=i(rt);ga=l(Ur,"labels"),Ur.forEach(s),za=l(se,"\uFF08\u56E0\u4E3A\u6A21\u578B\u671F\u671B\u53C2\u6570\u662F"),ot=p(se,"CODE",{});var Wr=i(ot);qa=l(Wr,"labels"),Wr.forEach(s),ya=l(se,"\uFF09\u3002"),se.forEach(s),Ca=d(Qe),nt=p(Qe,"LI",{});var Mr=i(nt);Aa=l(Mr,"\u8BBE\u7F6E\u6570\u636E\u96C6\u7684\u683C\u5F0F\uFF0C\u4F7F\u5176\u8FD4\u56DE PyTorch \u5F20\u91CF\u800C\u4E0D\u662F\u5217\u8868\u3002"),Mr.forEach(s),Qe.forEach(s),Xt=d(e),R=p(e,"P",{});var Xs=i(R);Pa=l(Xs,"\u9488\u5BF9\u4E0A\u9762\u7684\u6BCF\u4E2A\u6B65\u9AA4\uFF0C\u6211\u4EEC\u7684 "),pt=p(Xs,"CODE",{});var Fr=i(pt);Da=l(Fr,"tokenized_datasets"),Fr.forEach(s),Oa=l(Xs," \u90FD\u6709\u4E00\u4E2A\u65B9\u6CD5:"),Xs.forEach(s),Zt=d(e),u(ne.$$.fragment,e),es=d(e),Me=p(e,"P",{});var Lr=i(Me);Sa=l(Lr,"\u7136\u540E\uFF0C\u6211\u4EEC\u53EF\u4EE5\u68C0\u67E5\u7ED3\u679C\u4E2D\u662F\u5426\u53EA\u6709\u6A21\u578B\u80FD\u591F\u63A5\u53D7\u7684\u5217:"),Lr.forEach(s),ts=d(e),u(pe.$$.fragment,e),ss=d(e),Fe=p(e,"P",{});var Gr=i(Fe);xa=l(Gr,"\u81F3\u6B64\uFF0C\u6211\u4EEC\u53EF\u4EE5\u8F7B\u677E\u5B9A\u4E49\u6570\u636E\u52A0\u8F7D\u5668:"),Gr.forEach(s),as=d(e),u(ie.$$.fragment,e),ls=d(e),Le=p(e,"P",{});var Nr=i(Le);Ta=l(Nr,"\u4E3A\u4E86\u5FEB\u901F\u68C0\u9A8C\u6570\u636E\u5904\u7406\u4E2D\u6CA1\u6709\u9519\u8BEF\uFF0C\u6211\u4EEC\u53EF\u4EE5\u8FD9\u6837\u68C0\u9A8C\u5176\u4E2D\u7684\u4E00\u4E2A\u6279\u6B21:"),Nr.forEach(s),rs=d(e),u(ce.$$.fragment,e),os=d(e),u(de.$$.fragment,e),ns=d(e),F=p(e,"P",{});var Ve=i(F);Ua=l(Ve,"\u8BF7\u6CE8\u610F\uFF0C\u5B9E\u9645\u7684\u5F62\u72B6\u53EF\u80FD\u4E0E\u60A8\u7565\u6709\u4E0D\u540C\uFF0C\u56E0\u4E3A\u6211\u4EEC\u4E3A\u8BAD\u7EC3\u6570\u636E\u52A0\u8F7D\u5668\u8BBE\u7F6E\u4E86"),it=p(Ve,"CODE",{});var Hr=i(it);Wa=l(Hr,"shuffle=True"),Hr.forEach(s),Ma=l(Ve,"\uFF0C\u5E76\u4E14\u6A21\u578B\u4F1A\u5C06\u53E5\u5B50\u586B\u5145\u5230"),ct=p(Ve,"CODE",{});var Ir=i(ct);Fa=l(Ir,"batch"),Ir.forEach(s),La=l(Ve,"\u4E2D\u7684\u6700\u5927\u957F\u5EA6\u3002"),Ve.forEach(s),ps=d(e),Ge=p(e,"P",{});var Br=i(Ge);Ga=l(Br,"\u73B0\u5728\u6211\u4EEC\u5DF2\u7ECF\u5B8C\u5168\u5B8C\u6210\u4E86\u6570\u636E\u9884\u5904\u7406\uFF08\u5BF9\u4E8E\u4EFB\u4F55 ML \u4ECE\u4E1A\u8005\u6765\u8BF4\u90FD\u662F\u4E00\u4E2A\u4EE4\u4EBA\u6EE1\u610F\u4F46\u96BE\u4EE5\u5B9E\u73B0\u7684\u76EE\u6807\uFF09\uFF0C\u8BA9\u6211\u4EEC\u5C06\u6CE8\u610F\u529B\u8F6C\u5411\u6A21\u578B\u3002\u6211\u4EEC\u5B8C\u5168\u50CF\u5728\u4E0A\u4E00\u8282\u4E2D\u6240\u505A\u7684\u90A3\u6837\u5B9E\u4F8B\u5316\u5B83:"),Br.forEach(s),is=d(e),u(me.$$.fragment,e),cs=d(e),J=p(e,"P",{});var Zs=i(J);Na=l(Zs,"\u4E3A\u4E86\u786E\u4FDD\u8BAD\u7EC3\u8FC7\u7A0B\u4E2D\u4E00\u5207\u987A\u5229\uFF0C\u6211\u4EEC\u5C06"),dt=p(Zs,"CODE",{});var Rr=i(dt);Ha=l(Rr,"batch"),Rr.forEach(s),Ia=l(Zs,"\u4F20\u9012\u7ED9\u8FD9\u4E2A\u6A21\u578B:"),Zs.forEach(s),ds=d(e),u(ue.$$.fragment,e),ms=d(e),u(fe.$$.fragment,e),us=d(e),j=p(e,"P",{});var D=i(j);Ba=l(D,"\u5F53\u6211\u4EEC\u63D0\u4F9B "),mt=p(D,"CODE",{});var Jr=i(mt);Ra=l(Jr,"labels"),Jr.forEach(s),Ja=l(D," \u65F6\uFF0C \u{1F917} Transformers \u6A21\u578B\u90FD\u5C06\u8FD4\u56DE\u8FD9\u4E2A"),ut=p(D,"CODE",{});var Yr=i(ut);Ya=l(Yr,"batch"),Yr.forEach(s),Qa=l(D,"\u7684"),ft=p(D,"CODE",{});var Qr=i(ft);Ka=l(Qr,"loss"),Qr.forEach(s),Va=l(D,"\uFF0C\u6211\u4EEC\u8FD8\u5F97\u5230\u4E86 "),_t=p(D,"CODE",{});var Kr=i(_t);Xa=l(Kr,"logits"),Kr.forEach(s),Za=l(D,"("),ht=p(D,"CODE",{});var Vr=i(ht);el=l(Vr,"batch"),Vr.forEach(s),tl=l(D,"\u4E2D\u7684\u6BCF\u4E2A\u8F93\u5165\u6709\u4E24\u4E2A\uFF0C\u6240\u4EE5\u5F20\u91CF\u5927\u5C0F\u4E3A 8 x 2)\u3002"),D.forEach(s),fs=d(e),E=p(e,"P",{});var O=i(E);sl=l(O,"\u6211\u4EEC\u51E0\u4E4E\u51C6\u5907\u597D\u7F16\u5199\u6211\u4EEC\u7684\u8BAD\u7EC3\u5FAA\u73AF\u4E86\uFF01\u6211\u4EEC\u53EA\u662F\u7F3A\u5C11\u4E24\u4EF6\u4E8B\uFF1A\u4F18\u5316\u5668\u548C\u5B66\u4E60\u7387\u8C03\u5EA6\u5668\u3002\u7531\u4E8E\u6211\u4EEC\u8BD5\u56FE\u81EA\u884C\u5B9E\u73B0 "),$t=p(O,"CODE",{});var Xr=i($t);al=l(Xr,"Trainer"),Xr.forEach(s),ll=l(O,"\u7684\u529F\u80FD\uFF0C\u6211\u4EEC\u5C06\u4F7F\u7528\u76F8\u540C\u7684\u4F18\u5316\u5668\u548C\u5B66\u4E60\u7387\u8C03\u5EA6\u5668\u3002"),bt=p(O,"CODE",{});var Zr=i(bt);rl=l(Zr,"Trainer"),Zr.forEach(s),ol=l(O," \u4F7F\u7528\u7684\u4F18\u5316\u5668\u662F "),vt=p(O,"CODE",{});var eo=i(vt);nl=l(eo,"AdamW"),eo.forEach(s),pl=l(O," , \u4E0E "),kt=p(O,"CODE",{});var to=i(kt);il=l(to,"Adam"),to.forEach(s),cl=l(O," \u76F8\u540C\uFF0C\u4F46\u5728\u6743\u91CD\u8870\u51CF\u6B63\u5219\u5316\u65B9\u9762\u6709\u6240\u4E0D\u540C(\u53C2\u89C1"),_e=p(O,"A",{href:!0,rel:!0});var so=i(_e);dl=l(so,"\u201CDecoupled Weight Decay Regularization\u201D"),so.forEach(s),ml=l(O,"\u4F5C\u8005:Ilya Loshchilov \u548C Frank Hutter):"),O.forEach(s),_s=d(e),u(he.$$.fragment,e),hs=d(e),L=p(e,"P",{});var Xe=i(L);ul=l(Xe,"\u6700\u540E\uFF0C\u9ED8\u8BA4\u4F7F\u7528\u7684\u5B66\u4E60\u7387\u8C03\u5EA6\u5668\u53EA\u662F\u4ECE\u6700\u5927\u503C (5e-5) \u5230 0 \u7684\u7EBF\u6027\u8870\u51CF\u3002 \u4E3A\u4E86\u5B9A\u4E49\u5B83\uFF0C\u6211\u4EEC\u9700\u8981\u77E5\u9053\u6211\u4EEC\u8BAD\u7EC3\u7684\u6B21\u6570\uFF0C\u5373\u6240\u6709\u6570\u636E\u8BAD\u7EC3\u7684\u6B21\u6570(epochs)\u4E58\u4EE5\u7684\u6570\u636E\u91CF\uFF08\u8FD9\u662F\u6211\u4EEC\u6240\u6709\u8BAD\u7EC3\u6570\u636E\u7684\u6570\u91CF\uFF09\u3002"),jt=p(Xe,"CODE",{});var ao=i(jt);fl=l(ao,"Trainer"),ao.forEach(s),_l=l(Xe,"\u9ED8\u8BA4\u60C5\u51B5\u4E0B\u4F7F\u7528\u4E09\u4E2A"),Et=p(Xe,"CODE",{});var lo=i(Et);hl=l(lo,"epochs"),lo.forEach(s),$l=l(Xe,"\uFF0C\u56E0\u6B64\u6211\u4EEC\u5B9A\u4E49\u8BAD\u7EC3\u8FC7\u7A0B\u5982\u4E0B:"),Xe.forEach(s),$s=d(e),u($e.$$.fragment,e),bs=d(e),u(be.$$.fragment,e),vs=d(e),ve=p(e,"H3",{id:!0});var ro=i(ve);bl=l(ro,"\u8BAD\u7EC3\u5FAA\u73AF"),ro.forEach(s),ks=d(e),P=p(e,"P",{});var ae=i(P);vl=l(ae,"\u6700\u540E\u4E00\u4EF6\u4E8B\uFF1A\u5982\u679C\u6211\u4EEC\u53EF\u4EE5\u8BBF\u95EE GPU,\u6211\u4EEC\u5C06\u5E0C\u671B\u4F7F\u7528 GPU(\u5728 CPU \u4E0A\uFF0C\u8BAD\u7EC3\u53EF\u80FD\u9700\u8981\u51E0\u4E2A\u5C0F\u65F6\u800C\u4E0D\u662F\u51E0\u5206\u949F)\u3002\u4E3A\u6B64\uFF0C\u6211\u4EEC\u5B9A\u4E49\u4E86\u4E00\u4E2A "),wt=p(ae,"CODE",{});var oo=i(wt);kl=l(oo,"device"),oo.forEach(s),jl=l(ae,",\u5B83\u5728GPU\u53EF\u7528\u7684\u60C5\u51B5\u4E0B\u6307\u5411GPU \u6211\u4EEC\u5C06\u628A\u6211\u4EEC\u7684\u6A21\u578B\u548C"),gt=p(ae,"CODE",{});var no=i(gt);El=l(no,"batche"),no.forEach(s),wl=l(ae,"\u653E\u5728"),zt=p(ae,"CODE",{});var po=i(zt);gl=l(po,"device"),po.forEach(s),zl=l(ae,"\u4E0A:"),ae.forEach(s),js=d(e),u(ke.$$.fragment,e),Es=d(e),u(je.$$.fragment,e),ws=d(e),Y=p(e,"P",{});var ea=i(Y);ql=l(ea,"\u6211\u4EEC\u73B0\u5728\u51C6\u5907\u597D\u8BAD\u7EC3\u4E86\uFF01\u4E3A\u4E86\u4E86\u89E3\u8BAD\u7EC3\u4F55\u65F6\u7ED3\u675F\uFF0C\u6211\u4EEC\u4F7F\u7528 "),qt=p(ea,"CODE",{});var io=i(qt);yl=l(io,"tqdm"),io.forEach(s),Cl=l(ea," \u5E93,\u5728\u8BAD\u7EC3\u6B65\u9AA4\u6570\u4E0A\u6DFB\u52A0\u4E86\u4E00\u4E2A\u8FDB\u5EA6\u6761:"),ea.forEach(s),gs=d(e),u(Ee.$$.fragment,e),zs=d(e),Ne=p(e,"P",{});var co=i(Ne);Al=l(co,"\u60A8\u53EF\u4EE5\u770B\u5230\u8BAD\u7EC3\u5FAA\u73AF\u7684\u6838\u5FC3\u4E0E\u4ECB\u7ECD\u4E2D\u7684\u975E\u5E38\u76F8\u4F3C\u3002\u6211\u4EEC\u6CA1\u6709\u8981\u6C42\u4EFB\u4F55\u68C0\u9A8C\uFF0C\u6240\u4EE5\u8FD9\u4E2A\u8BAD\u7EC3\u5FAA\u73AF\u4E0D\u4F1A\u544A\u8BC9\u6211\u4EEC\u4EFB\u4F55\u5173\u4E8E\u6A21\u578B\u76EE\u524D\u7684\u72B6\u6001\u3002\u6211\u4EEC\u9700\u8981\u4E3A\u6B64\u6DFB\u52A0\u4E00\u4E2A\u8BC4\u4F30\u5FAA\u73AF\u3002"),co.forEach(s),qs=d(e),we=p(e,"H3",{id:!0});var mo=i(we);Pl=l(mo,"\u8BC4\u4F30\u5FAA\u73AF"),mo.forEach(s),ys=d(e),w=p(e,"P",{});var S=i(w);Dl=l(S,"\u6B63\u5982\u6211\u4EEC\u4E4B\u524D\u6240\u505A\u7684\u90A3\u6837\uFF0C\u6211\u4EEC\u5C06\u4F7F\u7528 \u{1F917} Evaluate \u5E93\u63D0\u4F9B\u7684\u6307\u6807\u3002\u6211\u4EEC\u5DF2\u7ECF\u4E86\u89E3\u4E86 "),yt=p(S,"CODE",{});var uo=i(yt);Ol=l(uo,"metric.compute()"),uo.forEach(s),Sl=l(S," \u65B9\u6CD5\uFF0C\u5F53\u6211\u4EEC\u4F7F\u7528 "),Ct=p(S,"CODE",{});var fo=i(Ct);xl=l(fo,"add_batch()"),fo.forEach(s),Tl=l(S,"\u65B9\u6CD5\u8FDB\u884C\u9884\u6D4B\u5FAA\u73AF\u65F6\uFF0C\u5B9E\u9645\u4E0A\u8BE5\u6307\u6807\u53EF\u4EE5\u4E3A\u6211\u4EEC\u7D2F\u79EF\u6240\u6709 "),At=p(S,"CODE",{});var _o=i(At);Ul=l(_o,"batch"),_o.forEach(s),Wl=l(S," \u7684\u7ED3\u679C\u3002\u4E00\u65E6\u6211\u4EEC\u7D2F\u79EF\u4E86\u6240\u6709 "),Pt=p(S,"CODE",{});var ho=i(Pt);Ml=l(ho,"batch"),ho.forEach(s),Fl=l(S," \uFF0C\u6211\u4EEC\u5C31\u53EF\u4EE5\u4F7F\u7528 "),Dt=p(S,"CODE",{});var $o=i(Dt);Ll=l($o,"metric.compute()"),$o.forEach(s),Gl=l(S," \u5F97\u5230\u6700\u7EC8\u7ED3\u679C .\u4EE5\u4E0B\u662F\u5728\u8BC4\u4F30\u5FAA\u73AF\u4E2D\u5B9E\u73B0\u6240\u6709\u8FD9\u4E9B\u7684\u65B9\u6CD5:"),S.forEach(s),Cs=d(e),u(ge.$$.fragment,e),As=d(e),u(ze.$$.fragment,e),Ps=d(e),He=p(e,"P",{});var bo=i(He);Nl=l(bo,"\u540C\u6837\uFF0C\u7531\u4E8E\u6A21\u578B\u5934\u90E8\u521D\u59CB\u5316\u548C\u6570\u636E\u6539\u7EC4\u7684\u968F\u673A\u6027\uFF0C\u60A8\u7684\u7ED3\u679C\u4F1A\u7565\u6709\u4E0D\u540C\uFF0C\u4F46\u5B83\u4EEC\u5E94\u8BE5\u5728\u540C\u4E00\u4E2A\u8303\u56F4\u5185\u3002"),bo.forEach(s),Ds=d(e),u(Q.$$.fragment,e),Os=d(e),H=p(e,"H3",{class:!0});var ta=i(H);K=p(ta,"A",{id:!0,class:!0,href:!0});var vo=i(K);Ot=p(vo,"SPAN",{});var ko=i(Ot);u(qe.$$.fragment,ko),ko.forEach(s),vo.forEach(s),Hl=d(ta),St=p(ta,"SPAN",{});var jo=i(St);Il=l(jo,"S\u4F7F\u7528\u{1F917} Accelerate\u52A0\u901F\u60A8\u7684\u8BAD\u7EC3\u5FAA\u73AF"),jo.forEach(s),ta.forEach(s),Ss=d(e),u(ye.$$.fragment,e),xs=d(e),V=p(e,"P",{});var sa=i(V);Bl=l(sa,"\u6211\u4EEC\u4E4B\u524D\u5B9A\u4E49\u7684\u8BAD\u7EC3\u5FAA\u73AF\u5728\u5355\u4E2A CPU \u6216 GPU \u4E0A\u8FD0\u884C\u826F\u597D\u3002\u4F46\u662F\u4F7F\u7528"),Ce=p(sa,"A",{href:!0,rel:!0});var Eo=i(Ce);Rl=l(Eo,"\u{1F917} Accelerate"),Eo.forEach(s),Jl=l(sa,"\u5E93\uFF0C\u53EA\u9700\u8FDB\u884C\u4E00\u4E9B\u8C03\u6574\uFF0C\u6211\u4EEC\u5C31\u53EF\u4EE5\u5728\u591A\u4E2A GPU \u6216 TPU \u4E0A\u542F\u7528\u5206\u5E03\u5F0F\u8BAD\u7EC3\u3002\u4ECE\u521B\u5EFA\u8BAD\u7EC3\u548C\u9A8C\u8BC1\u6570\u636E\u52A0\u8F7D\u5668\u5F00\u59CB\uFF0C\u6211\u4EEC\u7684\u624B\u52A8\u8BAD\u7EC3\u5FAA\u73AF\u5982\u4E0B\u6240\u793A\uFF1A"),sa.forEach(s),Ts=d(e),u(Ae.$$.fragment,e),Us=d(e),Ie=p(e,"P",{});var wo=i(Ie);Yl=l(wo,"\u4EE5\u4E0B\u662F\u53D8\u5316:"),wo.forEach(s),Ws=d(e),u(Pe.$$.fragment,e),Ms=d(e),y=p(e,"P",{});var G=i(y);Ql=l(G,"\u8981\u6DFB\u52A0\u7684\u7B2C\u4E00\u884C\u662F\u5BFC\u5165"),xt=p(G,"CODE",{});var go=i(xt);Kl=l(go,"Accelerator"),go.forEach(s),Vl=l(G,"\u3002\u7B2C\u4E8C\u884C\u5B9E\u4F8B\u5316\u4E00\u4E2A "),Tt=p(G,"CODE",{});var zo=i(Tt);Xl=l(zo,"Accelerator"),zo.forEach(s),Zl=l(G,"\u5BF9\u8C61 \uFF0C\u5B83\u5C06\u67E5\u770B\u73AF\u5883\u5E76\u521D\u59CB\u5316\u9002\u5F53\u7684\u5206\u5E03\u5F0F\u8BBE\u7F6E\u3002 \u{1F917} Accelerate \u4E3A\u60A8\u5904\u7406\u6570\u636E\u5728\u8BBE\u5907\u95F4\u7684\u4F20\u9012\uFF0C\u56E0\u6B64\u60A8\u53EF\u4EE5\u5220\u9664\u5C06\u6A21\u578B\u653E\u5728\u8BBE\u5907\u4E0A\u7684\u90A3\u884C\u4EE3\u7801\uFF08\u6216\u8005\uFF0C\u5982\u679C\u60A8\u613F\u610F\uFF0C\u53EF\u4F7F\u7528 "),Ut=p(G,"CODE",{});var qo=i(Ut);er=l(qo,"accelerator.device"),qo.forEach(s),tr=l(G," \u4EE3\u66FF "),Wt=p(G,"CODE",{});var yo=i(Wt);sr=l(yo,"device"),yo.forEach(s),ar=l(G," \uFF09\u3002"),G.forEach(s),Fs=d(e),k=p(e,"P",{});var C=i(k);lr=l(C,"\u7136\u540E\u5927\u90E8\u5206\u5DE5\u4F5C\u4F1A\u5728\u5C06\u6570\u636E\u52A0\u8F7D\u5668\u3001\u6A21\u578B\u548C\u4F18\u5316\u5668\u53D1\u9001\u5230\u7684"),Mt=p(C,"CODE",{});var Co=i(Mt);rr=l(Co,"accelerator.prepare()"),Co.forEach(s),or=l(C,"\u4E2D\u5B8C\u6210\u3002\u8FD9\u5C06\u4F1A\u628A\u8FD9\u4E9B\u5BF9\u8C61\u5305\u88C5\u5728\u9002\u5F53\u7684\u5BB9\u5668\u4E2D\uFF0C\u4EE5\u786E\u4FDD\u60A8\u7684\u5206\u5E03\u5F0F\u8BAD\u7EC3\u6309\u9884\u671F\u5DE5\u4F5C\u3002\u8981\u8FDB\u884C\u7684\u5176\u4F59\u66F4\u6539\u662F\u5220\u9664\u5C06"),Ft=p(C,"CODE",{});var Ao=i(Ft);nr=l(Ao,"batch"),Ao.forEach(s),pr=l(C,"\u653E\u5728 "),Lt=p(C,"CODE",{});var Po=i(Lt);ir=l(Po,"device"),Po.forEach(s),cr=l(C," \u7684\u90A3\u884C\u4EE3\u7801\uFF08\u540C\u6837\uFF0C\u5982\u679C\u60A8\u60F3\u4FDD\u7559\u5B83\uFF0C\u60A8\u53EF\u4EE5\u5C06\u5176\u66F4\u6539\u4E3A\u4F7F\u7528 "),Gt=p(C,"CODE",{});var Do=i(Gt);dr=l(Do,"accelerator.device"),Do.forEach(s),mr=l(C," ) \u5E76\u5C06 "),Nt=p(C,"CODE",{});var Oo=i(Nt);ur=l(Oo,"loss.backward()"),Oo.forEach(s),fr=l(C," \u66FF\u6362\u4E3A"),Ht=p(C,"CODE",{});var So=i(Ht);_r=l(So,"accelerator.backward(loss)"),So.forEach(s),hr=l(C,"\u3002"),C.forEach(s),Ls=d(e),u(X.$$.fragment,e),Gs=d(e),Be=p(e,"P",{});var xo=i(Be);$r=l(xo,"\u5982\u679C\u60A8\u60F3\u590D\u5236\u5E76\u7C98\u8D34\u6765\u76F4\u63A5\u8FD0\u884C\uFF0C\u4EE5\u4E0B\u662F \u{1F917} Accelerate \u7684\u5B8C\u6574\u8BAD\u7EC3\u5FAA\u73AF:"),xo.forEach(s),Ns=d(e),u(De.$$.fragment,e),Hs=d(e),Z=p(e,"P",{});var aa=i(Z);br=l(aa,"\u628A\u8FD9\u4E2A\u653E\u5728 "),It=p(aa,"CODE",{});var To=i(It);vr=l(To,"train.py"),To.forEach(s),kr=l(aa," \u6587\u4EF6\u4E2D\uFF0C\u53EF\u4EE5\u8BA9\u5B83\u5728\u4EFB\u4F55\u7C7B\u578B\u7684\u5206\u5E03\u5F0F\u8BBE\u7F6E\u4E0A\u8FD0\u884C\u3002\u8981\u5728\u5206\u5E03\u5F0F\u8BBE\u7F6E\u4E2D\u8BD5\u7528\u5B83\uFF0C\u8BF7\u8FD0\u884C\u4EE5\u4E0B\u547D\u4EE4:"),aa.forEach(s),Is=d(e),u(Oe.$$.fragment,e),Bs=d(e),Re=p(e,"P",{});var Uo=i(Re);jr=l(Uo,"\u8FD9\u5C06\u8BE2\u95EE\u60A8\u51E0\u4E2A\u914D\u7F6E\u7684\u95EE\u9898\u5E76\u5C06\u60A8\u7684\u56DE\u7B54\u8F6C\u50A8\u5230\u6B64\u547D\u4EE4\u4F7F\u7528\u7684\u914D\u7F6E\u6587\u4EF6\u4E2D:"),Uo.forEach(s),Rs=d(e),u(Se.$$.fragment,e),Js=d(e),Je=p(e,"P",{});var Wo=i(Je);Er=l(Wo,"\u8FD9\u5C06\u542F\u52A8\u5206\u5E03\u5F0F\u8BAD\u7EC3"),Wo.forEach(s),Ys=d(e),ee=p(e,"P",{});var la=i(ee);wr=l(la,"\u8FD9\u5C06\u542F\u52A8\u5206\u5E03\u5F0F\u8BAD\u7EC3\u3002\u5982\u679C\u60A8\u60F3\u5728 Notebook \u4E2D\u5C1D\u8BD5\u6B64\u64CD\u4F5C\uFF08\u4F8B\u5982\uFF0C\u5728 Colab \u4E0A\u4F7F\u7528 TPU \u8FDB\u884C\u6D4B\u8BD5\uFF09\uFF0C\u53EA\u9700\u5C06\u4EE3\u7801\u7C98\u8D34\u5230 "),Bt=p(la,"CODE",{});var Mo=i(Bt);gr=l(Mo,"training_function()"),Mo.forEach(s),zr=l(la," \u5E76\u4F7F\u7528\u4EE5\u4E0B\u547D\u4EE4\u8FD0\u884C\u6700\u540E\u4E00\u4E2A\u5355\u5143\u683C:"),la.forEach(s),Qs=d(e),u(xe.$$.fragment,e),Ks=d(e),te=p(e,"P",{});var ra=i(te);qr=l(ra,"\u60A8\u53EF\u4EE5\u5728"),Te=p(ra,"A",{href:!0,rel:!0});var Fo=i(Te);yr=l(Fo,"\u{1F917} Accelerate repo"),Fo.forEach(s),Cr=l(ra,"\u627E\u5230\u66F4\u591A\u7684\u793A\u4F8B\u3002"),ra.forEach(s),this.h()},h(){g(b,"name","hf:doc:metadata"),g(b,"content",JSON.stringify(Xo)),g(z,"id",""),g(oe,"id",""),g(_e,"href","https://arxiv.org/abs/1711.05101"),g(_e,"rel","nofollow"),g(ve,"id",""),g(we,"id",""),g(K,"id","s-accelerate"),g(K,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),g(K,"href","#s-accelerate"),g(H,"class","relative group"),g(Ce,"href","https://github.com/huggingface/accelerate"),g(Ce,"rel","nofollow"),g(Te,"href","https://github.com/huggingface/accelerate/tree/main/examples"),g(Te,"rel","nofollow")},m(e,r){t(document.head,b),o(e,q,r),o(e,z,r),t(z,le),o(e,I,r),f(A,e,r),o(e,x,r),f(U,e,r),o(e,Jt,r),o(e,B,r),t(B,oa),t(B,Ze),t(Ze,na),t(B,pa),o(e,Yt,r),f(re,e,r),o(e,Qt,r),o(e,oe,r),t(oe,ia),o(e,Kt,r),o(e,W,r),t(W,ca),t(W,et),t(et,da),t(W,ma),t(W,tt),t(tt,ua),t(W,fa),o(e,Vt,r),o(e,M,r),t(M,N),t(N,_a),t(N,st),t(st,ha),t(N,$a),t(N,at),t(at,ba),t(N,va),t(M,ka),t(M,T),t(T,ja),t(T,lt),t(lt,Ea),t(T,wa),t(T,rt),t(rt,ga),t(T,za),t(T,ot),t(ot,qa),t(T,ya),t(M,Ca),t(M,nt),t(nt,Aa),o(e,Xt,r),o(e,R,r),t(R,Pa),t(R,pt),t(pt,Da),t(R,Oa),o(e,Zt,r),f(ne,e,r),o(e,es,r),o(e,Me,r),t(Me,Sa),o(e,ts,r),f(pe,e,r),o(e,ss,r),o(e,Fe,r),t(Fe,xa),o(e,as,r),f(ie,e,r),o(e,ls,r),o(e,Le,r),t(Le,Ta),o(e,rs,r),f(ce,e,r),o(e,os,r),f(de,e,r),o(e,ns,r),o(e,F,r),t(F,Ua),t(F,it),t(it,Wa),t(F,Ma),t(F,ct),t(ct,Fa),t(F,La),o(e,ps,r),o(e,Ge,r),t(Ge,Ga),o(e,is,r),f(me,e,r),o(e,cs,r),o(e,J,r),t(J,Na),t(J,dt),t(dt,Ha),t(J,Ia),o(e,ds,r),f(ue,e,r),o(e,ms,r),f(fe,e,r),o(e,us,r),o(e,j,r),t(j,Ba),t(j,mt),t(mt,Ra),t(j,Ja),t(j,ut),t(ut,Ya),t(j,Qa),t(j,ft),t(ft,Ka),t(j,Va),t(j,_t),t(_t,Xa),t(j,Za),t(j,ht),t(ht,el),t(j,tl),o(e,fs,r),o(e,E,r),t(E,sl),t(E,$t),t($t,al),t(E,ll),t(E,bt),t(bt,rl),t(E,ol),t(E,vt),t(vt,nl),t(E,pl),t(E,kt),t(kt,il),t(E,cl),t(E,_e),t(_e,dl),t(E,ml),o(e,_s,r),f(he,e,r),o(e,hs,r),o(e,L,r),t(L,ul),t(L,jt),t(jt,fl),t(L,_l),t(L,Et),t(Et,hl),t(L,$l),o(e,$s,r),f($e,e,r),o(e,bs,r),f(be,e,r),o(e,vs,r),o(e,ve,r),t(ve,bl),o(e,ks,r),o(e,P,r),t(P,vl),t(P,wt),t(wt,kl),t(P,jl),t(P,gt),t(gt,El),t(P,wl),t(P,zt),t(zt,gl),t(P,zl),o(e,js,r),f(ke,e,r),o(e,Es,r),f(je,e,r),o(e,ws,r),o(e,Y,r),t(Y,ql),t(Y,qt),t(qt,yl),t(Y,Cl),o(e,gs,r),f(Ee,e,r),o(e,zs,r),o(e,Ne,r),t(Ne,Al),o(e,qs,r),o(e,we,r),t(we,Pl),o(e,ys,r),o(e,w,r),t(w,Dl),t(w,yt),t(yt,Ol),t(w,Sl),t(w,Ct),t(Ct,xl),t(w,Tl),t(w,At),t(At,Ul),t(w,Wl),t(w,Pt),t(Pt,Ml),t(w,Fl),t(w,Dt),t(Dt,Ll),t(w,Gl),o(e,Cs,r),f(ge,e,r),o(e,As,r),f(ze,e,r),o(e,Ps,r),o(e,He,r),t(He,Nl),o(e,Ds,r),f(Q,e,r),o(e,Os,r),o(e,H,r),t(H,K),t(K,Ot),f(qe,Ot,null),t(H,Hl),t(H,St),t(St,Il),o(e,Ss,r),f(ye,e,r),o(e,xs,r),o(e,V,r),t(V,Bl),t(V,Ce),t(Ce,Rl),t(V,Jl),o(e,Ts,r),f(Ae,e,r),o(e,Us,r),o(e,Ie,r),t(Ie,Yl),o(e,Ws,r),f(Pe,e,r),o(e,Ms,r),o(e,y,r),t(y,Ql),t(y,xt),t(xt,Kl),t(y,Vl),t(y,Tt),t(Tt,Xl),t(y,Zl),t(y,Ut),t(Ut,er),t(y,tr),t(y,Wt),t(Wt,sr),t(y,ar),o(e,Fs,r),o(e,k,r),t(k,lr),t(k,Mt),t(Mt,rr),t(k,or),t(k,Ft),t(Ft,nr),t(k,pr),t(k,Lt),t(Lt,ir),t(k,cr),t(k,Gt),t(Gt,dr),t(k,mr),t(k,Nt),t(Nt,ur),t(k,fr),t(k,Ht),t(Ht,_r),t(k,hr),o(e,Ls,r),f(X,e,r),o(e,Gs,r),o(e,Be,r),t(Be,$r),o(e,Ns,r),f(De,e,r),o(e,Hs,r),o(e,Z,r),t(Z,br),t(Z,It),t(It,vr),t(Z,kr),o(e,Is,r),f(Oe,e,r),o(e,Bs,r),o(e,Re,r),t(Re,jr),o(e,Rs,r),f(Se,e,r),o(e,Js,r),o(e,Je,r),t(Je,Er),o(e,Ys,r),o(e,ee,r),t(ee,wr),t(ee,Bt),t(Bt,gr),t(ee,zr),o(e,Qs,r),f(xe,e,r),o(e,Ks,r),o(e,te,r),t(te,qr),t(te,Te),t(Te,yr),t(te,Cr),Vs=!0},p(e,[r]){const Rt={};r&2&&(Rt.$$scope={dirty:r,ctx:e}),Q.$set(Rt);const Ue={};r&2&&(Ue.$$scope={dirty:r,ctx:e}),X.$set(Ue)},i(e){Vs||(_(A.$$.fragment,e),_(U.$$.fragment,e),_(re.$$.fragment,e),_(ne.$$.fragment,e),_(pe.$$.fragment,e),_(ie.$$.fragment,e),_(ce.$$.fragment,e),_(de.$$.fragment,e),_(me.$$.fragment,e),_(ue.$$.fragment,e),_(fe.$$.fragment,e),_(he.$$.fragment,e),_($e.$$.fragment,e),_(be.$$.fragment,e),_(ke.$$.fragment,e),_(je.$$.fragment,e),_(Ee.$$.fragment,e),_(ge.$$.fragment,e),_(ze.$$.fragment,e),_(Q.$$.fragment,e),_(qe.$$.fragment,e),_(ye.$$.fragment,e),_(Ae.$$.fragment,e),_(Pe.$$.fragment,e),_(X.$$.fragment,e),_(De.$$.fragment,e),_(Oe.$$.fragment,e),_(Se.$$.fragment,e),_(xe.$$.fragment,e),Vs=!0)},o(e){h(A.$$.fragment,e),h(U.$$.fragment,e),h(re.$$.fragment,e),h(ne.$$.fragment,e),h(pe.$$.fragment,e),h(ie.$$.fragment,e),h(ce.$$.fragment,e),h(de.$$.fragment,e),h(me.$$.fragment,e),h(ue.$$.fragment,e),h(fe.$$.fragment,e),h(he.$$.fragment,e),h($e.$$.fragment,e),h(be.$$.fragment,e),h(ke.$$.fragment,e),h(je.$$.fragment,e),h(Ee.$$.fragment,e),h(ge.$$.fragment,e),h(ze.$$.fragment,e),h(Q.$$.fragment,e),h(qe.$$.fragment,e),h(ye.$$.fragment,e),h(Ae.$$.fragment,e),h(Pe.$$.fragment,e),h(X.$$.fragment,e),h(De.$$.fragment,e),h(Oe.$$.fragment,e),h(Se.$$.fragment,e),h(xe.$$.fragment,e),Vs=!1},d(e){s(b),e&&s(q),e&&s(z),e&&s(I),$(A,e),e&&s(x),$(U,e),e&&s(Jt),e&&s(B),e&&s(Yt),$(re,e),e&&s(Qt),e&&s(oe),e&&s(Kt),e&&s(W),e&&s(Vt),e&&s(M),e&&s(Xt),e&&s(R),e&&s(Zt),$(ne,e),e&&s(es),e&&s(Me),e&&s(ts),$(pe,e),e&&s(ss),e&&s(Fe),e&&s(as),$(ie,e),e&&s(ls),e&&s(Le),e&&s(rs),$(ce,e),e&&s(os),$(de,e),e&&s(ns),e&&s(F),e&&s(ps),e&&s(Ge),e&&s(is),$(me,e),e&&s(cs),e&&s(J),e&&s(ds),$(ue,e),e&&s(ms),$(fe,e),e&&s(us),e&&s(j),e&&s(fs),e&&s(E),e&&s(_s),$(he,e),e&&s(hs),e&&s(L),e&&s($s),$($e,e),e&&s(bs),$(be,e),e&&s(vs),e&&s(ve),e&&s(ks),e&&s(P),e&&s(js),$(ke,e),e&&s(Es),$(je,e),e&&s(ws),e&&s(Y),e&&s(gs),$(Ee,e),e&&s(zs),e&&s(Ne),e&&s(qs),e&&s(we),e&&s(ys),e&&s(w),e&&s(Cs),$(ge,e),e&&s(As),$(ze,e),e&&s(Ps),e&&s(He),e&&s(Ds),$(Q,e),e&&s(Os),e&&s(H),$(qe),e&&s(Ss),$(ye,e),e&&s(xs),e&&s(V),e&&s(Ts),$(Ae,e),e&&s(Us),e&&s(Ie),e&&s(Ws),$(Pe,e),e&&s(Ms),e&&s(y),e&&s(Fs),e&&s(k),e&&s(Ls),$(X,e),e&&s(Gs),e&&s(Be),e&&s(Ns),$(De,e),e&&s(Hs),e&&s(Z),e&&s(Is),$(Oe,e),e&&s(Bs),e&&s(Re),e&&s(Rs),$(Se,e),e&&s(Js),e&&s(Je),e&&s(Ys),e&&s(ee),e&&s(Qs),$(xe,e),e&&s(Ks),e&&s(te)}}}const Xo={local:"",sections:[{local:"",title:"\u8BAD\u7EC3\u524D\u7684\u51C6\u5907"},{local:"",title:"\u8BAD\u7EC3\u5FAA\u73AF"},{local:"",title:"\u8BC4\u4F30\u5FAA\u73AF"},{local:"s-accelerate",title:"S\u4F7F\u7528\u{1F917} Accelerate\u52A0\u901F\u60A8\u7684\u8BAD\u7EC3\u5FAA\u73AF"}],title:"\u4E00\u4E2A\u5B8C\u6574\u7684\u8BAD\u7EC3"};function Zo(We){return Ro(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class nn extends No{constructor(b){super();Ho(this,b,Zo,Vo,Io,{})}}export{nn as default,Xo as metadata};
