<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><generator uri="https://jekyllrb.com/" version="3.9.3">Jekyll</generator><link href="https://agency301.github.io//feed.xml" rel="self" type="application/atom+xml" /><link href="https://agency301.github.io//" rel="alternate" type="text/html" hreflang="en" /><updated>2023-08-16T05:51:27+00:00</updated><id>https://agency301.github.io//feed.xml</id><title type="html">301 Agency</title><subtitle>301 Agency is a blog for up-to-date AI Paper Reviews, posting mathematical details.
</subtitle><entry><title type="html">Neural ODE를 이용한 연속적인 계층을 가진 신경망 모델링</title><link href="https://agency301.github.io//modeling/2023-08-11-NeuralODE/" rel="alternate" type="text/html" title="Neural ODE를 이용한 연속적인 계층을 가진 신경망 모델링" /><published>2023-08-11T11:18:10+00:00</published><updated>2023-08-11T11:18:10+00:00</updated><id>https://agency301.github.io//modeling/NeuralODE</id><content type="html" xml:base="https://agency301.github.io//modeling/2023-08-11-NeuralODE/">&lt;ol id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#prerequisites&quot; id=&quot;markdown-toc-prerequisites&quot;&gt;Prerequisites&lt;/a&gt;    &lt;ol&gt;
      &lt;li&gt;&lt;a href=&quot;#euler-method&quot; id=&quot;markdown-toc-euler-method&quot;&gt;Euler Method&lt;/a&gt;&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#paper-review&quot; id=&quot;markdown-toc-paper-review&quot;&gt;Paper Review&lt;/a&gt;    &lt;ol&gt;
      &lt;li&gt;&lt;a href=&quot;#neural-ordinary-differential-equations-20191214&quot; id=&quot;markdown-toc-neural-ordinary-differential-equations-20191214&quot;&gt;Neural Ordinary Differential Equations (2019.12.14.)&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#introduction&quot; id=&quot;markdown-toc-introduction&quot;&gt;Introduction&lt;/a&gt;        &lt;ol&gt;
          &lt;li&gt;&lt;a href=&quot;#idea&quot; id=&quot;markdown-toc-idea&quot;&gt;Idea&lt;/a&gt;&lt;/li&gt;
        &lt;/ol&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#continuous-backpropagation&quot; id=&quot;markdown-toc-continuous-backpropagation&quot;&gt;Continuous Backpropagation&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#benefits-of-neural-ode&quot; id=&quot;markdown-toc-benefits-of-neural-ode&quot;&gt;Benefits of Neural ODE&lt;/a&gt;        &lt;ol&gt;
          &lt;li&gt;&lt;a href=&quot;#continuous-time-series-model을-만들-수-있다&quot; id=&quot;markdown-toc-continuous-time-series-model을-만들-수-있다&quot;&gt;Continuous time-series model을 만들 수 있다.&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#계산의-정확도를-쉽게-조절할-수-있다&quot; id=&quot;markdown-toc-계산의-정확도를-쉽게-조절할-수-있다&quot;&gt;계산의 정확도를 쉽게 조절할 수 있다&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#continuous-normalizing-flow-모델-구현&quot; id=&quot;markdown-toc-continuous-normalizing-flow-모델-구현&quot;&gt;Continuous Normalizing Flow 모델 구현&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#메모리-효율성&quot; id=&quot;markdown-toc-메모리-효율성&quot;&gt;메모리 효율성&lt;/a&gt;&lt;/li&gt;
        &lt;/ol&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#conclusion&quot; id=&quot;markdown-toc-conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#related-works&quot; id=&quot;markdown-toc-related-works&quot;&gt;Related Works&lt;/a&gt;    &lt;ol&gt;
      &lt;li&gt;&lt;a href=&quot;#augmented-neural-ode&quot; id=&quot;markdown-toc-augmented-neural-ode&quot;&gt;Augmented Neural ODE&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#normalizing-flow&quot; id=&quot;markdown-toc-normalizing-flow&quot;&gt;Normalizing Flow&lt;/a&gt;&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;h4 id=&quot;euler-method&quot;&gt;Euler Method&lt;/h4&gt;

&lt;p&gt;Euler method는 미분방정식이 주어질 때 함수의 함숫값을 구하는 한 방법이다.&lt;/p&gt;

&lt;p&gt;이 논문에서는 hidden state의 연속적인 mapping을 위해서 상미분방정식을 사용하는데, 이 때 foward 와 backward 과정 모두에서 Euler method를 통해 식을 전개하게 된다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/NeuralODE/IMG_0068.jpeg&quot; alt=&quot;IMG_0068.jpeg&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;paper-review&quot;&gt;Paper Review&lt;/h2&gt;

&lt;h3 id=&quot;neural-ordinary-differential-equations-20191214&quot;&gt;Neural Ordinary Differential Equations (2019.12.14.)&lt;/h3&gt;

&lt;p&gt;paper link:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1806.07366&quot;&gt;Neural Ordinary Differential Equations&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;introduction&quot;&gt;Introduction&lt;/h3&gt;

&lt;p&gt;대부분의 인공지능 모델이 이산적인 시간에 대한 데이터를 다룬다. 심층 신경망 모델의 Layer 자체가 이산적인 형태이기도 하고, 데이터도 이산적인 형태의 것이기 때문이다.&lt;/p&gt;

&lt;p&gt;이 논문에서는 hidden state가 변화하는 과정을 상미분방정식으로 모델링하여 연속적인 깊이를 가진 모델을 구성하였다. 계층 구조가 이산적인 계산 형태에서 탈피하여 연속적인 모델링이 가능해지고, 연속적인 잠재 함수를 모델링하는 것에 대한 표현력이 증가함을 밝혔다.&lt;/p&gt;

&lt;p&gt;hidden state를 나타내는 함수의 derivative를 구하는 과정을 Ordinary Differential Equation(ODE)로 표현하였으며, 어떠한 ODE solver에 대해서도 Backpropagation을 수행할 수 있는 Black-box 모델을 통해 출력을 생성하게 된다.&lt;/p&gt;

&lt;h4 id=&quot;idea&quot;&gt;Idea&lt;/h4&gt;

&lt;p&gt;residual network, recurrent network 등을 생각해보면 모델에 들어온 입력에 Transformation을 가하는데, 이는 시간 $t$에 대해 이산적인 sequence를 생성한다. 이를 hidden state $\mathbf{h}_t$와 model parameter $\theta_t$에 관한 식으로 나타내면 다음과 같다.&lt;/p&gt;

&lt;p&gt;$\mathbf{h}_{t+1}=\mathbf{h}_t+f(\mathbf{h}_t, \theta_t)$&lt;/p&gt;

&lt;p&gt;하지만 만약에 hidden state를 계속 추가해서, 연속적인 시간에 대해 $\mathbf{h}_t$를 구할 수 있다면 어떨까? 이를 위해 다음과 같은 ODE를 구성한다.&lt;/p&gt;

&lt;p&gt;$\frac{d\mathbf{h}_t}{dt}=f(\mathbf{h}(t), t, \theta)$&lt;/p&gt;

&lt;p&gt;이를 이용하면 어떠한 연속적인 시점 $t$에 대해서도 model output $\mathbf{h}$를 구할 수 있을 것이다.&lt;/p&gt;

&lt;h3 id=&quot;continuous-backpropagation&quot;&gt;Continuous Backpropagation&lt;/h3&gt;

&lt;p&gt;Neural ODE 모델의 Backpropagation은 Euler method를 이용한 function approximation의 과정이다. 이 과정에서 gradient를 편리하게 계산할 수 있도록 Adjoint sensitivity method를 사용한다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/NeuralODE/IMG_0064.jpeg&quot; alt=&quot;IMG_0064.jpeg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;위 그림과 같이 hidden state가 $\mathbf{z}(t_0)$에서 $\mathbf{z}(t_N)$로 mapping되는 과정을 보자. ODE solver을 통해 최종적으로 도출해야 하는 것은 dynamic parameter $\theta$에 대한 Loss의 gradient인 $\frac{\partial{L}}{\partial{\theta}}$와 $\mathbf{z}(t_0)$보다 이전 계층으로의 gradient 전파를 위한 $\frac{\partial{L}}{\partial{\mathbf{z}(t_0)}}$이다.&lt;/p&gt;

&lt;p&gt;Loss는 scalar function이고, $f$는 neural network로 parameterize 되어 있다는 것에 주의하라.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;(1). $L(\mathbf{z}(t_N))=L(\mathbf{z}(t_0)+\int_{t_0}^{t_N}f(\mathbf{z}(t),t,\theta)dt)$&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Derivation of (1)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/NeuralODE/IMG_0055.jpeg&quot; alt=&quot;IMG_0055.jpeg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;이제 $\frac{\partial{L}}{\partial{\mathbf{z}(t_0)}}$와 $\frac{\partial{L}}{\partial{\theta}}$를 구해야 하는데, 이를 단번에 유도하기는 복잡하므로, adjoint $\mathbf{a}(t)=\frac{dL}{d\mathbf{z}(t)}$를 정의한다. adjoint $\mathbf{a}$는 어떤 시점 $t$에서의 상태 $\mathbf{z}$에 대한 Loss이다. adjoint는 다음과 같은 ODE를 통해 표현한다.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;(2). $\frac{d\mathbf{a}(t)}{dt}=-\mathbf{a}^T\frac{\partial{f(\mathbf{z}(t), t, \theta)}}{\partial{\mathbf{z}(t)}}$&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Derivation of (2)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/NeuralODE/IMG_0056.jpeg&quot; alt=&quot;IMG_0056.jpeg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/NeuralODE/IMG_0057.jpeg&quot; alt=&quot;IMG_0057.jpeg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;위의 adjoint 식 (2)를 Euler method에 적용시키면, $\frac{\partial{L}}{\partial{\mathbf{z}(t_0)}}$를 구할 수 있다.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;(3). $\frac{\partial{L}}{\partial{\mathbf{z}(t_0)}}=\frac{\partial{L}}{\partial{\mathbf{z}(t_N)}}-\int^{t_0}_{t_N}\mathbf{a}(t)^T\frac{\partial{f(\mathbf{z}(t), t, \theta)}}{\partial{\mathbf{z}}(t)}dt$&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Derivation of (3)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/NeuralODE/IMG_0065.jpeg&quot; alt=&quot;IMG_0065.jpeg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;마지막으로, 결국 optimize 해야 하는 것은 model parameter $\theta$이므로, $\frac{dL}{d\theta}$를 구한다.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;(4). $\frac{dL}{d\theta}=-\int_{t_N}^{t_0}\mathbf{a}(t)^T\frac{\partial{f(\mathbf{z}(t), t,\theta)}}{\partial{\theta}}dt$&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Derivation of (4)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/NeuralODE/IMG_0062.jpeg&quot; alt=&quot;IMG_0062.jpeg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;이로써 완전한 $f$의 update와 Loss backpropagation을 위한 모든 gradient를 얻을 수 있게 되었다.&lt;/p&gt;

