import{S as ba,i as va,s as ja,e as p,k as m,w,t as l,M as wa,c as n,d as e,m as c,x as k,a as f,h as o,b as A,G as t,g as r,y as q,q as P,o as y,B as C,v as ka}from"../../chunks/vendor-hf-doc-builder.js";import{T as Me}from"../../chunks/Tip-hf-doc-builder.js";import{Y as Oe}from"../../chunks/Youtube-hf-doc-builder.js";import{I as qa}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as M}from"../../chunks/CodeBlock-hf-doc-builder.js";import{D as Pa}from"../../chunks/DocNotebookDropdown-hf-doc-builder.js";import{F as ya}from"../../chunks/FrameworkSwitchCourse-hf-doc-builder.js";function Ca(F){let i,E,u,b,d;return{c(){i=p("p"),E=l("\u8BF7\u6CE8\u610F\u{1F917} Transformers \u6A21\u578B\u5177\u6709\u5927\u591A\u6570 Keras \u6A21\u578B\u6240\u6CA1\u6709\u7684\u7279\u6B8A\u80FD\u529B\u2014\u2014\u5B83\u4EEC\u53EF\u4EE5\u81EA\u52A8\u4F7F\u7528\u5185\u90E8\u8BA1\u7B97\u7684loss\u3002 \u5982\u679C\u60A8\u6CA1\u6709\u5728 "),u=p("code"),b=l("compile()"),d=l(" \u4E2D\u8BBE\u7F6E\u635F\u5931\u51FD\u6570\uFF0C\u4ED6\u4EEC\u5C06\u9ED8\u8BA4\u4F7F\u7528\u5185\u90E8\u8BA1\u7B97\u7684\u635F\u5931\u3002 \u8BF7\u6CE8\u610F\uFF0C\u8981\u4F7F\u7528\u5185\u90E8\u635F\u5931\uFF0C\u60A8\u9700\u8981\u5C06\u6807\u7B7E\u4F5C\u4E3A\u8F93\u5165\u7684\u4E00\u90E8\u5206\u4F20\u9012\uFF0C\u800C\u4E0D\u662F\u4F5C\u4E3A\u5355\u72EC\u7684\u6807\u7B7E\uFF08\u8FD9\u662F\u5728 Keras \u6A21\u578B\u4E2D\u4F7F\u7528\u6807\u7B7E\u7684\u6B63\u5E38\u65B9\u5F0F\uFF09\u3002 \u60A8\u5C06\u5728\u8BFE\u7A0B\u7684\u7B2C 2 \u90E8\u5206\u4E2D\u770B\u5230\u8FD9\u65B9\u9762\u7684\u793A\u4F8B\uFF0C\u5176\u4E2D\u5B9A\u4E49\u6B63\u786E\u7684\u635F\u5931\u51FD\u6570\u53EF\u80FD\u5F88\u68D8\u624B\u3002 \u7136\u800C\uFF0C\u5BF9\u4E8E\u5E8F\u5217\u5206\u7C7B\uFF0C\u6807\u51C6\u7684 Keras \u635F\u5931\u51FD\u6570\u53EF\u4EE5\u6B63\u5E38\u5DE5\u4F5C\uFF0C\u6240\u4EE5\u6211\u4EEC\u5C06\u5728\u8FD9\u91CC\u4F7F\u7528\u5B83\u3002")},l($){i=n($,"P",{});var h=f(i);E=o(h,"\u8BF7\u6CE8\u610F\u{1F917} Transformers \u6A21\u578B\u5177\u6709\u5927\u591A\u6570 Keras \u6A21\u578B\u6240\u6CA1\u6709\u7684\u7279\u6B8A\u80FD\u529B\u2014\u2014\u5B83\u4EEC\u53EF\u4EE5\u81EA\u52A8\u4F7F\u7528\u5185\u90E8\u8BA1\u7B97\u7684loss\u3002 \u5982\u679C\u60A8\u6CA1\u6709\u5728 "),u=n(h,"CODE",{});var v=f(u);b=o(v,"compile()"),v.forEach(e),d=o(h," \u4E2D\u8BBE\u7F6E\u635F\u5931\u51FD\u6570\uFF0C\u4ED6\u4EEC\u5C06\u9ED8\u8BA4\u4F7F\u7528\u5185\u90E8\u8BA1\u7B97\u7684\u635F\u5931\u3002 \u8BF7\u6CE8\u610F\uFF0C\u8981\u4F7F\u7528\u5185\u90E8\u635F\u5931\uFF0C\u60A8\u9700\u8981\u5C06\u6807\u7B7E\u4F5C\u4E3A\u8F93\u5165\u7684\u4E00\u90E8\u5206\u4F20\u9012\uFF0C\u800C\u4E0D\u662F\u4F5C\u4E3A\u5355\u72EC\u7684\u6807\u7B7E\uFF08\u8FD9\u662F\u5728 Keras \u6A21\u578B\u4E2D\u4F7F\u7528\u6807\u7B7E\u7684\u6B63\u5E38\u65B9\u5F0F\uFF09\u3002 \u60A8\u5C06\u5728\u8BFE\u7A0B\u7684\u7B2C 2 \u90E8\u5206\u4E2D\u770B\u5230\u8FD9\u65B9\u9762\u7684\u793A\u4F8B\uFF0C\u5176\u4E2D\u5B9A\u4E49\u6B63\u786E\u7684\u635F\u5931\u51FD\u6570\u53EF\u80FD\u5F88\u68D8\u624B\u3002 \u7136\u800C\uFF0C\u5BF9\u4E8E\u5E8F\u5217\u5206\u7C7B\uFF0C\u6807\u51C6\u7684 Keras \u635F\u5931\u51FD\u6570\u53EF\u4EE5\u6B63\u5E38\u5DE5\u4F5C\uFF0C\u6240\u4EE5\u6211\u4EEC\u5C06\u5728\u8FD9\u91CC\u4F7F\u7528\u5B83\u3002"),h.forEach(e)},m($,h){r($,i,h),t(i,E),t(i,u),t(u,b),t(i,d)},d($){$&&e(i)}}}function ga(F){let i,E,u,b,d,$,h,v;return{c(){i=p("p"),E=l("\u8BF7\u6CE8\u610F\u8FD9\u91CC\u6709\u4E00\u4E2A\u975E\u5E38\u5E38\u89C1\u7684\u9677\u9631\u2014\u2014\u4F60\u53EA\u662F"),u=p("em"),b=l("\u53EF\u4EE5"),d=l("\u5C06\u635F\u5931\u7684\u540D\u79F0\u4F5C\u4E3A\u5B57\u7B26\u4E32\u4F20\u9012\u7ED9 Keras\uFF0C\u4F46\u9ED8\u8BA4\u60C5\u51B5\u4E0B\uFF0CKeras \u4F1A\u5047\u8BBE\u4F60\u5DF2\u7ECF\u5BF9\u8F93\u51FA\u5E94\u7528\u4E86 softmax\u3002 \u7136\u800C\uFF0C\u8BB8\u591A\u6A21\u578B\u5728\u5E94\u7528 softmax \u4E4B\u524D\u5C31\u8F93\u51FA\uFF0C\u4E5F\u79F0\u4E3A "),$=p("em"),h=l("logits"),v=l("\u3002 \u6211\u4EEC\u9700\u8981\u544A\u8BC9\u635F\u5931\u51FD\u6570\u6211\u4EEC\u7684\u6A21\u578B\u662F\u5426\u7ECF\u8FC7\u4E86softmax\uFF0C\u552F\u4E00\u7684\u65B9\u6CD5\u662F\u76F4\u63A5\u8C03\u7528\u5B83\uFF0C\u800C\u4E0D\u662F\u7528\u5B57\u7B26\u4E32\u7684\u540D\u79F0\u3002")},l(j){i=n(j,"P",{});var _=f(i);E=o(_,"\u8BF7\u6CE8\u610F\u8FD9\u91CC\u6709\u4E00\u4E2A\u975E\u5E38\u5E38\u89C1\u7684\u9677\u9631\u2014\u2014\u4F60\u53EA\u662F"),u=n(_,"EM",{});var K=f(u);b=o(K,"\u53EF\u4EE5"),K.forEach(e),d=o(_,"\u5C06\u635F\u5931\u7684\u540D\u79F0\u4F5C\u4E3A\u5B57\u7B26\u4E32\u4F20\u9012\u7ED9 Keras\uFF0C\u4F46\u9ED8\u8BA4\u60C5\u51B5\u4E0B\uFF0CKeras \u4F1A\u5047\u8BBE\u4F60\u5DF2\u7ECF\u5BF9\u8F93\u51FA\u5E94\u7528\u4E86 softmax\u3002 \u7136\u800C\uFF0C\u8BB8\u591A\u6A21\u578B\u5728\u5E94\u7528 softmax \u4E4B\u524D\u5C31\u8F93\u51FA\uFF0C\u4E5F\u79F0\u4E3A "),$=n(_,"EM",{});var T=f($);h=o(T,"logits"),T.forEach(e),v=o(_,"\u3002 \u6211\u4EEC\u9700\u8981\u544A\u8BC9\u635F\u5931\u51FD\u6570\u6211\u4EEC\u7684\u6A21\u578B\u662F\u5426\u7ECF\u8FC7\u4E86softmax\uFF0C\u552F\u4E00\u7684\u65B9\u6CD5\u662F\u76F4\u63A5\u8C03\u7528\u5B83\uFF0C\u800C\u4E0D\u662F\u7528\u5B57\u7B26\u4E32\u7684\u540D\u79F0\u3002"),_.forEach(e)},m(j,_){r(j,i,_),t(i,E),t(i,u),t(u,b),t(i,d),t(i,$),t($,h),t(i,v)},d(j){j&&e(i)}}}function Ta(F){let i,E,u,b,d,$,h,v;return{c(){i=p("p"),E=l("\u{1F917} Transformers \u5E93\u8FD8\u6709\u4E00\u4E2A "),u=p("code"),b=l("create_optimizer()"),d=l(" \u51FD\u6570\uFF0C\u5B83\u5C06\u521B\u5EFA\u4E00\u4E2A\u5177\u6709\u5B66\u4E60\u7387\u8870\u51CF\u7684 "),$=p("code"),h=l("AdamW"),v=l(" \u4F18\u5316\u5668\u3002 \u8FD9\u662F\u4E00\u4E2A\u4FBF\u6377\u7684\u65B9\u5F0F\uFF0C\u60A8\u5C06\u5728\u672C\u8BFE\u7A0B\u7684\u540E\u7EED\u90E8\u5206\u4E2D\u8BE6\u7EC6\u4E86\u89E3\u3002")},l(j){i=n(j,"P",{});var _=f(i);E=o(_,"\u{1F917} Transformers \u5E93\u8FD8\u6709\u4E00\u4E2A "),u=n(_,"CODE",{});var K=f(u);b=o(K,"create_optimizer()"),K.forEach(e),d=o(_," \u51FD\u6570\uFF0C\u5B83\u5C06\u521B\u5EFA\u4E00\u4E2A\u5177\u6709\u5B66\u4E60\u7387\u8870\u51CF\u7684 "),$=n(_,"CODE",{});var T=f($);h=o(T,"AdamW"),T.forEach(e),v=o(_," \u4F18\u5316\u5668\u3002 \u8FD9\u662F\u4E00\u4E2A\u4FBF\u6377\u7684\u65B9\u5F0F\uFF0C\u60A8\u5C06\u5728\u672C\u8BFE\u7A0B\u7684\u540E\u7EED\u90E8\u5206\u4E2D\u8BE6\u7EC6\u4E86\u89E3\u3002"),_.forEach(e)},m(j,_){r(j,i,_),t(i,E),t(i,u),t(u,b),t(i,d),t(i,$),t($,h),t(i,v)},d(j){j&&e(i)}}}function Aa(F){let i,E,u,b,d,$,h,v,j,_,K;return{c(){i=p("p"),E=l("\u{1F4A1} \u5982\u679C\u60A8\u60F3\u5728\u8BAD\u7EC3\u671F\u95F4\u81EA\u52A8\u5C06\u6A21\u578B\u4E0A\u4F20\u5230 Hub\uFF0C\u60A8\u53EF\u4EE5\u5728 "),u=p("code"),b=l("model.fit()"),d=l(" \u65B9\u6CD5\u4E2D\u4F20\u9012 "),$=p("code"),h=l("PushToHubCallback"),v=l("\u3002 \u6211\u4EEC\u5C06\u5728 "),j=p("a"),_=l("\u7B2C\u56DB\u7AE0"),K=l(" \u4E2D\u8FDB\u884C\u4ECB\u7ECD"),this.h()},l(T){i=n(T,"P",{});var g=f(i);E=o(g,"\u{1F4A1} \u5982\u679C\u60A8\u60F3\u5728\u8BAD\u7EC3\u671F\u95F4\u81EA\u52A8\u5C06\u6A21\u578B\u4E0A\u4F20\u5230 Hub\uFF0C\u60A8\u53EF\u4EE5\u5728 "),u=n(g,"CODE",{});var Q=f(u);b=o(Q,"model.fit()"),Q.forEach(e),d=o(g," \u65B9\u6CD5\u4E2D\u4F20\u9012 "),$=n(g,"CODE",{});var z=f($);h=o(z,"PushToHubCallback"),z.forEach(e),v=o(g,"\u3002 \u6211\u4EEC\u5C06\u5728 "),j=n(g,"A",{href:!0});var bs=f(j);_=o(bs,"\u7B2C\u56DB\u7AE0"),bs.forEach(e),K=o(g," \u4E2D\u8FDB\u884C\u4ECB\u7ECD"),g.forEach(e),this.h()},h(){A(j,"href","/course/chapter4/3")},m(T,g){r(T,i,g),t(i,E),t(i,u),t(u,b),t(i,d),t(i,$),t($,h),t(i,v),t(i,j),t(j,_),t(i,K)},d(T){T&&e(i)}}}function Da(F){let i,E,u,b,d,$,h,v,j,_,K,T,g,Q,z,bs,Ks,Ue,Ge,X,He,Re,Qs,vs,Le,Xs,Z,Zs,ss,Be,se,js,Ne,ee,es,te,ws,Ie,ae,ts,le,O,We,ks,Ye,Je,zs,Ve,Qe,oe,as,re,L,Xe,qs,Ze,st,pe,U,et,xs,tt,at,Ss,lt,ot,ne,B,fe,ls,ie,N,me,os,rt,ce,rs,ue,I,pt,Fs,nt,ft,$e,D,it,Ms,mt,ct,Os,ut,$t,Us,_t,dt,Gs,ht,Et,_e,ps,de,W,he,Ps,bt,Ee,ns,be,ys,vt,ve,fs,je,Y,we,is,jt,ke,ms,qe,G,wt,Hs,kt,qt,Rs,Pt,yt,Pe,cs,ye,Cs,Ct,Ce,us,ge,$s,Te,x,gt,Ls,Tt,At,Bs,Dt,Kt,Ns,zt,xt,Ae,_s,De,ds,Ke,S,St,hs,Ft,Mt,Is,Ot,Ut,Ws,Gt,Ht,ze,gs,Rt,xe;return u=new ya({props:{fw:F[0]}}),v=new qa({}),g=new Pa({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Google Colab",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/chapter3/section3_tf.ipynb"},{label:"Aws Studio",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/chapter3/section3_tf.ipynb"}]}}),Z=new M({props:{code:`






`,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
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

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, return_tensors=<span class="hljs-string">&quot;tf&quot;</span>)

tf_train_dataset = tokenized_datasets[<span class="hljs-string">&quot;train&quot;</span>].to_tf_dataset(
    columns=[<span class="hljs-string">&quot;attention_mask&quot;</span>, <span class="hljs-string">&quot;input_ids&quot;</span>, <span class="hljs-string">&quot;token_type_ids&quot;</span>],
    label_cols=[<span class="hljs-string">&quot;labels&quot;</span>],
    shuffle=<span class="hljs-literal">True</span>,
    collate_fn=data_collator,
    batch_size=<span class="hljs-number">8</span>,
)

tf_validation_dataset = tokenized_datasets[<span class="hljs-string">&quot;validation&quot;</span>].to_tf_dataset(
    columns=[<span class="hljs-string">&quot;attention_mask&quot;</span>, <span class="hljs-string">&quot;input_ids&quot;</span>, <span class="hljs-string">&quot;token_type_ids&quot;</span>],
    label_cols=[<span class="hljs-string">&quot;labels&quot;</span>],
    shuffle=<span class="hljs-literal">False</span>,
    collate_fn=data_collator,
    batch_size=<span class="hljs-number">8</span>,
)`}}),es=new Oe({props:{id:"rnTGBy2ax1c"}}),ts=new Oe({props:{id:"AUozVp78dhk"}}),as=new M({props:{code:"",highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> TFAutoModelForSequenceClassification

model = TFAutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=<span class="hljs-number">2</span>)`}}),B=new Me({props:{$$slots:{default:[Ca]},$$scope:{ctx:F}}}),ls=new M({props:{code:"",highlighted:`<span class="hljs-keyword">from</span> tensorflow.keras.losses <span class="hljs-keyword">import</span> SparseCategoricalCrossentropy

model.<span class="hljs-built_in">compile</span>(
    optimizer=<span class="hljs-string">&quot;adam&quot;</span>,
    loss=SparseCategoricalCrossentropy(from_logits=<span class="hljs-literal">True</span>),
    metrics=[<span class="hljs-string">&quot;accuracy&quot;</span>],
)
model.fit(
    tf_train_dataset,
    validation_data=tf_validation_dataset,
)`}}),N=new Me({props:{warning:!0,$$slots:{default:[ga]},$$scope:{ctx:F}}}),rs=new Oe({props:{id:"cpzq6ESSM5c"}}),ps=new M({props:{code:`
`,highlighted:`<span class="hljs-keyword">from</span> tensorflow.keras.optimizers.schedules <span class="hljs-keyword">import</span> PolynomialDecay

batch_size = <span class="hljs-number">8</span>
num_epochs = <span class="hljs-number">3</span>
<span class="hljs-comment"># \u8BAD\u7EC3\u6B65\u6570\u662F\u6570\u636E\u96C6\u4E2D\u7684\u6837\u672C\u6570\u9664\u4EE5batch size\u518D\u4E58\u4EE5 epoch\u3002</span>
<span class="hljs-comment"># \u6CE8\u610F\u8FD9\u91CC\u7684tf_train_dataset\u662F\u4E00\u4E2A\u8F6C\u5316\u4E3Abatch\u540E\u7684 tf.data.Dataset\uFF0C</span>
<span class="hljs-comment"># \u4E0D\u662F\u539F\u6765\u7684 Hugging Face Dataset\uFF0C\u6240\u4EE5\u5B83\u7684 len() \u5DF2\u7ECF\u662F num_samples // batch_size\u3002</span>
num_train_steps = <span class="hljs-built_in">len</span>(tf_train_dataset) * num_epochs
lr_scheduler = PolynomialDecay(
    initial_learning_rate=<span class="hljs-number">5e-5</span>, end_learning_rate=<span class="hljs-number">0.0</span>, decay_steps=num_train_steps
)
<span class="hljs-keyword">from</span> tensorflow.keras.optimizers <span class="hljs-keyword">import</span> Adam

opt = Adam(learning_rate=lr_scheduler)`}}),W=new Me({props:{$$slots:{default:[Ta]},$$scope:{ctx:F}}}),ns=new M({props:{code:"",highlighted:`<span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf

model = TFAutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=<span class="hljs-number">2</span>)
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=<span class="hljs-literal">True</span>)
model.<span class="hljs-built_in">compile</span>(optimizer=opt, loss=loss, metrics=[<span class="hljs-string">&quot;accuracy&quot;</span>])`}}),fs=new M({props:{code:"model.fit(tf_train_dataset, validation_data=tf_validation_dataset, epochs=3)",highlighted:'model.fit(tf_train_dataset, validation_data=tf_validation_dataset, epochs=<span class="hljs-number">3</span>)'}}),Y=new Me({props:{$$slots:{default:[Aa]},$$scope:{ctx:F}}}),ms=new Oe({props:{id:"nx10eh4CoOs"}}),cs=new M({props:{code:'preds = model.predict(tf_validation_dataset)["logits"]',highlighted:'preds = model.predict(tf_validation_dataset)[<span class="hljs-string">&quot;logits&quot;</span>]'}}),us=new M({props:{code:`class_preds = np.argmax(preds, axis=1)
print(preds.shape, class_preds.shape)`,highlighted:`class_preds = np.argmax(preds, axis=<span class="hljs-number">1</span>)
<span class="hljs-built_in">print</span>(preds.shape, class_preds.shape)`}}),$s=new M({props:{code:"(408, 2) (408,)",highlighted:'(<span class="hljs-number">408</span>, <span class="hljs-number">2</span>) (<span class="hljs-number">408</span>,)'}}),_s=new M({props:{code:"",highlighted:`<span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_metric

