import{s as ve,n as be,o as ye}from"../chunks/scheduler.7bc62968.js";import{S as Te,i as ke,g as s,s as l,r as J,A as $e,h as a,f as i,c as o,j as we,u as Q,x as r,k as X,y as xe,a as n,v as Y,d as Z,t as ee,w as te}from"../chunks/index.2f8492b0.js";import{H as ie,E as _e}from"../chunks/EditOnGithub.2a9ce03a.js";function Le(ne){let h,P,H,C,u,D,d,le='We are going to learn about Knowledge Distillation, the method behind <a href="https://huggingface.co/distilgpt2" rel="nofollow">distilGPT</a> and <a href="https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english" rel="nofollow">distilbert</a>, two of <em>the most downloaded models on the Hugging Face Hub!</em>',K,f,oe=`Presumably, we’ve all had teachers who “teach” by simply providing us the correct answers and then testing us on questions we haven’t seen before, analogous to supervised learning
of machine learning models where we provide a labeled dataset to train on. Instead of having a model train on labels, however,
we can pursue <a href="https://arxiv.org/abs/1503.02531" rel="nofollow">Knowledge Distillation</a> as an alternative to arriving at a much smaller model that can perform comparably to the larger model and much faster to boot.`,I,p,q,c,se="Imagine you were given this multiple-choice question:",E,g,ae='<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/multiple-choice-question.png" alt="Multiple Choice Question"/>',N,w,re="If you had someone just tell you, “The answer is Draco Malfoy,” that doesn’t teach you a whole lot about each of the characters’ relative relationships with Harry Potter.",j,v,he=`On the other hand, if someone tells you, “I am very confident it is not Ron Weasley, I am somewhat confident it is not Neville Longbottom, and
I am very confident that it <em>is</em> Draco Malfoy”, this gives you some information about these characters’ relationships to Harry Potter!
This is precisely the kind of information that gets passed down to our student model under the Knowledge Distillation paradigm.`,z,b,V,y,me=`In the paper <a href="https://arxiv.org/abs/1503.02531" rel="nofollow"><em>Distilling the Knowledge in a Neural Network</em></a>, Hinton et al. introduced the training methodology known as knowledge distillation,
taking inspiration from <em>insects</em>, of all things. Just as insects transition from larval to adult forms that are optimized for different tasks, large-scale machine learning models can
initially be cumbersome, like larvae, for extracting structure from data but can distill their knowledge into smaller, more efficient models for deployment.`,A,T,ue=`The essence of Knowledge Distillation is using the predicted logits from a teacher network to pass information to a smaller, more efficient student model. We do this
by re-writing the loss function to contain a <em>distillation loss</em>, which encourages the student model’s distribution over the output space to approximate the teacher’s.`,O,k,de="The distillation loss is formulated as:",S,$,fe='<img src="https://huggingface.co/datasets/hf-vision/course-assets/resolve/main/KL-Loss.png" alt="Distillation Loss"/>',U,x,pe=`The KL loss refers to the <a href="https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence" rel="nofollow">Kullback-Leibler Divergence</a> between the teacher and the student’s output distributions.
The overall loss for the student model is then formulated as the sum of this distillation loss with the standard cross-entropy loss over the ground-truth labels.`,W,_,ce='To see this loss function implemented in Python and a fully worked out example in Python, let’s check out the <a href="https://github.com/johko/computer-vision-course/blob/main/notebooks/Unit%203%20-%20Vision%20Transformers/KnowledgeDistillation.ipynb" rel="nofollow">notebook for this section</a>.',B,m,ge='<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>',G,L,R,M,F;return u=new ie({props:{title:"Knowledge Distillation with Vision Transformers",local:"knowledge-distillation-with-vision-transformers",headingTag:"h1"}}),p=new ie({props:{title:"Intuition Behind Knowledge Distillation",local:"intuition-behind-knowledge-distillation",headingTag:"h2"}}),b=new ie({props:{title:"Distilling the Knowledge in a Neural Network",local:"distilling-the-knowledge-in-a-neural-network",headingTag:"h2"}}),L=new _e({props:{source:"https://github.com/johko/computer-vision-course/blob/main/chapters/en/unit3/vision-transformers/knowledge-distillation.mdx"}}),{c(){h=s("meta"),P=l(),H=s("p"),C=l(),J(u.$$.fragment),D=l(),d=s("p"),d.innerHTML=le,K=l(),f=s("p"),f.innerHTML=oe,I=l(),J(p.$$.fragment),q=l(),c=s("p"),c.textContent=se,E=l(),g=s("p"),g.innerHTML=ae,N=l(),w=s("p"),w.textContent=re,j=l(),v=s("p"),v.innerHTML=he,z=l(),J(b.$$.fragment),V=l(),y=s("p"),y.innerHTML=me,A=l(),T=s("p"),T.innerHTML=ue,O=l(),k=s("p"),k.textContent=de,S=l(),$=s("p"),$.innerHTML=fe,U=l(),x=s("p"),x.innerHTML=pe,W=l(),_=s("p"),_.innerHTML=ce,B=l(),m=s("a"),m.innerHTML=ge,G=l(),J(L.$$.fragment),R=l(),M=s("p"),this.h()},l(e){const t=$e("svelte-u9bgzb",document.head);h=a(t,"META",{name:!0,content:!0}),t.forEach(i),P=o(e),H=a(e,"P",{}),we(H).forEach(i),C=o(e),Q(u.$$.fragment,e),D=o(e),d=a(e,"P",{"data-svelte-h":!0}),r(d)!=="svelte-1ofcq6q"&&(d.innerHTML=le),K=o(e),f=a(e,"P",{"data-svelte-h":!0}),r(f)!=="svelte-18scrnu"&&(f.innerHTML=oe),I=o(e),Q(p.$$.fragment,e),q=o(e),c=a(e,"P",{"data-svelte-h":!0}),r(c)!=="svelte-v5zyyx"&&(c.textContent=se),E=o(e),g=a(e,"P",{"data-svelte-h":!0}),r(g)!=="svelte-1vpf9y2"&&(g.innerHTML=ae),N=o(e),w=a(e,"P",{"data-svelte-h":!0}),r(w)!=="svelte-193lyi0"&&(w.textContent=re),j=o(e),v=a(e,"P",{"data-svelte-h":!0}),r(v)!=="svelte-11ohx5b"&&(v.innerHTML=he),z=o(e),Q(b.$$.fragment,e),V=o(e),y=a(e,"P",{"data-svelte-h":!0}),r(y)!=="svelte-1m1x8ev"&&(y.innerHTML=me),A=o(e),T=a(e,"P",{"data-svelte-h":!0}),r(T)!=="svelte-1q33f44"&&(T.innerHTML=ue),O=o(e),k=a(e,"P",{"data-svelte-h":!0}),r(k)!=="svelte-1l2wqyt"&&(k.textContent=de),S=o(e),$=a(e,"P",{"data-svelte-h":!0}),r($)!=="svelte-y84sde"&&($.innerHTML=fe),U=o(e),x=a(e,"P",{"data-svelte-h":!0}),r(x)!=="svelte-1mhwb2d"&&(x.innerHTML=pe),W=o(e),_=a(e,"P",{"data-svelte-h":!0}),r(_)!=="svelte-1onfg6s"&&(_.innerHTML=ce),B=o(e),m=a(e,"A",{target:!0,href:!0,"data-svelte-h":!0}),r(m)!=="svelte-rzabs7"&&(m.innerHTML=ge),G=o(e),Q(L.$$.fragment,e),R=o(e),M=a(e,"P",{}),we(M).forEach(i),this.h()},h(){X(h,"name","hf:doc:metadata"),X(h,"content",He),X(m,"target","_blank"),X(m,"href","https://colab.research.google.com/github/johko/computer-vision-course/blob/main/notebooks/Unit%203%20-%20Vision%20Transformers/KnowledgeDistillation.ipynb")},m(e,t){xe(document.head,h),n(e,P,t),n(e,H,t),n(e,C,t),Y(u,e,t),n(e,D,t),n(e,d,t),n(e,K,t),n(e,f,t),n(e,I,t),Y(p,e,t),n(e,q,t),n(e,c,t),n(e,E,t),n(e,g,t),n(e,N,t),n(e,w,t),n(e,j,t),n(e,v,t),n(e,z,t),Y(b,e,t),n(e,V,t),n(e,y,t),n(e,A,t),n(e,T,t),n(e,O,t),n(e,k,t),n(e,S,t),n(e,$,t),n(e,U,t),n(e,x,t),n(e,W,t),n(e,_,t),n(e,B,t),n(e,m,t),n(e,G,t),Y(L,e,t),n(e,R,t),n(e,M,t),F=!0},p:be,i(e){F||(Z(u.$$.fragment,e),Z(p.$$.fragment,e),Z(b.$$.fragment,e),Z(L.$$.fragment,e),F=!0)},o(e){ee(u.$$.fragment,e),ee(p.$$.fragment,e),ee(b.$$.fragment,e),ee(L.$$.fragment,e),F=!1},d(e){e&&(i(P),i(H),i(C),i(D),i(d),i(K),i(f),i(I),i(q),i(c),i(E),i(g),i(N),i(w),i(j),i(v),i(z),i(V),i(y),i(A),i(T),i(O),i(k),i(S),i($),i(U),i(x),i(W),i(_),i(B),i(m),i(G),i(R),i(M)),i(h),te(u,e),te(p,e),te(b,e),te(L,e)}}}const He='{"title":"Knowledge Distillation with Vision Transformers","local":"knowledge-distillation-with-vision-transformers","sections":[{"title":"Intuition Behind Knowledge Distillation","local":"intuition-behind-knowledge-distillation","sections":[],"depth":2},{"title":"Distilling the Knowledge in a Neural Network","local":"distilling-the-knowledge-in-a-neural-network","sections":[],"depth":2}],"depth":1}';function Me(ne){return ye(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ke extends Te{constructor(h){super(),ke(this,h,Me,Le,ve,{})}}export{Ke as component};