&lt;h3 id=&quot;benefits-of-neural-ode&quot;&gt;Benefits of Neural ODE&lt;/h3&gt;

&lt;h4 id=&quot;continuous-time-series-model을-만들-수-있다&quot;&gt;Continuous time-series model을 만들 수 있다.&lt;/h4&gt;

&lt;p&gt;일정한 시간 간격을 가진 obeservation이 필요했던 기존의 모델과는 달리, arbitrary-time observation을 사용할 수 있게 되면서 데이터 손실을 최소화 할 수 있고, latent를 더 잘 모델링할 수 있게 된다.&lt;/p&gt;

&lt;h4 id=&quot;계산의-정확도를-쉽게-조절할-수-있다&quot;&gt;계산의 정확도를 쉽게 조절할 수 있다&lt;/h4&gt;

&lt;p&gt;오랜 기간 연구되어온 ODE solver인 Euler method를 사용하기 때문에 오차를 필요한 수준으로 감소시키는 작업이나, 계산을 가속화하는 것이 용이하다.&lt;/p&gt;

&lt;h4 id=&quot;continuous-normalizing-flow-모델-구현&quot;&gt;Continuous Normalizing Flow 모델 구현&lt;/h4&gt;

&lt;p&gt;Neural ODE를 이용한 Normalizing Flow 모델은 continuous해지며, 계산이 간편해진다. NF 모델에 관한 내용은 다음을 참고하라.&lt;/p&gt;

&lt;h4 id=&quot;메모리-효율성&quot;&gt;메모리 효율성&lt;/h4&gt;

&lt;p&gt;scalar Loss를 사용하는 Neural ODE 모델은 중간 과정의 값을 저장할 필요가 없기 때문에, 모델의 깊이에 관계없이 일정한 메모리를 차지한다.&lt;/p&gt;

&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;Neural ODE는 기존의 이산적인 모델 구조에서 벗어나 연속적인 계층을 가진 Neural Network를 구현한 점에서, 해당 분야의 길을 열었다고 평가받고 있다.&lt;/p&gt;

&lt;p&gt;실제로 Neural ODE의 등장 이후로 Continuous Normalizing Flow 등의 생성 모델 연구가 이루어지고 있으며, Neural ODE로 모든 함수의 근사가 불가능하다는 것이 밝혀지며 Augmented Neural ODE 모델이 등장하기도 하였다.&lt;/p&gt;

&lt;p&gt;연속적인 시간에 대한 sampling과 학습이 가능하고, 따라서 continuous function에 대한 latent가 더 높은 정확도로 생성되므로, 시간의 연속적인 정보가 데이터에서 중요한 기상, 경제 지표, 의료 등의 데이터 분야에서 큰 활약을 할 수 있을 것으로 보인다.&lt;/p&gt;

&lt;h2 id=&quot;related-works&quot;&gt;Related Works&lt;/h2&gt;

&lt;h4 id=&quot;augmented-neural-ode&quot;&gt;Augmented Neural ODE&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1904.01681&quot;&gt;Augmented Neural ODEs&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;normalizing-flow&quot;&gt;Normalizing Flow&lt;/h4&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1505.05770&quot;&gt;Variational Inference with Normalizing Flows&lt;/a&gt;&lt;/p&gt;</content><author><name>Gilmo Yang</name><email>epsilon2718@snu.ac.kr</email></author><category term="modeling" /><category term="Modeling" /><category term="modeling" /><summary type="html"></summary></entry><entry><title type="html">이미지와 텍스트의 표현 공간을 통합한 CLIP 논문 리뷰</title><link href="https://agency301.github.io//multimodal/2023-08-04-CLIP/" rel="alternate" type="text/html" title="이미지와 텍스트의 표현 공간을 통합한 CLIP 논문 리뷰" /><published>2023-08-04T17:13:45+00:00</published><updated>2023-08-04T17:13:45+00:00</updated><id>https://agency301.github.io//multimodal/CLIP</id><content type="html" xml:base="https://agency301.github.io//multimodal/2023-08-04-CLIP/">&lt;ol id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#paper-review&quot; id=&quot;markdown-toc-paper-review&quot;&gt;Paper Review&lt;/a&gt;    &lt;ol&gt;
      &lt;li&gt;&lt;a href=&quot;#introduction&quot; id=&quot;markdown-toc-introduction&quot;&gt;Introduction&lt;/a&gt;        &lt;ol&gt;
          &lt;li&gt;&lt;a href=&quot;#기존-연구의-한계&quot; id=&quot;markdown-toc-기존-연구의-한계&quot;&gt;기존 연구의 한계&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#why-clip&quot; id=&quot;markdown-toc-why-clip&quot;&gt;Why CLIP?&lt;/a&gt;&lt;/li&gt;
        &lt;/ol&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#clip의-이미지-및-텍스트-인코더&quot; id=&quot;markdown-toc-clip의-이미지-및-텍스트-인코더&quot;&gt;CLIP의 이미지 및 텍스트 인코더&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#clip의-학습-방법-contrastive-learning&quot; id=&quot;markdown-toc-clip의-학습-방법-contrastive-learning&quot;&gt;CLIP의 학습 방법; “Contrastive Learning”&lt;/a&gt;        &lt;ol&gt;
          &lt;li&gt;&lt;a href=&quot;#multi-class-n-pair-loss&quot; id=&quot;markdown-toc-multi-class-n-pair-loss&quot;&gt;Multi-class N-pair Loss&lt;/a&gt;&lt;/li&gt;
        &lt;/ol&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#code-review&quot; id=&quot;markdown-toc-code-review&quot;&gt;Code Review&lt;/a&gt;        &lt;ol&gt;
          &lt;li&gt;&lt;a href=&quot;#clip의-main-code&quot; id=&quot;markdown-toc-clip의-main-code&quot;&gt;CLIP의 Main Code&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#encoders&quot; id=&quot;markdown-toc-encoders&quot;&gt;Encoders&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#clip의-training-code&quot; id=&quot;markdown-toc-clip의-training-code&quot;&gt;CLIP의 Training Code&lt;/a&gt;&lt;/li&gt;
        &lt;/ol&gt;
      &lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;paper-review&quot;&gt;Paper Review&lt;/h2&gt;

&lt;p&gt;Paper Link:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/2103.00020&quot;&gt;Learning Transferable Visual Models From Natural Language Supervision&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;CLIP의 주요 포인트 몇가지를 위주로 리뷰해보려고 합니다.&lt;/p&gt;

&lt;p&gt;먼저 CLIP이 어떤 연구인지에 대하여 대략적으로 살펴본 후, 어떤 Encoder들을 사용했는지, 어떤 Loss를 사용했는지를 소개하겠습니다. 마무리로 간단하게 CLIP Official Repository에 있는 소스코드를 살펴보겠습니다.&lt;/p&gt;

&lt;h3 id=&quot;introduction&quot;&gt;Introduction&lt;/h3&gt;

&lt;p&gt;CLIP은 이미지와 텍스트를 같은 표현 공간(representation space)에 임베드(embed)하는 학습 기법입니다.&lt;/p&gt;

&lt;h4 id=&quot;기존-연구의-한계&quot;&gt;기존 연구의 한계&lt;/h4&gt;

&lt;p&gt;CLIP이 나오기 이전의 연구에서는 정해진 단어 집합(vocabulary)에 속한 단어들에 대하여 이미지들과의 관련도를 예측할 수 있도록 학습시켰습니다.&lt;/p&gt;

&lt;p&gt;이를 위해서는 학습하고자 하는 모든 단어들에 대하여 라벨링이 된 데이터셋(labeled dataset)을 마련해야 한다는 문제점이 있었습니다.&lt;/p&gt;

&lt;p&gt;CLIP 연구진들은 이로 인해 학습 가능한 데이터셋의 규모(scale)가 제한되며, 성능(performance)에도 제한이 있으리라고 보았습니다.&lt;/p&gt;

&lt;p&gt;또한, 각 단어들을 단어 집합 내에서의 순서에 따라 0, 1, 2, … 과 같은 숫자로만 치환해놓고 학습하는 경우에는, 해당 단어의 의미와 관련된 지식을 이용하기에 어렵다는 문제점이 발생했습니다.&lt;/p&gt;

&lt;h4 id=&quot;why-clip&quot;&gt;Why CLIP?&lt;/h4&gt;

&lt;p&gt;CLIP 연구진은 학습 가능한 데이터셋의 크기를 효과적으로 늘리기 위해서, 이미지와 그에 대응하는 캡션(caption) 또는 제목이 이미 매칭되어 있는 상태의 raw data를 수집했습니다. 예를 들어 인스타그램에서 이미지와 그에 대한 설명 또는 파일제목을 얻어 이를 데이터셋으로 사용했습니다.&lt;/p&gt;

&lt;p&gt;이미지 캡션, 제목과 같은 텍스트를 인코딩(encode)할 때는 BPE Tokenizer를 사용했으며, Transformer를 이용하여 임베딩을 구했습니다. 이 덕분에 규모가 클 뿐더러 사전에 포함되지 않는 단어가 등장하기도 하는 raw data를 학습하기에 용이했을 것입니다.&lt;/p&gt;

&lt;p&gt;CLIP은 기존보다 훨씬 큰 규모(scale)의 데이터셋(dataset)으로 학습할 수 있었으며, 따라서 학습에 사용한 데이터셋 (pretrained dataset)에 포함되지 않은 텍스트에 대해서도 더 잘 일반화할 수 있었다는 점에서 획기적인 연구였습니다.&lt;/p&gt;

&lt;h3 id=&quot;clip의-이미지-및-텍스트-인코더&quot;&gt;CLIP의 이미지 및 텍스트 인코더&lt;/h3&gt;

&lt;p&gt;CLIP에서는 텍스트 인코더와 이미지 인코더를 따로 두되, 두 인코더가 같은 표현 공간 (representation space)에서 임베딩(embedding)을 만들어내도록 학습했습니다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/CLIP/Untitled.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;p&gt;텍스트 인코더로는 masked attention을 포함한 Transformer를 사용했습니다. 앞서 언급했듯이 토크나이징(tokenizing)에는 BPE를 사용했습니다.&lt;/p&gt;

&lt;p&gt;이미지 인코더로는 두 가지 옵션을 두었습니다. 수정된 ResNet (modified ResNet)과 Vision-Transformer (ViT)입니다.&lt;/p&gt;

&lt;h3 id=&quot;clip의-학습-방법-contrastive-learning&quot;&gt;CLIP의 학습 방법; “Contrastive Learning”&lt;/h3&gt;

&lt;p&gt;Contrastive Learning은 유사한 두 오브젝트의 쌍(pair)들로 이루어진 데이터셋(paired datset)을 필요로 합니다. 유사도(similarity)를 계산하기 위한 metric을 정해 두고, 쌍을 이루는 유사한 두 오브젝트 간의 유사도가 최대가 되도록 학습(maximize)합니다.&lt;/p&gt;

