{
  "generatedAt": "2026-11-14T16:35:49.608Z",
  "meta": {
    "slug": "fundamentals-of-deep-learning-and-nearest-neighbor-classifiers",
    "type": "library",
    "status": "published",
    "publishAt": "2026-11-05T01:06:00+09:00",
    "updatedAt": "2026-11-05T01:06:00+09:00",
    "category": "ai",
    "tags": [
      "deep-learning",
      "image-classification",
      "computer-vision"
    ],
    "authors": [
      "kwon-minjae"
    ],
    "series": "deep-learning-notes",
    "seriesOrder": 1,
    "project": "",
    "thumbnail": "/data/img/cover.d27f12918b.1200.webp",
    "ogImage": "/data/og/fundamentals-of-deep-learning-and-nearest-neighbor-classifiers.jpg?v=7cad6ea9be",
    "noindex": false,
    "hasCover": true,
    "thumbnailSrcset": "/data/img/cover.d27f12918b.480.webp 480w, /data/img/cover.d27f12918b.1200.webp 1200w"
  },
  "locales": {
    "ko": {
      "language": "ko",
      "filename": "ko.html",
      "format": "html",
      "title": "딥러닝 기초와 Nearest Neighbor Classifiers",
      "summary": "서울대 이준석 교수님의 딥러닝 강의 1강을 보며, 딥러닝까지의 짧은 역사와 가장 단순한 이미지 분류기인 최근접 이웃 분류기를 처음부터 정리합니다.",
      "description": "퍼셉트론과 XOR 문제, 이미지 분류, 데이터 기반 접근, L1·L2 거리, k-최근접 이웃, 검증 셋과 교차 검증, 차원의 저주까지 그림과 숫자로 설명합니다.",
      "content": "<h3>들어가며</h3>\n<p>서울대학교 이준석 교수님의 딥러닝 강의 <a target=\"_blank\" rel=\"noopener\" href=\"https://www.youtube.com/watch?v=wHBitw5oJSU\">「Lecture 1. Intro to Deep Learning &amp; Nearest Neighbor Classifiers」</a>를 보고 정리한 글입니다. 2022년 가을 「Machine Learning &amp; Deep Learning I」 수업의 딥러닝 파트 첫 강의이고, <a target=\"_blank\" rel=\"noopener\" href=\"https://viplab.snu.ac.kr/viplab/courses/mldl1_2022_2/\">수업 페이지</a>에 강의 노트도 올라와 있습니다. 노트는 스탠퍼드 CS231n 강의 노트를 바탕으로 만들었다고 적혀 있습니다.</p>\n<p>이 강의를 차례로 따라가는 딥러닝 노트의 첫 글입니다. 앞서 쓴 <a href=\"https://stanase.com/posts/what-is-a-neural-network/\">「신경망이란 무엇인가」</a>를 읽지 않았어도 따라올 수 있도록, 처음 보는 용어는 나올 때마다 풀어 쓰겠습니다.</p>\n<p>강의는 두 부분으로 나뉩니다. 앞부분은 딥러닝이 어디서 왔는지 짧게 훑는 역사이고, 뒷부분은 <strong>이미지 분류</strong>라는 문제와, 그 문제를 푸는 가장 단순한 방법인 <strong>최근접 이웃 분류기</strong>(Nearest Neighbor Classifier)입니다.</p>\n\n<h2>1. 딥러닝까지의 짧은 역사</h2>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-timeline.2a1098aa32.1440.webp\" alt=\"1943년 McCulloch–Pitts 뉴런, 1957년 퍼셉트론, 1969년 XOR 문제를 지적한 책 Perceptrons, 1986년 역전파, 2012년 AlexNet을 시간 순서로 놓은 연표. 1969년과 1986년 사이 17년이 강조되어 있다.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"1943년 인공 뉴런에서 2012년 AlexNet까지. XOR 문제가 지적된 1969년부터 역전파가 알려진 1986년까지 17년이 걸렸습니다.\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-timeline.2a1098aa32.640.webp 640w, /data/img/fig-timeline.2a1098aa32.960.webp 960w, /data/img/fig-timeline.2a1098aa32.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"500\" data-img-tone=\"light\" decoding=\"async\"><figcaption>1943년 인공 뉴런에서 2012년 AlexNet까지. XOR 문제가 지적된 1969년부터 역전파가 알려진 1986년까지 17년이 걸렸습니다.</figcaption></figure>\n<h3>1943년, 뉴런을 흉내 낸 논리 회로</h3>\n<p>신경망의 출발점은 1943년 McCulloch와 Pitts가 제안한 인공 뉴런입니다. 사람의 뉴런이 어떻게 동작하는지 생각해 보고, 그것을 아주 단순한 계산으로 옮긴 것입니다. 입력을 여러 개 받아 더하고, 그 합이 정해 둔 <strong>문턱</strong>(threshold)을 넘으면 1, 넘지 못하면 0을 내놓습니다.</p>\n<p>이런 뉴런 하나로 AND, OR, NOT 같은 논리 게이트를 만들 수 있습니다. 게이트를 조합하면 어떤 논리든 만들 수 있으니, 뉴런을 엮으면 사람의 신경망처럼 동작하는 '전자 뇌'를 만들 수 있다는 생각이었습니다. 다만 이때는 입력을 어떻게 더하고 문턱을 얼마로 둘지를 사람이 정해 줘야 했습니다.</p>\n<h3>1957년, 퍼셉트론과 '학습'</h3>\n<p>14년 뒤인 1957년, Rosenblatt가 <strong>퍼셉트론</strong>(perceptron)을 발표합니다. 생김새는 비슷합니다. 입력마다 <strong>가중치</strong>(weight)라는 숫자를 곱해 모두 더하고, 그 합이 문턱을 넘으면 1을 내놓습니다. 입력이 두 개라면 이렇게 씁니다.</p>\n<math-block data-latex=\"y = \\begin{cases} 1 &amp; w_1 x_1 + w_2 x_2 + b &gt; 0 \\\\ 0 &amp; w_1 x_1 + w_2 x_2 + b \\le 0 \\end{cases}\"><span class=\"katex-display\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics><mrow><mi>y</mi><mo>=</mo><mrow><mo fence=\"true\">{</mo><mtable rowspacing=\"0.36em\" columnalign=\"left left\" columnspacing=\"1em\"><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mn>1</mn></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><msub><mi>w</mi><mn>1</mn></msub><msub><mi>x</mi><mn>1</mn></msub><mo>+</mo><msub><mi>w</mi><mn>2</mn></msub><msub><mi>x</mi><mn>2</mn></msub><mo>+</mo><mi>b</mi><mo>&gt;</mo><mn>0</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mn>0</mn></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><msub><mi>w</mi><mn>1</mn></msub><msub><mi>x</mi><mn>1</mn></msub><mo>+</mo><msub><mi>w</mi><mn>2</mn></msub><msub><mi>x</mi><mn>2</mn></msub><mo>+</mo><mi>b</mi><mo>≤</mo><mn>0</mn></mrow></mstyle></mtd></mtr></mtable></mrow></mrow><annotation encoding=\"application/x-tex\">y = \\begin{cases} 1 &amp; w_1 x_1 + w_2 x_2 + b &gt; 0 \\\\ 0 &amp; w_1 x_1 + w_2 x_2 + b \\le 0 \\end{cases}</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.625em;vertical-align:-0.1944em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.0359em;\">y</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:3em;vertical-align:-1.25em;\"></span><span class=\"minner\"><span class=\"mopen delimcenter\" style=\"top:0em;\"><span class=\"delimsizing size4\">{</span></span><span class=\"mord\"><span class=\"mtable\"><span class=\"col-align-l\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.69em;\"><span style=\"top:-3.69em;\"><span class=\"pstrut\" style=\"height:3.008em;\"></span><span class=\"mord\"><span class=\"mord\">1</span></span></span><span style=\"top:-2.25em;\"><span class=\"pstrut\" style=\"height:3.008em;\"></span><span class=\"mord\"><span class=\"mord\">0</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.19em;\"><span></span></span></span></span></span><span class=\"arraycolsep\" style=\"width:1em;\"></span><span class=\"col-align-l\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.69em;\"><span style=\"top:-3.69em;\"><span class=\"pstrut\" style=\"height:3.008em;\"></span><span class=\"mord\"><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0269em;\">w</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0269em;\">w</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord mathnormal\">b</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">&gt;</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mord\">0</span></span></span><span style=\"top:-2.25em;\"><span class=\"pstrut\" style=\"height:3.008em;\"></span><span class=\"mord\"><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0269em;\">w</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0269em;\">w</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord mathnormal\">b</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">≤</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mord\">0</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.19em;\"><span></span></span></span></span></span></span></span><span class=\"mclose nulldelimiter\"></span></span></span></span></span></span></math-block>\n<p><math-inline data-latex=\"x_1\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>x</mi><mn>1</mn></msub></mrow><annotation encoding=\"application/x-tex\">x_1</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline>, <math-inline data-latex=\"x_2\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>x</mi><mn>2</mn></msub></mrow><annotation encoding=\"application/x-tex\">x_2</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline>는 입력, <math-inline data-latex=\"w_1\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>w</mi><mn>1</mn></msub></mrow><annotation encoding=\"application/x-tex\">w_1</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0269em;\">w</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline>, <math-inline data-latex=\"w_2\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>w</mi><mn>2</mn></msub></mrow><annotation encoding=\"application/x-tex\">w_2</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0269em;\">w</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline>는 각 입력에 곱하는 가중치이고, <math-inline data-latex=\"w_1 x_1 + w_2 x_2\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>w</mi><mn>1</mn></msub><msub><mi>x</mi><mn>1</mn></msub><mo>+</mo><msub><mi>w</mi><mn>2</mn></msub><msub><mi>x</mi><mn>2</mn></msub></mrow><annotation encoding=\"application/x-tex\">w_1 x_1 + w_2 x_2</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.7333em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0269em;\">w</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0269em;\">w</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline>처럼 입력 × 가중치를 모두 더한 것을 <strong>가중합</strong>이라고 부릅니다. <math-inline data-latex=\"b\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>b</mi></mrow><annotation encoding=\"application/x-tex\">b</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.6944em;\"></span><span class=\"mord mathnormal\">b</span></span></span></span></math-inline>는 <strong>편향</strong>(bias)으로, 문턱을 식 안으로 옮겨 놓은 숫자입니다. 지난 글에서 문턱전압 같은 것이라고 정리했던 바로 그 숫자입니다.</p>\n<p>달라진 점은 이 가중치를 사람이 정하지 않고 <strong>데이터를 보며 맞춰 간다</strong>는 것입니다. 신경망에 '학습'이라는 개념이 처음 들어온 순간입니다.</p>\n<h3>1969년, XOR 문제</h3>\n<p>그런데 1969년 Minsky와 Papert가 책 『Perceptrons』에서 한계를 하나 보입니다. 퍼셉트론 하나로는 <strong>XOR</strong>을 만들 수 없다는 것입니다. XOR은 회로 쪽에서 보면 익숙한 논리 게이트로, 두 입력이 서로 다르면 1, 같으면 0을 냅니다.</p>\n<table style=\"min-width: 125px;\"><colgroup><col style=\"min-width: 25px;\"><col style=\"min-width: 25px;\"><col style=\"min-width: 25px;\"><col style=\"min-width: 25px;\"><col style=\"min-width: 25px;\"></colgroup><tbody><tr><th colspan=\"1\" rowspan=\"1\"><p><math-inline data-latex=\"x_1\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>x</mi><mn>1</mn></msub></mrow><annotation encoding=\"application/x-tex\">x_1</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline></p></th><th colspan=\"1\" rowspan=\"1\"><p><math-inline data-latex=\"x_2\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>x</mi><mn>2</mn></msub></mrow><annotation encoding=\"application/x-tex\">x_2</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline></p></th><th colspan=\"1\" rowspan=\"1\"><p>AND</p></th><th colspan=\"1\" rowspan=\"1\"><p>OR</p></th><th colspan=\"1\" rowspan=\"1\"><p>XOR</p></th></tr><tr><td colspan=\"1\" rowspan=\"1\"><p>0</p></td><td colspan=\"1\" rowspan=\"1\"><p>0</p></td><td colspan=\"1\" rowspan=\"1\"><p>0</p></td><td colspan=\"1\" rowspan=\"1\"><p>0</p></td><td colspan=\"1\" rowspan=\"1\"><p>0</p></td></tr><tr><td colspan=\"1\" rowspan=\"1\"><p>0</p></td><td colspan=\"1\" rowspan=\"1\"><p>1</p></td><td colspan=\"1\" rowspan=\"1\"><p>0</p></td><td colspan=\"1\" rowspan=\"1\"><p>1</p></td><td colspan=\"1\" rowspan=\"1\"><p>1</p></td></tr><tr><td colspan=\"1\" rowspan=\"1\"><p>1</p></td><td colspan=\"1\" rowspan=\"1\"><p>0</p></td><td colspan=\"1\" rowspan=\"1\"><p>0</p></td><td colspan=\"1\" rowspan=\"1\"><p>1</p></td><td colspan=\"1\" rowspan=\"1\"><p>1</p></td></tr><tr><td colspan=\"1\" rowspan=\"1\"><p>1</p></td><td colspan=\"1\" rowspan=\"1\"><p>1</p></td><td colspan=\"1\" rowspan=\"1\"><p>1</p></td><td colspan=\"1\" rowspan=\"1\"><p>1</p></td><td colspan=\"1\" rowspan=\"1\"><p>0</p></td></tr></tbody></table>\n<blockquote><p>XOR은 AND, OR, NOT으로 만들 수 있는데, 왜 문제라는 걸까?</p></blockquote>\n<p>전자과라서 저는 여기서 먼저 걸렸습니다. 회로에서는 XOR을 (A OR B) AND NOT(A AND B)처럼 다른 게이트를 조합해 만듭니다. 그러니 '게이트로 XOR을 못 만든다'는 뜻일 리는 없습니다. 문제는 게이트가 아니라 <strong>퍼셉트론 하나</strong>였습니다.</p>\n<p>퍼셉트론 하나가 하는 일을 그림으로 그려 보면 이유가 보입니다. 입력이 <math-inline data-latex=\"x_1\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>x</mi><mn>1</mn></msub></mrow><annotation encoding=\"application/x-tex\">x_1</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline>, <math-inline data-latex=\"x_2\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>x</mi><mn>2</mn></msub></mrow><annotation encoding=\"application/x-tex\">x_2</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline> 두 개라면 입력 하나하나를 평면 위의 점으로 찍을 수 있습니다. 퍼셉트론은 <math-inline data-latex=\"w_1 x_1 + w_2 x_2 + b = 0\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>w</mi><mn>1</mn></msub><msub><mi>x</mi><mn>1</mn></msub><mo>+</mo><msub><mi>w</mi><mn>2</mn></msub><msub><mi>x</mi><mn>2</mn></msub><mo>+</mo><mi>b</mi><mo>=</mo><mn>0</mn></mrow><annotation encoding=\"application/x-tex\">w_1 x_1 + w_2 x_2 + b = 0</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.7333em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0269em;\">w</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.7333em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0269em;\">w</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6944em;\"></span><span class=\"mord mathnormal\">b</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">0</span></span></span></span></math-inline>인 곳을 경계로 한쪽은 1, 반대쪽은 0이라고 답합니다. 이 식은 <math-inline data-latex=\"x\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>x</mi></mrow><annotation encoding=\"application/x-tex\">x</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.4306em;\"></span><span class=\"mord mathnormal\">x</span></span></span></span></math-inline>가 1제곱으로만 들어간 <strong>일차식</strong>이라서, 경계가 곧은 직선이 됩니다. 입력이 세 개면 평면, 더 많으면 평평한 면이 되지만 휘지 않는다는 점은 같습니다. 이렇게 곧은 경계 하나로 나누는 것을 <strong>선형</strong>(linear)이라고 하고, 그래서 퍼셉트론 하나는 <strong>선형 분류기</strong>입니다.</p>\n<p>AND와 OR은 직선 하나로 1과 0을 가를 수 있습니다. 하지만 XOR은 1인 점 (0, 1), (1, 0)과 0인 점 (0, 0), (1, 1)이 대각선으로 엇갈려 있어서, 직선을 어떻게 그어도 한 점은 반대편에 남습니다.</p>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-xor-layers.2cc4903f40.1440.webp\" alt=\"위에는 퍼셉트론을 하나만 둔 구성, 한 층에 OR와 NAND를 나란히 둔 구성, 그 둘의 출력을 AND에 넣은 구성이 있고, 아래에는 각 구성이 입력 평면을 어떻게 나누는지 그렸다. 하나일 때는 (1, 1)이 틀린 쪽에 들어가고, 쌓았을 때만 두 직선 사이의 띠가 XOR의 1인 점 두 개를 감싼다.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"퍼셉트론 하나는 직선 하나로만 나눕니다(왼쪽). 한 층에 두 개를 나란히 두면 직선은 둘이지만 답도 둘로 따로 나옵니다(가운데). 첫 층의 출력을 AND에 넣으면 두 직선 사이만 1이 되어 XOR이 됩니다(오른쪽). 검은 점은 XOR이 1, 흰 점은 0인 입력입니다.\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-xor-layers.2cc4903f40.640.webp 640w, /data/img/fig-xor-layers.2cc4903f40.960.webp 960w, /data/img/fig-xor-layers.2cc4903f40.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"900\" data-img-tone=\"light\" decoding=\"async\"><figcaption>퍼셉트론 하나는 직선 하나로만 나눕니다(왼쪽). 한 층에 두 개를 나란히 두면 직선은 둘이지만 답도 둘로 따로 나옵니다(가운데). 첫 층의 출력을 AND에 넣으면 두 직선 사이만 1이 되어 XOR이 됩니다(오른쪽). 검은 점은 XOR이 1, 흰 점은 0인 입력입니다.</figcaption></figure>\n<h3>17년 뒤, 층을 쌓다</h3>\n<p>이 문제는 1986년에야 풀립니다. 강의에서는 퍼셉트론을 \"병렬로 놓지 말고 시퀀스로 놓으라\"고 설명합니다.</p>\n<blockquote><p><strong>처음에 헷갈렸던 점.</strong> 저는 이 말을 데이터를 한꺼번에 병렬로 처리하지 말고 순서대로 하나씩 처리하라는 뜻으로 이해했고, 그러면 XOR을 안 써도 되는 건가 싶었습니다. 둘 다 틀린 이해였습니다. 이 말은 데이터가 아니라 <strong>퍼셉트론을 놓는 방식</strong> 이야기입니다. 한 층에 여러 개를 나란히 놓으면 각자 직선을 하나씩 그을 뿐이고, 앞 퍼셉트론의 출력을 뒤 퍼셉트론의 입력으로 이어서 쌓아야 XOR이 됩니다. XOR을 피하는 게 아니라, 층을 쌓아서 XOR을 만들 수 있게 된 것입니다.</p></blockquote>\n<p>회로에서 XOR을 만드는 방법과 똑같습니다. 첫 층에 OR와 NAND를 나란히 두고, 둘의 출력을 AND에 넣습니다.</p>\n<math-block data-latex=\"\\begin{aligned} h_1 &amp;= \\operatorname{OR}(x_1, x_2) \\\\ h_2 &amp;= \\operatorname{NAND}(x_1, x_2) \\\\ y &amp;= \\operatorname{AND}(h_1, h_2) \\end{aligned}\"><span class=\"katex-display\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics><mtable rowspacing=\"0.25em\" columnalign=\"right left\" columnspacing=\"0em\"><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><msub><mi>h</mi><mn>1</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><mrow><mrow></mrow><mo>=</mo><mi mathvariant=\"normal\">OR</mi><mo>⁡</mo><mo stretchy=\"false\">(</mo><msub><mi>x</mi><mn>1</mn></msub><mo separator=\"true\">,</mo><msub><mi>x</mi><mn>2</mn></msub><mo stretchy=\"false\">)</mo></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><msub><mi>h</mi><mn>2</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><mrow><mrow></mrow><mo>=</mo><mi mathvariant=\"normal\">NAND</mi><mo>⁡</mo><mo stretchy=\"false\">(</mo><msub><mi>x</mi><mn>1</mn></msub><mo separator=\"true\">,</mo><msub><mi>x</mi><mn>2</mn></msub><mo stretchy=\"false\">)</mo></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><mi>y</mi></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><mrow><mrow></mrow><mo>=</mo><mi mathvariant=\"normal\">AND</mi><mo>⁡</mo><mo stretchy=\"false\">(</mo><msub><mi>h</mi><mn>1</mn></msub><mo separator=\"true\">,</mo><msub><mi>h</mi><mn>2</mn></msub><mo stretchy=\"false\">)</mo></mrow></mstyle></mtd></mtr></mtable><annotation encoding=\"application/x-tex\">\\begin{aligned} h_1 &amp;= \\operatorname{OR}(x_1, x_2) \\\\ h_2 &amp;= \\operatorname{NAND}(x_1, x_2) \\\\ y &amp;= \\operatorname{AND}(h_1, h_2) \\end{aligned}</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:4.2em;vertical-align:-1.85em;\"></span><span class=\"mord\"><span class=\"mtable\"><span class=\"col-align-r\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:2.35em;\"><span style=\"top:-4.51em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mord\"><span class=\"mord mathnormal\">h</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span><span style=\"top:-3.01em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mord\"><span class=\"mord mathnormal\">h</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span><span style=\"top:-1.51em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0359em;\">y</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.85em;\"><span></span></span></span></span></span><span class=\"col-align-l\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:2.35em;\"><span style=\"top:-4.51em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mord\"></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mop\"><span class=\"mord mathrm\">OR</span></span><span class=\"mopen\">(</span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mpunct\">,</span><span class=\"mspace\" style=\"margin-right:0.1667em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mclose\">)</span></span></span><span style=\"top:-3.01em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mord\"></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mop\"><span class=\"mord mathrm\">NAND</span></span><span class=\"mopen\">(</span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mpunct\">,</span><span class=\"mspace\" style=\"margin-right:0.1667em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mclose\">)</span></span></span><span style=\"top:-1.51em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mord\"></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mop\"><span class=\"mord mathrm\">AND</span></span><span class=\"mopen\">(</span><span class=\"mord\"><span class=\"mord mathnormal\">h</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mpunct\">,</span><span class=\"mspace\" style=\"margin-right:0.1667em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">h</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mclose\">)</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.85em;\"><span></span></span></span></span></span></span></span></span></span></span></span></math-block>\n<p>세 게이트는 모두 퍼셉트론 하나로 만들 수 있습니다. 가중치와 편향을 이렇게 두면 됩니다.</p>\n<math-block data-latex=\"\\begin{aligned} \\operatorname{OR} &amp;: \\; x_1 + x_2 - 0.5 &gt; 0 \\\\ \\operatorname{NAND} &amp;: \\; -x_1 - x_2 + 1.5 &gt; 0 \\\\ \\operatorname{AND} &amp;: \\; h_1 + h_2 - 1.5 &gt; 0 \\end{aligned}\"><span class=\"katex-display\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics><mtable rowspacing=\"0.25em\" columnalign=\"right left\" columnspacing=\"0em\"><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><mi mathvariant=\"normal\">OR</mi><mo>⁡</mo></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><mrow><mrow></mrow><mo>:</mo><mtext>  </mtext><msub><mi>x</mi><mn>1</mn></msub><mo>+</mo><msub><mi>x</mi><mn>2</mn></msub><mo>−</mo><mn>0.5</mn><mo>&gt;</mo><mn>0</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><mi mathvariant=\"normal\">NAND</mi><mo>⁡</mo></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><mrow><mrow></mrow><mo>:</mo><mtext>  </mtext><mo>−</mo><msub><mi>x</mi><mn>1</mn></msub><mo>−</mo><msub><mi>x</mi><mn>2</mn></msub><mo>+</mo><mn>1.5</mn><mo>&gt;</mo><mn>0</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><mi mathvariant=\"normal\">AND</mi><mo>⁡</mo></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><mrow><mrow></mrow><mo>:</mo><mtext>  </mtext><msub><mi>h</mi><mn>1</mn></msub><mo>+</mo><msub><mi>h</mi><mn>2</mn></msub><mo>−</mo><mn>1.5</mn><mo>&gt;</mo><mn>0</mn></mrow></mstyle></mtd></mtr></mtable><annotation encoding=\"application/x-tex\">\\begin{aligned} \\operatorname{OR} &amp;: \\; x_1 + x_2 - 0.5 &gt; 0 \\\\ \\operatorname{NAND} &amp;: \\; -x_1 - x_2 + 1.5 &gt; 0 \\\\ \\operatorname{AND} &amp;: \\; h_1 + h_2 - 1.5 &gt; 0 \\end{aligned}</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:4.2em;vertical-align:-1.85em;\"></span><span class=\"mord\"><span class=\"mtable\"><span class=\"col-align-r\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:2.35em;\"><span style=\"top:-4.51em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mop\"><span class=\"mord mathrm\">OR</span></span></span></span><span style=\"top:-3.01em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mop\"><span class=\"mord mathrm\">NAND</span></span></span></span><span style=\"top:-1.51em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mop\"><span class=\"mord mathrm\">AND</span></span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.85em;\"><span></span></span></span></span></span><span class=\"col-align-l\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:2.35em;\"><span style=\"top:-4.51em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mord\"></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">:</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">−</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\">0.5</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">&gt;</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mord\">0</span></span></span><span style=\"top:-3.01em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mord\"></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">:</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mord\">−</span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">−</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\">1.5</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">&gt;</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mord\">0</span></span></span><span style=\"top:-1.51em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mord\"></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">:</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">h</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">h</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">−</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\">1.5</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">&gt;</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mord\">0</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.85em;\"><span></span></span></span></span></span></span></span></span></span></span></span></math-block>\n<p>여기서 −0.5, 1.5, −1.5가 편향, 즉 문턱입니다. 위 그림의 두 직선이 바로 <math-inline data-latex=\"x_1 + x_2 = 0.5\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>x</mi><mn>1</mn></msub><mo>+</mo><msub><mi>x</mi><mn>2</mn></msub><mo>=</mo><mn>0.5</mn></mrow><annotation encoding=\"application/x-tex\">x_1 + x_2 = 0.5</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.7333em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">0.5</span></span></span></span></math-inline>와 <math-inline data-latex=\"x_1 + x_2 = 1.5\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>x</mi><mn>1</mn></msub><mo>+</mo><msub><mi>x</mi><mn>2</mn></msub><mo>=</mo><mn>1.5</mn></mrow><annotation encoding=\"application/x-tex\">x_1 + x_2 = 1.5</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.7333em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">1.5</span></span></span></span></math-inline>입니다. 왜 쌓으면 풀리는지는, 첫 층이 점들을 어디로 옮기는지 보면 알 수 있습니다.</p>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-xor-remap.23304e5003.1440.webp\" alt=\"왼쪽은 입력 평면의 점 네 개, 오른쪽은 첫 층을 지난 뒤의 평면. 오른쪽에서는 (0, 1)과 (1, 0)에서 온 점이 한 자리에 모이고, AND의 직선 하나가 그 점을 나머지 두 점과 가른다.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"첫 층(OR, NAND)은 점 네 개를 새 자리로 옮깁니다. (0, 1)과 (1, 0)이 한 점으로 모이고, 옮긴 뒤에는 AND의 직선 하나로 나뉩니다.\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-xor-remap.23304e5003.640.webp 640w, /data/img/fig-xor-remap.23304e5003.960.webp 960w, /data/img/fig-xor-remap.23304e5003.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"620\" data-img-tone=\"light\" decoding=\"async\"><figcaption>첫 층(OR, NAND)은 점 네 개를 새 자리로 옮깁니다. (0, 1)과 (1, 0)이 한 점으로 모이고, 옮긴 뒤에는 AND의 직선 하나로 나뉩니다.</figcaption></figure>\n<p>첫 층을 지나면 (0, 1)과 (1, 0)은 둘 다 (1, 1)로 모이고, (0, 0)은 (0, 1)로, (1, 1)은 (1, 0)으로 갑니다. 옮기고 나면 직선 하나로 갈라지니, 두 번째 층은 퍼셉트론 하나(AND)로 충분합니다. <strong>앞 층이 문제를 직선 하나로 풀리는 모양으로 바꿔 놓는 것</strong>, 이것이 층을 쌓는 이유입니다. 지난 글에서 은닉층이 작은 특징을 뽑아 다음 층에 넘긴다고 했던 것과 같은 이야기입니다.</p>\n<p>이렇게 퍼셉트론을 여러 층으로 쌓은 것을 <strong>다층 퍼셉트론</strong>(multilayer perceptron, MLP)이라고 부릅니다. 지난 글에 남겨 둔 질문 하나가 여기서 풀렸습니다.</p>\n<p>그런데 왜 17년이나 걸렸을까요? 층을 쌓으면 된다는 것 자체는 그때도 알려져 있었습니다. 막혀 있던 것은 <strong>쌓은 퍼셉트론들의 가중치를 데이터로 맞추는 방법</strong>이었습니다. 1986년 Rumelhart, Hinton, Williams가 알린 <strong>역전파</strong>(backpropagation)가 그 방법입니다. 역전파가 어떻게 동작하는지는 이 강의의 3강에서 다룹니다.</p>\n<p>강의에서는 XOR 문제 이후 연구가 얼어붙은 시기를 'AI 겨울'(AI winter)이라고 부릅니다. 다만 XOR 하나 때문이라기보다, 기대만큼 결과가 나오지 않고 연구비가 줄어든 일 등이 겹친 결과로 보는 편입니다.</p>\n<h3>2012년, AlexNet</h3>\n<p>딥러닝이 크게 주목받은 계기는 <strong>ImageNet</strong> 대회입니다. ImageNet은 사진 수백만 장에 무엇이 찍혀 있는지 사람이 하나하나 이름표(<strong>라벨</strong>, label)를 붙여 둔 데이터 모음이고, 대회에서는 사진을 보고 1,000가지 종류 중 정답을 맞힙니다.</p>\n<p>점수는 <strong>top-5 오류율</strong>로 냅니다. 모델이 후보를 5개까지 낼 수 있고, 그 5개 안에 정답이 없으면 틀린 것으로 칩니다. 2010년 우승팀의 오류율이 28.2%, 2011년이 25.8%였는데, 2012년 Alex Krizhevsky가 Ilya Sutskever, Geoffrey Hinton과 함께 만든 <strong>AlexNet</strong>이 16.4%를 냅니다. 2등이 26.2%였으니 10%p 가까이 차이가 났습니다.</p>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-imagenet.7ddeab6926.1440.webp\" alt=\"ImageNet 대회 top-5 오류율 막대그래프. 2010년 우승 28.2%, 2011년 우승 25.8%, 2012년 2등 26.2%, 2012년 AlexNet 16.4%.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"ImageNet 대회의 top-5 오류율(낮을수록 좋음). 2012년 AlexNet은 2등보다 10%p 가까이 낮았습니다.\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-imagenet.7ddeab6926.640.webp 640w, /data/img/fig-imagenet.7ddeab6926.960.webp 960w, /data/img/fig-imagenet.7ddeab6926.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"420\" data-img-tone=\"light\" decoding=\"async\"><figcaption>ImageNet 대회의 top-5 오류율(낮을수록 좋음). 2012년 AlexNet은 2등보다 10%p 가까이 낮았습니다.</figcaption></figure>\n<p>AlexNet은 층을 깊게 쌓은 신경망이었습니다. 이렇게 <strong>층을 깊게 쌓은 신경망을 큰 데이터로 학습시키는 것</strong>이 딥러닝입니다. 신경망이 진화한 모습이 딥러닝인 셈입니다. 큰 데이터(ImageNet)와 빠른 계산 장치(GPU)가 갖춰지면서, 1986년의 아이디어가 비로소 힘을 낸 것입니다.</p>\n\n<h2>2. 이미지 분류 — 정해진 보기 중에서 고르기</h2>\n<p>강의 뒷부분은 <strong>이미지 분류</strong>(image classification) 문제입니다. 이미지를 하나 주면 무엇이 찍혀 있는지 맞히는 문제인데, 주관식이 아니라 객관식에 가깝습니다. 고양이, 개, 배처럼 고를 수 있는 보기를 미리 정해 두고 그중 하나를 고릅니다. 이 보기 하나하나를 <strong>클래스</strong>(class)라고 부릅니다.</p>\n<blockquote><p>사진 속에 물체가 하나가 아니라 여러 개라면 어떻게 할까?</p></blockquote>\n<p>이미지 분류는 사진 한 장에 답 하나를 내는 문제라서, 여러 물체를 각각 찾는 일은 따로 풀어야 하는 문제입니다. 이 질문은 글 끝의 '앞으로 알아볼 것'에 남겨 둡니다.</p>\n<h3>컴퓨터가 보는 이미지</h3>\n<p>컴퓨터에게 이미지는 숫자 묶음입니다. 컬러 이미지는 픽셀마다 빨강(R), 초록(G), 파랑(B)의 밝기를 0–255 사이 숫자로 하나씩, 모두 세 개 가집니다. 그래서 이미지 한 장은 R, G, B 세 장의 숫자 표를 겹쳐 놓은 것으로 볼 수 있습니다. 이렇게 숫자를 가로세로로 늘어놓은 표를 <strong>행렬</strong>(matrix)이라고 합니다.</p>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-rgb.24f61e3db0.1440.webp\" alt=\"16 × 16 픽셀로 그린 사과 그림에서 네모 친 3 × 3 픽셀을 R, G, B 세 장의 숫자 표로 펼친 그림. 사과의 빨간 픽셀은 R이 190 안팎이고 G와 B는 40 안팎이다.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"16 × 16 컬러 이미지에서 네모 친 3 × 3 픽셀을 R, G, B 세 장으로 펼쳤습니다. 칸이 밝을수록 그 색이 강하고, 빨간 사과 부분은 R만 큽니다.\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-rgb.24f61e3db0.640.webp 640w, /data/img/fig-rgb.24f61e3db0.960.webp 960w, /data/img/fig-rgb.24f61e3db0.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"560\" data-img-tone=\"light\" decoding=\"async\"><figcaption>16 × 16 컬러 이미지에서 네모 친 3 × 3 픽셀을 R, G, B 세 장으로 펼쳤습니다. 칸이 밝을수록 그 색이 강하고, 빨간 사과 부분은 R만 큽니다.</figcaption></figure>\n<p>강의에서 예로 쓰는 CIFAR-10 데이터는 가로세로 32픽셀짜리 작은 컬러 이미지라서, 한 장이 <math-inline data-latex=\"32 \\times 32 \\times 3 = 3{,}072\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mn>32</mn><mo>×</mo><mn>32</mn><mo>×</mo><mn>3</mn><mo>=</mo><mn>3,072</mn></mrow><annotation encoding=\"application/x-tex\">32 \\times 32 \\times 3 = 3{,}072</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.7278em;vertical-align:-0.0833em;\"></span><span class=\"mord\">32</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">×</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.7278em;vertical-align:-0.0833em;\"></span><span class=\"mord\">32</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">×</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">3</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.8389em;vertical-align:-0.1944em;\"></span><span class=\"mord\">3</span><span class=\"mord\"><span class=\"mpunct\">,</span></span><span class=\"mord\">072</span></span></span></span></math-inline>개의 숫자입니다. 결국 우리가 만들 것은 숫자 3,072개를 받아 클래스 이름 하나를 돌려주는 함수입니다.</p>\n<pre><code>def classify_image(image):\n    # some magic here?\n    return class_label</code></pre>\n\n<h2>3. 규칙으로 짜려던 시도</h2>\n<blockquote><p>여기서 정렬(sorting)은 왜 나왔을까?</p></blockquote>\n<p>강의는 여기서 갑자기 정렬 이야기를 꺼냅니다. 처음에는 버블 정렬, 선택 정렬, 삽입 정렬, 셸 정렬처럼 정렬의 종류부터 떠올렸는데, 요점은 종류가 아니었습니다. 숫자를 크기순으로 정렬하는 일은 단계를 하나하나 코드로 적을 수 있습니다. 그런데 사진에서 고양이를 찾는 일은 그렇게 적을 방법이 보이지 않습니다. 이 차이를 보여 주려고 꺼낸 예였습니다.</p>\n<p>그래도 사람들은 규칙을 손으로 짜 보려고 했습니다. 먼저 이미지에서 <strong>가장자리</strong>(edge)를 찾습니다. 밝기가 급하게 바뀌는 곳을 찾는 Canny 엣지 검출 같은 방법이 대표적입니다. 그다음 가장자리가 꺾이는 모서리(corner)를 찾고, \"삼각형 귀가 두 개면 고양이\" 같은 규칙을 적는 식입니다.</p>\n<p>강의에서는 이런 시도가 전부 실패했다고 합니다. 같은 고양이라도 보는 방향, 조명, 자세, 배경에 따라 전혀 다른 숫자 묶음이 되고, 클래스가 하나 늘 때마다 규칙을 처음부터 다시 짜야 하기 때문입니다.</p>\n\n<h2>4. 데이터 기반 접근 — 규칙 대신 예시</h2>\n<p>그래서 나온 것이 <strong>데이터 기반 접근</strong>(data-driven approach)입니다. 일상에서는 이 말을 감 대신 데이터와 수치로 결정한다는 뜻으로 많이 쓰는데, 여기서는 뜻이 조금 다릅니다. 규칙을 사람이 코드로 적는 대신, <strong>정답이 달린 예시를 많이 모아서 컴퓨터가 규칙을 찾게 하는 것</strong>입니다. 강의 노트는 아이에게 고양이 사진을 많이 보여 주며 고양이를 알려 주는 것에 비유합니다.</p>\n<ol><li><p>이미지와 정답(라벨)을 클래스별로 모읍니다. 이것을 <strong>학습 데이터</strong>(training set)라고 합니다.</p></li><li><p>학습 데이터로 분류기를 학습시킵니다.</p></li><li><p>처음 보는 이미지를 넣어 분류기가 정답을 맞히는지 확인합니다.</p></li></ol>\n<p>그래서 이제 짜야 할 함수는 하나가 아니라 두 개입니다. 학습하는 함수 train과, 실제로 예측하는 함수 predict입니다.</p>\n<pre><code>def train(images, labels):\n    # some machine learning\n    return model\n\ndef predict(model, image):\n    # use the model\n    return predicted_label</code></pre>\n<blockquote><p>강의에서는 '이걸' 만드는 방법이 최근접 이웃 분류기라고 하는데, 이게 뭘까?</p></blockquote>\n<p>다시 보니 '이것'은 train과 predict, 두 함수의 속을 실제로 채운 것, 즉 <strong>분류기</strong>(classifier, 모델)였습니다. 지금부터 볼 최근접 이웃 분류기는 이 두 함수를 채우는 가장 단순한 방법입니다.</p>\n\n<h2>5. 최근접 이웃 분류기</h2>\n<p>이름 그대로 <strong>가장 가까운 이웃을 보고 답하는 분류기</strong>입니다. 정답을 알려 주며 배우게 하는 방식을 <strong>지도 학습</strong>(supervised learning)이라고 하는데, 그 가장 단순한 예입니다.</p>\n<ul><li><p><strong>train</strong>: 학습 데이터를 전부 그대로 기억합니다. 그게 끝입니다.</p></li><li><p><strong>predict</strong>: 새 이미지가 들어오면 기억해 둔 이미지 전부와 비교해서, <strong>가장 가까운 이미지의 라벨</strong>을 답으로 냅니다.</p></li></ul>\n<p>여기서 바로 질문이 생깁니다.</p>\n<blockquote><p>이미지끼리 '가깝다'는 건 무슨 기준일까?</p></blockquote>\n<p>이미지는 숫자 표이니, 두 이미지의 같은 자리 숫자끼리 비교하면 됩니다. 이렇게 같은 자리끼리 하는 계산을 <strong>원소별</strong>(element-wise) 계산이라고 합니다. 같은 자리끼리 빼고, 그 차이에서 부호를 떼고(절댓값), 모두 더하면 숫자 하나가 나옵니다. 이 숫자를 두 이미지 사이의 <strong>거리</strong>(distance)로 씁니다.</p>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-l1.120e086dee.1440.webp\" alt=\"4 × 4 테스트 이미지에서 4 × 4 학습 이미지를 칸마다 빼고 절댓값을 취한 표를 만든 뒤, 16칸을 모두 더해 182가 나오는 계산.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"같은 자리끼리 빼고, 부호를 떼고, 모두 더합니다. 이 예에서 두 이미지의 거리는 182입니다. 색 채널 하나만 그렸습니다.\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-l1.120e086dee.640.webp 640w, /data/img/fig-l1.120e086dee.960.webp 960w, /data/img/fig-l1.120e086dee.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"460\" data-img-tone=\"light\" decoding=\"async\"><figcaption>같은 자리끼리 빼고, 부호를 떼고, 모두 더합니다. 이 예에서 두 이미지의 거리는 182입니다. 색 채널 하나만 그렸습니다.</figcaption></figure>\n<p>주의할 점이 하나 있습니다. 이 숫자는 '얼마나 일치하는가'가 아니라 <strong>'얼마나 다른가'</strong>입니다. 그래서 <strong>작을수록 비슷</strong>하고, 똑같은 이미지끼리는 0이 나옵니다. 저는 처음에 이걸 '일치율'이라고 적어 두었는데, 방향이 반대였습니다.</p>\n<p>이 거리를 쓰는 분류기를 코드로 쓰면 이렇습니다. 이해를 돕기 위한 코드라서 반복문으로 하나씩 비교합니다.</p>\n<pre><code>import numpy as np\nimport sys\n\nclass NearestNeighbor:\n    def __init__(self):\n        pass\n\n    def train(self, images, labels):\n        # simply remembers all the training data\n        self.images = images\n        self.labels = labels\n\n    def predict(self, test_image):\n        # assume that each image is vectorized to 1D\n        min_dist = sys.maxsize  # maximum integer in python\n\n        for i in range(self.images.shape[0]):\n            dist = np.sum(np.abs(self.images[i, :] - test_image))\n            if dist &lt; min_dist:\n                min_dist = dist\n                min_index = i\n\n        return self.labels[min_index]</code></pre>\n<p>predict가 하는 일을 풀어 쓰면 이렇습니다. 이미지는 미리 숫자 3,072개짜리 한 줄로 펴 두었다고 가정합니다.</p>\n<ol><li><p>지금까지 찾은 가장 작은 거리 <code>min_dist</code>를 아주 큰 수로 시작합니다.</p></li><li><p>저장된 이미지(<code>self.images</code>)를 처음부터 끝까지 돌며 테스트 이미지와의 거리를 잽니다.</p></li><li><p>지금까지보다 더 가까운 이미지가 나오면, 그 거리와 번호(<code>min_index</code>)로 바꿔 적습니다.</p></li><li><p>다 돌고 나면 가장 가까운 이미지의 번호가 남고, 그 번호의 라벨을 돌려줍니다.</p></li></ol>\n<p>실제로 쓸 때는 반복문 대신 행렬 계산으로 한꺼번에 처리합니다. 넘파이(numpy)가 이런 계산을 한 번에 해 주어서 훨씬 빠릅니다.</p>\n<pre><code>def predict(self, test_image):\n    dists = np.sum(np.abs(self.images - test_image), axis=1)  # N distances at once\n    return self.labels[np.argmin(dists)]</code></pre>\n<p>직접 돌려 볼 때 함정이 하나 있습니다. 이미지를 0–255 정수 자료형(uint8)으로 읽은 채로 빼면 3 − 5가 −2가 아니라 254가 됩니다. 이 자료형은 음수를 담지 못해서 값이 한 바퀴 돌아 버리기 때문입니다. 빼기 전에 실수(float)로 바꿔 두어야 합니다.</p>\n<p>강의 노트에 따르면 이 분류기를 CIFAR-10(학습 이미지 5만 장, 클래스 10개)에 돌리면 약 38.6%를 맞힙니다. 아무렇게나 찍으면 10%이니 무언가를 배운 것은 맞지만, 사람(약 94%)과는 거리가 멉니다.</p>\n\n<h2>6. L1 거리와 L2 거리</h2>\n<p>위에서 쓴 거리(빼고, 절댓값, 다 더하기)를 <strong>L1 거리</strong>라고 부릅니다. 강의에는 <strong>L2 거리</strong>도 나옵니다. 둘 다 두 이미지가 얼마나 다른지를 숫자 하나로 나타내는 방법이고, 차이를 모으는 방식만 다릅니다.</p>\n<math-block data-latex=\"d_1(I_1, I_2) = \\sum_p \\left| I_1^p - I_2^p \\right|\"><span class=\"katex-display\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics><mrow><msub><mi>d</mi><mn>1</mn></msub><mo stretchy=\"false\">(</mo><msub><mi>I</mi><mn>1</mn></msub><mo separator=\"true\">,</mo><msub><mi>I</mi><mn>2</mn></msub><mo stretchy=\"false\">)</mo><mo>=</mo><munder><mo>∑</mo><mi>p</mi></munder><mrow><mo fence=\"true\">∣</mo><msubsup><mi>I</mi><mn>1</mn><mi>p</mi></msubsup><mo>−</mo><msubsup><mi>I</mi><mn>2</mn><mi>p</mi></msubsup><mo fence=\"true\">∣</mo></mrow></mrow><annotation encoding=\"application/x-tex\">d_1(I_1, I_2) = \\sum_p \\left| I_1^p - I_2^p \\right|</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1em;vertical-align:-0.25em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">d</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mopen\">(</span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mpunct\">,</span><span class=\"mspace\" style=\"margin-right:0.1667em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mclose\">)</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:2.4361em;vertical-align:-1.3861em;\"></span><span class=\"mop op-limits\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.05em;\"><span style=\"top:-1.9em;margin-left:0em;\"><span class=\"pstrut\" style=\"height:3.05em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">p</span></span></span><span style=\"top:-3.05em;\"><span class=\"pstrut\" style=\"height:3.05em;\"></span><span><span class=\"mop op-symbol large-op\">∑</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.3861em;\"><span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.1667em;\"></span><span class=\"minner\"><span class=\"mopen delimcenter\" style=\"top:0em;\">∣</span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.7823em;\"><span style=\"top:-2.4337em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span><span style=\"top:-3.1809em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">p</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.2663em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">−</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.7823em;\"><span style=\"top:-2.4337em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span><span style=\"top:-3.1809em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">p</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.2663em;\"><span></span></span></span></span></span></span><span class=\"mclose delimcenter\" style=\"top:0em;\">∣</span></span></span></span></span></span></math-block>\n<math-block data-latex=\"d_2(I_1, I_2) = \\sqrt{\\sum_p \\left( I_1^p - I_2^p \\right)^2}\"><span class=\"katex-display\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics><mrow><msub><mi>d</mi><mn>2</mn></msub><mo stretchy=\"false\">(</mo><msub><mi>I</mi><mn>1</mn></msub><mo separator=\"true\">,</mo><msub><mi>I</mi><mn>2</mn></msub><mo stretchy=\"false\">)</mo><mo>=</mo><msqrt><mrow><munder><mo>∑</mo><mi>p</mi></munder><msup><mrow><mo fence=\"true\">(</mo><msubsup><mi>I</mi><mn>1</mn><mi>p</mi></msubsup><mo>−</mo><msubsup><mi>I</mi><mn>2</mn><mi>p</mi></msubsup><mo fence=\"true\">)</mo></mrow><mn>2</mn></msup></mrow></msqrt></mrow><annotation encoding=\"application/x-tex\">d_2(I_1, I_2) = \\sqrt{\\sum_p \\left( I_1^p - I_2^p \\right)^2}</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1em;vertical-align:-0.25em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">d</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mopen\">(</span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mpunct\">,</span><span class=\"mspace\" style=\"margin-right:0.1667em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mclose\">)</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:3.04em;vertical-align:-1.5742em;\"></span><span class=\"mord sqrt\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.4658em;\"><span class=\"svg-align\" style=\"top:-5em;\"><span class=\"pstrut\" style=\"height:5em;\"></span><span class=\"mord\" style=\"padding-left:1em;\"><span class=\"mop op-limits\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.05em;\"><span style=\"top:-1.9em;margin-left:0em;\"><span class=\"pstrut\" style=\"height:3.05em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">p</span></span></span><span style=\"top:-3.05em;\"><span class=\"pstrut\" style=\"height:3.05em;\"></span><span><span class=\"mop op-symbol large-op\">∑</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.3861em;\"><span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.1667em;\"></span><span class=\"minner\"><span class=\"minner\"><span class=\"mopen delimcenter\" style=\"top:0em;\">(</span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.7823em;\"><span style=\"top:-2.4337em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span><span style=\"top:-3.1809em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">p</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.2663em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">−</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.7823em;\"><span style=\"top:-2.4337em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span><span style=\"top:-3.1809em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">p</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.2663em;\"><span></span></span></span></span></span></span><span class=\"mclose delimcenter\" style=\"top:0em;\">)</span></span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.9863em;\"><span style=\"top:-3.2352em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span></span></span></span></span></span></span><span style=\"top:-3.4258em;\"><span class=\"pstrut\" style=\"height:5em;\"></span><span class=\"hide-tail\" style=\"min-width:1.02em;height:3.08em;\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"400em\" height=\"3.08em\" viewBox=\"0 0 400000 3240\" preserveAspectRatio=\"xMinYMin slice\"><path d=\"M473,2793\nc339.3,-1799.3,509.3,-2700,510,-2702 l0 -0\nc3.3,-7.3,9.3,-11,18,-11 H400000v40H1017.7\ns-90.5,478,-276.2,1466c-185.7,988,-279.5,1483,-281.5,1485c-2,6,-10,9,-24,9\nc-8,0,-12,-0.7,-12,-2c0,-1.3,-5.3,-32,-16,-92c-50.7,-293.3,-119.7,-693.3,-207,-1200\nc0,-1.3,-5.3,8.7,-16,30c-10.7,21.3,-21.3,42.7,-32,64s-16,33,-16,33s-26,-26,-26,-26\ns76,-153,76,-153s77,-151,77,-151c0.7,0.7,35.7,202,105,604c67.3,400.7,102,602.7,104,\n606zM1001 80h400000v40H1017.7z\"/></svg></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.5742em;\"><span></span></span></span></span></span></span></span></span></span></math-block>\n<p>기호를 하나씩 읽어 보겠습니다. <math-inline data-latex=\"I_1\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>I</mi><mn>1</mn></msub></mrow><annotation encoding=\"application/x-tex\">I_1</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.8333em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline>과 <math-inline data-latex=\"I_2\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>I</mi><mn>2</mn></msub></mrow><annotation encoding=\"application/x-tex\">I_2</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.8333em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline>는 비교할 두 이미지이고, <math-inline data-latex=\"p\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>p</mi></mrow><annotation encoding=\"application/x-tex\">p</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.625em;vertical-align:-0.1944em;\"></span><span class=\"mord mathnormal\">p</span></span></span></span></math-inline>는 픽셀 자리 하나를 가리킵니다. <math-inline data-latex=\"I_1^p\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msubsup><mi>I</mi><mn>1</mn><mi>p</mi></msubsup></mrow><annotation encoding=\"application/x-tex\">I_1^p</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1.0486em;vertical-align:-0.2663em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.7823em;\"><span style=\"top:-2.4337em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span><span style=\"top:-3.1809em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">p</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.2663em;\"><span></span></span></span></span></span></span></span></span></span></math-inline>는 이미지 <math-inline data-latex=\"I_1\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>I</mi><mn>1</mn></msub></mrow><annotation encoding=\"application/x-tex\">I_1</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.8333em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline>의 <math-inline data-latex=\"p\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>p</mi></mrow><annotation encoding=\"application/x-tex\">p</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.625em;vertical-align:-0.1944em;\"></span><span class=\"mord mathnormal\">p</span></span></span></span></math-inline>번째 숫자입니다. <math-inline data-latex=\"\\sum_p\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mo>∑</mo><mi>p</mi></msub></mrow><annotation encoding=\"application/x-tex\">\\sum_p</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1.1858em;vertical-align:-0.4358em;\"></span><span class=\"mop\"><span class=\"mop op-symbol small-op\" style=\"position:relative;top:0em;\">∑</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.0017em;\"><span style=\"top:-2.4003em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">p</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.4358em;\"><span></span></span></span></span></span></span></span></span></span></math-inline>(시그마)는 \"모든 자리 <math-inline data-latex=\"p\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>p</mi></mrow><annotation encoding=\"application/x-tex\">p</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.625em;vertical-align:-0.1944em;\"></span><span class=\"mord mathnormal\">p</span></span></span></span></math-inline>에 대해 더한다\"는 기호입니다.</p>\n<ul><li><p><strong>L1 거리</strong>(맨해튼 거리): 차이의 절댓값을 모두 더합니다.</p></li><li><p><strong>L2 거리</strong>(유클리드 거리): 차이를 제곱해서 모두 더한 뒤 제곱근을 씌웁니다. 학교에서 배운 두 점 사이의 직선거리, 피타고라스 정리와 같은 계산입니다.</p></li></ul>\n<p>코드에서는 거리를 구하는 한 줄만 바꾸면 L2가 됩니다.</p>\n<pre><code>dists = np.sqrt(np.sum((self.images - test_image) ** 2, axis=1))</code></pre>\n<p>픽셀이 두 개뿐인 이미지를 생각하면 둘의 차이가 눈에 보입니다. 이미지 하나가 평면 위의 점 하나가 되기 때문입니다. (0, 0)에서 (3, 4)까지 재면 L1은 <math-inline data-latex=\"3 + 4 = 7\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mn>3</mn><mo>+</mo><mn>4</mn><mo>=</mo><mn>7</mn></mrow><annotation encoding=\"application/x-tex\">3 + 4 = 7</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.7278em;vertical-align:-0.0833em;\"></span><span class=\"mord\">3</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">4</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">7</span></span></span></span></math-inline>, L2는 <math-inline data-latex=\"\\sqrt{3^2 + 4^2} = 5\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msqrt><mrow><msup><mn>3</mn><mn>2</mn></msup><mo>+</mo><msup><mn>4</mn><mn>2</mn></msup></mrow></msqrt><mo>=</mo><mn>5</mn></mrow><annotation encoding=\"application/x-tex\">\\sqrt{3^2 + 4^2} = 5</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1.04em;vertical-align:-0.1266em;\"></span><span class=\"mord sqrt\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.9134em;\"><span class=\"svg-align\" style=\"top:-3em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\" style=\"padding-left:0.833em;\"><span class=\"mord\"><span class=\"mord\">3</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.7401em;\"><span style=\"top:-2.989em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\"><span class=\"mord\">4</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.7401em;\"><span style=\"top:-2.989em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span></span></span></span></span></span></span><span style=\"top:-2.8734em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"hide-tail\" style=\"min-width:0.853em;height:1.08em;\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"400em\" height=\"1.08em\" viewBox=\"0 0 400000 1080\" preserveAspectRatio=\"xMinYMin slice\"><path d=\"M95,702\nc-2.7,0,-7.17,-2.7,-13.5,-8c-5.8,-5.3,-9.5,-10,-9.5,-14\nc0,-2,0.3,-3.3,1,-4c1.3,-2.7,23.83,-20.7,67.5,-54\nc44.2,-33.3,65.8,-50.3,66.5,-51c1.3,-1.3,3,-2,5,-2c4.7,0,8.7,3.3,12,10\ns173,378,173,378c0.7,0,35.3,-71,104,-213c68.7,-142,137.5,-285,206.5,-429\nc69,-144,104.5,-217.7,106.5,-221\nl0 -0\nc5.3,-9.3,12,-14,20,-14\nH400000v40H845.2724\ns-225.272,467,-225.272,467s-235,486,-235,486c-2.7,4.7,-9,7,-19,7\nc-6,0,-10,-1,-12,-3s-194,-422,-194,-422s-65,47,-65,47z\nM834 80h400000v40h-400000z\"/></svg></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.1266em;\"><span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">5</span></span></span></span></math-inline>입니다. L1은 바둑판처럼 난 길을 따라 걷는 거리이고, L2는 곧장 가로지르는 거리입니다. 맨해튼 거리라는 이름도 바둑판 모양 도로를 따라 달리는 택시에서 왔습니다.</p>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-l1-l2.b7a9fc01a0.1440.webp\" alt=\"왼쪽: (0, 0)에서 (3, 4)까지 격자를 따라가는 L1 경로(7)와 곧장 가는 L2 선(5). 가운데: 중심에서 L1 거리가 1인 점은 마름모, L2 거리가 1인 점은 원. 오른쪽: 차이가 10씩 네 곳에 퍼진 B와 한 곳에 40이 몰린 C의 막대. L1은 둘 다 40, L2는 B가 20, C가 40.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"왼쪽: 같은 두 점이라도 L1은 7, L2는 5입니다. 가운데: 중심에서 거리가 1인 점을 모으면 L1은 마름모, L2는 원이 됩니다. 오른쪽: 차이를 고르게 나눈 B와 한 곳에 몬 C는 L1이 같지만, L2는 C를 두 배 멀다고 봅니다.\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-l1-l2.b7a9fc01a0.640.webp 640w, /data/img/fig-l1-l2.b7a9fc01a0.960.webp 960w, /data/img/fig-l1-l2.b7a9fc01a0.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"590\" data-img-tone=\"light\" decoding=\"async\"><figcaption>왼쪽: 같은 두 점이라도 L1은 7, L2는 5입니다. 가운데: 중심에서 거리가 1인 점을 모으면 L1은 마름모, L2는 원이 됩니다. 오른쪽: 차이를 고르게 나눈 B와 한 곳에 몬 C는 L1이 같지만, L2는 C를 두 배 멀다고 봅니다.</figcaption></figure>\n<p>이미지에서 정말 차이가 나는 곳은 <strong>차이가 고르게 퍼져 있는지, 한 곳에 몰려 있는지</strong>입니다.</p>\n<table style=\"min-width: 100px;\"><colgroup><col style=\"min-width: 25px;\"><col style=\"min-width: 25px;\"><col style=\"min-width: 25px;\"><col style=\"min-width: 25px;\"></colgroup><tbody><tr><th colspan=\"1\" rowspan=\"1\"><p>비교 대상</p></th><th colspan=\"1\" rowspan=\"1\"><p>픽셀별 차이</p></th><th colspan=\"1\" rowspan=\"1\"><p>L1</p></th><th colspan=\"1\" rowspan=\"1\"><p>L2</p></th></tr><tr><td colspan=\"1\" rowspan=\"1\"><p>B</p></td><td colspan=\"1\" rowspan=\"1\"><p>10, 10, 10, 10</p></td><td colspan=\"1\" rowspan=\"1\"><p>40</p></td><td colspan=\"1\" rowspan=\"1\"><p>20</p></td></tr><tr><td colspan=\"1\" rowspan=\"1\"><p>C</p></td><td colspan=\"1\" rowspan=\"1\"><p>40, 0, 0, 0</p></td><td colspan=\"1\" rowspan=\"1\"><p>40</p></td><td colspan=\"1\" rowspan=\"1\"><p>40</p></td></tr></tbody></table>\n<p>L1은 B와 C를 똑같이 멀다고 봅니다. L2는 제곱 때문에 큰 차이에 더 큰 벌점을 주어서, 한 곳이 크게 다른 C를 두 배 멀다고 봅니다. 강의 노트의 표현을 빌리면, L2는 큰 차이 하나보다 중간 크기 차이 여러 개를 더 너그럽게 봅니다.</p>\n<p>어느 쪽이 늘 낫다는 정답은 없습니다. CIFAR-10에서는 L1이 38.6%, L2가 35.4%로 L1이 조금 나았지만, 데이터가 바뀌면 결과도 바뀝니다. 그래서 어떤 거리를 쓸지도 직접 시험해 보고 골라야 하는 선택지입니다. 이 이야기는 9장에서 이어집니다.</p>\n<p>L1, L2라는 이름은 수학의 Lp 노름에서 왔습니다. <strong>노름</strong>(norm)은 벡터, 즉 숫자를 한 줄로 세운 것의 크기를 재는 방법입니다.</p>\n<math-block data-latex=\"\\lVert x \\rVert_p = \\left( \\sum_i |x_i|^p \\right)^{1/p}\"><span class=\"katex-display\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics><mrow><mo stretchy=\"false\">∥</mo><mi>x</mi><msub><mo stretchy=\"false\">∥</mo><mi>p</mi></msub><mo>=</mo><msup><mrow><mo fence=\"true\">(</mo><munder><mo>∑</mo><mi>i</mi></munder><mi mathvariant=\"normal\">∣</mi><msub><mi>x</mi><mi>i</mi></msub><msup><mi mathvariant=\"normal\">∣</mi><mi>p</mi></msup><mo fence=\"true\">)</mo></mrow><mrow><mn>1</mn><mi mathvariant=\"normal\">/</mi><mi>p</mi></mrow></msup></mrow><annotation encoding=\"application/x-tex\">\\lVert x \\rVert_p = \\left( \\sum_i |x_i|^p \\right)^{1/p}</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1.0361em;vertical-align:-0.2861em;\"></span><span class=\"mopen\">∥</span><span class=\"mord mathnormal\">x</span><span class=\"mclose\"><span class=\"mclose\">∥</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.1514em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">p</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.2861em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:3.3056em;vertical-align:-1.2777em;\"></span><span class=\"minner\"><span class=\"minner\"><span class=\"mopen delimcenter\" style=\"top:0em;\"><span class=\"delimsizing size4\">(</span></span><span class=\"mop op-limits\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.05em;\"><span style=\"top:-1.8723em;margin-left:0em;\"><span class=\"pstrut\" style=\"height:3.05em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">i</span></span></span><span style=\"top:-3.05em;\"><span class=\"pstrut\" style=\"height:3.05em;\"></span><span><span class=\"mop op-symbol large-op\">∑</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.2777em;\"><span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.1667em;\"></span><span class=\"mord\">∣</span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3117em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">i</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mord\"><span class=\"mord\">∣</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.7144em;\"><span style=\"top:-3.113em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">p</span></span></span></span></span></span></span></span><span class=\"mclose delimcenter\" style=\"top:0em;\"><span class=\"delimsizing size4\">)</span></span></span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:2.0279em;\"><span style=\"top:-4.2029em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\"><span class=\"mord mtight\">1/</span><span class=\"mord mathnormal mtight\">p</span></span></span></span></span></span></span></span></span></span></span></span></span></math-block>\n<p>여기에 <math-inline data-latex=\"p = 1\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>p</mi><mo>=</mo><mn>1</mn></mrow><annotation encoding=\"application/x-tex\">p = 1</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.625em;vertical-align:-0.1944em;\"></span><span class=\"mord mathnormal\">p</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">1</span></span></span></span></math-inline>을 넣으면 L1, <math-inline data-latex=\"p = 2\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>p</mi><mo>=</mo><mn>2</mn></mrow><annotation encoding=\"application/x-tex\">p = 2</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.625em;vertical-align:-0.1944em;\"></span><span class=\"mord mathnormal\">p</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">2</span></span></span></span></math-inline>를 넣으면 L2가 됩니다. 두 이미지의 차이를 한 줄로 세운 다음 그 크기를 잰 것이 두 이미지 사이의 거리입니다. 덧붙여, 가장 가까운 이미지를 찾기만 할 때는 L2의 제곱근을 빼도 됩니다. 제곱근은 크기 순서를 바꾸지 않아서, 가장 가까운 이미지는 그대로이기 때문입니다.</p>\n\n<h2>7. 시간 복잡도 — 학습은 빠른데 예측이 느리다</h2>\n<p>이제 이 분류기가 얼마나 빠른지 보겠습니다. 알고리즘의 속도는 보통 <strong>시간 복잡도</strong>(time complexity)로 말합니다. 데이터가 <math-inline data-latex=\"N\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>N</mi></mrow><annotation encoding=\"application/x-tex\">N</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.6833em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.109em;\">N</span></span></span></span></math-inline>개일 때 걸리는 시간이 <math-inline data-latex=\"N\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>N</mi></mrow><annotation encoding=\"application/x-tex\">N</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.6833em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.109em;\">N</span></span></span></span></math-inline>에 따라 어떻게 늘어나는지를 대략 나타낸 것으로, 빅오 표기법(big-O)으로 씁니다.</p>\n<ul><li><p><math-inline data-latex=\"O(1)\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>O</mi><mo stretchy=\"false\">(</mo><mn>1</mn><mo stretchy=\"false\">)</mo></mrow><annotation encoding=\"application/x-tex\">O(1)</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1em;vertical-align:-0.25em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.0278em;\">O</span><span class=\"mopen\">(</span><span class=\"mord\">1</span><span class=\"mclose\">)</span></span></span></span></math-inline>: <math-inline data-latex=\"N\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>N</mi></mrow><annotation encoding=\"application/x-tex\">N</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.6833em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.109em;\">N</span></span></span></span></math-inline>이 얼마든 걸리는 시간이 같습니다.</p></li><li><p><math-inline data-latex=\"O(N)\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>O</mi><mo stretchy=\"false\">(</mo><mi>N</mi><mo stretchy=\"false\">)</mo></mrow><annotation encoding=\"application/x-tex\">O(N)</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1em;vertical-align:-0.25em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.0278em;\">O</span><span class=\"mopen\">(</span><span class=\"mord mathnormal\" style=\"margin-right:0.109em;\">N</span><span class=\"mclose\">)</span></span></span></span></math-inline>: <math-inline data-latex=\"N\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>N</mi></mrow><annotation encoding=\"application/x-tex\">N</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.6833em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.109em;\">N</span></span></span></span></math-inline>이 두 배가 되면 시간도 두 배가 됩니다.</p></li></ul>\n<p>최근접 이웃 분류기의 train은 <math-inline data-latex=\"O(1)\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>O</mi><mo stretchy=\"false\">(</mo><mn>1</mn><mo stretchy=\"false\">)</mo></mrow><annotation encoding=\"application/x-tex\">O(1)</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1em;vertical-align:-0.25em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.0278em;\">O</span><span class=\"mopen\">(</span><span class=\"mord\">1</span><span class=\"mclose\">)</span></span></span></span></math-inline>입니다. 데이터를 실제로 읽어 들이는 시간은 <math-inline data-latex=\"N\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>N</mi></mrow><annotation encoding=\"application/x-tex\">N</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.6833em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.109em;\">N</span></span></span></span></math-inline>에 따라 늘겠지만, 수업에서는 그 시간은 빼고 데이터를 포인터로 가리켜 두기만 한다고 보았습니다. 따로 계산할 것도 없습니다. 반면 predict는 들어온 이미지를 저장된 <math-inline data-latex=\"N\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>N</mi></mrow><annotation encoding=\"application/x-tex\">N</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.6833em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.109em;\">N</span></span></span></span></math-inline>장 전부와 비교해야 하니 <math-inline data-latex=\"O(N)\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>O</mi><mo stretchy=\"false\">(</mo><mi>N</mi><mo stretchy=\"false\">)</mo></mrow><annotation encoding=\"application/x-tex\">O(N)</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1em;vertical-align:-0.25em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.0278em;\">O</span><span class=\"mopen\">(</span><span class=\"mord mathnormal\" style=\"margin-right:0.109em;\">N</span><span class=\"mclose\">)</span></span></span></span></math-inline>입니다. 정확히는 한 장을 비교할 때마다 숫자 3,072개를 다 보니, <math-inline data-latex=\"N \\times 3{,}072\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>N</mi><mo>×</mo><mn>3,072</mn></mrow><annotation encoding=\"application/x-tex\">N \\times 3{,}072</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.7667em;vertical-align:-0.0833em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.109em;\">N</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">×</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.8389em;vertical-align:-0.1944em;\"></span><span class=\"mord\">3</span><span class=\"mord\"><span class=\"mpunct\">,</span></span><span class=\"mord\">072</span></span></span></span></math-inline>번쯤 계산합니다.</p>\n<blockquote><p>학습은 빠르고 예측이 느리면, 좋은 시스템일까?</p></blockquote>\n<p>제 생각에는 좋지 않아 보였습니다. 학습은 한 번 해 두면 끝나지만, 예측은 쓸 때마다 하기 때문입니다. 유튜브를 예로 들어 보겠습니다. 지금 보는 영상과 비슷한 영상을 추천해 주는 것이 예측입니다. 그런데 추천할 때마다 유튜브의 모든 영상과 하나하나 비교한다면, 제대로 돌아갈 리가 없습니다. 실제로 쓰려면 학습이 오래 걸리더라도 예측이 빨라야 합니다.</p>\n<p>강의 노트를 다시 보니 결론이 같았습니다. 노트는 이런 구조를 \"거꾸로 됐다\"고 하고, 앞으로 배울 신경망은 정반대라고 설명합니다. 학습에는 시간이 많이 들지만, 학습이 끝나면 새 이미지 하나를 분류하는 일은 아주 빠릅니다. 스스로 먼저 낸 결론이 수업의 결론과 같아서 조금 뿌듯했습니다.</p>\n<p>궁금해서 찾아보니, 유튜브 추천도 '가까운 것 찾기'를 쓰고 있었습니다. 2016년 구글이 발표한 <a target=\"_blank\" rel=\"noopener\" href=\"https://research.google/pubs/pub45530/\">유튜브 추천 논문</a>을 보면, 영상과 사용자를 학습으로 만든 짧은 숫자 목록으로 바꿔 두고, 추천할 때 가까운 영상을 찾습니다. 다만 모든 영상과 일일이 비교하지 않고, 정확도를 조금 내주는 대신 훨씬 빨리 찾는 방법을 씁니다. 11장에서 볼 근사 최근접 이웃입니다. 매번 전부 비교하면 안 된다는 직감은 맞았고, 해결책은 강의 끝에 나와 있었습니다.</p>\n\n<h2>8. 결정 경계와 k-최근접 이웃</h2>\n<p>분류기가 실제로 어떻게 답하는지 보려면 <strong>결정 경계</strong>(decision boundary)를 그려 보면 됩니다. 입력이 될 수 있는 모든 자리에 대해 분류기가 무엇이라고 답하는지 칠해 보면, 답이 바뀌는 선이 생깁니다. 그 선이 결정 경계입니다. 애매한 구역이라기보다는, 넘으면 답이 바뀌는 국경선에 가깝습니다.</p>\n<p>이미지는 숫자가 3,072개라서 그림으로 그릴 수 없으니, 숫자 두 개짜리 점으로 대신 보겠습니다.</p>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-knn.4fcadf00fb.1440.webp\" alt=\"세 클래스의 점 80개를 찍은 그림과, 같은 점으로 1-NN과 5-NN이 평면을 칠한 결정 경계. 1-NN에서는 다른 무리 안에 들어간 점마다 작은 섬이 생기고, 5-NN에서는 섬이 사라지고 경계가 매끄러우며 동률인 곳이 흰색으로 남는다.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"점 하나가 숫자 두 개인 예시 데이터입니다(점선 동그라미는 다른 무리 안에 들어간 점). 가운데는 1-NN, 오른쪽은 5-NN이 칠한 결정 경계이고, 흰 곳은 투표가 동률인 곳입니다. 거리는 L2로 쟀습니다.\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-knn.4fcadf00fb.640.webp 640w, /data/img/fig-knn.4fcadf00fb.960.webp 960w, /data/img/fig-knn.4fcadf00fb.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"660\" data-img-tone=\"light\" decoding=\"async\"><figcaption>점 하나가 숫자 두 개인 예시 데이터입니다(점선 동그라미는 다른 무리 안에 들어간 점). 가운데는 1-NN, 오른쪽은 5-NN이 칠한 결정 경계이고, 흰 곳은 투표가 동률인 곳입니다. 거리는 L2로 쟀습니다.</figcaption></figure>\n<p>가장 가까운 하나만 보는 1-NN은 경계가 들쭉날쭉합니다. 다른 무리 한가운데 섞여 있는 점 하나에도 그 점만의 작은 섬이 생깁니다. 이런 점은 라벨을 잘못 붙인 실수이거나 예외적인 데이터인 경우가 많은데, 1-NN은 그것까지 그대로 따라갑니다.</p>\n<p>그래서 나온 것이 <strong>k-최근접 이웃 분류기</strong>(k-Nearest Neighbor, kNN)입니다. 가장 가까운 점 하나가 아니라 <strong>가장 가까운 점 K개를 찾아 다수결</strong>로 정합니다. K = 1이면 처음의 최근접 이웃 분류기와 같습니다. K를 키우면 주변 여러 점의 의견을 듣기 때문에, 혼자 튀는 점의 섬이 사라지고 경계가 매끄러워집니다. 라벨이 조금 섞여 있어도 주변의 많은 데이터를 보고 같은 묶음으로 판단하는 셈입니다.</p>\n<p>오른쪽 그림의 흰 부분은 <strong>투표가 동률</strong>이라 답을 정하지 못한 곳입니다. 이웃 5개가 A 2개, B 2개, C 1개처럼 나뉘는 경우입니다.</p>\n<blockquote><p>동률을 피하려면 K를 홀수로 하면 되지 않을까?</p></blockquote>\n<p>그래서 K는 보통 1, 3, 5처럼 홀수로 고릅니다. 클래스가 두 개라면 홀수 K에서는 동률이 생기지 않습니다. 하지만 클래스가 세 개 이상이면 홀수여도 위의 2 : 2 : 1처럼 동률이 생길 수 있어서, 완전히 없어지지는 않습니다.</p>\n<p>K를 너무 키워도 문제입니다. 경계가 지나치게 뭉개져서 작은 무리가 큰 무리에 먹히고, 극단적으로 K가 데이터 전체 개수가 되면 언제나 가장 많은 클래스만 답하게 됩니다.</p>\n\n<h2>9. K는 어떻게 고를까 — 검증 셋과 교차 검증</h2>\n<p>K를 몇으로 할지, L1과 L2 중 무엇을 쓸지처럼 학습으로 정해지지 않고 <strong>사람이 미리 정해 주는 값</strong>을 <strong>하이퍼파라미터</strong>(hyperparameter)라고 합니다. 이런 값은 여러 개를 시험해 보고 가장 잘 맞는 것을 고릅니다. 여기서 중요한 것은 <strong>무엇으로 시험하느냐</strong>입니다.</p>\n<ul><li><p><strong>테스트 셋</strong>(test set)은 진짜 시험입니다. 맨 마지막에 딱 한 번만 씁니다. 테스트 셋을 보며 K를 고르면 시험 문제를 보고 공부한 셈이라, 실제로 썼을 때보다 성능이 좋게 나옵니다.</p></li><li><p><strong>검증 셋</strong>(validation set)은 연습 시험입니다. 학습 데이터 일부를 떼어 두고, K를 바꿔 가며 여기서 점수를 매깁니다. 검증 셋도 정답이 달린 데이터지만 학습에는 쓰지 않습니다.</p></li></ul>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-split.f0937d12f4.1440.webp\" alt=\"맨 위는 학습 데이터와 테스트 데이터, 가운데는 학습 데이터 일부를 검증 셋으로 떼어 낸 모습, 아래는 학습 데이터를 다섯 조각으로 나눠 한 조각씩 돌아가며 검증에 쓰고 점수 다섯 개를 평균하는 5겹 교차 검증.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"테스트 셋은 마지막에 한 번만 씁니다. K는 학습 데이터에서 떼어 둔 검증 셋으로 고르고(가운데), 데이터가 적으면 다섯 조각을 돌아가며 검증에 쓰는 교차 검증으로 고릅니다(아래).\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-split.f0937d12f4.640.webp 640w, /data/img/fig-split.f0937d12f4.960.webp 960w, /data/img/fig-split.f0937d12f4.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"700\" data-img-tone=\"light\" decoding=\"async\"><figcaption>테스트 셋은 마지막에 한 번만 씁니다. K는 학습 데이터에서 떼어 둔 검증 셋으로 고르고(가운데), 데이터가 적으면 다섯 조각을 돌아가며 검증에 쓰는 교차 검증으로 고릅니다(아래).</figcaption></figure>\n<p>학습 데이터가 적어서 검증 셋을 넉넉히 떼기 어려울 때는 <strong>교차 검증</strong>(cross-validation)을 씁니다. 학습 데이터를 예를 들어 다섯 조각(fold)으로 나누고, 한 조각씩 돌아가며 검증용으로 쓴 다음 다섯 점수를 평균합니다. 한 번만 나눌 때보다 점수가 덜 흔들려서 더 믿을 만합니다. 강의 노트의 예에서는 CIFAR-10에서 K가 7 근처일 때 가장 좋았습니다.</p>\n<p>다만 교차 검증은 학습과 평가를 다섯 번 해야 해서 계산이 많이 듭니다. 그래서 데이터가 충분히 많으면 보통 검증 셋 하나로 끝냅니다.</p>\n\n<h2>10. 이미지에는 왜 잘 안 쓸까</h2>\n<p>강의에서는 이미지를 다룰 때 kNN을 거의 쓰지 않는다고 합니다. 아주 단순한 경우가 아니면 쓰지 않는데, 이유는 세 가지입니다.</p>\n<h3>픽셀 거리는 사람이 느끼는 차이와 다르다</h3>\n<p>사진 한 장을 세 가지로 바꿔 보겠습니다. 옆으로 조금 민 것, 눈과 입을 가린 것, 전체를 조금 어둡게 한 것입니다. 사람이 보기에 가장 달라 보이는 것은 눈과 입을 가린 사진입니다. 그런데 L2 거리로 재면 세 장이 원본과 <strong>똑같이</strong> 떨어져 있도록 만들 수 있습니다.</p>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-face.01ed022e4a.1440.webp\" alt=\"48 × 48 흑백으로 그린 얼굴의 원본, 옆으로 민 것, 눈과 입을 검은 네모로 가린 것, 어둡게 한 것. 세 장 모두 원본과의 L2 거리가 2153이다.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"오른쪽 세 장은 모두 원본과의 L2 거리가 2153입니다. 민 정도(약 4픽셀)와 어둡게 한 정도(밝기 45)를 그렇게 되도록 맞췄습니다. 48 × 48 흑백으로 직접 그린 얼굴입니다.\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-face.01ed022e4a.640.webp 640w, /data/img/fig-face.01ed022e4a.960.webp 960w, /data/img/fig-face.01ed022e4a.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"520\" data-img-tone=\"light\" decoding=\"async\"><figcaption>오른쪽 세 장은 모두 원본과의 L2 거리가 2153입니다. 민 정도(약 4픽셀)와 어둡게 한 정도(밝기 45)를 그렇게 되도록 맞췄습니다. 48 × 48 흑백으로 직접 그린 얼굴입니다.</figcaption></figure>\n<p>픽셀 거리는 위치가 조금 밀리거나 전체 밝기가 바뀌는 데에 크게 반응하지만, 무엇이 찍혀 있는지에는 관심이 없습니다. 강의 노트에는 CIFAR-10 이미지를 픽셀 거리 기준으로 늘어놓으면 같은 종류끼리가 아니라 <strong>배경색이 비슷한 것끼리</strong> 모인다는 예도 나옵니다. 흰 배경의 개 사진 옆에 흰 배경의 개구리 사진이 놓이는 식입니다.</p>\n<h3>예측이 느리다</h3>\n<p>7장에서 본 대로, 예측할 때마다 모든 학습 이미지와 비교해야 합니다. 요즘 이미지는 픽셀 수 자체가 많아서, 한 장끼리 비교하는 데에도 시간이 꽤 듭니다.</p>\n<h3>차원의 저주</h3>\n<p>최근접 이웃이 잘 맞으려면, 새 이미지 가까이에 진짜로 비슷한 학습 이미지가 있어야 합니다. 학습 데이터가 공간을 듬성듬성 채우고 있으면 '가장 가까운' 이웃도 사실은 멀리 있어서, 그 답을 믿기 어렵습니다.</p>\n<p>문제는 숫자 하나하나가 공간의 축 하나, 즉 <strong>차원</strong>(dimension)이라는 점입니다. 축마다 점을 4개씩 고르게 깔아 공간을 채운다고 해 보겠습니다. 1차원이면 4개, 2차원이면 <math-inline data-latex=\"4 \\times 4 = 16\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mn>4</mn><mo>×</mo><mn>4</mn><mo>=</mo><mn>16</mn></mrow><annotation encoding=\"application/x-tex\">4 \\times 4 = 16</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.7278em;vertical-align:-0.0833em;\"></span><span class=\"mord\">4</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">×</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">4</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">16</span></span></span></span></math-inline>개, 3차원이면 64개가 필요합니다. 차원이 하나 늘 때마다 4배씩 늘어납니다.</p>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-curse.0bcf1f8161.1440.webp\" alt=\"축마다 점 4개로 공간을 채운 그림. 1차원은 선 위의 점 4개, 2차원은 4 × 4 격자 16개, 3차원은 정육면체 안의 64개, 그리고 3,072차원에는 4의 3072제곱 개가 필요하다는 글.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"축마다 점 4개로 공간을 채우려면 차원이 하나 늘 때마다 4배가 필요합니다. 32 × 32 컬러 이미지(3,072차원)라면 4의 3072제곱 개, 1,850자리 수만큼입니다.\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-curse.0bcf1f8161.640.webp 640w, /data/img/fig-curse.0bcf1f8161.960.webp 960w, /data/img/fig-curse.0bcf1f8161.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"520\" data-img-tone=\"light\" decoding=\"async\"><figcaption>축마다 점 4개로 공간을 채우려면 차원이 하나 늘 때마다 4배가 필요합니다. 32 × 32 컬러 이미지(3,072차원)라면 4의 3072제곱 개, 1,850자리 수만큼입니다.</figcaption></figure>\n<p>32 × 32 컬러 이미지는 3,072차원입니다. 같은 방식으로 채우려면 <math-inline data-latex=\"4^{3072}\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msup><mn>4</mn><mn>3072</mn></msup></mrow><annotation encoding=\"application/x-tex\">4^{3072}</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.8141em;\"></span><span class=\"mord\"><span class=\"mord\">4</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.8141em;\"><span style=\"top:-3.063em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\"><span class=\"mord mtight\">3072</span></span></span></span></span></span></span></span></span></span></span></span></math-inline>개, 1,850자리나 되는 수만큼의 이미지가 필요합니다. 관측 가능한 우주의 원자 수가 약 <math-inline data-latex=\"10^{80}\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msup><mn>10</mn><mn>80</mn></msup></mrow><annotation encoding=\"application/x-tex\">10^{80}</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.8141em;\"></span><span class=\"mord\">1</span><span class=\"mord\"><span class=\"mord\">0</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.8141em;\"><span style=\"top:-3.063em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\"><span class=\"mord mtight\">80</span></span></span></span></span></span></span></span></span></span></span></span></math-inline>개, 81자리 수라고 하니 비교도 되지 않습니다. 이렇게 차원이 늘수록 필요한 데이터가 감당할 수 없이 늘어나는 현상을 <strong>차원의 저주</strong>(curse of dimensionality)라고 합니다.</p>\n\n<h2>11. 그래도 써야 한다면</h2>\n<p>강의에서는 그래도 kNN을 써야 할 때 고려할 방법들을 정리해 줍니다.</p>\n<ul><li><p><strong>정규화</strong>(normalization): 칸마다 평균이 0, 표준편차가 1이 되도록 값을 맞춥니다. 평균은 값들의 가운데, 표준편차는 값이 평균에서 얼마나 퍼져 있는지를 나타내는 숫자입니다. 거리는 모든 칸의 차이를 더하니까, 범위가 큰 칸 하나가 거리를 혼자 좌우할 수 있습니다. 예를 들어 사람을 나이와 연봉(원 단위)으로 비교하면, 나이 30살 차이보다 연봉 1만 원 차이가 더 크게 잡힙니다. 정규화는 이걸 막아 줍니다. 다만 픽셀은 원래 모두 0–255 범위라서 이미지에서는 덜 중요하다고 합니다.</p></li><li><p><strong>차원 축소</strong>(dimensionality reduction): 숫자 3,072개에서 중요한 정보만 남겨 수십 개로 줄인 다음 비교합니다. PCA(주성분 분석)가 대표적입니다.</p></li><li><p><strong>하이퍼파라미터 고르기</strong>: 9장처럼 검증 셋이나 교차 검증으로 K와 거리(L1, L2)를 고릅니다.</p></li><li><p><strong>근사 최근접 이웃</strong>(Approximate Nearest Neighbor, ANN): 데이터가 아주 많으면 진짜 가장 가까운 이웃을 끝까지 찾는 대신, 충분히 가까운 이웃을 찾으면 멈춥니다. 정확도를 조금 내주고 속도를 크게 얻는 방법이고, 데이터를 미리 정리해 둔 색인(kd-tree 같은 것)을 씁니다.</p></li></ul>\n<p>마지막 항목은 강의를 들으면서 \"니오레스 네이버?\"라고 받아 적어 두었던 부분입니다. 무슨 말인지 몰라 물음표만 쳐 두었는데, 강의 노트를 보니 Approximate Nearest Neighbor였습니다. 7장에서 이야기한 유튜브 추천이 바로 이 방법을 씁니다.</p>\n\n<h2>마무리</h2>\n<ul><li><p><strong>퍼셉트론</strong> 하나는 곧은 경계 하나로 나누는 선형 분류기라서 XOR을 못 만들고, 층을 쌓으면 만들 수 있습니다. 쌓은 층을 학습시키는 방법이 역전파입니다.</p></li><li><p><strong>딥러닝</strong>은 층을 깊게 쌓은 신경망을 큰 데이터로 학습시키는 것이고, 2012년 AlexNet이 그 힘을 보여 줬습니다.</p></li><li><p><strong>이미지 분류</strong>는 정해진 클래스 중 하나를 고르는 문제이고, 규칙을 손으로 짜는 대신 데이터로 배우게 합니다(train과 predict).</p></li><li><p><strong>최근접 이웃 분류기</strong>는 전부 기억했다가 가장 가까운 이미지의 라벨을 답합니다. '가깝다'는 L1이나 L2 거리로 잽니다.</p></li><li><p><strong>kNN</strong>은 K개 이웃의 다수결이고, K와 거리 종류는 검증 셋으로 고릅니다.</p></li><li><p>하지만 예측이 느리고, 픽셀 거리가 사람의 눈과 다르고, 차원의 저주 때문에 이미지에는 잘 쓰지 않습니다.</p></li></ul>\n<blockquote><p>최근접 이웃 분류기는 학습이 공짜인 대신 예측이 비싸다. 신경망은 이것을 뒤집는다.</p></blockquote>\n\n<h2>용어 정리</h2>\n<ul><li><p><strong>퍼셉트론</strong>: 입력 × 가중치의 합이 문턱을 넘으면 1을 내는 계산 단위. 가중치를 데이터로 맞춘다.</p></li><li><p><strong>선형 분류기</strong>: 곧은 경계(직선, 평면) 하나로 나누는 분류기.</p></li><li><p><strong>다층 퍼셉트론</strong>(MLP): 퍼셉트론을 여러 층으로 쌓은 것. 앞 층의 출력이 뒤 층의 입력이 된다.</p></li><li><p><strong>역전파</strong>: 여러 층의 가중치를 데이터로 맞추는 방법. 1986년에 알려졌다.</p></li><li><p><strong>클래스·라벨</strong>: 고를 수 있는 보기, 그리고 데이터에 붙은 정답.</p></li><li><p><strong>데이터 기반 접근</strong>: 규칙을 손으로 적지 않고, 정답 달린 예시로 배우게 하는 방식.</p></li><li><p><strong>최근접 이웃 분류기</strong>: 학습 데이터를 전부 기억했다가 가장 가까운 것의 라벨을 답하는 분류기.</p></li><li><p><strong>L1 거리</strong>: 차이의 절댓값을 모두 더한 것. <strong>L2 거리</strong>: 차이의 제곱을 모두 더해 제곱근을 씌운 것.</p></li><li><p><strong>시간 복잡도</strong>: 데이터 수에 따라 걸리는 시간이 어떻게 늘어나는지. O(1), O(N)처럼 쓴다.</p></li><li><p><strong>결정 경계</strong>: 분류기의 답이 바뀌는 경계선.</p></li><li><p><strong>kNN</strong>: 가장 가까운 K개의 다수결로 정하는 분류기.</p></li><li><p><strong>하이퍼파라미터</strong>: K나 거리 종류처럼 학습 전에 사람이 정하는 값.</p></li><li><p><strong>검증 셋·테스트 셋</strong>: 하이퍼파라미터를 고르는 연습 시험, 마지막에 한 번만 보는 진짜 시험.</p></li><li><p><strong>교차 검증</strong>: 학습 데이터를 여러 조각으로 나눠 돌아가며 검증에 쓰고 평균하는 방법.</p></li><li><p><strong>차원의 저주</strong>: 차원이 늘수록 공간을 채우는 데 필요한 데이터가 폭발적으로 늘어나는 현상.</p></li><li><p><strong>근사 최근접 이웃</strong>(ANN): 정확도를 조금 내주고 가까운 이웃을 빨리 찾는 방법.</p></li></ul>\n\n<h2>앞으로 알아볼 것</h2>\n<p>이번 강의를 정리하면서 따로 알아보고 싶은 것들이 남았습니다.</p>\n<ul><li><p><strong>Canny 엣지 검출</strong>은 실제로 어떻게 가장자리를 찾을까? 흐리게 만들기, 밝기 기울기 구하기, 선을 얇게 깎기, 문턱 두 개로 거르기의 순서로 동작한다는데, 문턱을 두 개 쓰는 방식이 슈미트 트리거와 비슷해 보여서 더 궁금합니다. 예전에 쓴 <a href=\"https://stanase.com/posts/opencv-lane-detection-basics/\">「OpenCV 차선 인식 주요 기능 정리」</a>에서는 사용법만 짧게 다뤘습니다.</p></li><li><p>사진 한 장에 <strong>물체가 여러 개</strong>일 때는 어떻게 찾을까? 객체 검출(object detection)이라는 다른 문제라고 합니다.</p></li><li><p><strong>역전파</strong>는 어떻게 여러 층의 가중치를 한꺼번에 맞출까? 이 강의의 3강과, 지난 글에 남긴 질문이 같은 곳을 가리킵니다.</p></li><li><p>2강의 <strong>선형 분류기</strong>는 최근접 이웃 분류기와 무엇이 다를까? 학습은 오래 걸려도 예측은 빠른, 이번 글과 반대 방향의 분류기일 것 같습니다.</p></li><li><p><strong>PCA</strong>는 숫자 3,072개에서 무엇을 남기고 무엇을 버릴까?</p></li><li><p><strong>근사 최근접 이웃</strong>은 어떻게 전부 비교하지 않고도 가까운 것을 찾을까?</p></li></ul>",
      "rendered": true
    },
    "en": {
      "language": "en",
      "filename": "en.html",
      "format": "html",
      "title": "Fundamentals of Deep Learning and Nearest Neighbor Classifiers",
      "summary": "Notes on Lecture 1 of Prof. Joonseok Lee’s deep learning course at Seoul National University: a short history up to deep learning, and the simplest image classifier, the nearest neighbor classifier, from zero.",
      "description": "Perceptrons and the XOR problem, image classification, the data-driven approach, L1 and L2 distances, k-nearest neighbors, validation sets and cross-validation, and the curse of dimensionality, explained with figures and numbers.",
      "content": "<h3>Introduction</h3>\n<p>These are my notes on <a target=\"_blank\" rel=\"noopener\" href=\"https://www.youtube.com/watch?v=wHBitw5oJSU\">“Lecture 1. Intro to Deep Learning &amp; Nearest Neighbor Classifiers”</a> from Prof. Joonseok Lee’s deep learning course at Seoul National University. It is the first deep learning lecture of the Fall 2022 course “Machine Learning &amp; Deep Learning I”, and the lecture notes are on <a target=\"_blank\" rel=\"noopener\" href=\"https://viplab.snu.ac.kr/viplab/courses/mldl1_2022_2/\">the course page</a>. The notes say they are based on Stanford’s CS231n lecture notes.</p>\n<p>This is the first post of Deep Learning Notes, which follows the course lecture by lecture. You don’t need to have read my earlier post <a href=\"https://stanase.com/posts/what-is-a-neural-network/\">“What Is a Neural Network?”</a>: I explain each new term when it first appears.</p>\n<p>The lecture has two parts. The first is a short history of where deep learning came from. The second is the problem of <strong>image classification</strong> and the simplest way to solve it, the <strong>nearest neighbor classifier</strong>.</p>\n\n<h2>1. A Short History up to Deep Learning</h2>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-timeline.2a1098aa32.1440.webp\" alt=\"A timeline from 1943 to 2012: the McCulloch–Pitts neuron in 1943, the perceptron in 1957, the book Perceptrons pointing out the XOR problem in 1969, backpropagation in 1986 and AlexNet in 2012. The 17 years between 1969 and 1986 are highlighted.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"From the artificial neuron of 1943 to AlexNet in 2012. It took 17 years from the XOR problem (1969) to backpropagation (1986).\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-timeline.2a1098aa32.640.webp 640w, /data/img/fig-timeline.2a1098aa32.960.webp 960w, /data/img/fig-timeline.2a1098aa32.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"500\" data-img-tone=\"light\" decoding=\"async\"><figcaption>From the artificial neuron of 1943 to AlexNet in 2012. It took 17 years from the XOR problem (1969) to backpropagation (1986).</figcaption></figure>\n<h3>1943: a logic circuit that imitates a neuron</h3>\n<p>Neural networks start with the artificial neuron that McCulloch and Pitts proposed in 1943. They thought about how a human neuron works and turned it into a very simple calculation. It takes several inputs, adds them up, and outputs 1 if the sum is above a set <strong>threshold</strong>, or 0 if it is not.</p>\n<p>A single neuron like this can act as a logic gate such as AND, OR or NOT. Since gates can be combined into any logic, the idea was that wiring neurons together could make an “electronic brain” that works like the nervous system. At that point, though, a person had to decide how the inputs were added and where the threshold sat.</p>\n<h3>1957: the perceptron, and “learning”</h3>\n<p>Fourteen years later, in 1957, Rosenblatt introduced the <strong>perceptron</strong>. It looks similar: each input is multiplied by a number called a <strong>weight</strong>, the results are added up, and the output is 1 if the sum is above the threshold. With two inputs it is written like this:</p>\n<math-block data-latex=\"y = \\begin{cases} 1 &amp; w_1 x_1 + w_2 x_2 + b &gt; 0 \\\\ 0 &amp; w_1 x_1 + w_2 x_2 + b \\le 0 \\end{cases}\"><span class=\"katex-display\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics><mrow><mi>y</mi><mo>=</mo><mrow><mo fence=\"true\">{</mo><mtable rowspacing=\"0.36em\" columnalign=\"left left\" columnspacing=\"1em\"><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mn>1</mn></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><msub><mi>w</mi><mn>1</mn></msub><msub><mi>x</mi><mn>1</mn></msub><mo>+</mo><msub><mi>w</mi><mn>2</mn></msub><msub><mi>x</mi><mn>2</mn></msub><mo>+</mo><mi>b</mi><mo>&gt;</mo><mn>0</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mn>0</mn></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"false\"><mrow><msub><mi>w</mi><mn>1</mn></msub><msub><mi>x</mi><mn>1</mn></msub><mo>+</mo><msub><mi>w</mi><mn>2</mn></msub><msub><mi>x</mi><mn>2</mn></msub><mo>+</mo><mi>b</mi><mo>≤</mo><mn>0</mn></mrow></mstyle></mtd></mtr></mtable></mrow></mrow><annotation encoding=\"application/x-tex\">y = \\begin{cases} 1 &amp; w_1 x_1 + w_2 x_2 + b &gt; 0 \\\\ 0 &amp; w_1 x_1 + w_2 x_2 + b \\le 0 \\end{cases}</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.625em;vertical-align:-0.1944em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.0359em;\">y</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:3em;vertical-align:-1.25em;\"></span><span class=\"minner\"><span class=\"mopen delimcenter\" style=\"top:0em;\"><span class=\"delimsizing size4\">{</span></span><span class=\"mord\"><span class=\"mtable\"><span class=\"col-align-l\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.69em;\"><span style=\"top:-3.69em;\"><span class=\"pstrut\" style=\"height:3.008em;\"></span><span class=\"mord\"><span class=\"mord\">1</span></span></span><span style=\"top:-2.25em;\"><span class=\"pstrut\" style=\"height:3.008em;\"></span><span class=\"mord\"><span class=\"mord\">0</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.19em;\"><span></span></span></span></span></span><span class=\"arraycolsep\" style=\"width:1em;\"></span><span class=\"col-align-l\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.69em;\"><span style=\"top:-3.69em;\"><span class=\"pstrut\" style=\"height:3.008em;\"></span><span class=\"mord\"><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0269em;\">w</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0269em;\">w</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord mathnormal\">b</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">&gt;</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mord\">0</span></span></span><span style=\"top:-2.25em;\"><span class=\"pstrut\" style=\"height:3.008em;\"></span><span class=\"mord\"><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0269em;\">w</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0269em;\">w</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord mathnormal\">b</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">≤</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mord\">0</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.19em;\"><span></span></span></span></span></span></span></span><span class=\"mclose nulldelimiter\"></span></span></span></span></span></span></math-block>\n<p><math-inline data-latex=\"x_1\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>x</mi><mn>1</mn></msub></mrow><annotation encoding=\"application/x-tex\">x_1</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline> and <math-inline data-latex=\"x_2\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>x</mi><mn>2</mn></msub></mrow><annotation encoding=\"application/x-tex\">x_2</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline> are the inputs, and <math-inline data-latex=\"w_1\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>w</mi><mn>1</mn></msub></mrow><annotation encoding=\"application/x-tex\">w_1</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0269em;\">w</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline> and <math-inline data-latex=\"w_2\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>w</mi><mn>2</mn></msub></mrow><annotation encoding=\"application/x-tex\">w_2</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0269em;\">w</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline> are the weights they are multiplied by. The sum of input × weight, <math-inline data-latex=\"w_1 x_1 + w_2 x_2\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>w</mi><mn>1</mn></msub><msub><mi>x</mi><mn>1</mn></msub><mo>+</mo><msub><mi>w</mi><mn>2</mn></msub><msub><mi>x</mi><mn>2</mn></msub></mrow><annotation encoding=\"application/x-tex\">w_1 x_1 + w_2 x_2</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.7333em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0269em;\">w</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0269em;\">w</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline>, is called the <strong>weighted sum</strong>. <math-inline data-latex=\"b\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>b</mi></mrow><annotation encoding=\"application/x-tex\">b</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.6944em;\"></span><span class=\"mord mathnormal\">b</span></span></span></span></math-inline> is the <strong>bias</strong>, the threshold moved into the equation. It is the same number I described in the previous post as something like a threshold voltage.</p>\n<p>What changed is that the weights are no longer set by a person but <strong>adjusted by looking at data</strong>. This is where the idea of “learning” first entered neural networks.</p>\n<h3>1969: the XOR problem</h3>\n<p>In 1969, however, Minsky and Papert showed a limit in their book <em>Perceptrons</em>: a single perceptron cannot compute <strong>XOR</strong>. XOR is a familiar logic gate from circuits. It outputs 1 when the two inputs differ and 0 when they are the same.</p>\n<table style=\"min-width: 125px;\"><colgroup><col style=\"min-width: 25px;\"><col style=\"min-width: 25px;\"><col style=\"min-width: 25px;\"><col style=\"min-width: 25px;\"><col style=\"min-width: 25px;\"></colgroup><tbody><tr><th colspan=\"1\" rowspan=\"1\"><p><math-inline data-latex=\"x_1\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>x</mi><mn>1</mn></msub></mrow><annotation encoding=\"application/x-tex\">x_1</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline></p></th><th colspan=\"1\" rowspan=\"1\"><p><math-inline data-latex=\"x_2\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>x</mi><mn>2</mn></msub></mrow><annotation encoding=\"application/x-tex\">x_2</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline></p></th><th colspan=\"1\" rowspan=\"1\"><p>AND</p></th><th colspan=\"1\" rowspan=\"1\"><p>OR</p></th><th colspan=\"1\" rowspan=\"1\"><p>XOR</p></th></tr><tr><td colspan=\"1\" rowspan=\"1\"><p>0</p></td><td colspan=\"1\" rowspan=\"1\"><p>0</p></td><td colspan=\"1\" rowspan=\"1\"><p>0</p></td><td colspan=\"1\" rowspan=\"1\"><p>0</p></td><td colspan=\"1\" rowspan=\"1\"><p>0</p></td></tr><tr><td colspan=\"1\" rowspan=\"1\"><p>0</p></td><td colspan=\"1\" rowspan=\"1\"><p>1</p></td><td colspan=\"1\" rowspan=\"1\"><p>0</p></td><td colspan=\"1\" rowspan=\"1\"><p>1</p></td><td colspan=\"1\" rowspan=\"1\"><p>1</p></td></tr><tr><td colspan=\"1\" rowspan=\"1\"><p>1</p></td><td colspan=\"1\" rowspan=\"1\"><p>0</p></td><td colspan=\"1\" rowspan=\"1\"><p>0</p></td><td colspan=\"1\" rowspan=\"1\"><p>1</p></td><td colspan=\"1\" rowspan=\"1\"><p>1</p></td></tr><tr><td colspan=\"1\" rowspan=\"1\"><p>1</p></td><td colspan=\"1\" rowspan=\"1\"><p>1</p></td><td colspan=\"1\" rowspan=\"1\"><p>1</p></td><td colspan=\"1\" rowspan=\"1\"><p>1</p></td><td colspan=\"1\" rowspan=\"1\"><p>0</p></td></tr></tbody></table>\n<blockquote><p>XOR can be built from AND, OR and NOT, so why is it a problem?</p></blockquote>\n<p>I studied electronics, so this is where I got stuck first. In a circuit, XOR is built by combining other gates, for example (A OR B) AND NOT(A AND B). So the problem cannot be that gates can’t make XOR. The problem was not the gates but <strong>a single perceptron</strong>.</p>\n<p>Drawing what one perceptron does shows why. With two inputs <math-inline data-latex=\"x_1\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>x</mi><mn>1</mn></msub></mrow><annotation encoding=\"application/x-tex\">x_1</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline> and <math-inline data-latex=\"x_2\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>x</mi><mn>2</mn></msub></mrow><annotation encoding=\"application/x-tex\">x_2</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline>, every input can be plotted as a point on a plane. The perceptron answers 1 on one side of <math-inline data-latex=\"w_1 x_1 + w_2 x_2 + b = 0\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>w</mi><mn>1</mn></msub><msub><mi>x</mi><mn>1</mn></msub><mo>+</mo><msub><mi>w</mi><mn>2</mn></msub><msub><mi>x</mi><mn>2</mn></msub><mo>+</mo><mi>b</mi><mo>=</mo><mn>0</mn></mrow><annotation encoding=\"application/x-tex\">w_1 x_1 + w_2 x_2 + b = 0</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.7333em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0269em;\">w</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.7333em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0269em;\">w</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0269em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6944em;\"></span><span class=\"mord mathnormal\">b</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">0</span></span></span></span></math-inline> and 0 on the other. This equation is of <strong>degree one</strong> (each <math-inline data-latex=\"x\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>x</mi></mrow><annotation encoding=\"application/x-tex\">x</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.4306em;\"></span><span class=\"mord mathnormal\">x</span></span></span></span></math-inline> appears only to the first power), so the boundary is a straight line. With three inputs it becomes a plane, and with more a flat sheet, but it never bends. Splitting with one straight, flat boundary like this is called <strong>linear</strong>, and so a single perceptron is a <strong>linear classifier</strong>.</p>\n<p>For AND and OR, one straight line separates the 1s from the 0s. For XOR, the 1 points (0, 1) and (1, 0) and the 0 points (0, 0) and (1, 1) sit on crossing diagonals, so wherever you draw the line, one point ends up on the wrong side.</p>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-xor-layers.2cc4903f40.1440.webp\" alt=\"Top: one perceptron alone, an OR and a NAND side by side in one layer, and the two feeding an AND. Bottom: how each splits the input plane. With one perceptron, (1, 1) falls on the wrong side; only the stacked version makes a band between two lines that holds the two XOR = 1 points.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"One perceptron splits with a single line (left). Two side by side in one layer give two lines, but also two separate answers (middle). Feeding the first layer into an AND makes only the band between the lines 1, which is XOR (right). Black dots are inputs where XOR is 1, white dots where it is 0.\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-xor-layers.2cc4903f40.640.webp 640w, /data/img/fig-xor-layers.2cc4903f40.960.webp 960w, /data/img/fig-xor-layers.2cc4903f40.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"900\" data-img-tone=\"light\" decoding=\"async\"><figcaption>One perceptron splits with a single line (left). Two side by side in one layer give two lines, but also two separate answers (middle). Feeding the first layer into an AND makes only the band between the lines 1, which is XOR (right). Black dots are inputs where XOR is 1, white dots where it is 0.</figcaption></figure>\n<h3>17 years later: stacking layers</h3>\n<p>The problem was not solved until 1986. In the lecture, the fix is put as “don’t place perceptrons in parallel; place them in sequence.”</p>\n<blockquote><p><strong>What confused me at first.</strong> I took this to mean that data should be processed one item at a time instead of all at once in parallel, and I wondered whether that meant we no longer needed XOR. Both readings were wrong. The sentence is about <strong>how the perceptrons are arranged</strong>, not about the data. Several perceptrons side by side in one layer each draw just one line; you get XOR only by chaining them, so that the outputs of earlier perceptrons become the inputs of later ones. XOR is not avoided; stacking layers is what makes it possible.</p></blockquote>\n<p>This is exactly how XOR is built in a circuit. Put an OR and a NAND side by side in the first layer and feed both outputs into an AND.</p>\n<math-block data-latex=\"\\begin{aligned} h_1 &amp;= \\operatorname{OR}(x_1, x_2) \\\\ h_2 &amp;= \\operatorname{NAND}(x_1, x_2) \\\\ y &amp;= \\operatorname{AND}(h_1, h_2) \\end{aligned}\"><span class=\"katex-display\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics><mtable rowspacing=\"0.25em\" columnalign=\"right left\" columnspacing=\"0em\"><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><msub><mi>h</mi><mn>1</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><mrow><mrow></mrow><mo>=</mo><mi mathvariant=\"normal\">OR</mi><mo>⁡</mo><mo stretchy=\"false\">(</mo><msub><mi>x</mi><mn>1</mn></msub><mo separator=\"true\">,</mo><msub><mi>x</mi><mn>2</mn></msub><mo stretchy=\"false\">)</mo></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><msub><mi>h</mi><mn>2</mn></msub></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><mrow><mrow></mrow><mo>=</mo><mi mathvariant=\"normal\">NAND</mi><mo>⁡</mo><mo stretchy=\"false\">(</mo><msub><mi>x</mi><mn>1</mn></msub><mo separator=\"true\">,</mo><msub><mi>x</mi><mn>2</mn></msub><mo stretchy=\"false\">)</mo></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><mi>y</mi></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><mrow><mrow></mrow><mo>=</mo><mi mathvariant=\"normal\">AND</mi><mo>⁡</mo><mo stretchy=\"false\">(</mo><msub><mi>h</mi><mn>1</mn></msub><mo separator=\"true\">,</mo><msub><mi>h</mi><mn>2</mn></msub><mo stretchy=\"false\">)</mo></mrow></mstyle></mtd></mtr></mtable><annotation encoding=\"application/x-tex\">\\begin{aligned} h_1 &amp;= \\operatorname{OR}(x_1, x_2) \\\\ h_2 &amp;= \\operatorname{NAND}(x_1, x_2) \\\\ y &amp;= \\operatorname{AND}(h_1, h_2) \\end{aligned}</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:4.2em;vertical-align:-1.85em;\"></span><span class=\"mord\"><span class=\"mtable\"><span class=\"col-align-r\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:2.35em;\"><span style=\"top:-4.51em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mord\"><span class=\"mord mathnormal\">h</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span><span style=\"top:-3.01em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mord\"><span class=\"mord mathnormal\">h</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span><span style=\"top:-1.51em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0359em;\">y</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.85em;\"><span></span></span></span></span></span><span class=\"col-align-l\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:2.35em;\"><span style=\"top:-4.51em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mord\"></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mop\"><span class=\"mord mathrm\">OR</span></span><span class=\"mopen\">(</span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mpunct\">,</span><span class=\"mspace\" style=\"margin-right:0.1667em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mclose\">)</span></span></span><span style=\"top:-3.01em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mord\"></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mop\"><span class=\"mord mathrm\">NAND</span></span><span class=\"mopen\">(</span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mpunct\">,</span><span class=\"mspace\" style=\"margin-right:0.1667em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mclose\">)</span></span></span><span style=\"top:-1.51em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mord\"></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mop\"><span class=\"mord mathrm\">AND</span></span><span class=\"mopen\">(</span><span class=\"mord\"><span class=\"mord mathnormal\">h</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mpunct\">,</span><span class=\"mspace\" style=\"margin-right:0.1667em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">h</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mclose\">)</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.85em;\"><span></span></span></span></span></span></span></span></span></span></span></span></math-block>\n<p>Each of the three gates is a single perceptron. These weights and biases do it:</p>\n<math-block data-latex=\"\\begin{aligned} \\operatorname{OR} &amp;: \\; x_1 + x_2 - 0.5 &gt; 0 \\\\ \\operatorname{NAND} &amp;: \\; -x_1 - x_2 + 1.5 &gt; 0 \\\\ \\operatorname{AND} &amp;: \\; h_1 + h_2 - 1.5 &gt; 0 \\end{aligned}\"><span class=\"katex-display\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics><mtable rowspacing=\"0.25em\" columnalign=\"right left\" columnspacing=\"0em\"><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><mi mathvariant=\"normal\">OR</mi><mo>⁡</mo></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><mrow><mrow></mrow><mo>:</mo><mtext>  </mtext><msub><mi>x</mi><mn>1</mn></msub><mo>+</mo><msub><mi>x</mi><mn>2</mn></msub><mo>−</mo><mn>0.5</mn><mo>&gt;</mo><mn>0</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><mi mathvariant=\"normal\">NAND</mi><mo>⁡</mo></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><mrow><mrow></mrow><mo>:</mo><mtext>  </mtext><mo>−</mo><msub><mi>x</mi><mn>1</mn></msub><mo>−</mo><msub><mi>x</mi><mn>2</mn></msub><mo>+</mo><mn>1.5</mn><mo>&gt;</mo><mn>0</mn></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><mi mathvariant=\"normal\">AND</mi><mo>⁡</mo></mstyle></mtd><mtd><mstyle scriptlevel=\"0\" displaystyle=\"true\"><mrow><mrow></mrow><mo>:</mo><mtext>  </mtext><msub><mi>h</mi><mn>1</mn></msub><mo>+</mo><msub><mi>h</mi><mn>2</mn></msub><mo>−</mo><mn>1.5</mn><mo>&gt;</mo><mn>0</mn></mrow></mstyle></mtd></mtr></mtable><annotation encoding=\"application/x-tex\">\\begin{aligned} \\operatorname{OR} &amp;: \\; x_1 + x_2 - 0.5 &gt; 0 \\\\ \\operatorname{NAND} &amp;: \\; -x_1 - x_2 + 1.5 &gt; 0 \\\\ \\operatorname{AND} &amp;: \\; h_1 + h_2 - 1.5 &gt; 0 \\end{aligned}</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:4.2em;vertical-align:-1.85em;\"></span><span class=\"mord\"><span class=\"mtable\"><span class=\"col-align-r\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:2.35em;\"><span style=\"top:-4.51em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mop\"><span class=\"mord mathrm\">OR</span></span></span></span><span style=\"top:-3.01em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mop\"><span class=\"mord mathrm\">NAND</span></span></span></span><span style=\"top:-1.51em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mop\"><span class=\"mord mathrm\">AND</span></span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.85em;\"><span></span></span></span></span></span><span class=\"col-align-l\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:2.35em;\"><span style=\"top:-4.51em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mord\"></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">:</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">−</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\">0.5</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">&gt;</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mord\">0</span></span></span><span style=\"top:-3.01em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mord\"></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">:</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mord\">−</span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">−</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\">1.5</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">&gt;</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mord\">0</span></span></span><span style=\"top:-1.51em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\"><span class=\"mord\"></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">:</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">h</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">h</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">−</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\">1.5</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">&gt;</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mord\">0</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.85em;\"><span></span></span></span></span></span></span></span></span></span></span></span></math-block>\n<p>Here −0.5, 1.5 and −1.5 are the biases, that is, the thresholds. The two lines in the figure above are exactly <math-inline data-latex=\"x_1 + x_2 = 0.5\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>x</mi><mn>1</mn></msub><mo>+</mo><msub><mi>x</mi><mn>2</mn></msub><mo>=</mo><mn>0.5</mn></mrow><annotation encoding=\"application/x-tex\">x_1 + x_2 = 0.5</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.7333em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">0.5</span></span></span></span></math-inline> and <math-inline data-latex=\"x_1 + x_2 = 1.5\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>x</mi><mn>1</mn></msub><mo>+</mo><msub><mi>x</mi><mn>2</mn></msub><mo>=</mo><mn>1.5</mn></mrow><annotation encoding=\"application/x-tex\">x_1 + x_2 = 1.5</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.7333em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.5806em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">1.5</span></span></span></span></math-inline>. To see why stacking works, look at where the first layer moves the points.</p>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-xor-remap.23304e5003.1440.webp\" alt=\"Left: the four points in the input plane. Right: the plane after the first layer, where the points from (0, 1) and (1, 0) land on the same spot and one AND line separates it from the other two points.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"The first layer (OR, NAND) moves the four points to new places. (0, 1) and (1, 0) merge into one point, and afterwards a single AND line separates them.\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-xor-remap.23304e5003.640.webp 640w, /data/img/fig-xor-remap.23304e5003.960.webp 960w, /data/img/fig-xor-remap.23304e5003.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"620\" data-img-tone=\"light\" decoding=\"async\"><figcaption>The first layer (OR, NAND) moves the four points to new places. (0, 1) and (1, 0) merge into one point, and afterwards a single AND line separates them.</figcaption></figure>\n<p>After the first layer, (0, 1) and (1, 0) both land on (1, 1), (0, 0) moves to (0, 1), and (1, 1) moves to (1, 0). Now one straight line separates them, so a single perceptron (AND) is enough for the second layer. <strong>The earlier layer reshapes the problem so that one straight line can solve it</strong>; that is why we stack layers. It is the same idea as in the previous post, where hidden layers pick out small features and pass them on.</p>\n<p>Perceptrons stacked in several layers like this are called a <strong>multilayer perceptron</strong> (MLP). One of the questions I left at the end of the previous post is answered here.</p>\n<p>So why did it take 17 years? People already knew that stacking layers would work. What was missing was <strong>a way to fit the weights of stacked perceptrons to data</strong>. <strong>Backpropagation</strong>, made known by Rumelhart, Hinton and Williams in 1986, is that way. How it works is the topic of Lecture 3 of this course.</p>\n<p>The lecture calls the quiet years after the XOR problem the “AI winter”. That said, it is usually seen as the result of several things together, such as results falling short of expectations and funding cuts, rather than XOR alone.</p>\n<h3>2012: AlexNet</h3>\n<p>Deep learning took the spotlight through the <strong>ImageNet</strong> competition. ImageNet is a collection of millions of photos, each tagged by people with what it shows (its <strong>label</strong>). In the competition, a model looks at a photo and picks the right answer out of 1,000 categories.</p>\n<p>The score is the <strong>top-5 error</strong>: the model may give up to five guesses, and it counts as wrong if the right answer is not among them. The winning error was 28.2% in 2010 and 25.8% in 2011. In 2012, <strong>AlexNet</strong>, built by Alex Krizhevsky with Ilya Sutskever and Geoffrey Hinton, reached 16.4%. The runner-up had 26.2%, a gap of almost 10 percentage points.</p>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-imagenet.7ddeab6926.1440.webp\" alt=\"Bar chart of ImageNet top-5 error: 2010 winner 28.2%, 2011 winner 25.8%, 2012 runner-up 26.2%, 2012 AlexNet 16.4%.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"Top-5 error in the ImageNet competition (lower is better). In 2012, AlexNet was almost 10 percentage points below the runner-up.\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-imagenet.7ddeab6926.640.webp 640w, /data/img/fig-imagenet.7ddeab6926.960.webp 960w, /data/img/fig-imagenet.7ddeab6926.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"420\" data-img-tone=\"light\" decoding=\"async\"><figcaption>Top-5 error in the ImageNet competition (lower is better). In 2012, AlexNet was almost 10 percentage points below the runner-up.</figcaption></figure>\n<p>AlexNet was a neural network with many layers. <strong>Training neural networks with many layers on large data</strong> is what deep learning means: deep learning is how neural networks grew up. With large data (ImageNet) and fast hardware (GPUs) in place, the idea from 1986 finally showed its strength.</p>\n\n<h2>2. Image Classification: Picking from a Fixed List</h2>\n<p>The second half of the lecture is about <strong>image classification</strong>. Given an image, say what is in it. It is more like a multiple-choice question than an open one: the possible answers, such as cat, dog or ship, are fixed in advance, and you pick one. Each possible answer is called a <strong>class</strong>.</p>\n<blockquote><p>What if the photo has not one object but several?</p></blockquote>\n<p>Image classification gives one answer per photo, so finding several objects one by one is a separate problem. I leave this question for “What to Look Into Next” at the end.</p>\n<h3>An image as a computer sees it</h3>\n<p>To a computer, an image is a block of numbers. In a color image, each pixel holds three numbers from 0 to 255: the brightness of red (R), green (G) and blue (B). So one image can be seen as three tables of numbers, R, G and B, stacked together. A table of numbers laid out in rows and columns like this is called a <strong>matrix</strong>.</p>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-rgb.24f61e3db0.1440.webp\" alt=\"A 16 × 16 pixel apple, with a boxed 3 × 3 patch spread out into three tables of numbers for R, G and B. The red apple pixels have R around 190 and G and B around 40.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"The boxed 3 × 3 pixels of a 16 × 16 color image, spread out into R, G and B. Brighter cells mean more of that color; on the red apple only R is high.\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-rgb.24f61e3db0.640.webp 640w, /data/img/fig-rgb.24f61e3db0.960.webp 960w, /data/img/fig-rgb.24f61e3db0.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"560\" data-img-tone=\"light\" decoding=\"async\"><figcaption>The boxed 3 × 3 pixels of a 16 × 16 color image, spread out into R, G and B. Brighter cells mean more of that color; on the red apple only R is high.</figcaption></figure>\n<p>CIFAR-10, the dataset used in the lecture, has small color images 32 pixels on a side, so each image is <math-inline data-latex=\"32 \\times 32 \\times 3 = 3{,}072\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mn>32</mn><mo>×</mo><mn>32</mn><mo>×</mo><mn>3</mn><mo>=</mo><mn>3,072</mn></mrow><annotation encoding=\"application/x-tex\">32 \\times 32 \\times 3 = 3{,}072</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.7278em;vertical-align:-0.0833em;\"></span><span class=\"mord\">32</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">×</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.7278em;vertical-align:-0.0833em;\"></span><span class=\"mord\">32</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">×</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">3</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.8389em;vertical-align:-0.1944em;\"></span><span class=\"mord\">3</span><span class=\"mord\"><span class=\"mpunct\">,</span></span><span class=\"mord\">072</span></span></span></span></math-inline> numbers. What we want, then, is a function that takes 3,072 numbers and returns one class name.</p>\n<pre><code>def classify_image(image):\n    # some magic here?\n    return class_label</code></pre>\n\n<h2>3. Trying to Write the Rules by Hand</h2>\n<blockquote><p>Why does sorting come up here?</p></blockquote>\n<p>Here the lecture suddenly talks about sorting. My first thought was the kinds of sorting, such as bubble sort, selection sort, insertion sort and shell sort, but the kinds were not the point. Sorting numbers by size can be written down in code step by step. Finding a cat in a photo cannot be written down like that, at least not in any obvious way. Sorting was brought up to show that contrast.</p>\n<p>People still tried to write the rules by hand. First, find the <strong>edges</strong> in the image, the places where brightness changes sharply; Canny edge detection is the classic method. Then find the corners where edges bend, and write rules like “two triangular ears means a cat.”</p>\n<p>According to the lecture, every one of these attempts failed. The same cat becomes a completely different block of numbers depending on viewpoint, lighting, pose and background, and every new class needs a new set of rules written from scratch.</p>\n\n<h2>4. The Data-Driven Approach: Examples Instead of Rules</h2>\n<p>The answer was the <strong>data-driven approach</strong>. In everyday use the phrase means deciding by data and numbers rather than gut feeling, but here it means something a little different: instead of a person writing the rules in code, <strong>collect many examples with the right answers and let the computer find the rules</strong>. The lecture notes compare it to teaching a child what a cat is by showing many cat photos.</p>\n<ol><li><p>Collect images and their right answers (labels), class by class. This is the <strong>training set</strong>.</p></li><li><p>Train a classifier on the training set.</p></li><li><p>Check whether the classifier gets the right answer on images it has never seen.</p></li></ol>\n<p>So there are now two functions to write instead of one: <code>train</code>, which learns, and <code>predict</code>, which makes the actual predictions.</p>\n<pre><code>def train(images, labels):\n    # some machine learning\n    return model\n\ndef predict(model, image):\n    # use the model\n    return predicted_label</code></pre>\n<blockquote><p>The lecture says the nearest neighbor classifier is a way to build “this”. But what is “this”?</p></blockquote>\n<p>Looking again, “this” is what actually fills in the two functions <code>train</code> and <code>predict</code>: a <strong>classifier</strong> (a model). The nearest neighbor classifier is the simplest way to fill them in.</p>\n\n<h2>5. The Nearest Neighbor Classifier</h2>\n<p>As the name says, it is <strong>a classifier that answers by looking at the nearest neighbor</strong>. Learning from examples that come with the right answers is called <strong>supervised learning</strong>, and this is its simplest example.</p>\n<ul><li><p><strong>train</strong>: remember all the training data as it is. That’s all.</p></li><li><p><strong>predict</strong>: when a new image comes in, compare it with every stored image and answer with <strong>the label of the closest one</strong>.</p></li></ul>\n<p>Which raises a question right away.</p>\n<blockquote><p>What does it mean for two images to be “close”?</p></blockquote>\n<p>Images are tables of numbers, so compare the numbers at the same position in both images. A calculation done position by position like this is called <strong>element-wise</strong>. Subtract at each position, drop the sign (take the absolute value), and add everything up: you get one number. That number is used as the <strong>distance</strong> between the two images.</p>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-l1.120e086dee.1440.webp\" alt=\"A 4 × 4 training image subtracted cell by cell from a 4 × 4 test image, the absolute values in a third table, and the 16 cells added up to 182.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"Subtract at each position, drop the sign, and add everything up. Here the distance between the two images is 182. Only one color channel is shown.\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-l1.120e086dee.640.webp 640w, /data/img/fig-l1.120e086dee.960.webp 960w, /data/img/fig-l1.120e086dee.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"460\" data-img-tone=\"light\" decoding=\"async\"><figcaption>Subtract at each position, drop the sign, and add everything up. Here the distance between the two images is 182. Only one color channel is shown.</figcaption></figure>\n<p>One thing to watch: this number says <strong>how different</strong> the images are, not how much they match. So <strong>smaller means more similar</strong>, and two identical images give 0. In my notes I first called it a “match rate”, which is the wrong way round.</p>\n<p>Here is a classifier that uses this distance. It is written to be easy to follow, so it compares the images one by one in a loop.</p>\n<pre><code>import numpy as np\nimport sys\n\nclass NearestNeighbor:\n    def __init__(self):\n        pass\n\n    def train(self, images, labels):\n        # simply remembers all the training data\n        self.images = images\n        self.labels = labels\n\n    def predict(self, test_image):\n        # assume that each image is vectorized to 1D\n        min_dist = sys.maxsize  # maximum integer in python\n\n        for i in range(self.images.shape[0]):\n            dist = np.sum(np.abs(self.images[i, :] - test_image))\n            if dist &lt; min_dist:\n                min_dist = dist\n                min_index = i\n\n        return self.labels[min_index]</code></pre>\n<p>Step by step, <code>predict</code> does this. Each image is assumed to be flattened into one row of 3,072 numbers beforehand.</p>\n<ol><li><p>Start the smallest distance found so far, <code>min_dist</code>, at a huge number.</p></li><li><p>Go through the stored images (<code>self.images</code>) from first to last and measure each one’s distance to the test image.</p></li><li><p>Whenever an image is closer than any so far, write down its distance and its index (<code>min_index</code>).</p></li><li><p>At the end, the index of the closest image is left; return its label.</p></li></ol>\n<p>In practice, the loop is replaced by one matrix calculation. NumPy does the whole thing in one step, which is much faster.</p>\n<pre><code>def predict(self, test_image):\n    dists = np.sum(np.abs(self.images - test_image), axis=1)  # N distances at once\n    return self.labels[np.argmin(dists)]</code></pre>\n<p>One trap if you run this yourself: if the images are still in the 0–255 integer type (uint8) when you subtract, 3 − 5 comes out as 254, not −2. That type cannot hold negative numbers, so the value wraps around. Convert to floating point before subtracting.</p>\n<p>According to the lecture notes, this classifier gets about 38.6% right on CIFAR-10 (50,000 training images, 10 classes). Guessing at random gives 10%, so it does learn something, but it is far from people (about 94%).</p>\n\n<h2>6. L1 Distance and L2 Distance</h2>\n<p>The distance above (subtract, absolute value, add up) is called the <strong>L1 distance</strong>. The lecture also uses the <strong>L2 distance</strong>. Both turn the difference between two images into one number; they only differ in how they add up the differences.</p>\n<math-block data-latex=\"d_1(I_1, I_2) = \\sum_p \\left| I_1^p - I_2^p \\right|\"><span class=\"katex-display\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics><mrow><msub><mi>d</mi><mn>1</mn></msub><mo stretchy=\"false\">(</mo><msub><mi>I</mi><mn>1</mn></msub><mo separator=\"true\">,</mo><msub><mi>I</mi><mn>2</mn></msub><mo stretchy=\"false\">)</mo><mo>=</mo><munder><mo>∑</mo><mi>p</mi></munder><mrow><mo fence=\"true\">∣</mo><msubsup><mi>I</mi><mn>1</mn><mi>p</mi></msubsup><mo>−</mo><msubsup><mi>I</mi><mn>2</mn><mi>p</mi></msubsup><mo fence=\"true\">∣</mo></mrow></mrow><annotation encoding=\"application/x-tex\">d_1(I_1, I_2) = \\sum_p \\left| I_1^p - I_2^p \\right|</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1em;vertical-align:-0.25em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">d</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mopen\">(</span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mpunct\">,</span><span class=\"mspace\" style=\"margin-right:0.1667em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mclose\">)</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:2.4361em;vertical-align:-1.3861em;\"></span><span class=\"mop op-limits\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.05em;\"><span style=\"top:-1.9em;margin-left:0em;\"><span class=\"pstrut\" style=\"height:3.05em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">p</span></span></span><span style=\"top:-3.05em;\"><span class=\"pstrut\" style=\"height:3.05em;\"></span><span><span class=\"mop op-symbol large-op\">∑</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.3861em;\"><span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.1667em;\"></span><span class=\"minner\"><span class=\"mopen delimcenter\" style=\"top:0em;\">∣</span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.7823em;\"><span style=\"top:-2.4337em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span><span style=\"top:-3.1809em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">p</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.2663em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">−</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.7823em;\"><span style=\"top:-2.4337em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span><span style=\"top:-3.1809em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">p</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.2663em;\"><span></span></span></span></span></span></span><span class=\"mclose delimcenter\" style=\"top:0em;\">∣</span></span></span></span></span></span></math-block>\n<math-block data-latex=\"d_2(I_1, I_2) = \\sqrt{\\sum_p \\left( I_1^p - I_2^p \\right)^2}\"><span class=\"katex-display\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics><mrow><msub><mi>d</mi><mn>2</mn></msub><mo stretchy=\"false\">(</mo><msub><mi>I</mi><mn>1</mn></msub><mo separator=\"true\">,</mo><msub><mi>I</mi><mn>2</mn></msub><mo stretchy=\"false\">)</mo><mo>=</mo><msqrt><mrow><munder><mo>∑</mo><mi>p</mi></munder><msup><mrow><mo fence=\"true\">(</mo><msubsup><mi>I</mi><mn>1</mn><mi>p</mi></msubsup><mo>−</mo><msubsup><mi>I</mi><mn>2</mn><mi>p</mi></msubsup><mo fence=\"true\">)</mo></mrow><mn>2</mn></msup></mrow></msqrt></mrow><annotation encoding=\"application/x-tex\">d_2(I_1, I_2) = \\sqrt{\\sum_p \\left( I_1^p - I_2^p \\right)^2}</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1em;vertical-align:-0.25em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\">d</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mopen\">(</span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mpunct\">,</span><span class=\"mspace\" style=\"margin-right:0.1667em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mclose\">)</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:3.04em;vertical-align:-1.5742em;\"></span><span class=\"mord sqrt\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.4658em;\"><span class=\"svg-align\" style=\"top:-5em;\"><span class=\"pstrut\" style=\"height:5em;\"></span><span class=\"mord\" style=\"padding-left:1em;\"><span class=\"mop op-limits\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.05em;\"><span style=\"top:-1.9em;margin-left:0em;\"><span class=\"pstrut\" style=\"height:3.05em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">p</span></span></span><span style=\"top:-3.05em;\"><span class=\"pstrut\" style=\"height:3.05em;\"></span><span><span class=\"mop op-symbol large-op\">∑</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.3861em;\"><span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.1667em;\"></span><span class=\"minner\"><span class=\"minner\"><span class=\"mopen delimcenter\" style=\"top:0em;\">(</span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.7823em;\"><span style=\"top:-2.4337em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span><span style=\"top:-3.1809em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">p</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.2663em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">−</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.7823em;\"><span style=\"top:-2.4337em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span><span style=\"top:-3.1809em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">p</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.2663em;\"><span></span></span></span></span></span></span><span class=\"mclose delimcenter\" style=\"top:0em;\">)</span></span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.9863em;\"><span style=\"top:-3.2352em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span></span></span></span></span></span></span><span style=\"top:-3.4258em;\"><span class=\"pstrut\" style=\"height:5em;\"></span><span class=\"hide-tail\" style=\"min-width:1.02em;height:3.08em;\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"400em\" height=\"3.08em\" viewBox=\"0 0 400000 3240\" preserveAspectRatio=\"xMinYMin slice\"><path d=\"M473,2793\nc339.3,-1799.3,509.3,-2700,510,-2702 l0 -0\nc3.3,-7.3,9.3,-11,18,-11 H400000v40H1017.7\ns-90.5,478,-276.2,1466c-185.7,988,-279.5,1483,-281.5,1485c-2,6,-10,9,-24,9\nc-8,0,-12,-0.7,-12,-2c0,-1.3,-5.3,-32,-16,-92c-50.7,-293.3,-119.7,-693.3,-207,-1200\nc0,-1.3,-5.3,8.7,-16,30c-10.7,21.3,-21.3,42.7,-32,64s-16,33,-16,33s-26,-26,-26,-26\ns76,-153,76,-153s77,-151,77,-151c0.7,0.7,35.7,202,105,604c67.3,400.7,102,602.7,104,\n606zM1001 80h400000v40H1017.7z\"/></svg></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.5742em;\"><span></span></span></span></span></span></span></span></span></span></math-block>\n<p>Reading the symbols one at a time: <math-inline data-latex=\"I_1\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>I</mi><mn>1</mn></msub></mrow><annotation encoding=\"application/x-tex\">I_1</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.8333em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline> and <math-inline data-latex=\"I_2\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>I</mi><mn>2</mn></msub></mrow><annotation encoding=\"application/x-tex\">I_2</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.8333em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline> are the two images being compared, and <math-inline data-latex=\"p\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>p</mi></mrow><annotation encoding=\"application/x-tex\">p</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.625em;vertical-align:-0.1944em;\"></span><span class=\"mord mathnormal\">p</span></span></span></span></math-inline> stands for one pixel position. <math-inline data-latex=\"I_1^p\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msubsup><mi>I</mi><mn>1</mn><mi>p</mi></msubsup></mrow><annotation encoding=\"application/x-tex\">I_1^p</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1.0486em;vertical-align:-0.2663em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.7823em;\"><span style=\"top:-2.4337em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span><span style=\"top:-3.1809em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">p</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.2663em;\"><span></span></span></span></span></span></span></span></span></span></math-inline> is the <math-inline data-latex=\"p\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>p</mi></mrow><annotation encoding=\"application/x-tex\">p</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.625em;vertical-align:-0.1944em;\"></span><span class=\"mord mathnormal\">p</span></span></span></span></math-inline>-th number of image <math-inline data-latex=\"I_1\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mi>I</mi><mn>1</mn></msub></mrow><annotation encoding=\"application/x-tex\">I_1</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.8333em;vertical-align:-0.15em;\"></span><span class=\"mord\"><span class=\"mord mathnormal\" style=\"margin-right:0.0785em;\">I</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3011em;\"><span style=\"top:-2.55em;margin-left:-0.0785em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">1</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span></span></span></span></math-inline>. <math-inline data-latex=\"\\sum_p\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msub><mo>∑</mo><mi>p</mi></msub></mrow><annotation encoding=\"application/x-tex\">\\sum_p</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1.1858em;vertical-align:-0.4358em;\"></span><span class=\"mop\"><span class=\"mop op-symbol small-op\" style=\"position:relative;top:0em;\">∑</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.0017em;\"><span style=\"top:-2.4003em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">p</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.4358em;\"><span></span></span></span></span></span></span></span></span></span></math-inline> (sigma) means “add up over every position <math-inline data-latex=\"p\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>p</mi></mrow><annotation encoding=\"application/x-tex\">p</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.625em;vertical-align:-0.1944em;\"></span><span class=\"mord mathnormal\">p</span></span></span></span></math-inline>.”</p>\n<ul><li><p><strong>L1 distance</strong> (Manhattan distance): add up the absolute differences.</p></li><li><p><strong>L2 distance</strong> (Euclidean distance): square the differences, add them up, then take the square root. It is the straight-line distance between two points from school, the Pythagorean theorem.</p></li></ul>\n<p>In the code, changing the one line that computes the distance gives L2:</p>\n<pre><code>dists = np.sqrt(np.sum((self.images - test_image) ** 2, axis=1))</code></pre>\n<p>Picture images with only two pixels and the difference becomes visible, because each image is then a point on a plane. From (0, 0) to (3, 4), L1 is <math-inline data-latex=\"3 + 4 = 7\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mn>3</mn><mo>+</mo><mn>4</mn><mo>=</mo><mn>7</mn></mrow><annotation encoding=\"application/x-tex\">3 + 4 = 7</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.7278em;vertical-align:-0.0833em;\"></span><span class=\"mord\">3</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">4</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">7</span></span></span></span></math-inline> and L2 is <math-inline data-latex=\"\\sqrt{3^2 + 4^2} = 5\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msqrt><mrow><msup><mn>3</mn><mn>2</mn></msup><mo>+</mo><msup><mn>4</mn><mn>2</mn></msup></mrow></msqrt><mo>=</mo><mn>5</mn></mrow><annotation encoding=\"application/x-tex\">\\sqrt{3^2 + 4^2} = 5</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1.04em;vertical-align:-0.1266em;\"></span><span class=\"mord sqrt\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.9134em;\"><span class=\"svg-align\" style=\"top:-3em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"mord\" style=\"padding-left:0.833em;\"><span class=\"mord\"><span class=\"mord\">3</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.7401em;\"><span style=\"top:-2.989em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">+</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mord\"><span class=\"mord\">4</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.7401em;\"><span style=\"top:-2.989em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\">2</span></span></span></span></span></span></span></span></span></span><span style=\"top:-2.8734em;\"><span class=\"pstrut\" style=\"height:3em;\"></span><span class=\"hide-tail\" style=\"min-width:0.853em;height:1.08em;\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"400em\" height=\"1.08em\" viewBox=\"0 0 400000 1080\" preserveAspectRatio=\"xMinYMin slice\"><path d=\"M95,702\nc-2.7,0,-7.17,-2.7,-13.5,-8c-5.8,-5.3,-9.5,-10,-9.5,-14\nc0,-2,0.3,-3.3,1,-4c1.3,-2.7,23.83,-20.7,67.5,-54\nc44.2,-33.3,65.8,-50.3,66.5,-51c1.3,-1.3,3,-2,5,-2c4.7,0,8.7,3.3,12,10\ns173,378,173,378c0.7,0,35.3,-71,104,-213c68.7,-142,137.5,-285,206.5,-429\nc69,-144,104.5,-217.7,106.5,-221\nl0 -0\nc5.3,-9.3,12,-14,20,-14\nH400000v40H845.2724\ns-225.272,467,-225.272,467s-235,486,-235,486c-2.7,4.7,-9,7,-19,7\nc-6,0,-10,-1,-12,-3s-194,-422,-194,-422s-65,47,-65,47z\nM834 80h400000v40h-400000z\"/></svg></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.1266em;\"><span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">5</span></span></span></span></math-inline>. L1 walks along a grid of streets; L2 cuts straight across. The name Manhattan distance comes from taxis driving along Manhattan’s grid of streets.</p>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-l1-l2.b7a9fc01a0.1440.webp\" alt=\"Left: from (0, 0) to (3, 4), the L1 path along the grid (7) and the straight L2 line (5). Middle: points at L1 distance 1 from the center form a diamond, points at L2 distance 1 a circle. Right: bars for B, with differences of 10 at four places, and C, with 40 at one place; L1 is 40 for both, L2 is 20 for B and 40 for C.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"Left: for the same two points, L1 is 7 and L2 is 5. Middle: the points at distance 1 from the center form a diamond for L1 and a circle for L2. Right: B spreads its differences evenly and C puts them in one place; L1 is the same, but L2 says C is twice as far.\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-l1-l2.b7a9fc01a0.640.webp 640w, /data/img/fig-l1-l2.b7a9fc01a0.960.webp 960w, /data/img/fig-l1-l2.b7a9fc01a0.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"590\" data-img-tone=\"light\" decoding=\"async\"><figcaption>Left: for the same two points, L1 is 7 and L2 is 5. Middle: the points at distance 1 from the center form a diamond for L1 and a circle for L2. Right: B spreads its differences evenly and C puts them in one place; L1 is the same, but L2 says C is twice as far.</figcaption></figure>\n<p>Where the two really differ is <strong>whether the differences are spread out or piled into one place</strong>.</p>\n<table style=\"min-width: 100px;\"><colgroup><col style=\"min-width: 25px;\"><col style=\"min-width: 25px;\"><col style=\"min-width: 25px;\"><col style=\"min-width: 25px;\"></colgroup><tbody><tr><th colspan=\"1\" rowspan=\"1\"><p>Compared</p></th><th colspan=\"1\" rowspan=\"1\"><p>Pixel differences</p></th><th colspan=\"1\" rowspan=\"1\"><p>L1</p></th><th colspan=\"1\" rowspan=\"1\"><p>L2</p></th></tr><tr><td colspan=\"1\" rowspan=\"1\"><p>B</p></td><td colspan=\"1\" rowspan=\"1\"><p>10, 10, 10, 10</p></td><td colspan=\"1\" rowspan=\"1\"><p>40</p></td><td colspan=\"1\" rowspan=\"1\"><p>20</p></td></tr><tr><td colspan=\"1\" rowspan=\"1\"><p>C</p></td><td colspan=\"1\" rowspan=\"1\"><p>40, 0, 0, 0</p></td><td colspan=\"1\" rowspan=\"1\"><p>40</p></td><td colspan=\"1\" rowspan=\"1\"><p>40</p></td></tr></tbody></table>\n<p>L1 says B and C are equally far. L2 squares the differences, so a big difference costs much more, and it calls C, with one big difference, twice as far. In the words of the lecture notes, L2 prefers many medium disagreements to one big one.</p>\n<p>Neither is always better. On CIFAR-10, L1 did slightly better (38.6% against 35.4% for L2), but other data can give other results. So the choice of distance is also something to test and pick; more on that in section 9.</p>\n<p>The names L1 and L2 come from the Lp norm in mathematics. A <strong>norm</strong> measures the size of a vector, that is, a row of numbers.</p>\n<math-block data-latex=\"\\lVert x \\rVert_p = \\left( \\sum_i |x_i|^p \\right)^{1/p}\"><span class=\"katex-display\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\" display=\"block\"><semantics><mrow><mo stretchy=\"false\">∥</mo><mi>x</mi><msub><mo stretchy=\"false\">∥</mo><mi>p</mi></msub><mo>=</mo><msup><mrow><mo fence=\"true\">(</mo><munder><mo>∑</mo><mi>i</mi></munder><mi mathvariant=\"normal\">∣</mi><msub><mi>x</mi><mi>i</mi></msub><msup><mi mathvariant=\"normal\">∣</mi><mi>p</mi></msup><mo fence=\"true\">)</mo></mrow><mrow><mn>1</mn><mi mathvariant=\"normal\">/</mi><mi>p</mi></mrow></msup></mrow><annotation encoding=\"application/x-tex\">\\lVert x \\rVert_p = \\left( \\sum_i |x_i|^p \\right)^{1/p}</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1.0361em;vertical-align:-0.2861em;\"></span><span class=\"mopen\">∥</span><span class=\"mord mathnormal\">x</span><span class=\"mclose\"><span class=\"mclose\">∥</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.1514em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">p</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.2861em;\"><span></span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:3.3056em;vertical-align:-1.2777em;\"></span><span class=\"minner\"><span class=\"minner\"><span class=\"mopen delimcenter\" style=\"top:0em;\"><span class=\"delimsizing size4\">(</span></span><span class=\"mop op-limits\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.05em;\"><span style=\"top:-1.8723em;margin-left:0em;\"><span class=\"pstrut\" style=\"height:3.05em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">i</span></span></span><span style=\"top:-3.05em;\"><span class=\"pstrut\" style=\"height:3.05em;\"></span><span><span class=\"mop op-symbol large-op\">∑</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:1.2777em;\"><span></span></span></span></span></span><span class=\"mspace\" style=\"margin-right:0.1667em;\"></span><span class=\"mord\">∣</span><span class=\"mord\"><span class=\"mord mathnormal\">x</span><span class=\"msupsub\"><span class=\"vlist-t vlist-t2\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.3117em;\"><span style=\"top:-2.55em;margin-left:0em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">i</span></span></span></span><span class=\"vlist-s\">​</span></span><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.15em;\"><span></span></span></span></span></span></span><span class=\"mord\"><span class=\"mord\">∣</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.7144em;\"><span style=\"top:-3.113em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mathnormal mtight\">p</span></span></span></span></span></span></span></span><span class=\"mclose delimcenter\" style=\"top:0em;\"><span class=\"delimsizing size4\">)</span></span></span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:2.0279em;\"><span style=\"top:-4.2029em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\"><span class=\"mord mtight\">1/</span><span class=\"mord mathnormal mtight\">p</span></span></span></span></span></span></span></span></span></span></span></span></span></math-block>\n<p>With <math-inline data-latex=\"p = 1\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>p</mi><mo>=</mo><mn>1</mn></mrow><annotation encoding=\"application/x-tex\">p = 1</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.625em;vertical-align:-0.1944em;\"></span><span class=\"mord mathnormal\">p</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">1</span></span></span></span></math-inline> this is L1, and with <math-inline data-latex=\"p = 2\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>p</mi><mo>=</mo><mn>2</mn></mrow><annotation encoding=\"application/x-tex\">p = 2</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.625em;vertical-align:-0.1944em;\"></span><span class=\"mord mathnormal\">p</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">2</span></span></span></span></math-inline> it is L2. Put the differences between two images in one row and measure its size: that is the distance between the images. One more thing: when you only want the closest image, you can skip the square root in L2. The square root keeps the order, so the closest image stays the same.</p>\n\n<h2>7. Time Complexity: Fast to Train, Slow to Predict</h2>\n<p>Now, how fast is this classifier? The speed of an algorithm is usually given as its <strong>time complexity</strong>: roughly, how the running time grows with the number of data points <math-inline data-latex=\"N\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>N</mi></mrow><annotation encoding=\"application/x-tex\">N</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.6833em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.109em;\">N</span></span></span></span></math-inline>. It is written in big-O notation.</p>\n<ul><li><p><math-inline data-latex=\"O(1)\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>O</mi><mo stretchy=\"false\">(</mo><mn>1</mn><mo stretchy=\"false\">)</mo></mrow><annotation encoding=\"application/x-tex\">O(1)</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1em;vertical-align:-0.25em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.0278em;\">O</span><span class=\"mopen\">(</span><span class=\"mord\">1</span><span class=\"mclose\">)</span></span></span></span></math-inline>: the time is the same whatever <math-inline data-latex=\"N\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>N</mi></mrow><annotation encoding=\"application/x-tex\">N</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.6833em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.109em;\">N</span></span></span></span></math-inline> is.</p></li><li><p><math-inline data-latex=\"O(N)\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>O</mi><mo stretchy=\"false\">(</mo><mi>N</mi><mo stretchy=\"false\">)</mo></mrow><annotation encoding=\"application/x-tex\">O(N)</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1em;vertical-align:-0.25em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.0278em;\">O</span><span class=\"mopen\">(</span><span class=\"mord mathnormal\" style=\"margin-right:0.109em;\">N</span><span class=\"mclose\">)</span></span></span></span></math-inline>: when <math-inline data-latex=\"N\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>N</mi></mrow><annotation encoding=\"application/x-tex\">N</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.6833em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.109em;\">N</span></span></span></span></math-inline> doubles, the time doubles.</p></li></ul>\n<p><code>train</code> of the nearest neighbor classifier is <math-inline data-latex=\"O(1)\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>O</mi><mo stretchy=\"false\">(</mo><mn>1</mn><mo stretchy=\"false\">)</mo></mrow><annotation encoding=\"application/x-tex\">O(1)</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1em;vertical-align:-0.25em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.0278em;\">O</span><span class=\"mopen\">(</span><span class=\"mord\">1</span><span class=\"mclose\">)</span></span></span></span></math-inline>. Actually reading the data would take longer as <math-inline data-latex=\"N\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>N</mi></mrow><annotation encoding=\"application/x-tex\">N</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.6833em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.109em;\">N</span></span></span></span></math-inline> grows, but the course leaves that out and treats training as just pointing at the data, with nothing to compute. <code>predict</code>, on the other hand, must compare the new image with all <math-inline data-latex=\"N\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>N</mi></mrow><annotation encoding=\"application/x-tex\">N</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.6833em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.109em;\">N</span></span></span></span></math-inline> stored images, so it is <math-inline data-latex=\"O(N)\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>O</mi><mo stretchy=\"false\">(</mo><mi>N</mi><mo stretchy=\"false\">)</mo></mrow><annotation encoding=\"application/x-tex\">O(N)</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:1em;vertical-align:-0.25em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.0278em;\">O</span><span class=\"mopen\">(</span><span class=\"mord mathnormal\" style=\"margin-right:0.109em;\">N</span><span class=\"mclose\">)</span></span></span></span></math-inline>. More precisely, each comparison looks at all 3,072 numbers, so it takes about <math-inline data-latex=\"N \\times 3{,}072\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi>N</mi><mo>×</mo><mn>3,072</mn></mrow><annotation encoding=\"application/x-tex\">N \\times 3{,}072</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.7667em;vertical-align:-0.0833em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.109em;\">N</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">×</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.8389em;vertical-align:-0.1944em;\"></span><span class=\"mord\">3</span><span class=\"mord\"><span class=\"mpunct\">,</span></span><span class=\"mord\">072</span></span></span></span></math-inline> steps.</p>\n<blockquote><p>If training is fast and prediction is slow, is that a good system?</p></blockquote>\n<p>To me it did not look good. You train once, but you predict every time the system is used. Take YouTube: recommending videos like the one you are watching is a prediction. If every recommendation meant comparing against every video on YouTube one by one, it could never keep up. To be useful in practice, prediction has to be fast, even if training takes long.</p>\n<p>When I went back to the lecture notes, they said the same thing. The notes call this arrangement “backwards” and explain that the neural networks coming later in the course are the opposite: training takes a lot of time, but once trained, classifying a new image is very fast. Reaching the course’s conclusion on my own made me a little proud.</p>\n<p>Out of curiosity I looked it up, and YouTube’s recommendations do use “find the closest ones”. In a <a target=\"_blank\" rel=\"noopener\" href=\"https://research.google/pubs/pub45530/\">2016 Google paper on YouTube recommendations</a>, videos and users are turned into short lists of numbers learned in training, and recommending means finding the nearby videos. But they don’t compare against every video; they use a method that gives up a little accuracy for much faster search, the approximate nearest neighbor search of section 11. My hunch that comparing everything every time can’t work was right, and the fix was at the end of the lecture.</p>\n\n<h2>8. Decision Boundaries and k-Nearest Neighbors</h2>\n<p>To see how a classifier actually answers, draw its <strong>decision boundary</strong>. Color every possible input by the classifier’s answer, and lines appear where the answer changes; those lines are the decision boundary. It is less a gray zone than a border: cross it and the answer changes.</p>\n<p>An image has 3,072 numbers and can’t be drawn, so let’s use points with just two numbers instead.</p>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-knn.4fcadf00fb.1440.webp\" alt=\"80 points in three classes, and the decision boundaries 1-NN and 5-NN paint with the same points. In 1-NN every point sitting inside another group gets a small island; in 5-NN the islands disappear, the boundaries are smoother, and tied places stay white.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"Example data where each point is two numbers (dashed rings mark points sitting inside another group). The middle panel is the 1-NN decision boundary, the right one 5-NN; white areas are tied votes. Distances are L2.\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-knn.4fcadf00fb.640.webp 640w, /data/img/fig-knn.4fcadf00fb.960.webp 960w, /data/img/fig-knn.4fcadf00fb.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"660\" data-img-tone=\"light\" decoding=\"async\"><figcaption>Example data where each point is two numbers (dashed rings mark points sitting inside another group). The middle panel is the 1-NN decision boundary, the right one 5-NN; white areas are tied votes. Distances are L2.</figcaption></figure>\n<p>1-NN, which looks only at the single closest point, has a ragged boundary. Even one point stuck inside another group gets its own little island. Such points are often labeling mistakes or unusual data, and 1-NN follows them anyway.</p>\n<p>This is where the <strong>k-nearest neighbor classifier</strong> (kNN) comes in. Instead of the single closest point, it <strong>finds the K closest points and takes a majority vote</strong>. With K = 1 it is the nearest neighbor classifier we started with. A larger K listens to more neighbors, so the islands of lone points disappear and the boundaries smooth out. Even if a few labels are mixed up, it goes by the many points around them.</p>\n<p>The white parts of the right panel are places where <strong>the vote is tied</strong> and no answer wins, for example when the five neighbors split 2 A, 2 B and 1 C.</p>\n<blockquote><p>Wouldn’t an odd K avoid ties?</p></blockquote>\n<p>That is why K is usually odd, such as 1, 3 or 5. With two classes, an odd K can never tie. With three or more classes, though, an odd K can still tie, as in the 2 : 2 : 1 case above, so ties never disappear completely.</p>\n<p>Too large a K is a problem too. The boundaries blur so much that small groups get swallowed by big ones, and in the extreme, with K equal to the whole dataset, the answer is always the most common class.</p>\n\n<h2>9. How to Pick K: Validation Sets and Cross-Validation</h2>\n<p>Values like K, or the choice between L1 and L2, are not learned in training but <strong>set by a person beforehand</strong>. They are called <strong>hyperparameters</strong>. You try several and keep the one that works best. The important part is <strong>what you test them on</strong>.</p>\n<ul><li><p>The <strong>test set</strong> is the real exam. Use it only once, at the very end. Picking K while looking at the test set is like studying with the exam questions in hand: the score comes out better than real use will be.</p></li><li><p>The <strong>validation set</strong> is a practice exam. Set aside part of the training data and score each K on it. The validation set has right answers too, but it is not used for training.</p></li></ul>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-split.f0937d12f4.1440.webp\" alt=\"Top: training data and test data. Middle: part of the training data held out as a validation set. Bottom: 5-fold cross-validation, where the training data is cut into five folds, each fold takes a turn as the validation set, and the five scores are averaged.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"Use the test set only once, at the end. Pick K with a validation set held out from the training data (middle), or, with little data, with cross-validation, where five folds take turns as the validation set (bottom).\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-split.f0937d12f4.640.webp 640w, /data/img/fig-split.f0937d12f4.960.webp 960w, /data/img/fig-split.f0937d12f4.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"700\" data-img-tone=\"light\" decoding=\"async\"><figcaption>Use the test set only once, at the end. Pick K with a validation set held out from the training data (middle), or, with little data, with cross-validation, where five folds take turns as the validation set (bottom).</figcaption></figure>\n<p>When there is too little training data to hold out a big validation set, use <strong>cross-validation</strong>. Split the training data into, say, five <strong>folds</strong>, let each fold take a turn as the validation set, and average the five scores. The score wobbles less than with a single split, so it is easier to trust. In the lecture notes’ example on CIFAR-10, K around 7 worked best.</p>\n<p>Cross-validation means training and scoring five times, though, so it costs a lot of computation. With plenty of data, a single validation set is the usual choice.</p>\n\n<h2>10. Why It Is Rarely Used on Images</h2>\n<p>The lecture says kNN is hardly ever used on images, except for very simple cases. There are three reasons.</p>\n<h3>Pixel distance is not what people see</h3>\n<p>Take one picture and change it three ways: shift it a little sideways, cover the eyes and mouth, and darken the whole thing slightly. To a person, the one with covered eyes and mouth looks the most different. Yet you can make all three exactly <strong>the same</strong> L2 distance from the original.</p>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-face.01ed022e4a.1440.webp\" alt=\"A 48 × 48 grayscale drawn face: the original, shifted sideways, eyes and mouth covered with black boxes, and darkened. All three changed versions are at L2 distance 2153 from the original.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"The three on the right are all at L2 distance 2153 from the original. The shift (about 4 pixels) and the darkening (45 brightness levels) were tuned to make it so. A 48 × 48 grayscale face I drew for this.\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-face.01ed022e4a.640.webp 640w, /data/img/fig-face.01ed022e4a.960.webp 960w, /data/img/fig-face.01ed022e4a.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"520\" data-img-tone=\"light\" decoding=\"async\"><figcaption>The three on the right are all at L2 distance 2153 from the original. The shift (about 4 pixels) and the darkening (45 brightness levels) were tuned to make it so. A 48 × 48 grayscale face I drew for this.</figcaption></figure>\n<p>Pixel distance reacts strongly to small shifts and overall brightness, but it does not care what is in the picture. The lecture notes also show CIFAR-10 images laid out by pixel distance: they gather by <strong>similar background color</strong>, not by kind. A dog on a white background lands next to a frog on a white background.</p>\n<h3>Prediction is slow</h3>\n<p>As in section 7, every prediction compares against all the training images. Today’s images have many pixels, so even comparing two images takes a while.</p>\n<h3>The curse of dimensionality</h3>\n<p>For the nearest neighbor to be right, a truly similar training image has to lie near the new image. If the training data covers the space only sparsely, even the “nearest” neighbor is actually far away, and its answer is hard to trust.</p>\n<p>The catch is that every number is one axis of the space, one <strong>dimension</strong>. Say we fill the space by placing 4 evenly spaced points along each axis. One dimension needs 4 points, two need <math-inline data-latex=\"4 \\times 4 = 16\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mn>4</mn><mo>×</mo><mn>4</mn><mo>=</mo><mn>16</mn></mrow><annotation encoding=\"application/x-tex\">4 \\times 4 = 16</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.7278em;vertical-align:-0.0833em;\"></span><span class=\"mord\">4</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span><span class=\"mbin\">×</span><span class=\"mspace\" style=\"margin-right:0.2222em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">4</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span><span class=\"mrel\">=</span><span class=\"mspace\" style=\"margin-right:0.2778em;\"></span></span><span class=\"base\"><span class=\"strut\" style=\"height:0.6444em;\"></span><span class=\"mord\">16</span></span></span></span></math-inline>, and three need 64. Each extra dimension multiplies the count by 4.</p>\n<figure data-type=\"image-block\" class=\"image-block image-block--center\" style=\"max-width: min(100%, 100%);\"><img src=\"/data/img/fig-curse.0bcf1f8161.1440.webp\" alt=\"Filling space with 4 points per axis: 4 points on a line in one dimension, a 4 × 4 grid of 16 in two, 64 in a cube in three, and a note that 3,072 dimensions need 4 to the power 3072 points.\" title=\"\" data-width=\"100\" data-align=\"center\" data-caption=\"Filling space with 4 points per axis takes 4 times more points for every extra dimension. For a 32 × 32 color image (3,072 dimensions) that is 4 to the power 3072, a number with 1,850 digits.\" class=\"editor-image image-align-center\" loading=\"lazy\" style=\"width: 100%; height: auto;\" srcset=\"/data/img/fig-curse.0bcf1f8161.640.webp 640w, /data/img/fig-curse.0bcf1f8161.960.webp 960w, /data/img/fig-curse.0bcf1f8161.1440.webp 1440w\" sizes=\"(max-width: 767px) calc(100vw - 32px), 720px\" width=\"1440\" height=\"520\" data-img-tone=\"light\" decoding=\"async\"><figcaption>Filling space with 4 points per axis takes 4 times more points for every extra dimension. For a 32 × 32 color image (3,072 dimensions) that is 4 to the power 3072, a number with 1,850 digits.</figcaption></figure>\n<p>A 32 × 32 color image has 3,072 dimensions. Filling that space the same way takes <math-inline data-latex=\"4^{3072}\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msup><mn>4</mn><mn>3072</mn></msup></mrow><annotation encoding=\"application/x-tex\">4^{3072}</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.8141em;\"></span><span class=\"mord\"><span class=\"mord\">4</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.8141em;\"><span style=\"top:-3.063em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\"><span class=\"mord mtight\">3072</span></span></span></span></span></span></span></span></span></span></span></span></math-inline> images, a number with 1,850 digits. The observable universe has about <math-inline data-latex=\"10^{80}\"><span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><msup><mn>10</mn><mn>80</mn></msup></mrow><annotation encoding=\"application/x-tex\">10^{80}</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.8141em;\"></span><span class=\"mord\">1</span><span class=\"mord\"><span class=\"mord\">0</span><span class=\"msupsub\"><span class=\"vlist-t\"><span class=\"vlist-r\"><span class=\"vlist\" style=\"height:0.8141em;\"><span style=\"top:-3.063em;margin-right:0.05em;\"><span class=\"pstrut\" style=\"height:2.7em;\"></span><span class=\"sizing reset-size6 size3 mtight\"><span class=\"mord mtight\"><span class=\"mord mtight\">80</span></span></span></span></span></span></span></span></span></span></span></span></math-inline> atoms, an 81-digit number, so there is no comparison. This blow-up in the data needed as dimensions grow is called the <strong>curse of dimensionality</strong>.</p>\n\n<h2>11. If You Still Want to Use It</h2>\n<p>The lecture lists what to consider if you still need kNN.</p>\n<ul><li><p><strong>Normalization</strong>: rescale every feature to mean 0 and standard deviation 1. The mean is the middle of the values; the standard deviation says how far the values spread around it. Distance adds up the differences of all features, so one feature with a big range can decide the distance on its own. Compare people by age and yearly salary in won, for example, and a 10,000-won salary gap counts for more than a 30-year age gap. Normalization prevents that. Pixels are all in 0–255 to begin with, though, so it matters less for images.</p></li><li><p><strong>Dimensionality reduction</strong>: keep only the important information and shrink the 3,072 numbers to a few dozen before comparing. PCA (principal component analysis) is the best-known method.</p></li><li><p><strong>Picking hyperparameters</strong>: choose K and the distance (L1 or L2) with a validation set or cross-validation, as in section 9.</p></li><li><p><strong>Approximate nearest neighbor</strong> (ANN) search: with a lot of data, instead of searching to the end for the true nearest neighbor, stop once a neighbor is close enough. It gives up a little accuracy for a lot of speed, using an index built over the data in advance (such as a kd-tree).</p></li></ul>\n<p>The last item is the one I wrote down during the lecture as “니오레스 네이버?”, my Korean spelling of what I thought I heard. I had no idea what it meant and just left a question mark, but the lecture notes showed it was Approximate Nearest Neighbor. The YouTube recommendations from section 7 use exactly this.</p>\n\n<h2>Wrap-up</h2>\n<ul><li><p>A single <strong>perceptron</strong> is a linear classifier that splits with one straight boundary, so it cannot make XOR; stacked layers can. Backpropagation is how stacked layers are trained.</p></li><li><p><strong>Deep learning</strong> is training neural networks with many layers on large data, and AlexNet showed its strength in 2012.</p></li><li><p><strong>Image classification</strong> means picking one of a fixed set of classes, and instead of writing rules by hand we let the computer learn from data (<code>train</code> and <code>predict</code>).</p></li><li><p>The <strong>nearest neighbor classifier</strong> remembers everything and answers with the label of the closest image. “Close” is measured with the L1 or L2 distance.</p></li><li><p><strong>kNN</strong> takes a majority vote of K neighbors; K and the distance are picked with a validation set.</p></li><li><p>But prediction is slow, pixel distance differs from what people see, and the curse of dimensionality bites, so it is rarely used on images.</p></li></ul>\n<blockquote><p>A nearest neighbor classifier trains for free and pays at prediction time. Neural networks turn that around.</p></blockquote>\n\n<h2>Glossary</h2>\n<ul><li><p><strong>Perceptron</strong>: a unit that outputs 1 when the sum of input × weight is above a threshold. Its weights are fitted to data.</p></li><li><p><strong>Linear classifier</strong>: a classifier that splits with one straight, flat boundary (a line or a plane).</p></li><li><p><strong>Multilayer perceptron</strong> (MLP): perceptrons stacked in layers, where each layer’s outputs are the next layer’s inputs.</p></li><li><p><strong>Backpropagation</strong>: the method for fitting the weights of many layers to data. Made known in 1986.</p></li><li><p><strong>Class and label</strong>: the possible answers, and the right answer attached to each piece of data.</p></li><li><p><strong>Data-driven approach</strong>: learning from examples with right answers instead of hand-written rules.</p></li><li><p><strong>Nearest neighbor classifier</strong>: remembers all the training data and answers with the label of the closest example.</p></li><li><p><strong>L1 distance</strong>: the sum of absolute differences. <strong>L2 distance</strong>: the square root of the sum of squared differences.</p></li><li><p><strong>Time complexity</strong>: how running time grows with the amount of data, written as O(1), O(N) and so on.</p></li><li><p><strong>Decision boundary</strong>: where the classifier’s answer changes.</p></li><li><p><strong>kNN</strong>: a classifier that decides by a majority vote of the K closest examples.</p></li><li><p><strong>Hyperparameter</strong>: a value such as K or the kind of distance that a person sets before training.</p></li><li><p><strong>Validation set and test set</strong>: the practice exam for picking hyperparameters, and the real exam taken once at the end.</p></li><li><p><strong>Cross-validation</strong>: splitting the training data into folds that take turns as the validation set, then averaging.</p></li><li><p><strong>Curse of dimensionality</strong>: the explosion in data needed to fill a space as the number of dimensions grows.</p></li><li><p><strong>Approximate nearest neighbor</strong> (ANN) search: finding close neighbors fast by giving up a little accuracy.</p></li></ul>\n\n<h2>What to Look Into Next</h2>\n<p>Writing up this lecture left me with things I want to look into separately.</p>\n<ul><li><p>How does <strong>Canny edge detection</strong> actually find edges? It reportedly works in four steps: blur, compute brightness gradients, thin the lines, and filter with two thresholds. The two thresholds remind me of a Schmitt trigger, which makes me even more curious. My earlier post <a href=\"https://stanase.com/posts/opencv-lane-detection-basics/\">“Summary of Key Features of OpenCV Lane Detection”</a> only covered how to call it.</p></li><li><p>How do you find things when <strong>a photo has several objects</strong>? That is a different problem, called object detection.</p></li><li><p>How does <strong>backpropagation</strong> fit the weights of all the layers at once? Lecture 3 of this course and the question from my previous post point to the same place.</p></li><li><p>How is the <strong>linear classifier</strong> of Lecture 2 different from the nearest neighbor classifier? I expect the opposite trade: slow to train, fast to predict.</p></li><li><p>What does <strong>PCA</strong> keep, and what does it throw away, out of the 3,072 numbers?</p></li><li><p>How does <strong>approximate nearest neighbor</strong> search find close points without comparing against all of them?</p></li></ul>",
      "rendered": true
    }
  }
}
