<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Distil</title>
    <description>The latest articles on DEV Community by Distil (@distil-md).</description>
    <link>https://dev.to/distil-md</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4221577%2Fac846be6-5b22-440d-95ed-68c7dd9c0771.png</url>
      <title>DEV Community: Distil</title>
      <link>https://dev.to/distil-md</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/distil-md"/>
    <language>en</language>
    <item>
      <title>Distillation didn't help our small model until we took the labels away</title>
      <dc:creator>Distil</dc:creator>
      <pubDate>Mon, 02 Nov 2026 17:38:35 +0000</pubDate>
      <link>https://dev.to/distil-md/distillation-didnt-help-our-small-model-until-we-took-the-labels-away-4ko4</link>
      <guid>https://dev.to/distil-md/distillation-didnt-help-our-small-model-until-we-took-the-labels-away-4ko4</guid>
      <description>&lt;p&gt;&lt;em&gt;Disclosure: this post was written by AI, and AI tools helped write the book it comes from. The experiment was run end to end on a laptop, and every number below is real output. The code is MIT licensed.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We distilled a BERT-base sentiment model (110M parameters, 92.55% on SST-2) into BERT-Mini (11M parameters) on a laptop CPU with no GPU. We expected the usual story: the student learns from the teacher's soft targets and beats a student trained on labels alone.&lt;/p&gt;

&lt;p&gt;That's not what happened.&lt;/p&gt;

&lt;h2&gt;
  
  
  Round 1: 10,000 labels, no gain
&lt;/h2&gt;

&lt;p&gt;Both students saw the same 10,000 labeled SST-2 sentences. One trained on the labels alone. The other trained on the standard blend of the teacher's softened probabilities and the labels (T = 2, α = 0.5):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;L = α · T² · KL(p_T ‖ q_T) + (1 − α) · CE(y, q_1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With seed 42, the distilled student came out 0.57 points ahead. That's five sentences out of 872. So we reran both students with two more seeds:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Student&lt;/th&gt;
&lt;th&gt;Seed 42&lt;/th&gt;
&lt;th&gt;Seed 1&lt;/th&gt;
&lt;th&gt;Seed 2&lt;/th&gt;
&lt;th&gt;Mean ± std&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Labels only&lt;/td&gt;
&lt;td&gt;82.00%&lt;/td&gt;
&lt;td&gt;83.26%&lt;/td&gt;
&lt;td&gt;82.57%&lt;/td&gt;
&lt;td&gt;82.61 ± 0.63&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Distilled&lt;/td&gt;
&lt;td&gt;82.57%&lt;/td&gt;
&lt;td&gt;82.00%&lt;/td&gt;
&lt;td&gt;83.03%&lt;/td&gt;
&lt;td&gt;82.53 ± 0.52&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The single best run in the table is a baseline. Seed noise moved the baseline by 11 sentences, which is more than twice the "gain" we saw at first. With three seeds, the honest result is no measurable difference.&lt;/p&gt;

&lt;p&gt;The reason showed up once we looked at the soft targets. At T = 2 the teacher put 99.75% of its probability on its chosen class, on average. SST-2 has two classes, and the teacher had been fine-tuned on this exact training split, so it was nearly certain about every sentence. It also agreed with the human label 98.9% of the time. On almost every sentence the "dark knowledge" just repeated the label. Raising T to 4 and 8 softened the targets but didn't improve accuracy either.&lt;/p&gt;

&lt;h2&gt;
  
  
  Round 2: 1,000 labels plus 9,000 unlabeled sentences
&lt;/h2&gt;

&lt;p&gt;Hinton, Vinyals and Dean pointed out that the transfer set doesn't need labels. The teacher can supply the targets. So we asked a more practical question: if you can only afford to label 1,000 sentences, how much does the teacher help?&lt;/p&gt;

&lt;p&gt;The trick in PyTorch is small. Hide the labels with &lt;code&gt;-100&lt;/code&gt;, which is cross-entropy's default &lt;code&gt;ignore_index&lt;/code&gt;, and give each sentence its own α. Labeled sentences get 0.5, and unlabeled ones get 1.0, which means they learn from the teacher only:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;labelled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;train&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;unlabelled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="n"&gt;sentence&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;sentence&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;train&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;:]]&lt;/span&gt;
&lt;span class="n"&gt;alpha&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;torch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tensor&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;labelled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unlabelled&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also ran a control, A′, that trains on the same 1,000 labels for as many optimizer steps as the distilled run. Without it you can't tell "had a teacher" apart from "trained longer".&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Student&lt;/th&gt;
&lt;th&gt;Mean ± std (3 seeds)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;A: 1,000 labels&lt;/td&gt;
&lt;td&gt;73.85 ± 1.89&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A′: 1,000 labels, equal steps&lt;/td&gt;
&lt;td&gt;73.78 ± 1.44&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;B: 1,000 labels + 9,000 teacher-labeled&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;82.34 ± 0.50&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That's +8.5 points. B beat A on every seed, and the worst B run was more than five points above the best A run. B also landed within 0.3 points of the student trained on all 10,000 human labels. Here, a teacher plus unlabeled text did nearly the same job as 9,000 more labels.&lt;/p&gt;

&lt;h2&gt;
  
  
  What we took from it
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Run a labels-only baseline, and run seeds.&lt;/strong&gt; One seed made distillation look like it helped when it didn't.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check how soft your teacher's targets really are.&lt;/strong&gt; If its top probability is around 0.99, the soft targets carry little more than the labels do.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Distillation earns its keep when labels are scarce and unlabeled data is cheap.&lt;/strong&gt; That's the case it was designed for.&lt;/li&gt;
&lt;li&gt;The student is about 10x smaller and ran 16–19x faster per sentence on the CPU, but that comes from picking a small model, not from distillation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The full walkthrough is a book, &lt;em&gt;Small Model, Big Teacher&lt;/em&gt;. It covers the loss, why T² is there, choosing a teacher whose license allows this, the training loop line by line, and the results above. Chapters 1–2 are free at &lt;a href="https://distil.md/?utm_source=devto&amp;amp;utm_medium=article" rel="noopener noreferrer"&gt;distil.md&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>pytorch</category>
      <category>nlp</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