&lt;p&gt;CLIP은 이러한 Contastive Loss를 사용하는 Contrastive Learning입니다. CLIP에서 사용한 Loss는 그 중에서도 ‘Multi-class N-pair Loss’라고 불립니다.&lt;/p&gt;

&lt;h4 id=&quot;multi-class-n-pair-loss&quot;&gt;Multi-class N-pair Loss&lt;/h4&gt;

&lt;p&gt;CLIP에서는 각 Batch마다 서로 유사한 텍스트-이미지 쌍을 N쌍씩 학습시킵니다. 즉, 텍스트-이미지 쌍 N개가 나오도록 텍스트 N개와 이미지 N개씩을 준비해야 합니다.&lt;/p&gt;

&lt;p&gt;이러한 텍스트 N개와 이미지 N개로부터 나올 수 있는 모든 조합의 텍스트-이미지 쌍을 만들면 N×N개의 쌍이 생깁니다. 이는 서로 유사한 텍스트-이미지 N쌍과 서로 유사하지 않은 텍스트-이미지 N×(N-1)쌍으로 이루어질 것입니다.&lt;/p&gt;

&lt;p&gt;아래 그림을 보는 것이 이해를 도울 것입니다. 그림에서 파란색으로 표시된 쌍들은 서로 유사한 Positive Pair이며, 표시되지 않은 쌍들은 서로 유사하지 않은 Negative Pair입니다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/CLIP/Untitled%201.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;p&gt;유사도 메트릭 (similarity metric)으로는 코사인 유사도 (cosine similarity)를 사용합니다.&lt;/p&gt;

&lt;p&gt;N개의 Positive Pair에 대해서는 코사인 유사도를 Maximize하고 N×(N-1)개의 Negative Pair에 대해서는 코사인 유사도를 Minimize하도록 학습합니다.&lt;/p&gt;

&lt;p&gt;구체적으로, Symmetric Cross Entropy Loss를 사용합니다. 위 그림 상에서 Row별로, Column별로 각각 Binary Cross Entropy Loss를 구한 후, 두 Loss 값의 산술평균한 것을 Final Loss로 삼아 학습시킵니다.&lt;/p&gt;

&lt;p&gt;논문에 첨부된 아래 Pseudo Code를 참고해서 이해할 수 있습니다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/CLIP/Untitled%202.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;code-review&quot;&gt;Code Review&lt;/h3&gt;

&lt;p&gt;Official Code Link:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/openai/CLIP&quot;&gt;GitHub - openai/CLIP: CLIP (Contrastive Language-Image Pretraining),  Predict the most relevant text snippet given an image&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;마지막으로 위 Repository의 clip/model.py 소스코드를 리뷰해보겠습니다.&lt;/p&gt;

&lt;h4 id=&quot;clip의-main-code&quot;&gt;CLIP의 Main Code&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CLIP&lt;/code&gt; 클래스&lt;/p&gt;

    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forward&lt;/code&gt; 메서드을 보면, 아래 코드와 같이, 먼저 이미지와 텍스트를 각자의 인코더를 이용해 임베딩합니다.&lt;/p&gt;

    &lt;p&gt;이 임베딩에 Normalization을 거친 후, 이미지와 텍스트를 각각 기준으로 잡고 코사인 유사도를 구합니다.&lt;/p&gt;

    &lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
  	&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;image_features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode_image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;
  	&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text_features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;

  	&lt;span class=&quot;c1&quot;&gt;# normalized features
&lt;/span&gt;  	&lt;span class=&quot;n&quot;&gt;image_features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;image_features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;image_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;norm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keepdim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  	&lt;span class=&quot;n&quot;&gt;text_features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text_features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;norm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keepdim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

  	&lt;span class=&quot;c1&quot;&gt;# cosine similarity as logits
&lt;/span&gt;  	&lt;span class=&quot;n&quot;&gt;logit_scale&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logit_scale&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  	&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logits_per_image&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logit_scale&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;image_features&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text_features&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
  	&lt;span class=&quot;n&quot;&gt;logits_per_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logits_per_image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;

  	&lt;span class=&quot;c1&quot;&gt;# shape = [global_batch_size, global_batch_size]
&lt;/span&gt;  	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logits_per_image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logits_per_text&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;

    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;encode_image&lt;/code&gt;와 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;encode_text&lt;/code&gt;는 각각 사용할 인코더를 호출하는 메서드입니다.&lt;/p&gt;

    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;encode_image&lt;/code&gt;는 아래 코드와 같이, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ModifiedResNet&lt;/code&gt;과 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VisionTransformer&lt;/code&gt; 중 하나로 지정된 Visual Encoder를 호출합니다.&lt;/p&gt;

    &lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;isinstance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vision_layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;tuple&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)):&lt;/span&gt;
  	&lt;span class=&quot;n&quot;&gt;vision_heads&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vision_width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;
  	&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;visual&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ModifiedResNet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  	    &lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vision_layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  	    &lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embed_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  	    &lt;span class=&quot;n&quot;&gt;heads&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vision_heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  	    &lt;span class=&quot;n&quot;&gt;input_resolution&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;image_resolution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  	    &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vision_width&lt;/span&gt;
  	&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
  	&lt;span class=&quot;n&quot;&gt;vision_heads&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;vision_width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;
  	&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;visual&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;VisionTransformer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  	    &lt;span class=&quot;n&quot;&gt;input_resolution&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;image_resolution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  	    &lt;span class=&quot;n&quot;&gt;patch_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vision_patch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  	    &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vision_width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  	    &lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vision_layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  	    &lt;span class=&quot;n&quot;&gt;heads&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vision_heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  	    &lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embed_dim&lt;/span&gt;
  	&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;

    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;encode_text&lt;/code&gt;는 아래 코드와 같이, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;token_embedding&lt;/code&gt;과 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;positional_embedding&lt;/code&gt;을 더하고 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Transformer&lt;/code&gt;에 넣은 후 Laynorm과 Linear Projection을 거칩니다.&lt;/p&gt;

    &lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;encode_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;token_embedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# [batch_size, n_ctx, d_model]
&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;positional_embedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;permute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# NLD -&amp;gt; LND
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transformer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;permute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# LND -&amp;gt; NLD
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ln_final&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;# x.shape = [batch_size, n_ctx, transformer.width]
&lt;/span&gt;    &lt;span class=&quot;c1&quot;&gt;# take features from the eot embedding (eot_token is the highest number in each sequence)
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argmax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text_projection&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;encoders&quot;&gt;Encoders&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ModifiedResNet&lt;/code&gt; 클래스&lt;/p&gt;

    &lt;p&gt;기존 ResNet과 다른 점이 3가지 있습니다. 기존 ResNet 구현은 아래 이미지를 참고하면 됩니다.&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/CLIP/Untitled%203.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

    &lt;ol&gt;
      &lt;li&gt;
        &lt;p&gt;Residual Layer들 이전 &lt;strong&gt;stem layer&lt;/strong&gt;가 다릅니다.&lt;/p&gt;

        &lt;p&gt;아래 코드의 모듈들을 순서대로 적용합니다.&lt;/p&gt;

        &lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;c1&quot;&gt;# __init__ 내부
&lt;/span&gt;
 &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Conv2d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kernel_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stride&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
 &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bn1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BatchNorm2d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
 &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;relu1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ReLU&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inplace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
 &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Conv2d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kernel_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
 &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bn2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BatchNorm2d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
 &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;relu2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ReLU&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inplace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
 &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Conv2d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kernel_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;padding&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
 &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bn3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BatchNorm2d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
 &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;relu3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ReLU&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inplace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
 &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;avgpool&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AvgPool2d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

 &lt;span class=&quot;c1&quot;&gt;# ------------
&lt;/span&gt;
 &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
 	  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;stem&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
 	      &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;relu1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bn1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
 	      &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;relu2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bn2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
 	      &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;relu3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bn3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;
 	      &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;avgpool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
 	      &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;        &lt;/div&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;strong&gt;Average Pooling&lt;/strong&gt;을 사용합니다.&lt;/p&gt;

        &lt;p&gt;기존 ResNet과 달리, stem layer의 마지막과 각 residual layer의 마지막에서 Average Pooling을 사용합니다. (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BottleNeck&lt;/code&gt; 클래스에서 확인 가능)&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;strong&gt;Attention Pooling&lt;/strong&gt;을 사용합니다.&lt;/p&gt;

        &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ModifiedResNet&lt;/code&gt;의 마지막 layer는 Attention Pooling입니다. 그 구현은 아래 코드를 참고할 수 있습니다. 이는 multi-head attention을 포함하고 있습니다.&lt;/p&gt;

        &lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AttentionPool2d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spacial_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
         &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;positional_embedding&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spacial_dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed_dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k_proj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embed_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q_proj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embed_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v_proj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embed_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_proj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;embed_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;embed_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
         &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_heads&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num_heads&lt;/span&gt;

     &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
         &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flatten&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start_dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;permute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# NCHW -&amp;gt; (HW)NC
&lt;/span&gt;         &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;keepdim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# (HW+1)NC
&lt;/span&gt;         &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;positional_embedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# (HW+1)NC
&lt;/span&gt;         &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multi_head_attention_forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;embed_dim_to_check&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;num_heads&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;q_proj_weight&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q_proj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;k_proj_weight&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k_proj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;v_proj_weight&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v_proj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;in_proj_weight&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;in_proj_bias&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;q_proj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k_proj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v_proj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;bias_k&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;bias_v&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;add_zero_attn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;dropout_p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;out_proj_weight&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_proj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weight&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;out_proj_bias&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c_proj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;use_separate_proj_weight&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;training&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;training&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;n&quot;&gt;need_weights&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;
         &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
         &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;squeeze&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;        &lt;/div&gt;
      &lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Transformer&lt;/code&gt; 클래스&lt;/p&gt;

    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Transformer&lt;/code&gt;는 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ResidualAttentionBlock&lt;/code&gt; 여러 층을 쌓은 것입니다.&lt;/p&gt;

    &lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Transformer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attn_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
          &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
          &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;
          &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;
          &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;resblocks&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sequential&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ResidualAttentionBlock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attn_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt;

      &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;resblocks&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;

    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ResidualAttentionBlock&lt;/code&gt;은 아래 코드와 같이 먼저&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LayerNorm → (Masked) Multi-Head Attention&lt;/code&gt;을 하면서 Residual Connection을 한 번 넣은 후, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LayerNorm → MLP&lt;/code&gt;를 하면서 Residual Connection을 한 번 넣는 연산입니다.&lt;/p&gt;

    &lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ResidualAttentionBlock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n_head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attn_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
          &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

          &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MultiheadAttention&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n_head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ln_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LayerNorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mlp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sequential&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OrderedDict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;
              &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;c_fc&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
              &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;gelu&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;QuickGELU&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()),&lt;/span&gt;
              &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;c_proj&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Linear&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
          &lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
          &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ln_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LayerNorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;d_model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attn_mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attn_mask&lt;/span&gt;

      &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;attention&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
          &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attn_mask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attn_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attn_mask&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;need_weights&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attn_mask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attn_mask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;

      &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attention&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ln_1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mlp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ln_2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
          &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VisionTransformer&lt;/code&gt; 클래스&lt;/p&gt;

    &lt;p&gt;Convolution으로 Image Feature를 얻고, positional embedding도 더하며, 이후 위에서 소개한 Transformer로 처리합니다.&lt;/p&gt;

    &lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;VisionTransformer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_resolution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;patch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
          &lt;span class=&quot;nb&quot;&gt;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
          &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_resolution&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input_resolution&lt;/span&gt;
          &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt;
          &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Conv2d&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in_channels&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;out_channels&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;kernel_size&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;patch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stride&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;patch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

          &lt;span class=&quot;n&quot;&gt;scale&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;
          &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;class_embedding&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scale&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
          &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;positional_embedding&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scale&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;input_resolution&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;//&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;patch_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
          &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ln_pre&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LayerNorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

          &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transformer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Transformer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;layers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;heads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

          &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ln_post&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LayerNorm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Parameter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scale&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;width&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output_dim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

      &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tensor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conv1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# shape = [*, width, grid, grid]