metric = load_metric(<span class="hljs-string">&quot;glue&quot;</span>, <span class="hljs-string">&quot;mrpc&quot;</span>)
metric.compute(predictions=class_preds, references=raw_datasets[<span class="hljs-string">&quot;validation&quot;</span>][<span class="hljs-string">&quot;label&quot;</span>])`}}),ds=new M({props:{code:"{'accuracy': 0.8578431372549019, 'f1': 0.8996539792387542}",highlighted:'{<span class="hljs-string">&#x27;accuracy&#x27;</span>: <span class="hljs-number">0.8578431372549019</span>, <span class="hljs-string">&#x27;f1&#x27;</span>: <span class="hljs-number">0.8996539792387542</span>}'}}),{c(){i=p("meta"),E=m(),w(u.$$.fragment),b=m(),d=p("h1"),$=p("a"),h=p("span"),w(v.$$.fragment),j=m(),_=p("span"),K=l("\u4F7F\u7528 Keras \u5FAE\u8C03\u4E00\u4E2A\u6A21\u578B"),T=m(),w(g.$$.fragment),Q=m(),z=p("p"),bs=l("\u5B8C\u6210\u4E0A\u4E00\u8282\u4E2D\u7684\u6240\u6709\u6570\u636E\u9884\u5904\u7406\u5DE5\u4F5C\u540E\uFF0C\u60A8\u53EA\u5269\u4E0B\u6700\u540E\u7684\u51E0\u4E2A\u6B65\u9AA4\u6765\u8BAD\u7EC3\u6A21\u578B\u3002 \u4F46\u662F\u8BF7\u6CE8\u610F\uFF0C"),Ks=p("code"),Ue=l("model.fit()"),Ge=l(" \u547D\u4EE4\u5728 CPU \u4E0A\u8FD0\u884C\u4F1A\u975E\u5E38\u7F13\u6162\u3002 \u5982\u679C\u60A8\u6CA1\u6709GPU\uFF0C\u5219\u53EF\u4EE5\u5728 "),X=p("a"),He=l("Google Colab"),Re=l(" \u4E0A\u4F7F\u7528\u514D\u8D39\u7684 GPU \u6216 TPU(\u9700\u8981\u68AF\u5B50)\u3002"),Qs=m(),vs=p("p"),Le=l("\u8FD9\u4E00\u8282\u7684\u4EE3\u7801\u793A\u4F8B\u5047\u8BBE\u60A8\u5DF2\u7ECF\u6267\u884C\u4E86\u4E0A\u4E00\u8282\u4E2D\u7684\u4EE3\u7801\u793A\u4F8B\u3002 \u4E0B\u9762\u4E00\u4E2A\u7B80\u77ED\u7684\u6458\u8981\uFF0C\u5305\u542B\u4E86\u5728\u5F00\u59CB\u5B66\u4E60\u8FD9\u4E00\u8282\u4E4B\u524D\u60A8\u9700\u8981\u7684\u6267\u884C\u7684\u4EE3\u7801\uFF1A"),Xs=m(),w(Z.$$.fragment),Zs=m(),ss=p("h3"),Be=l("\u8BAD\u7EC3\u6A21\u578B"),se=m(),js=p("p"),Ne=l("\u4ECE\u{1F917} Transformers \u5BFC\u5165\u7684 TensorFlow \u6A21\u578B\u5DF2\u7ECF\u662F Keras \u6A21\u578B\u3002 \u4E0B\u9762\u7684\u89C6\u9891\u662F\u5BF9 Keras \u7684\u7B80\u77ED\u4ECB\u7ECD\u3002"),ee=m(),w(es.$$.fragment),te=m(),ws=p("p"),Ie=l("\u8FD9\u610F\u5473\u7740\uFF0C\u4E00\u65E6\u6211\u4EEC\u6709\u4E86\u6570\u636E\uFF0C\u5C31\u9700\u8981\u5F88\u5C11\u7684\u5DE5\u4F5C\u5C31\u53EF\u4EE5\u5F00\u59CB\u5BF9\u5176\u8FDB\u884C\u8BAD\u7EC3\u3002"),ae=m(),w(ts.$$.fragment),le=m(),O=p("p"),We=l("\u548C"),ks=p("a"),Ye=l("\u7B2C\u4E8C\u7AE0"),Je=l("\u4F7F\u7528\u7684\u65B9\u6CD5\u4E00\u6837, \u6211\u4EEC\u5C06\u4F7F\u7528\u4E8C\u5206\u7C7B\u7684 "),zs=p("code"),Ve=l("TFAutoModelForSequenceClassification"),Qe=l("\u7C7B:"),oe=m(),w(as.$$.fragment),re=m(),L=p("p"),Xe=l("\u60A8\u4F1A\u6CE8\u610F\u5230\uFF0C\u4E0E "),qs=p("a"),Ze=l("\u7B2C\u4E8C\u7AE0"),st=l(" \u4E0D\u540C\u7684\u662F\uFF0C\u60A8\u5728\u5B9E\u4F8B\u5316\u6B64\u9884\u8BAD\u7EC3\u6A21\u578B\u540E\u4F1A\u6536\u5230\u8B66\u544A\u3002 \u8FD9\u662F\u56E0\u4E3A BERT \u6CA1\u6709\u5BF9\u53E5\u5B50\u5BF9\u8FDB\u884C\u5206\u7C7B\u8FDB\u884C\u9884\u8BAD\u7EC3\uFF0C\u6240\u4EE5\u9884\u8BAD\u7EC3\u6A21\u578B\u7684 head \u5DF2\u7ECF\u88AB\u4E22\u5F03\uFF0C\u800C\u662F\u63D2\u5165\u4E86\u4E00\u4E2A\u9002\u5408\u5E8F\u5217\u5206\u7C7B\u7684\u65B0 head\u3002 \u8B66\u544A\u8868\u660E\u4E00\u4E9B\u6743\u91CD\u6CA1\u6709\u4F7F\u7528\uFF08\u5BF9\u5E94\u4E8E\u4E22\u5F03\u7684\u9884\u8BAD\u7EC3\u5934\uFF09\uFF0C\u800C\u5176\u4ED6\u4E00\u4E9B\u6743\u91CD\u662F\u968F\u673A\u521D\u59CB\u5316\u7684\uFF08\u65B0\u5934\u7684\u6743\u91CD\uFF09\u3002 \u6700\u540E\u9F13\u52B1\u60A8\u8BAD\u7EC3\u6A21\u578B\uFF0C\u8FD9\u6B63\u662F\u6211\u4EEC\u73B0\u5728\u8981\u505A\u7684\u3002"),pe=m(),U=p("p"),et=l("\u8981\u5728\u6211\u4EEC\u7684\u6570\u636E\u96C6\u4E0A\u5FAE\u8C03\u6A21\u578B\uFF0C\u6211\u4EEC\u53EA\u9700\u8981\u5728\u6211\u4EEC\u7684\u6A21\u578B\u4E0A\u8C03\u7528 "),xs=p("code"),tt=l("compile()"),at=l(" \u65B9\u6CD5\uFF0C\u7136\u540E\u5C06\u6211\u4EEC\u7684\u6570\u636E\u4F20\u9012\u7ED9 "),Ss=p("code"),lt=l("fit()"),ot=l(" \u65B9\u6CD5\u3002 \u8FD9\u5C06\u542F\u52A8\u5FAE\u8C03\u8FC7\u7A0B\uFF08\u5728 GPU \u4E0A\u5E94\u8BE5\u9700\u8981\u51E0\u5206\u949F\uFF09\u5E76\u8F93\u51FA\u8BAD\u7EC3loss\uFF0C\u4EE5\u53CA\u6BCF\u4E2A epoch \u7ED3\u675F\u65F6\u7684\u9A8C\u8BC1loss\u3002"),ne=m(),w(B.$$.fragment),fe=m(),w(ls.$$.fragment),ie=m(),w(N.$$.fragment),me=m(),os=p("h3"),rt=l("\u63D0\u5347\u8BAD\u7EC3\u7684\u6548\u679C"),ce=m(),w(rs.$$.fragment),ue=m(),I=p("p"),pt=l("\u5982\u679C\u60A8\u5C1D\u8BD5\u4E0A\u9762\u7684\u4EE3\u7801\uFF0C\u5B83\u80AF\u5B9A\u4F1A\u8FD0\u884C\uFF0C\u4F46\u60A8\u4F1A\u53D1\u73B0loss\u53EA\u662F\u7F13\u6162\u6216\u96F6\u661F\u5730\u4E0B\u964D\u3002 \u4E3B\u8981\u539F\u56E0\u662F"),Fs=p("em"),nt=l("\u5B66\u4E60\u7387"),ft=l("\u3002 \u4E0Eloss\u4E00\u6837\uFF0C\u5F53\u6211\u4EEC\u5C06\u4F18\u5316\u5668\u7684\u540D\u79F0\u4F5C\u4E3A\u5B57\u7B26\u4E32\u4F20\u9012\u7ED9 Keras \u65F6\uFF0CKeras \u4F1A\u521D\u59CB\u5316\u8BE5\u4F18\u5316\u5668\u5177\u6709\u6240\u6709\u53C2\u6570\u7684\u9ED8\u8BA4\u503C\uFF0C\u5305\u62EC\u5B66\u4E60\u7387\u3002 \u4F46\u662F\uFF0C\u6839\u636E\u957F\u671F\u7ECF\u9A8C\uFF0C\u6211\u4EEC\u77E5\u9053Transformer \u6A21\u578B\u66F4\u9002\u5408\u4F7F\u7528\u6BD4 Adam \u7684\u9ED8\u8BA4\u503C\uFF081e-3\uFF09\u4E5F\u5199\u6210\u4E3A 10 \u7684 -3 \u6B21\u65B9\uFF0C\u6216 0.001\uFF0C\u4F4E\u5F97\u591A\u7684\u5B66\u4E60\u7387\u3002 5e-5 (0.00005) \u6BD4\u9ED8\u8BA4\u503C\u5927\u7EA6\u4F4E 20 \u500D\uFF0C\u662F\u4E00\u4E2A\u66F4\u597D\u7684\u8D77\u70B9\u3002"),$e=m(),D=p("p"),it=l("\u9664\u4E86\u964D\u4F4E\u5B66\u4E60\u7387\uFF0C\u6211\u4EEC\u8FD8\u6709\u7B2C\u4E8C\u4E2A\u6280\u5DE7\uFF1A\u6211\u4EEC\u53EF\u4EE5\u6162\u6162\u964D\u4F4E\u5B66\u4E60\u7387\u3002\u5728\u8BAD\u7EC3\u8FC7\u7A0B\u4E2D\u3002 \u5728\u6587\u732E\u4E2D\uFF0C\u60A8\u6709\u65F6\u4F1A\u770B\u5230\u8FD9\u88AB\u79F0\u4E3A "),Ms=p("em"),mt=l("decaying"),ct=l(" \u6216 "),Os=p("em"),ut=l("annealing"),$t=l("\u5B66\u4E60\u7387\u3002 \u5728 Keras \u4E2D\uFF0C\u6700\u597D\u7684\u65B9\u6CD5\u662F\u4F7F\u7528 "),Us=p("em"),_t=l("learning rate scheduler"),dt=l("\u3002 \u4E00\u4E2A\u597D\u7528\u7684\u662F"),Gs=p("code"),ht=l("PolynomialDecay"),Et=l("\u2014\u2014\u5C3D\u7BA1\u6709\u8FD9\u4E2A\u540D\u5B57\uFF0C\u4F46\u5728\u9ED8\u8BA4\u8BBE\u7F6E\u4E0B\uFF0C\u5B83\u53EA\u662F\u7B80\u5355\u5730\u4ECE\u521D\u59CB\u503C\u7EBF\u6027\u8870\u51CF\u5B66\u4E60\u7387\u503C\u5728\u8BAD\u7EC3\u8FC7\u7A0B\u4E2D\u7684\u6700\u7EC8\u503C\uFF0C\u8FD9\u6B63\u662F\u6211\u4EEC\u60F3\u8981\u7684\u3002\u4F46\u662F\uFF0C \u4E3A\u4E86\u6B63\u786E\u4F7F\u7528\u8C03\u5EA6\u7A0B\u5E8F\uFF0C\u6211\u4EEC\u9700\u8981\u544A\u8BC9\u5B83\u8BAD\u7EC3\u7684\u6B21\u6570\u3002 \u6211\u4EEC\u5C06\u5728\u4E0B\u9762\u4E3A\u5176\u8BA1\u7B97\u201Cnum_train_steps\u201D\u3002"),_e=m(),w(ps.$$.fragment),de=m(),w(W.$$.fragment),he=m(),Ps=p("p"),bt=l("\u73B0\u5728\u6211\u4EEC\u6709\u4E86\u5168\u65B0\u7684\u4F18\u5316\u5668\uFF0C\u6211\u4EEC\u53EF\u4EE5\u5C1D\u8BD5\u4F7F\u7528\u5B83\u8FDB\u884C\u8BAD\u7EC3\u3002 \u9996\u5148\uFF0C\u8BA9\u6211\u4EEC\u91CD\u65B0\u52A0\u8F7D\u6A21\u578B\uFF0C\u4EE5\u91CD\u7F6E\u6211\u4EEC\u521A\u521A\u8FDB\u884C\u7684\u8BAD\u7EC3\u8FD0\u884C\u5BF9\u6743\u91CD\u7684\u66F4\u6539\uFF0C\u7136\u540E\u6211\u4EEC\u53EF\u4EE5\u4F7F\u7528\u65B0\u7684\u4F18\u5316\u5668\u5BF9\u5176\u8FDB\u884C\u7F16\u8BD1\uFF1A"),Ee=m(),w(ns.$$.fragment),be=m(),ys=p("p"),vt=l("\u73B0\u5728\uFF0C\u6211\u4EEC\u518D\u6B21\u8FDB\u884Cfit\uFF1A"),ve=m(),w(fs.$$.fragment),je=m(),w(Y.$$.fragment),we=m(),is=p("h3"),jt=l("\u6A21\u578B\u9884\u6D4B"),ke=m(),w(ms.$$.fragment),qe=m(),G=p("p"),wt=l("\u8BAD\u7EC3\u548C\u89C2\u5BDF\u7684loss\u4E0B\u964D\u90FD\u975E\u5E38\u597D\uFF0C\u4F46\u662F\u5982\u679C\u6211\u4EEC\u60F3\u4ECE\u8BAD\u7EC3\u540E\u7684\u6A21\u578B\u4E2D\u83B7\u5F97\u8F93\u51FA\uFF0C\u6216\u8005\u8BA1\u7B97\u4E00\u4E9B\u6307\u6807\uFF0C\u6216\u8005\u5728\u751F\u4EA7\u4E2D\u4F7F\u7528\u6A21\u578B\u5462\uFF1F \u4E3A\u6B64\uFF0C\u6211\u4EEC\u53EF\u4EE5\u4F7F\u7528"),Hs=p("code"),kt=l("predict()"),qt=l(" \u65B9\u6CD5\u3002 \u8FD9\u5C06\u8FD4\u56DE\u6A21\u578B\u7684\u8F93\u51FA\u5934\u7684"),Rs=p("em"),Pt=l("logits"),yt=l("\u6570\u503C\uFF0C\u6BCF\u4E2A\u7C7B\u4E00\u4E2A\u3002"),Pe=m(),w(cs.$$.fragment),ye=m(),Cs=p("p"),Ct=l("\u6211\u4EEC\u53EF\u4EE5\u5C06\u8FD9\u4E9B logit \u8F6C\u6362\u4E3A\u6A21\u578B\u7684\u7C7B\u522B\u9884\u6D4B\uFF0C\u65B9\u6CD5\u662F\u4F7F\u7528 argmax \u627E\u5230\u6700\u9AD8\u7684 logit\uFF0C\u5B83\u5BF9\u5E94\u4E8E\u6700\u6709\u53EF\u80FD\u7684\u7C7B\u522B\uFF1A"),Ce=m(),w(us.$$.fragment),ge=m(),w($s.$$.fragment),Te=m(),x=p("p"),gt=l("\u73B0\u5728\uFF0C\u8BA9\u6211\u4EEC\u4F7F\u7528\u8FD9\u4E9B "),Ls=p("code"),Tt=l("preds"),At=l(" \u6765\u8BA1\u7B97\u4E00\u4E9B\u6307\u6807\uFF01 \u6211\u4EEC\u53EF\u4EE5\u50CF\u52A0\u8F7D\u6570\u636E\u96C6\u4E00\u6837\u8F7B\u677E\u5730\u52A0\u8F7D\u4E0E MRPC \u6570\u636E\u96C6\u76F8\u5173\u7684\u6307\u6807\uFF0C\u8FD9\u6B21\u4F7F\u7528\u7684\u662F "),Bs=p("code"),Dt=l("load_metric()"),Kt=l(" \u51FD\u6570\u3002 \u8FD4\u56DE\u7684\u5BF9\u8C61\u6709\u4E00\u4E2A "),Ns=p("code"),zt=l("compute()"),xt=l(" \u65B9\u6CD5\uFF0C\u6211\u4EEC\u53EF\u4EE5\u4F7F\u7528\u5B83\u6765\u8FDB\u884C\u5EA6\u91CF\u8BA1\u7B97\uFF1A"),Ae=m(),w(_s.$$.fragment),De=m(),w(ds.$$.fragment),Ke=m(),S=p("p"),St=l("\u60A8\u83B7\u5F97\u7684\u786E\u5207\u7ED3\u679C\u53EF\u80FD\u4F1A\u6709\u6240\u4E0D\u540C\uFF0C\u56E0\u4E3A\u6A21\u578B\u5934\u7684\u968F\u673A\u521D\u59CB\u5316\u53EF\u80FD\u4F1A\u6539\u53D8\u5B83\u83B7\u5F97\u7684\u6307\u6807\u3002 \u5728\u8FD9\u91CC\uFF0C\u6211\u4EEC\u53EF\u4EE5\u770B\u5230\u6211\u4EEC\u7684\u6A21\u578B\u5728\u9A8C\u8BC1\u96C6\u4E0A\u7684\u51C6\u786E\u7387\u4E3A 85.78%\uFF0CF1 \u5F97\u5206\u4E3A 89.97\u3002 \u8FD9\u4E9B\u662F\u7528\u4E8E\u8BC4\u4F30 GLUE \u57FA\u51C6\u7684 MRPC \u6570\u636E\u96C6\u7ED3\u679C\u7684\u4E24\u4E2A\u6307\u6807\u3002 "),hs=p("a"),Ft=l("BERT \u8BBA\u6587"),Mt=l(" \u4E2D\u7684\u8868\u683C\u62A5\u544A\u4E86\u57FA\u672C\u6A21\u578B\u7684 F1 \u5206\u6570\u4E3A 88.9\u3002 \u90A3\u662F "),Is=p("code"),Ot=l("uncased"),Ut=l(" \u6A21\u578B\uFF0C\u800C\u6211\u4EEC\u76EE\u524D\u4F7F\u7528\u7684\u662F "),Ws=p("code"),Gt=l("cased"),Ht=l(" \u6A21\u578B\uFF0C\u8FD9\u89E3\u91CA\u4E86\u4E3A\u4EC0\u4E48\u6211\u4EEC\u4F1A\u83B7\u5F97\u66F4\u597D\u7684\u7ED3\u679C\u3002"),ze=m(),gs=p("p"),Rt=l("\u4F7F\u7528 Keras API \u8FDB\u884C\u5FAE\u8C03\u7684\u4ECB\u7ECD\u5230\u6B64\u7ED3\u675F\u3002 \u7B2C 7 \u7AE0\u5C06\u7ED9\u51FA\u5BF9\u5927\u591A\u6570\u5E38\u89C1 NLP \u4EFB\u52A1\u6267\u884C\u6B64\u64CD\u4F5C\u7684\u793A\u4F8B\u3002\u5982\u679C\u60A8\u60F3\u5728 Keras API \u4E0A\u78E8\u7EC3\u81EA\u5DF1\u7684\u6280\u80FD\uFF0C\u8BF7\u5C1D\u8BD5\u4F7F\u7B2C\u4E8C\u8282\u6240\u4F7F\u7528\u7684\u7684\u6570\u636E\u5904\u7406\u5728 GLUE SST-2 \u6570\u636E\u96C6\u4E0A\u5FAE\u8C03\u6A21\u578B\u3002"),this.h()},l(s){const a=wa('[data-svelte="svelte-1phssyn"]',document.head);i=n(a,"META",{name:!0,content:!0}),a.forEach(e),E=c(s),k(u.$$.fragment,s),b=c(s),d=n(s,"H1",{class:!0});var Es=f(d);$=n(Es,"A",{id:!0,class:!0,href:!0});var Ys=f($);h=n(Ys,"SPAN",{});var Js=f(h);k(v.$$.fragment,Js),Js.forEach(e),Ys.forEach(e),j=c(Es),_=n(Es,"SPAN",{});var Vs=f(_);K=o(Vs,"\u4F7F\u7528 Keras \u5FAE\u8C03\u4E00\u4E2A\u6A21\u578B"),Vs.forEach(e),Es.forEach(e),T=c(s),k(g.$$.fragment,s),Q=c(s),z=n(s,"P",{});var R=f(z);bs=o(R,"\u5B8C\u6210\u4E0A\u4E00\u8282\u4E2D\u7684\u6240\u6709\u6570\u636E\u9884\u5904\u7406\u5DE5\u4F5C\u540E\uFF0C\u60A8\u53EA\u5269\u4E0B\u6700\u540E\u7684\u51E0\u4E2A\u6B65\u9AA4\u6765\u8BAD\u7EC3\u6A21\u578B\u3002 \u4F46\u662F\u8BF7\u6CE8\u610F\uFF0C"),Ks=n(R,"CODE",{});var Lt=f(Ks);Ue=o(Lt,"model.fit()"),Lt.forEach(e),Ge=o(R," \u547D\u4EE4\u5728 CPU \u4E0A\u8FD0\u884C\u4F1A\u975E\u5E38\u7F13\u6162\u3002 \u5982\u679C\u60A8\u6CA1\u6709GPU\uFF0C\u5219\u53EF\u4EE5\u5728 "),X=n(R,"A",{href:!0,rel:!0});var Bt=f(X);He=o(Bt,"Google Colab"),Bt.forEach(e),Re=o(R," \u4E0A\u4F7F\u7528\u514D\u8D39\u7684 GPU \u6216 TPU(\u9700\u8981\u68AF\u5B50)\u3002"),R.forEach(e),Qs=c(s),vs=n(s,"P",{});var Nt=f(vs);Le=o(Nt,"\u8FD9\u4E00\u8282\u7684\u4EE3\u7801\u793A\u4F8B\u5047\u8BBE\u60A8\u5DF2\u7ECF\u6267\u884C\u4E86\u4E0A\u4E00\u8282\u4E2D\u7684\u4EE3\u7801\u793A\u4F8B\u3002 \u4E0B\u9762\u4E00\u4E2A\u7B80\u77ED\u7684\u6458\u8981\uFF0C\u5305\u542B\u4E86\u5728\u5F00\u59CB\u5B66\u4E60\u8FD9\u4E00\u8282\u4E4B\u524D\u60A8\u9700\u8981\u7684\u6267\u884C\u7684\u4EE3\u7801\uFF1A"),Nt.forEach(e),Xs=c(s),k(Z.$$.fragment,s),Zs=c(s),ss=n(s,"H3",{id:!0});var It=f(ss);Be=o(It,"\u8BAD\u7EC3\u6A21\u578B"),It.forEach(e),se=c(s),js=n(s,"P",{});var Wt=f(js);Ne=o(Wt,"\u4ECE\u{1F917} Transformers \u5BFC\u5165\u7684 TensorFlow \u6A21\u578B\u5DF2\u7ECF\u662F Keras \u6A21\u578B\u3002 \u4E0B\u9762\u7684\u89C6\u9891\u662F\u5BF9 Keras \u7684\u7B80\u77ED\u4ECB\u7ECD\u3002"),Wt.forEach(e),ee=c(s),k(es.$$.fragment,s),te=c(s),ws=n(s,"P",{});var Yt=f(ws);Ie=o(Yt,"\u8FD9\u610F\u5473\u7740\uFF0C\u4E00\u65E6\u6211\u4EEC\u6709\u4E86\u6570\u636E\uFF0C\u5C31\u9700\u8981\u5F88\u5C11\u7684\u5DE5\u4F5C\u5C31\u53EF\u4EE5\u5F00\u59CB\u5BF9\u5176\u8FDB\u884C\u8BAD\u7EC3\u3002"),Yt.forEach(e),ae=c(s),k(ts.$$.fragment,s),le=c(s),O=n(s,"P",{});var Ts=f(O);We=o(Ts,"\u548C"),ks=n(Ts,"A",{href:!0});var Jt=f(ks);Ye=o(Jt,"\u7B2C\u4E8C\u7AE0"),Jt.forEach(e),Je=o(Ts,"\u4F7F\u7528\u7684\u65B9\u6CD5\u4E00\u6837, \u6211\u4EEC\u5C06\u4F7F\u7528\u4E8C\u5206\u7C7B\u7684 "),zs=n(Ts,"CODE",{});var Vt=f(zs);Ve=o(Vt,"TFAutoModelForSequenceClassification"),Vt.forEach(e),Qe=o(Ts,"\u7C7B:"),Ts.forEach(e),oe=c(s),k(as.$$.fragment,s),re=c(s),L=n(s,"P",{});var Se=f(L);Xe=o(Se,"\u60A8\u4F1A\u6CE8\u610F\u5230\uFF0C\u4E0E "),qs=n(Se,"A",{href:!0});var Qt=f(qs);Ze=o(Qt,"\u7B2C\u4E8C\u7AE0"),Qt.forEach(e),st=o(Se," \u4E0D\u540C\u7684\u662F\uFF0C\u60A8\u5728\u5B9E\u4F8B\u5316\u6B64\u9884\u8BAD\u7EC3\u6A21\u578B\u540E\u4F1A\u6536\u5230\u8B66\u544A\u3002 \u8FD9\u662F\u56E0\u4E3A BERT \u6CA1\u6709\u5BF9\u53E5\u5B50\u5BF9\u8FDB\u884C\u5206\u7C7B\u8FDB\u884C\u9884\u8BAD\u7EC3\uFF0C\u6240\u4EE5\u9884\u8BAD\u7EC3\u6A21\u578B\u7684 head \u5DF2\u7ECF\u88AB\u4E22\u5F03\uFF0C\u800C\u662F\u63D2\u5165\u4E86\u4E00\u4E2A\u9002\u5408\u5E8F\u5217\u5206\u7C7B\u7684\u65B0 head\u3002 \u8B66\u544A\u8868\u660E\u4E00\u4E9B\u6743\u91CD\u6CA1\u6709\u4F7F\u7528\uFF08\u5BF9\u5E94\u4E8E\u4E22\u5F03\u7684\u9884\u8BAD\u7EC3\u5934\uFF09\uFF0C\u800C\u5176\u4ED6\u4E00\u4E9B\u6743\u91CD\u662F\u968F\u673A\u521D\u59CB\u5316\u7684\uFF08\u65B0\u5934\u7684\u6743\u91CD\uFF09\u3002 \u6700\u540E\u9F13\u52B1\u60A8\u8BAD\u7EC3\u6A21\u578B\uFF0C\u8FD9\u6B63\u662F\u6211\u4EEC\u73B0\u5728\u8981\u505A\u7684\u3002"),Se.forEach(e),pe=c(s),U=n(s,"P",{});var As=f(U);et=o(As,"\u8981\u5728\u6211\u4EEC\u7684\u6570\u636E\u96C6\u4E0A\u5FAE\u8C03\u6A21\u578B\uFF0C\u6211\u4EEC\u53EA\u9700\u8981\u5728\u6211\u4EEC\u7684\u6A21\u578B\u4E0A\u8C03\u7528 "),xs=n(As,"CODE",{});var Xt=f(xs);tt=o(Xt,"compile()"),Xt.forEach(e),at=o(As," \u65B9\u6CD5\uFF0C\u7136\u540E\u5C06\u6211\u4EEC\u7684\u6570\u636E\u4F20\u9012\u7ED9 "),Ss=n(As,"CODE",{});var Zt=f(Ss);lt=o(Zt,"fit()"),Zt.forEach(e),ot=o(As," \u65B9\u6CD5\u3002 \u8FD9\u5C06\u542F\u52A8\u5FAE\u8C03\u8FC7\u7A0B\uFF08\u5728 GPU \u4E0A\u5E94\u8BE5\u9700\u8981\u51E0\u5206\u949F\uFF09\u5E76\u8F93\u51FA\u8BAD\u7EC3loss\uFF0C\u4EE5\u53CA\u6BCF\u4E2A epoch \u7ED3\u675F\u65F6\u7684\u9A8C\u8BC1loss\u3002"),As.forEach(e),ne=c(s),k(B.$$.fragment,s),fe=c(s),k(ls.$$.fragment,s),ie=c(s),k(N.$$.fragment,s),me=c(s),os=n(s,"H3",{id:!0});var sa=f(os);rt=o(sa,"\u63D0\u5347\u8BAD\u7EC3\u7684\u6548\u679C"),sa.forEach(e),ce=c(s),k(rs.$$.fragment,s),ue=c(s),I=n(s,"P",{});var Fe=f(I);pt=o(Fe,"\u5982\u679C\u60A8\u5C1D\u8BD5\u4E0A\u9762\u7684\u4EE3\u7801\uFF0C\u5B83\u80AF\u5B9A\u4F1A\u8FD0\u884C\uFF0C\u4F46\u60A8\u4F1A\u53D1\u73B0loss\u53EA\u662F\u7F13\u6162\u6216\u96F6\u661F\u5730\u4E0B\u964D\u3002 \u4E3B\u8981\u539F\u56E0\u662F"),Fs=n(Fe,"EM",{});var ea=f(Fs);nt=o(ea,"\u5B66\u4E60\u7387"),ea.forEach(e),ft=o(Fe,"\u3002 \u4E0Eloss\u4E00\u6837\uFF0C\u5F53\u6211\u4EEC\u5C06\u4F18\u5316\u5668\u7684\u540D\u79F0\u4F5C\u4E3A\u5B57\u7B26\u4E32\u4F20\u9012\u7ED9 Keras \u65F6\uFF0CKeras \u4F1A\u521D\u59CB\u5316\u8BE5\u4F18\u5316\u5668\u5177\u6709\u6240\u6709\u53C2\u6570\u7684\u9ED8\u8BA4\u503C\uFF0C\u5305\u62EC\u5B66\u4E60\u7387\u3002 \u4F46\u662F\uFF0C\u6839\u636E\u957F\u671F\u7ECF\u9A8C\uFF0C\u6211\u4EEC\u77E5\u9053Transformer \u6A21\u578B\u66F4\u9002\u5408\u4F7F\u7528\u6BD4 Adam \u7684\u9ED8\u8BA4\u503C\uFF081e-3\uFF09\u4E5F\u5199\u6210\u4E3A 10 \u7684 -3 \u6B21\u65B9\uFF0C\u6216 0.001\uFF0C\u4F4E\u5F97\u591A\u7684\u5B66\u4E60\u7387\u3002 5e-5 (0.00005) \u6BD4\u9ED8\u8BA4\u503C\u5927\u7EA6\u4F4E 20 \u500D\uFF0C\u662F\u4E00\u4E2A\u66F4\u597D\u7684\u8D77\u70B9\u3002"),Fe.forEach(e),$e=c(s),D=n(s,"P",{});var H=f(D);it=o(H,"\u9664\u4E86\u964D\u4F4E\u5B66\u4E60\u7387\uFF0C\u6211\u4EEC\u8FD8\u6709\u7B2C\u4E8C\u4E2A\u6280\u5DE7\uFF1A\u6211\u4EEC\u53EF\u4EE5\u6162\u6162\u964D\u4F4E\u5B66\u4E60\u7387\u3002\u5728\u8BAD\u7EC3\u8FC7\u7A0B\u4E2D\u3002 \u5728\u6587\u732E\u4E2D\uFF0C\u60A8\u6709\u65F6\u4F1A\u770B\u5230\u8FD9\u88AB\u79F0\u4E3A "),Ms=n(H,"EM",{});var ta=f(Ms);mt=o(ta,"decaying"),ta.forEach(e),ct=o(H," \u6216 "),Os=n(H,"EM",{});var aa=f(Os);ut=o(aa,"annealing"),aa.forEach(e),$t=o(H,"\u5B66\u4E60\u7387\u3002 \u5728 Keras \u4E2D\uFF0C\u6700\u597D\u7684\u65B9\u6CD5\u662F\u4F7F\u7528 "),Us=n(H,"EM",{});var la=f(Us);_t=o(la,"learning rate scheduler"),la.forEach(e),dt=o(H,"\u3002 \u4E00\u4E2A\u597D\u7528\u7684\u662F"),Gs=n(H,"CODE",{});var oa=f(Gs);ht=o(oa,"PolynomialDecay"),oa.forEach(e),Et=o(H,"\u2014\u2014\u5C3D\u7BA1\u6709\u8FD9\u4E2A\u540D\u5B57\uFF0C\u4F46\u5728\u9ED8\u8BA4\u8BBE\u7F6E\u4E0B\uFF0C\u5B83\u53EA\u662F\u7B80\u5355\u5730\u4ECE\u521D\u59CB\u503C\u7EBF\u6027\u8870\u51CF\u5B66\u4E60\u7387\u503C\u5728\u8BAD\u7EC3\u8FC7\u7A0B\u4E2D\u7684\u6700\u7EC8\u503C\uFF0C\u8FD9\u6B63\u662F\u6211\u4EEC\u60F3\u8981\u7684\u3002\u4F46\u662F\uFF0C \u4E3A\u4E86\u6B63\u786E\u4F7F\u7528\u8C03\u5EA6\u7A0B\u5E8F\uFF0C\u6211\u4EEC\u9700\u8981\u544A\u8BC9\u5B83\u8BAD\u7EC3\u7684\u6B21\u6570\u3002 \u6211\u4EEC\u5C06\u5728\u4E0B\u9762\u4E3A\u5176\u8BA1\u7B97\u201Cnum_train_steps\u201D\u3002"),H.forEach(e),_e=c(s),k(ps.$$.fragment,s),de=c(s),k(W.$$.fragment,s),he=c(s),Ps=n(s,"P",{});var ra=f(Ps);bt=o(ra,"\u73B0\u5728\u6211\u4EEC\u6709\u4E86\u5168\u65B0\u7684\u4F18\u5316\u5668\uFF0C\u6211\u4EEC\u53EF\u4EE5\u5C1D\u8BD5\u4F7F\u7528\u5B83\u8FDB\u884C\u8BAD\u7EC3\u3002 \u9996\u5148\uFF0C\u8BA9\u6211\u4EEC\u91CD\u65B0\u52A0\u8F7D\u6A21\u578B\uFF0C\u4EE5\u91CD\u7F6E\u6211\u4EEC\u521A\u521A\u8FDB\u884C\u7684\u8BAD\u7EC3\u8FD0\u884C\u5BF9\u6743\u91CD\u7684\u66F4\u6539\uFF0C\u7136\u540E\u6211\u4EEC\u53EF\u4EE5\u4F7F\u7528\u65B0\u7684\u4F18\u5316\u5668\u5BF9\u5176\u8FDB\u884C\u7F16\u8BD1\uFF1A"),ra.forEach(e),Ee=c(s),k(ns.$$.fragment,s),be=c(s),ys=n(s,"P",{});var pa=f(ys);vt=o(pa,"\u73B0\u5728\uFF0C\u6211\u4EEC\u518D\u6B21\u8FDB\u884Cfit\uFF1A"),pa.forEach(e),ve=c(s),k(fs.$$.fragment,s),je=c(s),k(Y.$$.fragment,s),we=c(s),is=n(s,"H3",{id:!0});var na=f(is);jt=o(na,"\u6A21\u578B\u9884\u6D4B"),na.forEach(e),ke=c(s),k(ms.$$.fragment,s),qe=c(s),G=n(s,"P",{});var Ds=f(G);wt=o(Ds,"\u8BAD\u7EC3\u548C\u89C2\u5BDF\u7684loss\u4E0B\u964D\u90FD\u975E\u5E38\u597D\uFF0C\u4F46\u662F\u5982\u679C\u6211\u4EEC\u60F3\u4ECE\u8BAD\u7EC3\u540E\u7684\u6A21\u578B\u4E2D\u83B7\u5F97\u8F93\u51FA\uFF0C\u6216\u8005\u8BA1\u7B97\u4E00\u4E9B\u6307\u6807\uFF0C\u6216\u8005\u5728\u751F\u4EA7\u4E2D\u4F7F\u7528\u6A21\u578B\u5462\uFF1F \u4E3A\u6B64\uFF0C\u6211\u4EEC\u53EF\u4EE5\u4F7F\u7528"),Hs=n(Ds,"CODE",{});var fa=f(Hs);kt=o(fa,"predict()"),fa.forEach(e),qt=o(Ds," \u65B9\u6CD5\u3002 \u8FD9\u5C06\u8FD4\u56DE\u6A21\u578B\u7684\u8F93\u51FA\u5934\u7684"),Rs=n(Ds,"EM",{});var ia=f(Rs);Pt=o(ia,"logits"),ia.forEach(e),yt=o(Ds,"\u6570\u503C\uFF0C\u6BCF\u4E2A\u7C7B\u4E00\u4E2A\u3002"),Ds.forEach(e),Pe=c(s),k(cs.$$.fragment,s),ye=c(s),Cs=n(s,"P",{});var ma=f(Cs);Ct=o(ma,"\u6211\u4EEC\u53EF\u4EE5\u5C06\u8FD9\u4E9B logit \u8F6C\u6362\u4E3A\u6A21\u578B\u7684\u7C7B\u522B\u9884\u6D4B\uFF0C\u65B9\u6CD5\u662F\u4F7F\u7528 argmax \u627E\u5230\u6700\u9AD8\u7684 logit\uFF0C\u5B83\u5BF9\u5E94\u4E8E\u6700\u6709\u53EF\u80FD\u7684\u7C7B\u522B\uFF1A"),ma.forEach(e),Ce=c(s),k(us.$$.fragment,s),ge=c(s),k($s.$$.fragment,s),Te=c(s),x=n(s,"P",{});var J=f(x);gt=o(J,"\u73B0\u5728\uFF0C\u8BA9\u6211\u4EEC\u4F7F\u7528\u8FD9\u4E9B "),Ls=n(J,"CODE",{});var ca=f(Ls);Tt=o(ca,"preds"),ca.forEach(e),At=o(J," \u6765\u8BA1\u7B97\u4E00\u4E9B\u6307\u6807\uFF01 \u6211\u4EEC\u53EF\u4EE5\u50CF\u52A0\u8F7D\u6570\u636E\u96C6\u4E00\u6837\u8F7B\u677E\u5730\u52A0\u8F7D\u4E0E MRPC \u6570\u636E\u96C6\u76F8\u5173\u7684\u6307\u6807\uFF0C\u8FD9\u6B21\u4F7F\u7528\u7684\u662F "),Bs=n(J,"CODE",{});var ua=f(Bs);Dt=o(ua,"load_metric()"),ua.forEach(e),Kt=o(J," \u51FD\u6570\u3002 \u8FD4\u56DE\u7684\u5BF9\u8C61\u6709\u4E00\u4E2A "),Ns=n(J,"CODE",{});var $a=f(Ns);zt=o($a,"compute()"),$a.forEach(e),xt=o(J," \u65B9\u6CD5\uFF0C\u6211\u4EEC\u53EF\u4EE5\u4F7F\u7528\u5B83\u6765\u8FDB\u884C\u5EA6\u91CF\u8BA1\u7B97\uFF1A"),J.forEach(e),Ae=c(s),k(_s.$$.fragment,s),De=c(s),k(ds.$$.fragment,s),Ke=c(s),S=n(s,"P",{});var V=f(S);St=o(V,"\u60A8\u83B7\u5F97\u7684\u786E\u5207\u7ED3\u679C\u53EF\u80FD\u4F1A\u6709\u6240\u4E0D\u540C\uFF0C\u56E0\u4E3A\u6A21\u578B\u5934\u7684\u968F\u673A\u521D\u59CB\u5316\u53EF\u80FD\u4F1A\u6539\u53D8\u5B83\u83B7\u5F97\u7684\u6307\u6807\u3002 \u5728\u8FD9\u91CC\uFF0C\u6211\u4EEC\u53EF\u4EE5\u770B\u5230\u6211\u4EEC\u7684\u6A21\u578B\u5728\u9A8C\u8BC1\u96C6\u4E0A\u7684\u51C6\u786E\u7387\u4E3A 85.78%\uFF0CF1 \u5F97\u5206\u4E3A 89.97\u3002 \u8FD9\u4E9B\u662F\u7528\u4E8E\u8BC4\u4F30 GLUE \u57FA\u51C6\u7684 MRPC \u6570\u636E\u96C6\u7ED3\u679C\u7684\u4E24\u4E2A\u6307\u6807\u3002 "),hs=n(V,"A",{href:!0,rel:!0});var _a=f(hs);Ft=o(_a,"BERT \u8BBA\u6587"),_a.forEach(e),Mt=o(V," \u4E2D\u7684\u8868\u683C\u62A5\u544A\u4E86\u57FA\u672C\u6A21\u578B\u7684 F1 \u5206\u6570\u4E3A 88.9\u3002 \u90A3\u662F "),Is=n(V,"CODE",{});var da=f(Is);Ot=o(da,"uncased"),da.forEach(e),Ut=o(V," \u6A21\u578B\uFF0C\u800C\u6211\u4EEC\u76EE\u524D\u4F7F\u7528\u7684\u662F "),Ws=n(V,"CODE",{});var ha=f(Ws);Gt=o(ha,"cased"),ha.forEach(e),Ht=o(V," \u6A21\u578B\uFF0C\u8FD9\u89E3\u91CA\u4E86\u4E3A\u4EC0\u4E48\u6211\u4EEC\u4F1A\u83B7\u5F97\u66F4\u597D\u7684\u7ED3\u679C\u3002"),V.forEach(e),ze=c(s),gs=n(s,"P",{});var Ea=f(gs);Rt=o(Ea,"\u4F7F\u7528 Keras API \u8FDB\u884C\u5FAE\u8C03\u7684\u4ECB\u7ECD\u5230\u6B64\u7ED3\u675F\u3002 \u7B2C 7 \u7AE0\u5C06\u7ED9\u51FA\u5BF9\u5927\u591A\u6570\u5E38\u89C1 NLP \u4EFB\u52A1\u6267\u884C\u6B64\u64CD\u4F5C\u7684\u793A\u4F8B\u3002\u5982\u679C\u60A8\u60F3\u5728 Keras API \u4E0A\u78E8\u7EC3\u81EA\u5DF1\u7684\u6280\u80FD\uFF0C\u8BF7\u5C1D\u8BD5\u4F7F\u7B2C\u4E8C\u8282\u6240\u4F7F\u7528\u7684\u7684\u6570\u636E\u5904\u7406\u5728 GLUE SST-2 \u6570\u636E\u96C6\u4E0A\u5FAE\u8C03\u6A21\u578B\u3002"),Ea.forEach(e),this.h()},h(){A(i,"name","hf:doc:metadata"),A(i,"content",JSON.stringify(Ka)),A($,"id","keras"),A($,"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"),A($,"href","#keras"),A(d,"class","relative group"),A(X,"href","https://colab.research.google.com/"),A(X,"rel","nofollow"),A(ss,"id",""),A(ks,"href","/course/chapter2"),A(qs,"href","/course/chapter2"),A(os,"id",""),A(is,"id",""),A(hs,"href","https://arxiv.org/pdf/1810.04805.pdf"),A(hs,"rel","nofollow")},m(s,a){t(document.head,i),r(s,E,a),q(u,s,a),r(s,b,a),r(s,d,a),t(d,$),t($,h),q(v,h,null),t(d,j),t(d,_),t(_,K),r(s,T,a),q(g,s,a),r(s,Q,a),r(s,z,a),t(z,bs),t(z,Ks),t(Ks,Ue),t(z,Ge),t(z,X),t(X,He),t(z,Re),r(s,Qs,a),r(s,vs,a),t(vs,Le),r(s,Xs,a),q(Z,s,a),r(s,Zs,a),r(s,ss,a),t(ss,Be),r(s,se,a),r(s,js,a),t(js,Ne),r(s,ee,a),q(es,s,a),r(s,te,a),r(s,ws,a),t(ws,Ie),r(s,ae,a),q(ts,s,a),r(s,le,a),r(s,O,a),t(O,We),t(O,ks),t(ks,Ye),t(O,Je),t(O,zs),t(zs,Ve),t(O,Qe),r(s,oe,a),q(as,s,a),r(s,re,a),r(s,L,a),t(L,Xe),t(L,qs),t(qs,Ze),t(L,st),r(s,pe,a),r(s,U,a),t(U,et),t(U,xs),t(xs,tt),t(U,at),t(U,Ss),t(Ss,lt),t(U,ot),r(s,ne,a),q(B,s,a),r(s,fe,a),q(ls,s,a),r(s,ie,a),q(N,s,a),r(s,me,a),r(s,os,a),t(os,rt),r(s,ce,a),q(rs,s,a),r(s,ue,a),r(s,I,a),t(I,pt),t(I,Fs),t(Fs,nt),t(I,ft),r(s,$e,a),r(s,D,a),t(D,it),t(D,Ms),t(Ms,mt),t(D,ct),t(D,Os),t(Os,ut),t(D,$t),t(D,Us),t(Us,_t),t(D,dt),t(D,Gs),t(Gs,ht),t(D,Et),r(s,_e,a),q(ps,s,a),r(s,de,a),q(W,s,a),r(s,he,a),r(s,Ps,a),t(Ps,bt),r(s,Ee,a),q(ns,s,a),r(s,be,a),r(s,ys,a),t(ys,vt),r(s,ve,a),q(fs,s,a),r(s,je,a),q(Y,s,a),r(s,we,a),r(s,is,a),t(is,jt),r(s,ke,a),q(ms,s,a),r(s,qe,a),r(s,G,a),t(G,wt),t(G,Hs),t(Hs,kt),t(G,qt),t(G,Rs),t(Rs,Pt),t(G,yt),r(s,Pe,a),q(cs,s,a),r(s,ye,a),r(s,Cs,a),t(Cs,Ct),r(s,Ce,a),q(us,s,a),r(s,ge,a),q($s,s,a),r(s,Te,a),r(s,x,a),t(x,gt),t(x,Ls),t(Ls,Tt),t(x,At),t(x,Bs),t(Bs,Dt),t(x,Kt),t(x,Ns),t(Ns,zt),t(x,xt),r(s,Ae,a),q(_s,s,a),r(s,De,a),q(ds,s,a),r(s,Ke,a),r(s,S,a),t(S,St),t(S,hs),t(hs,Ft),t(S,Mt),t(S,Is),t(Is,Ot),t(S,Ut),t(S,Ws),t(Ws,Gt),t(S,Ht),r(s,ze,a),r(s,gs,a),t(gs,Rt),xe=!0},p(s,[a]){const Es={};a&1&&(Es.fw=s[0]),u.$set(Es);const Ys={};a&2&&(Ys.$$scope={dirty:a,ctx:s}),B.$set(Ys);const Js={};a&2&&(Js.$$scope={dirty:a,ctx:s}),N.$set(Js);const Vs={};a&2&&(Vs.$$scope={dirty:a,ctx:s}),W.$set(Vs);const R={};a&2&&(R.$$scope={dirty:a,ctx:s}),Y.$set(R)},i(s){xe||(P(u.$$.fragment,s),P(v.$$.fragment,s),P(g.$$.fragment,s),P(Z.$$.fragment,s),P(es.$$.fragment,s),P(ts.$$.fragment,s),P(as.$$.fragment,s),P(B.$$.fragment,s),P(ls.$$.fragment,s),P(N.$$.fragment,s),P(rs.$$.fragment,s),P(ps.$$.fragment,s),P(W.$$.fragment,s),P(ns.$$.fragment,s),P(fs.$$.fragment,s),P(Y.$$.fragment,s),P(ms.$$.fragment,s),P(cs.$$.fragment,s),P(us.$$.fragment,s),P($s.$$.fragment,s),P(_s.$$.fragment,s),P(ds.$$.fragment,s),xe=!0)},o(s){y(u.$$.fragment,s),y(v.$$.fragment,s),y(g.$$.fragment,s),y(Z.$$.fragment,s),y(es.$$.fragment,s),y(ts.$$.fragment,s),y(as.$$.fragment,s),y(B.$$.fragment,s),y(ls.$$.fragment,s),y(N.$$.fragment,s),y(rs.$$.fragment,s),y(ps.$$.fragment,s),y(W.$$.fragment,s),y(ns.$$.fragment,s),y(fs.$$.fragment,s),y(Y.$$.fragment,s),y(ms.$$.fragment,s),y(cs.$$.fragment,s),y(us.$$.fragment,s),y($s.$$.fragment,s),y(_s.$$.fragment,s),y(ds.$$.fragment,s),xe=!1},d(s){e(i),s&&e(E),C(u,s),s&&e(b),s&&e(d),C(v),s&&e(T),C(g,s),s&&e(Q),s&&e(z),s&&e(Qs),s&&e(vs),s&&e(Xs),C(Z,s),s&&e(Zs),s&&e(ss),s&&e(se),s&&e(js),s&&e(ee),C(es,s),s&&e(te),s&&e(ws),s&&e(ae),C(ts,s),s&&e(le),s&&e(O),s&&e(oe),C(as,s),s&&e(re),s&&e(L),s&&e(pe),s&&e(U),s&&e(ne),C(B,s),s&&e(fe),C(ls,s),s&&e(ie),C(N,s),s&&e(me),s&&e(os),s&&e(ce),C(rs,s),s&&e(ue),s&&e(I),s&&e($e),s&&e(D),s&&e(_e),C(ps,s),s&&e(de),C(W,s),s&&e(he),s&&e(Ps),s&&e(Ee),C(ns,s),s&&e(be),s&&e(ys),s&&e(ve),C(fs,s),s&&e(je),C(Y,s),s&&e(we),s&&e(is),s&&e(ke),C(ms,s),s&&e(qe),s&&e(G),s&&e(Pe),C(cs,s),s&&e(ye),s&&e(Cs),s&&e(Ce),C(us,s),s&&e(ge),C($s,s),s&&e(Te),s&&e(x),s&&e(Ae),C(_s,s),s&&e(De),C(ds,s),s&&e(Ke),s&&e(S),s&&e(ze),s&&e(gs)}}}const Ka={local:"keras",sections:[{local:"",title:"\u8BAD\u7EC3\u6A21\u578B"},{local:"",title:"\u63D0\u5347\u8BAD\u7EC3\u7684\u6548\u679C"},{local:"",title:"\u6A21\u578B\u9884\u6D4B"}],title:"\u4F7F\u7528 Keras \u5FAE\u8C03\u4E00\u4E2A\u6A21\u578B"};function za(F,i,E){let u="pt";return ka(()=>{const b=new URLSearchParams(window.location.search);E(0,u=b.get("fw")||"pt")}),[u]}class Ha extends ba{constructor(i){super();va(this,i,za,Da,ja,{})}}export{Ha as default,Ka as metadata};