&lt;/span&gt;          &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# shape = [*, width, grid ** 2]
&lt;/span&gt;          &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;permute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# shape = [*, grid ** 2, width]
&lt;/span&gt;          &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;class_embedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zeros&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# shape = [*, grid ** 2 + 1, width]
&lt;/span&gt;          &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;positional_embedding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dtype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ln_pre&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

          &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;permute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# NLD -&amp;gt; LND
&lt;/span&gt;          &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transformer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;permute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;# LND -&amp;gt; NLD
&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ln_post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:])&lt;/span&gt;

          &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proj&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;None&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
              &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;@&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proj&lt;/span&gt;

          &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;clip의-training-code&quot;&gt;CLIP의 Training Code&lt;/h4&gt;

&lt;p&gt;CLIP의 학습에 관해서는 위에 링크를 걸어놓은 Official Repo에서 공개해놓은 코드가 따로 없어서 아래 링크에 누군가 올려놓은 코드를 잠시 살펴보겠습니다.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/openai/CLIP/issues/83&quot;&gt;CLIP Training Code · Issue #83 · openai/CLIP&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;아래 코드에서는 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CLIP&lt;/code&gt;에서 얻은 텍스트, 이미지 사이의 코사인 유사도에 대하여 Ground Truth와의 Cross-Entropy Loss를 계산했습니다. 그렇게 구한 두 가지 Loss 값을 평균낸 것을 최종 Loss로 사용하여 학습합니다.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;loss_img&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CrossEntropyLoss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;loss_txt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CrossEntropyLoss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;optim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Adam&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parameters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;5e-5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;betas&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.98&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eps&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1e-6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weight_decay&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#Params from paper
&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;train_dataloader&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zero_grad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;list_image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list_txt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;batch&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#list_images is list of image in numpy array(np.uint8)
&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;images&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;preprocess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fromarray&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;img&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;img&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;list_image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dim&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;texts&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tokenize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;list_txt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;logits_per_image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;logits_per_text&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;images&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;texts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;ground_truth&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;torch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BATCH_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;device&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;total_loss&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss_img&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logits_per_image&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ground_truth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loss_txt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logits_per_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ground_truth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;total_loss&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;backward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;convert_models_to_fp32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;optimizer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;clip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;convert_weights&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Repo에 CLIP 사용 예제 cookbook도 포함되어 있던데, CLIP을 어떻게 이용할 수 있을지 궁금하다면 참고하면 되겠습니다 (Interesting 하더라구요 ~~)&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/openai/CLIP/blob/main/notebooks/Interacting_with_CLIP.ipynb&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;이상, CLIP Paper Review를 마치겠습니다. 읽어주셔서 감사합니다~!&lt;/p&gt;</content><author><name>Yoonah Park</name><email>wisdomsword21@snu.ac.kr</email></author><category term="multimodal" /><category term="embedding" /><category term="transformer" /><summary type="html"></summary></entry><entry><title type="html">Evolutionary Search를 통해 찾아낸 새로운 Optimizer, Lion.</title><link href="https://agency301.github.io//modeling/2023-08-03-Lion/" rel="alternate" type="text/html" title="Evolutionary Search를 통해 찾아낸 새로운 Optimizer, Lion." /><published>2023-08-03T05:48:43+00:00</published><updated>2023-08-03T05:48:43+00:00</updated><id>https://agency301.github.io//modeling/Lion</id><content type="html" xml:base="https://agency301.github.io//modeling/2023-08-03-Lion/">&lt;ol id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#paper-review&quot; id=&quot;markdown-toc-paper-review&quot;&gt;Paper Review&lt;/a&gt;    &lt;ol&gt;
      &lt;li&gt;&lt;a href=&quot;#introduction&quot; id=&quot;markdown-toc-introduction&quot;&gt;Introduction&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#program-search-space-designing&quot; id=&quot;markdown-toc-program-search-space-designing&quot;&gt;Program search space designing&lt;/a&gt;        &lt;ol&gt;
          &lt;li&gt;&lt;a href=&quot;#train-function&quot; id=&quot;markdown-toc-train-function&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;train&lt;/code&gt; function&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#building-blocks&quot; id=&quot;markdown-toc-building-blocks&quot;&gt;building blocks&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#mutations-and-redundant-statements&quot; id=&quot;markdown-toc-mutations-and-redundant-statements&quot;&gt;mutations and redundant statements&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#infinite-and-sparse-search-space&quot; id=&quot;markdown-toc-infinite-and-sparse-search-space&quot;&gt;infinite and sparse search space&lt;/a&gt;&lt;/li&gt;
        &lt;/ol&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#efficient-search-techniques&quot; id=&quot;markdown-toc-efficient-search-techniques&quot;&gt;Efficient search techniques&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#program-selection-methodology&quot; id=&quot;markdown-toc-program-selection-methodology&quot;&gt;Program selection methodology&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#proposition-lion-evolved-sign-momentum-optimizer&quot; id=&quot;markdown-toc-proposition-lion-evolved-sign-momentum-optimizer&quot;&gt;Proposition: Lion (EvoLved Sign Momentum) Optimizer&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#evaluation-of-lion&quot; id=&quot;markdown-toc-evaluation-of-lion&quot;&gt;Evaluation of Lion&lt;/a&gt;&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#related-works&quot; id=&quot;markdown-toc-related-works&quot;&gt;Related Works&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;paper-review&quot;&gt;Paper Review&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/2302.06675&quot;&gt;Symbolic Discovery of Optimization Algorithms&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;introduction&quot;&gt;Introduction&lt;/h3&gt;

&lt;p&gt;이 논문은 AutoML-Zero 와 상당히 유사한 내용을 가지고 있다. 하단 링크 참고&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/2003.03384&quot;&gt;AutoML-Zero: Evolving Machine Learning Algorithms From Scratch&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;AutoML-Zero는 최대한 사람의 개입 없이 ML Architecture 를 구성하는 것에 대한 논문인데, 미분같은 복잡한 연산은 제외하고 65가지의 연산, 함수(instruction이라 한다.)를 조합하여 program flow를 구성하는 것을 목표로 program space를 search하였다고 한다. 사용된 방법은 evolutionary search로,  genetic algorithm에 기반하여 instruction을 넣거나 빼고, 대체하는 등의 mutation 연산, 그리고 한 time-step(=generation)이 지날 때마다 N개의 top rated program을 parents로 하여 copy + mutation하는 절차를 통해 다양한 program을 만들어냈다고 한다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Lion/Untitled.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;p&gt;본 논문도 마찬가지로 program search를 통해 최적의 optimizer를 찾고자 했다. 본 논문에서는 algorithm을 개발하는 데에 있어 program을 symbolic representation으로 사용하는 것에 대한 3가지 장점을 밝히고 있다.&lt;/p&gt;

&lt;p&gt;(1). 어차피 계산 하려면 프로그램으로 나타내야 한다 (2). 분석하기 편하고 다른 분야로 transfer가 용이하다 (3). 프로그램의 길이를 알고리즘의 복잡도를 추정하는데 사용할 수 있어, 더 간단하고 일반적인 알고리즘을 찾는데 도움이 된다.&lt;/p&gt;

&lt;h3 id=&quot;program-search-space-designing&quot;&gt;Program search space designing&lt;/h3&gt;

&lt;p&gt;해당 논문에서는 program search space를 설계할 때 고려할 점을 3가지 밝힌다.&lt;/p&gt;

&lt;p&gt;(1). 신박한 알고리즘을 얻기 위해 충분히 넓어야 한다&lt;/p&gt;

&lt;p&gt;(2). ML의 workflow에 적합해야 한다&lt;/p&gt;

&lt;p&gt;(3). high-level design에만 집중한다&lt;/p&gt;

&lt;p&gt;다음은 program search를 위한 4가지 구성이다.&lt;/p&gt;

&lt;h4 id=&quot;train-function&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;train&lt;/code&gt; function&lt;/h4&gt;

&lt;p&gt;input으로 weight $w$, gradient $g$, learning rate $lr$을 받아 weight update를 output 하는 main objective이다. 즉, search의 객체이다.&lt;/p&gt;

&lt;p&gt;historical value를 저장하는 extra variable을 둘 수 있다&lt;/p&gt;

&lt;h4 id=&quot;building-blocks&quot;&gt;building blocks&lt;/h4&gt;

&lt;p&gt;train function은 statement와 local variable 개수에 제약을 받지 않는다. 본 논문에서는 45개의 commom math function을 채택하였고, program의 간결성을 위해 linear interpolation function $interp(x, y, a)=(1-a)x+ay$ 등을 정의하였다.&lt;/p&gt;

&lt;p&gt;또한 기존의 연구에서는 반복문, 조건문 등의 complex feature를 도입하였었는데 유의미한 성능 진전이 없었으므로 본 연구에서는 제외하였다고 한다.&lt;/p&gt;

&lt;p&gt;차원에 맞지 않는 데이터 연산은 automatically casted되도록 했다고 한다.(ex. matrix + scalar)&lt;/p&gt;

&lt;h4 id=&quot;mutations-and-redundant-statements&quot;&gt;mutations and redundant statements&lt;/h4&gt;

&lt;p&gt;3가지 mutation operation을 정의한다&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;random function을 random location에 삽입&lt;/li&gt;
  &lt;li&gt;randomly chosen statement 삭제&lt;/li&gt;
  &lt;li&gt;random function의 argument를 수정&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;random variable이나 constant는 normal distribution에서 추출하며, constant들은 차후에 tunable hyperparameter로 기능할 수 있다.&lt;/p&gt;

&lt;h4 id=&quot;infinite-and-sparse-search-space&quot;&gt;infinite and sparse search space&lt;/h4&gt;

&lt;p&gt;internal arguments, state, function의 개수에 제약을 두지 않고, mutation연산의 존재 때문에 program search space가 infinite하다.&lt;/p&gt;

&lt;p&gt;high-performing program은 search space상에 sparse하게 존재하기 때문에, 이를 빠르게 search하기 위해 low-cost proxy task를 설정하여 evaluation하는 것으로 보인다.&lt;/p&gt;

&lt;h3 id=&quot;efficient-search-techniques&quot;&gt;Efficient search techniques&lt;/h3&gt;

&lt;p&gt;high-performing algorithm이 너무 sparse하게 존재하는 문제점 때문에 본 논문에서는 각종 search technique를 도입한다.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Evolution with warm-start and restart&lt;/p&gt;

    &lt;p&gt;warm-start: search acceleration을 위해 AdamW에서 시작&lt;/p&gt;

    &lt;p&gt;restart:&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;exploration: start from initial program&lt;/li&gt;
      &lt;li&gt;exploitation: start from best program&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Pruning through abstract execution&lt;/p&gt;

    &lt;p&gt;별 건 아니고 에러 나는 거 없애주고, program 마다 hash값 만들어서 똑같은 program 삭제하는 것&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Proxy tasks and search cost&lt;/p&gt;

    &lt;p&gt;low-cost proxy task로 search 진행. funnel selection과 연관.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;program-selection-methodology&quot;&gt;Program selection methodology&lt;/h3&gt;

&lt;p&gt;매우 큰 program space를 search하기 때문에 proxy task를 필연적으로 이용하게 되는데, proxy task와 target task의 차이, 그리고 다양한 domain에 적용될 수 있는 generalization 능력을 원하기 때문에, 학습과 활용에 큰 차이가 발생한다고 밝히고 있다. 이에 본 논문에서는 2가지 해결책을 사용한다.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Funnel selection&lt;/p&gt;

    &lt;p&gt;funnel selection은 proxy task를 통해 generalization power를 검증하는 방법이다.&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;proxy task A를 통해 problem set $S_1$을 평가한다 → selection → $S_2$&lt;/li&gt;
      &lt;li&gt;A보다 10배 더 큰 task B로 $S_2$의 평가를 진행한다 → selection → $S_3$&lt;/li&gt;
      &lt;li&gt;$S_3$를 A보다 100배 더 큰 task C로 평가한다 → selection&lt;/li&gt;
    &lt;/ul&gt;

    &lt;p&gt;이러한 과정을 통해 generalization performance가 떨어지는 program을 낙오시킨다.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Simplification&lt;/p&gt;

    &lt;p&gt;더 간단할 수록 generalization 능력이 높을 것이라는 직관에 의해, input과 output이 identical하거나, 없어져도 거의 영향을 미치지 않는 statement, function을 제거한다.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;proposition-lion-evolved-sign-momentum-optimizer&quot;&gt;Proposition: Lion (EvoLved Sign Momentum) Optimizer&lt;/h3&gt;

&lt;p&gt;&lt;del&gt;너무 억지 작명인 것 같다&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;해당 논문에서 찾아낸 Lion Opimizer이다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Lion/Untitled%201.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Sign update and regularization&lt;/p&gt;

    &lt;p&gt;sign 함수를 이용하기 때문에, 모든 dimension에서 uniform magnitude를 가진다고 한다. 이 특성이 noise를 일으켜 regularization 처럼 기능한다고 설명한다.&lt;/p&gt;

    &lt;p&gt;cf. sign 함수가 noise를 일으키는 이유? → Lion은 RMSprop이나 Adam처럼 EMA를 사용하는데(linear interpolation 부분), 여기에 sign을 취해버려서 magnitude는 반영을 안하고 tendency만 반영하게 되어서 update가 non-smooth하게 되어서 noisy하다는 것 같음&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Momentum tracking&lt;/p&gt;

    &lt;p&gt;$\beta_1, \beta_2$를 이용하는데, 2개를 써야하는 이유는 appendix의 ablation study에서 증명했다.(사실 evolutionary search로 찾아낸 것이기 때문에 사후 정당화에 가깝다)&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Hyperparameter and batch size choices&lt;/p&gt;

    &lt;p&gt;Lion Optimizer의 성능은 Batch size가 커질 수록 비례하여 증가한다.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Memory and runtime benefits&lt;/p&gt;

    &lt;p&gt;task에 따라 AdamW 보다 2-15% 가량의 속도 향상을 보였다.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;evaluation-of-lion&quot;&gt;Evaluation of Lion&lt;/h3&gt;

&lt;p&gt;ViT, LiT, Autoregressive Model 등 다양한 task에 적용했는데 AdamW보다 항상 낫거나 비슷했다고 한다. benchmark table이 너무 많으니까 논문 참조.&lt;/p&gt;

&lt;h2 id=&quot;related-works&quot;&gt;Related Works&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;https://arxiv.org/abs/1908.00709&quot;&gt;AutoML&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</content><author><name>Gilmo Yang</name><email>epsilon2718@snu.ac.kr</email></author><category term="modeling" /><category term="modeling" /><summary type="html"></summary></entry><entry><title type="html">Denoising Diffusion Probabilistic Models(DDPM)</title><link href="https://agency301.github.io//cv/2023-08-01-DDPM/" rel="alternate" type="text/html" title="Denoising Diffusion Probabilistic Models(DDPM)" /><published>2023-08-01T01:19:40+00:00</published><updated>2023-08-01T01:19:40+00:00</updated><id>https://agency301.github.io//cv/DDPM</id><content type="html" xml:base="https://agency301.github.io//cv/2023-08-01-DDPM/">&lt;ol id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#denoising-diffusion-probabilistic-modelsddpm&quot; id=&quot;markdown-toc-denoising-diffusion-probabilistic-modelsddpm&quot;&gt;Denoising Diffusion Probabilistic Models(DDPM)&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#paper-review&quot; id=&quot;markdown-toc-paper-review&quot;&gt;Paper Review&lt;/a&gt;    &lt;ol&gt;
      &lt;li&gt;&lt;a href=&quot;#denoising-diffusion-probabilistic-models-ddpm&quot; id=&quot;markdown-toc-denoising-diffusion-probabilistic-models-ddpm&quot;&gt;Denoising Diffusion Probabilistic Models (DDPM)&lt;/a&gt;        &lt;ol&gt;
          &lt;li&gt;&lt;a href=&quot;#denoising-diffusion-probabilistic-models&quot; id=&quot;markdown-toc-denoising-diffusion-probabilistic-models&quot;&gt;Denoising Diffusion Probabilistic Models&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#diffusion-model-introduction&quot; id=&quot;markdown-toc-diffusion-model-introduction&quot;&gt;Diffusion model introduction&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#idea-of-reverse-process--forward-process&quot; id=&quot;markdown-toc-idea-of-reverse-process--forward-process&quot;&gt;Idea of reverse process &amp;amp; forward process&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#loss-derivation-appendix-a&quot; id=&quot;markdown-toc-loss-derivation-appendix-a&quot;&gt;Loss derivation (Appendix A)&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#approximation-of-qx_tvert-x_0&quot; id=&quot;markdown-toc-approximation-of-qx_tvert-x_0&quot;&gt;Approximation of $q(x_t\vert x_0)$&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#parameterization-of-p_thetax_t-1vert-x_t&quot; id=&quot;markdown-toc-parameterization-of-p_thetax_t-1vert-x_t&quot;&gt;Parameterization of $p_{\theta}(x_{t-1}\vert x_t)$&lt;/a&gt;&lt;/li&gt;
        &lt;/ol&gt;
      &lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#other-source&quot; id=&quot;markdown-toc-other-source&quot;&gt;Other source&lt;/a&gt;        &lt;ol&gt;
          &lt;li&gt;&lt;a href=&quot;#helo&quot; id=&quot;markdown-toc-helo&quot;&gt;Helo&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#diffusion-model-입문하기&quot; id=&quot;markdown-toc-diffusion-model-입문하기&quot;&gt;Diffusion Model 입문하기&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#denoising-diffusion-probabilistic-models-논문-리뷰-ddpm-설명-ddpm-증명&quot; id=&quot;markdown-toc-denoising-diffusion-probabilistic-models-논문-리뷰-ddpm-설명-ddpm-증명&quot;&gt;Denoising Diffusion Probabilistic Models 논문 리뷰 (DDPM 설명, DDPM 증명)&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#concept-diffusion-models--with-ddpm-&quot; id=&quot;markdown-toc-concept-diffusion-models--with-ddpm-&quot;&gt;Concept. Diffusion Models ( with. DDPM )&lt;/a&gt;&lt;/li&gt;
        &lt;/ol&gt;
      &lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&quot;denoising-diffusion-probabilistic-modelsddpm&quot;&gt;Denoising Diffusion Probabilistic Models(DDPM)&lt;/h1&gt;

&lt;h1 id=&quot;paper-review&quot;&gt;Paper Review&lt;/h1&gt;

&lt;h2 id=&quot;denoising-diffusion-probabilistic-models-ddpm&quot;&gt;Denoising Diffusion Probabilistic Models (DDPM)&lt;/h2&gt;

&lt;h3 id=&quot;denoising-diffusion-probabilistic-models&quot;&gt;&lt;a href=&quot;https://arxiv.org/abs/2006.11239&quot;&gt;Denoising Diffusion Probabilistic Models&lt;/a&gt;&lt;/h3&gt;

&lt;h3 id=&quot;diffusion-model-introduction&quot;&gt;Diffusion model introduction&lt;/h3&gt;

&lt;p&gt;Diffusion model은 물질의 확산에 기반한 diffusion process의 inverse process를 학습하는 과정을 통해 데이터를 생성하는 generative model이다.&lt;/p&gt;

&lt;p&gt;Diffusion model의 process는 크게 2개로 나뉘는데, 첫 번째는 diffusion mechanism을 통해 각 time step마다 데이터에 infinitesimal한 noise를 가해 결국에는 데이터와 independent한 noise를 만드는 과정이고, 두 번째는 diffusion의 reverse process를 모델링하여 random noise로부터 noise를 제거하여 원하는 데이터를 생성해내는 과정이다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/DDPM/Untitled.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;p&gt;그림에서 $q(\bold{x}&lt;em&gt;t\vert \bold{x}&lt;/em&gt;{t-1})$은 원본 데이터인 $\bold{x}&lt;em&gt;0$에 noise를 가하는 diffusion process이며, $p&lt;/em&gt;\theta(\bold{x}_{t-1}\vert \bold{x}_t)$는 forward diffusion process의 reverse process로써 학습의 대상이다.&lt;/p&gt;

&lt;p&gt;각각의 process는 finite time 안에 이루어지는 discrete한 과정이며, parameterized Markov chain이라고 할 수 있다.&lt;/p&gt;

&lt;h3 id=&quot;idea-of-reverse-process--forward-process&quot;&gt;Idea of reverse process &amp;amp; forward process&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/DDPM/Screenshot_20230711-171451_Samsung_Notes.jpg&quot; alt=&quot;Screenshot_20230711-171451_Samsung Notes.jpg&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;loss-derivation-appendix-a&quot;&gt;Loss derivation (Appendix A)&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/DDPM/Untitled%201.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/DDPM/Untitled%202.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/DDPM/Untitled%203.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;approximation-of-qx_tvert-x_0&quot;&gt;Approximation of $q(x_t\vert x_0)$&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/DDPM/Screenshot_20230710-204950_Samsung_Notes.jpg&quot; alt=&quot;Screenshot_20230710-204950_Samsung Notes.jpg&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;proof&lt;/p&gt;

    &lt;p&gt;&lt;a href=&quot;https://lilianweng.github.io/posts/2021-07-11-diffusion-models/&quot;&gt;What are Diffusion Models?&lt;/a&gt;&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/DDPM/Untitled%204.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;parameterization-of-p_thetax_t-1vert-x_t&quot;&gt;Parameterization of $p_{\theta}(x_{t-1}\vert x_t)$&lt;/h3&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/DDPM/Untitled%205.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;$\sum_{\theta}(x_t, t)$&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/DDPM/Untitled%206.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;p&gt;when&lt;/p&gt;

        &lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/DDPM/Untitled%207.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

        &lt;p&gt;or (similar results)&lt;/p&gt;

        &lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/DDPM/Untitled%208.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;$\mu_{\theta}(x_t, t)$&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/DDPM/Untitled%209.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;when parameterized&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/DDPM/Untitled%2010.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;then&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/DDPM/Untitled%2011.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;thus, $\mu_{\theta}(x_t, t)$ is parameterized to&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/DDPM/Untitled%2012.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;using this, $L_{t-1}-C$ can be simplified to&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/DDPM/Untitled%2013.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;p&gt;메모&lt;/p&gt;

        &lt;p&gt;$\mu$를 근사하기 위하여 $\mu_{\theta}$를 학습하는 것인데, parameterization을 수정함으로써 $\epsilon$를 학습하도록 훈련할 수 있다는 것.&lt;/p&gt;

        &lt;ul&gt;
          &lt;li&gt;model object가 $q(x_{t-1}\vert x_t, x_0)$와 $p_{\theta}(x_{t-1}\vert x_t)$의 분포를 유사하게 하는 것인데, 이는 곧 $\mu_{\theta}$와 $\tilde{\mu}$의 MSE를 최소화하는 것. 이때 $\tilde{\mu}$는 $x_0, x_t$에 대한 함수임.&lt;/li&gt;
          &lt;li&gt;여기에서 새로운 parameter $\epsilon$을 등장시킴. 앞서서 $x_t$를 $x_0$으로부터 sampling한 것으로 볼 수 있다고 했는데, 이에 reparameterization trick을 적용했을 때, 표준정규분포로부터 sampling한 noise의 값이 $\epsilon$.&lt;/li&gt;
          &lt;li&gt;$\epsilon_{\theta}$와 $\epsilon$ 사이의 MSE를 최소화하도록 loss를 simplify할 수 있음&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;To sample $x_{t-1}$~$p_{\theta}(x_{t-1}\vert x_t)$&lt;/p&gt;

    &lt;p&gt;compute&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/DDPM/Untitled%2014.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;→ reparameterization trick, z는 N(0, I)&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;other-source&quot;&gt;Other source&lt;/h2&gt;

&lt;h3 id=&quot;helo&quot;&gt;Helo&lt;/h3&gt;

&lt;h3 id=&quot;diffusion-model-입문하기&quot;&gt;&lt;a href=&quot;https://velog.io/@bismute/Diffusion-Model-입문하기&quot;&gt;Diffusion Model 입문하기&lt;/a&gt;&lt;/h3&gt;

&lt;h3 id=&quot;denoising-diffusion-probabilistic-models-논문-리뷰-ddpm-설명-ddpm-증명&quot;&gt;&lt;a href=&quot;https://process-mining.tistory.com/188&quot;&gt;Denoising Diffusion Probabilistic Models 논문 리뷰 (DDPM 설명, DDPM 증명)&lt;/a&gt;&lt;/h3&gt;

&lt;h3 id=&quot;concept-diffusion-models--with-ddpm-&quot;&gt;&lt;a href=&quot;https://hyoseok-personality.tistory.com/entry/Concept-Diffusion-Models-with-DDPM-DDIM&quot;&gt;Concept. Diffusion Models ( with. DDPM )&lt;/a&gt;&lt;/h3&gt;</content><author><name>Gilmo Yang</name><email>epsilon2718@snu.ac.kr</email></author><category term="cv" /><category term="diffusion" /><summary type="html"></summary></entry><entry><title type="html">Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks</title><link href="https://agency301.github.io//cv/computer-vision/2023-07-30-Unpaired-Image-to-Image-Translation-using-Cycle-Consistent-Adversarial-Networks/" rel="alternate" type="text/html" title="Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks" /><published>2023-07-30T07:49:21+00:00</published><updated>2023-07-30T07:49:21+00:00</updated><id>https://agency301.github.io//cv/computer-vision/Unpaired-Image-to-Image-Translation-using-Cycle-Consistent-Adversarial-Networks</id><content type="html" xml:base="https://agency301.github.io//cv/computer-vision/2023-07-30-Unpaired-Image-to-Image-Translation-using-Cycle-Consistent-Adversarial-Networks/">&lt;h2 id=&quot;paper-review&quot;&gt;Paper Review&lt;/h2&gt;

&lt;h1 id=&quot;unpaired-image-to-image-translation-using-cycle-consistent-adversarial-networks&quot;&gt;Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks&lt;/h1&gt;

&lt;h2 id=&quot;unpaired-image-to-image-translation-using-cycle-consistent&quot;&gt;&lt;a href=&quot;https://arxiv.org/abs/1703.10593&quot;&gt;Unpaired Image-to-Image Translation using Cycle-Consistent…&lt;/a&gt;&lt;/h2&gt;

&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;기존의 pix2pix model은 image mapping에 있어 generalizable하다는 특성이 있으나, 반드시 paired dataset이 필요하다는 단점이 있었다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Unpaired-Image-to-Image-Translation-using-Cycle-Consistent-Adversarial-Networks/Untitled.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;p&gt;실제로 많은 양의 paired dataset을 구축하기는 어렵기 때문에, cycleGAN은 unpaired dataset으로 image translation을 하는 방법을 제안한다.&lt;/p&gt;

&lt;h2 id=&quot;model-architecture&quot;&gt;Model Architecture&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;structure&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Unpaired-Image-to-Image-Translation-using-Cycle-Consistent-Adversarial-Networks/Untitled%201.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;cycleGAN의 구조이다. $X→Y$를 mapping하는 $G$, 그리고 $Y→X$를 mapping하는 $F$가 있고, 각각이 생성한 데이터를 평가하는 Discriminator $D_Y, D_X$가 있다. 이들은 기존의 GAN과 마찬가지로 Adversarial Loss를 이용하여 학습하는데, $X, Y$가 paired 되어있지 않으므로, $G, F$가 $D_Y, D_X$를 속이기 위한 데이터를 생성해 input과는 irrelevant한 데이터를 생성하는 문제인 mode collapse가 발생했다고 한다. 그래서 도입된 것이 cycleGAN의 cycle-consistancy loss이다.&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;cycle-consistency loss&lt;/li&gt;
    &lt;/ul&gt;

    &lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Unpaired-Image-to-Image-Translation-using-Cycle-Consistent-Adversarial-Networks/Untitled%202.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;cycle-consistency loss의 요점은 $X$를 $Y$에 mapping한 데이터를 다시 inverse mapping하였을 때 input data를 reconstruction 할 수 있게 loss를 구성하는 것이다. 즉, 이 과정에서 input의 content를 파괴하지 않는 mapping이 학습될 것이다.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;loss&lt;/p&gt;

    &lt;p&gt;cycleGAN의 loss term은 GAN의 Adversarial loss + cycle-consistency loss 이다&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Unpaired-Image-to-Image-Translation-using-Cycle-Consistent-Adversarial-Networks/Untitled%203.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Unpaired-Image-to-Image-Translation-using-Cycle-Consistent-Adversarial-Networks/Untitled%204.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;full loss term&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Unpaired-Image-to-Image-Translation-using-Cycle-Consistent-Adversarial-Networks/Untitled%205.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

    &lt;p&gt;$\lambda$는 $L_{GAN}$과 $L_{cyc}$ 사이의 중요도를 반영하는 값이다.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;code-review&quot;&gt;Code review&lt;/h2&gt;

&lt;p&gt;https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix&lt;/p&gt;

&lt;p&gt;대부분이 GAN과 유사하기 때문에 차이점이 있는 부분만 보겠다.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;optimize_parameters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;forward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_requires_grad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;netD_A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;netD_B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimizer_G&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zero_grad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;  
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;backward_G&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;      
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimizer_G&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;    
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set_requires_grad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;netD_A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;netD_B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimizer_D&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;zero_grad&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; 
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;backward_D_A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;   
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;backward_D_B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;      
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;optimizer_D&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Generator와 Discriminator가 각각 2개 있기 때문에 optimizing하는 코드에 반영이 되어있다. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forward()&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;backward_G()&lt;/code&gt; method는 각각 2개의 Generator model에 관해 작동한다.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;backward_G&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;lambda_idt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;opt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lambda_identity&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;lambda_A&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;opt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lambda_A&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;lambda_B&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;opt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lambda_B&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lambda_idt&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idt_A&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;netG_A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;real_B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss_idt_A&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;criterionIdt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idt_A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;real_B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lambda_B&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lambda_idt&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idt_B&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;netG_B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;real_A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss_idt_B&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;criterionIdt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idt_B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;real_A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lambda_A&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lambda_idt&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss_idt_A&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
            &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss_idt_B&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;

        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss_G_A&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;criterionGAN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;netD_A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fake_B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss_G_B&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;criterionGAN&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;netD_B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fake_A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss_cycle_A&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;criterionCycle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rec_A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;real_A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lambda_A&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss_cycle_B&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;criterionCycle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rec_B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;real_B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lambda_B&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss_G&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss_G_A&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss_G_B&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss_cycle_A&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss_cycle_B&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss_idt_A&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss_idt_B&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;loss_G&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;backward&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;backward_G()&lt;/code&gt; method에 cycle-consistency loss 가 반영되어 있다. identitiy loss는 generator의 mapping이 identity function에 가까워지도록 하는 loss term인데, target domain을 아는 경우에 사용하면 mapping consistency를 높여주는 역할을 한다.&lt;/p&gt;

&lt;p&gt;cf) Identity loss는 &lt;strong&gt;**&lt;a href=&quot;https://arxiv.org/abs/1611.02200&quot;&gt;Unsupervised Cross-Domain Image Generation&lt;/a&gt;&lt;/strong&gt; 에서 처음으로 사용되었다.&lt;/p&gt;</content><author><name>Gilmo Yang</name><email>epsilon2718@snu.ac.kr</email></author><category term="cv" /><category term="Computer-Vision" /><category term="gan" /><summary type="html">Paper Review</summary></entry><entry><title type="html">Encoding Recurrence into Transformers</title><link href="https://agency301.github.io//modeling/2023-07-29-Encoding-Recurrence-into-Transformers/" rel="alternate" type="text/html" title="Encoding Recurrence into Transformers" /><published>2023-07-29T20:21:04+00:00</published><updated>2023-07-29T20:21:04+00:00</updated><id>https://agency301.github.io//modeling/Encoding-Recurrence-into-Transformers</id><content type="html" xml:base="https://agency301.github.io//modeling/2023-07-29-Encoding-Recurrence-into-Transformers/">&lt;h2 id=&quot;paper-review&quot;&gt;Paper Review&lt;/h2&gt;

&lt;h1 id=&quot;encoding-recurrence-into-transformers&quot;&gt;Encoding Recurrence into Transformers&lt;/h1&gt;

&lt;h2 id=&quot;encoding-recurrence-into-transformers-1&quot;&gt;&lt;a href=&quot;https://openreview.net/forum?id=7YfHla7IxBJ&quot;&gt;Encoding Recurrence into Transformers&lt;/a&gt;&lt;/h2&gt;

&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Encoding-Recurrence-into-Transformers/Untitled.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;p&gt;문제 상황: RNN은 구조상, recurrency 가 높은 데이터에 대해서는 학습 데이터가 적어도 효율이 나오는데, transformer는 그렇지 않다. 다만 transformer는 학습 데이터셋 크기가 크면 data의 recurrency에 관계없이 효율이 나온다. 이 점에 착안하여 RNN이 recurrence를 잘 capture하는 특징과 Transformer의 Attention 매커니즘을 결합하여 sequential data에 대한 학습 효율을 증가시키자는 것이 골자이다.&lt;/p&gt;

&lt;h2 id=&quot;model-architecture&quot;&gt;Model Architecture&lt;/h2&gt;

&lt;p&gt;용어:&lt;/p&gt;

&lt;p&gt;RSA: Self-Attention with Recurrence, 이 논문에서 propose한 것&lt;/p&gt;

&lt;p&gt;REM: Recurrence Encoding Matrix, 여기에 rnn의 본질인 recurrence dynamics가 positional encoding의 형태로 담겨 있다.&lt;/p&gt;

&lt;p&gt;수식이 많은데 요점은…&lt;/p&gt;

&lt;p&gt;RNN은 원래 병렬 처리가 안된다. → 은닉층 가중치가 지수적으로 곱해지기 때문&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Encoding-Recurrence-into-Transformers/Untitled%201.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;p&gt;$W^{j}_{h}$ 이 부분이 문제임 → 하지만 Jordan Form으로 semi-diagonalize하면 손실 조금만 나면서 행렬 정리 가능&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Encoding-Recurrence-into-Transformers/Untitled%202.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Encoding-Recurrence-into-Transformers/Untitled%203.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;p&gt;위처럼 block RNN 구조로 바꾼 다음에 Self-Attention의 form 으로 이것을 표현하기 위해 아래와 같은 masking 행렬을 정의한다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Encoding-Recurrence-into-Transformers/Untitled%204.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Encoding-Recurrence-into-Transformers/Untitled%205.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;p&gt;그리고 이것을 $h^{C_1}&lt;em&gt;{t}, h^{C_2}&lt;/em&gt;{t}$에 대해서도 똑같이 정의하고, Multi-head Self-Attention에 넣어주면 된다.&lt;/p&gt;

&lt;p&gt;결국 RNN의 Recurrence 를 Capture하는 능력이 REM에 담기게 되지만, 계산 상 Attention과는 큰 관련이 없는데, Attention이 non-recurrent한 데이터에 대한 성능도 뛰어나니까 gated unit 하나로 REM과 conventional Self-Attention을 통합한 것이 RSA이다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Encoding-Recurrence-into-Transformers/Untitled%206.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;p&gt;$P$가 REM이다. $\sigma(\mu)$는 learnable gate value이고, attention과 REM 을 사용하는 것의 비중을 정해준다.&lt;/p&gt;

&lt;p&gt;(data의 reccurence가 심한 경우 이 값이 크게 학습된다.)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Encoding-Recurrence-into-Transformers/Untitled%207.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;p&gt;derivation은 밑 토글 참조.&lt;/p&gt;

&lt;p&gt;암튼 RSA는 입력과 출력만 보았을 때 self-attention이랑 같기 때문에, 기존에 SA를 사용하던 Transformer 모델에 RSA를 대체로 넣어주기만 하면 되어서 편하다고 한다.&lt;/p&gt;

&lt;p&gt;그리고 Introduction에 있는 그림처럼 RSA를 쓰면 그냥 SA보다 표현력이 증가해서 여러 sequential data learning tasks에서 좋은 결과를 얻었다고 한다…(대단해 O~O)&lt;/p&gt;

&lt;p&gt;time series data를 이용한 비교이다. 본 논문에서 가장 recurrent한게 time series라고 밝혔듯이 확실히 개선된 결과를 보여준다. (prefix RSA가 붙은게 RSA를 대체로 쓴 모델이다)&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Encoding-Recurrence-into-Transformers/Untitled%208.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;p&gt;이 외에도 벤치마크 몇 개 더 있는데 암튼 개선된 것 같다!&lt;/p&gt;

&lt;h2 id=&quot;mathmatics&quot;&gt;Mathmatics&lt;/h2&gt;

&lt;p&gt;REM 식을 derivation하는데 필요한 수학 지식이다.&lt;/p&gt;

&lt;p&gt;A. Jordan form&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Encoding-Recurrence-into-Transformers/IMG_0044.jpeg&quot; alt=&quot;IMG_0044.jpeg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Encoding-Recurrence-into-Transformers/IMG_0045.jpeg&quot; alt=&quot;IMG_0045.jpeg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Encoding-Recurrence-into-Transformers/IMG_0046.jpeg&quot; alt=&quot;IMG_0046.jpeg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;B. Applied Theorems&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Encoding-Recurrence-into-Transformers/IMG_0047.jpeg&quot; alt=&quot;IMG_0047.jpeg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;추가) 어떤 행렬이 eigenvalue로 어떤 complex를 가지면 그 conjugate도 eigenvalue이다&lt;/p&gt;

&lt;p&gt;RNN 표현&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Encoding-Recurrence-into-Transformers/IMG_0049.jpeg&quot; alt=&quot;IMG_0049.jpeg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Encoding-Recurrence-into-Transformers/IMG_0050.jpeg&quot; alt=&quot;IMG_0050.jpeg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Encoding-Recurrence-into-Transformers/IMG_0052.jpeg&quot; alt=&quot;IMG_0052.jpeg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://agency301.github.io/assets/img/Encoding-Recurrence-into-Transformers/IMG_0054.jpeg&quot; alt=&quot;IMG_0054.jpeg&quot; /&gt;&lt;/p&gt;</content><author><name>Gilmo Yang</name><email>epsilon2718@snu.ac.kr</email></author><category term="modeling" /><category term="transformer" /><summary type="html">Paper Review</summary></entry><entry><title type="html">Welcome to Jekyll!</title><link href="https://agency301.github.io//jekyll/update/2023-07-25-welcome-to-jekyll/" rel="alternate" type="text/html" title="Welcome to Jekyll!" /><published>2023-07-25T14:28:26+00:00</published><updated>2023-07-25T14:28:26+00:00</updated><id>https://agency301.github.io//jekyll/update/welcome-to-jekyll</id><content type="html" xml:base="https://agency301.github.io//jekyll/update/2023-07-25-welcome-to-jekyll/">&lt;hr /&gt;

&lt;ol id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#symbolic-discovery-of-optimization-algorithms-20235&quot; id=&quot;markdown-toc-symbolic-discovery-of-optimization-algorithms-20235&quot;&gt;Symbolic Discovery of Optimization Algorithms (2023.5)&lt;/a&gt;    &lt;ol&gt;
      &lt;li&gt;&lt;a href=&quot;#symbolic-discovery-of-optimization-algorithms&quot; id=&quot;markdown-toc-symbolic-discovery-of-optimization-algorithms&quot;&gt;Symbolic Discovery of Optimization Algorithms&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#introduction&quot; id=&quot;markdown-toc-introduction&quot;&gt;Introduction&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#program-search-space-designing&quot; id=&quot;markdown-toc-program-search-space-designing&quot;&gt;Program search space designing&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#efficient-search-techniques&quot; id=&quot;markdown-toc-efficient-search-techniques&quot;&gt;Efficient search techniques&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#program-selection-methodology&quot; id=&quot;markdown-toc-program-selection-methodology&quot;&gt;Program selection methodology&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#proposition-lion-evolved-sign-momentum-optimizer&quot; id=&quot;markdown-toc-proposition-lion-evolved-sign-momentum-optimizer&quot;&gt;Proposition: Lion (EvoLved Sign Momentum) Optimizer&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#evaluation-of-lion&quot; id=&quot;markdown-toc-evaluation-of-lion&quot;&gt;Evaluation of Lion&lt;/a&gt;&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#atlasyang-gilmo-yang&quot; id=&quot;markdown-toc-atlasyang-gilmo-yang&quot;&gt;AtlasYang (Gilmo Yang)&lt;/a&gt;    &lt;ol&gt;
      &lt;li&gt;&lt;a href=&quot;#bio&quot; id=&quot;markdown-toc-bio&quot;&gt;BIO&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#organization&quot; id=&quot;markdown-toc-organization&quot;&gt;Organization&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#contact&quot; id=&quot;markdown-toc-contact&quot;&gt;Contact&lt;/a&gt;&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&quot;symbolic-discovery-of-optimization-algorithms-20235&quot;&gt;Symbolic Discovery of Optimization Algorithms (2023.5)&lt;/h1&gt;

&lt;h2 id=&quot;symbolic-discovery-of-optimization-algorithms&quot;&gt;&lt;a href=&quot;https://arxiv.org/abs/2302.06675&quot;&gt;Symbolic Discovery of Optimization Algorithms&lt;/a&gt;&lt;/h2&gt;

&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;이 논문은 AutoML-Zero 와 상당히 유사한 내용을 가지고 있다. 하단 링크 참고&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/2003.03384&quot;&gt;AutoML-Zero: Evolving Machine Learning Algorithms From Scratch&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;AutoML-Zero는 최대한 사람의 개입 없이 ML Architecture 를 구성하는 것에 대한 논문인데, 미분같은 복잡한 연산은 제외하고 65가지의 연산, 함수(instruction이라 한다.)를 조합하여 program flow를 구성하는 것을 목표로 program space를 search하였다고 한다. 사용된 방법은 evolutionary search로,  genetic algorithm에 기반하여 instruction을 넣거나 빼고, 대체하는 등의 mutation 연산, 그리고 한 time-step(=generation)이 지날 때마다 N개의 top rated program을 parents로 하여 copy + mutation하는 절차를 통해 다양한 program을 만들어냈다고 한다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://s3-us-west-2.amazonaws.com/secure.notion-static.com/39c37380-e519-4582-94b2-3185da47edd5/Untitled.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;p&gt;본 논문도 마찬가지로 program search를 통해 최적의 optimizer를 찾고자 했다. 본 논문에서는 algorithm을 개발하는 데에 있어 program을 symbolic representation으로 사용하는 것에 대한 3가지 장점을 밝히고 있다.&lt;/p&gt;

&lt;p&gt;(1). 어차피 계산 하려면 프로그램으로 나타내야 한다 (2). 분석하기 편하고 다른 분야로 transfer가 용이하다 (3). 프로그램의 길이를 알고리즘의 복잡도를 추정하는데 사용할 수 있어, 더 간단하고 일반적인 알고리즘을 찾는데 도움이 된다.&lt;/p&gt;

&lt;h2 id=&quot;program-search-space-designing&quot;&gt;Program search space designing&lt;/h2&gt;

&lt;p&gt;해당 논문에서는 program search space를 설계할 때 고려할 점을 3가지 밝힌다.&lt;/p&gt;

&lt;p&gt;(1). 신박한 알고리즘을 얻기 위해 충분히 넓어야 한다 (2). ML의 workflow에 적합해야 한다 (3). high-level design에만 집중한다&lt;/p&gt;

&lt;p&gt;다음은 program search를 위한 4가지 구성이다.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;train&lt;/code&gt; function&lt;/p&gt;

    &lt;p&gt;input으로 weight $w$, gradient $g$, learning rate $lr$을 받아 weight update를 output 하는 main objective이다. 즉, search의 객체이다.&lt;/p&gt;

    &lt;p&gt;historical value를 저장하는 extra variable을 둘 수 있다&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;building blocks&lt;/p&gt;

    &lt;p&gt;train function은 statement와 local variable 개수에 제약을 받지 않는다. 본 논문에서는 45개의 commom math function을 채택하였고, program의 간결성을 위해 linear interpolation function $interp(x, y, a)=(1-a)x+ay$ 등을 정의하였다.&lt;/p&gt;

    &lt;p&gt;또한 기존의 연구에서는 반복문, 조건문 등의 complex feature를 도입하였었는데 유의미한 성능 진전이 없었으므로 본 연구에서는 제외하였다고 한다.&lt;/p&gt;

    &lt;p&gt;차원에 맞지 않는 데이터 연산은 automatically casted되도록 했다고 한다.(ex. matrix + scalar)&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;mutations and redundant statements&lt;/p&gt;

    &lt;p&gt;3가지 mutation operation을 정의한다&lt;/p&gt;

    &lt;ol&gt;
      &lt;li&gt;random function을 random location에 삽입&lt;/li&gt;
      &lt;li&gt;randomly chosen statement 삭제&lt;/li&gt;
      &lt;li&gt;random function의 argument를 수정&lt;/li&gt;
    &lt;/ol&gt;

    &lt;p&gt;random variable이나 constant는 normal distribution에서 추출하며, constant들은 차후에 tunable hyperparameter로 기능할 수 있다.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;infinite and sparse search space&lt;/p&gt;

    &lt;p&gt;internal arguments, state, function의 개수에 제약을 두지 않고, mutation연산의 존재 때문에 program search space가 infinite하다.&lt;/p&gt;

    &lt;p&gt;high-performing program은 search space상에 sparse하게 존재하기 때문에, 이를 빠르게 search하기 위해 low-cost proxy task를 설정하여 evaluation하는 것으로 보인다.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;efficient-search-techniques&quot;&gt;Efficient search techniques&lt;/h2&gt;

&lt;p&gt;high-performing algorithm이 너무 sparse하게 존재하는 문제점 때문에 본 논문에서는 각종 search technique를 도입한다.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Evolution with warm-start and restart&lt;/p&gt;

    &lt;p&gt;warm-start: search acceleration을 위해 AdamW에서 시작&lt;/p&gt;

    &lt;p&gt;restart:&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;exploration: start from initial program&lt;/li&gt;
      &lt;li&gt;exploitation: start from best program&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Pruning through abstract execution&lt;/p&gt;

    &lt;p&gt;별 건 아니고 에러 나는 거 없애주고, program 마다 hash값 만들어서 똑같은 program 삭제하는 것&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Proxy tasks and search cost&lt;/p&gt;

    &lt;p&gt;low-cost proxy task로 search 진행. funnel selection과 연관.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;program-selection-methodology&quot;&gt;Program selection methodology&lt;/h2&gt;

&lt;p&gt;매우 큰 program space를 search하기 때문에 proxy task를 필연적으로 이용하게 되는데, proxy task와 target task의 차이, 그리고 다양한 domain에 적용될 수 있는 generalization 능력을 원하기 때문에, 학습과 활용에 큰 차이가 발생한다고 밝히고 있다. 이에 본 논문에서는 2가지 해결책을 사용한다.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Funnel selection&lt;/p&gt;

    &lt;p&gt;funnel selection은 proxy task를 통해 generalization power를 검증하는 방법이다.&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;proxy task A를 통해 problem set $S_1$을 평가한다 → selection → $S_2$&lt;/li&gt;
      &lt;li&gt;A보다 10배 더 큰 task B로 $S_2$의 평가를 진행한다 → selection → $S_3$&lt;/li&gt;
      &lt;li&gt;$S_3$를 A보다 100배 더 큰 task C로 평가한다 → selection&lt;/li&gt;
    &lt;/ul&gt;

    &lt;p&gt;이러한 과정을 통해 generalization performance가 떨어지는 program을 낙오시킨다.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Simplification&lt;/p&gt;

    &lt;p&gt;더 간단할 수록 generalization 능력이 높을 것이라는 직관에 의해, input과 output이 identical하거나, 없어져도 거의 영향을 미치지 않는 statement, function을 제거한다.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;proposition-lion-evolved-sign-momentum-optimizer&quot;&gt;Proposition: Lion (EvoLved Sign Momentum) Optimizer&lt;/h2&gt;

&lt;p&gt;&lt;del&gt;너무 억지 작명인 것 같다&lt;/del&gt;&lt;/p&gt;

&lt;p&gt;해당 논문에서 찾아낸 Lion Opimizer이다.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://s3-us-west-2.amazonaws.com/secure.notion-static.com/a1890379-340a-46e6-9c12-fee66350fcf4/Untitled.png&quot; alt=&quot;Untitled&quot; /&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Sign update and regularization&lt;/p&gt;

    &lt;p&gt;sign 함수를 이용하기 때문에, 모든 dimension에서 uniform magnitude를 가진다고 한다. 이 특성이 noise를 일으켜 regularization 처럼 기능한다고 설명한다.&lt;/p&gt;

    &lt;p&gt;cf. sign 함수가 noise를 일으키는 이유? → Lion은 RMSprop이나 Adam처럼 EMA를 사용하는데(linear interpolation 부분), 여기에 sign을 취해버려서 magnitude는 반영을 안하고 tendency만 반영하게 되어서 update가 non-smooth하게 되어서 noisy하다는 것 같음&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Momentum tracking&lt;/p&gt;

    &lt;p&gt;$\beta_1, \beta_2$를 이용하는데, 2개를 써야하는 이유는 appendix의 ablation study에서 증명했다.(사실 evolutionary search로 찾아낸 것이기 때문에 사후 정당화에 가깝다)&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Hyperparameter and batch size choices&lt;/p&gt;

    &lt;p&gt;Lion Optimizer의 성능은 Batch size가 커질 수록 비례하여 증가한다.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Memory and runtime benefits&lt;/p&gt;

    &lt;p&gt;task에 따라 AdamW 보다 2-15% 가량의 속도 향상을 보였다.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;evaluation-of-lion&quot;&gt;Evaluation of Lion&lt;/h2&gt;

&lt;p&gt;ViT, LiT, Autoregressive Model 등 다양한 task에 적용했는데 AdamW보다 항상 낫거나 비슷했다고 한다. benchmark table이 너무 많으니까 논문 참조.&lt;/p&gt;

&lt;h1 id=&quot;atlasyang-gilmo-yang&quot;&gt;AtlasYang (Gilmo Yang)&lt;/h1&gt;
&lt;h2 id=&quot;bio&quot;&gt;BIO&lt;/h2&gt;
&lt;hr /&gt;
&lt;p&gt;Undergraduate Student majoring Computer Science &amp;amp; Engineering, Interested in Cognitive Architecture, Cellular Automata, and other DL, ML branches of study.&lt;/p&gt;

&lt;h2 id=&quot;organization&quot;&gt;Organization&lt;/h2&gt;
&lt;hr /&gt;
&lt;p&gt;Seoul National University, Dept. of Computer Science &amp;amp; Engineering&lt;/p&gt;

&lt;p&gt;AttentionX&lt;/p&gt;

&lt;h2 id=&quot;contact&quot;&gt;Contact&lt;/h2&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;a href=&quot;atlas.yang3598@gmail.com&quot;&gt;E-mail&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/AtlasYang&quot;&gt;GitHub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://blog.naver.com/epsilon2718&quot;&gt;Blog&lt;/a&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;p&gt;You’ll find this post in your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_posts&lt;/code&gt; directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different ways, but the most common way is to run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jekyll serve&lt;/code&gt;, which launches a web server and auto-regenerates your site when a file is updated.&lt;/p&gt;

&lt;p&gt;Jekyll requires blog post files to be named according to the following format:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YEAR-MONTH-DAY-title.MARKUP&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;YEAR&lt;/code&gt; is a four-digit number, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MONTH&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;DAY&lt;/code&gt; are both two-digit numbers, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MARKUP&lt;/code&gt; is the file extension representing the format used in the file. After that, include the necessary front matter. Take a look at the source for this post to get an idea about how it works.&lt;/p&gt;

&lt;p&gt;Jekyll also offers powerful support for code snippets:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;print_hi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Hi, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;print_hi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'Tom'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;#=&amp;gt; prints 'Hi, Tom' to STDOUT.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Check out the &lt;a href=&quot;https://jekyllrb.com/docs/home&quot;&gt;Jekyll docs&lt;/a&gt; for more info on how to get the most out of Jekyll. File all bugs/feature requests at &lt;a href=&quot;https://github.com/jekyll/jekyll&quot;&gt;Jekyll’s GitHub repo&lt;/a&gt;. If you have questions, you can ask them on &lt;a href=&quot;https://talk.jekyllrb.com/&quot;&gt;Jekyll Talk&lt;/a&gt;.&lt;/p&gt;</content><author><name></name></author><category term="jekyll" /><category term="update" /><summary type="html"></summary></entry></feed>