*Triplet loss (Weinberger et al. 2006) operates on a triplet of vectors whose labels follow ๐ฆ_๐=๐ฆ_๐ and ๐ฆ_๐โ ๐ฆ_๐. That is to say two of the three (๐ณ_๐ข and ๐ณ_๐ฃ) shared the same label and a third vector ๐ณ_๐ค has a different label. In triplet learning literatures, they are termed anchor (z_i), positive (z_j), and negative (z_k), respectively. where ๐ again is a margin parameter that requires the delta distances between anchor-positive and anchor-negative to be larger than ๐. A triplet may have the following three scenarios before any training.* ์ด ๋ด์ฉ์ ์ดํดํ๊ณ ์ค๋ฉด ์ดํด๊ฐ ๊ต์ฅํ ์ฌ์์ง๋ค.
์ ์์์ triplet loss ์ ํ์ ์ ์ฐพ๋๋ค๊ณ ์๊ฐํด ๋ณด์. z_i ๋ achor ์, z_j ๋ positive sample ์ ๋๋ณํ๋ ์น๊ตฌ, z_k ๋ negative sample ์ ๋๋ณํ๋ ์น๊ตฌ์ด๋ค. negative sample ๊ณผ anchor ๋ด์ ๊ฐ์ ์ฐจ๋ฅผ ํตํด loss ๋ฅผ ๊ณ์ฐํ๋ค. ์ด๋ ์ฌ๊ธฐ์ N ์ด ์๋๊ณ 2N ์ธ ์ด์ ๋, N ์ ๋จ์๋ pair ์ด๊ธฐ ๋๋ฌธ์ด๋ค. (์ฐธ๊ณ :
Encoder ๋ฐ projection head๋ batch ๋จ์๋ก ํ์ตํ๊ฒ ๋๋๋ฐ, ๋ง์ฝ N์ batch size๋ฅผ ์ด์ฉํ๊ฒ ๋๋ค๋ฉด ๊ฐ๊ฐ data augmentation์ ๊ฑฐ์ณ์ 2N๊ฐ์ sample์ ์ป์ ์ ์์ต๋๋ค. ์ด๋ ๊ฒ ๋๋ฉด ๊ฐ sample ๋ณ๋ก 1์์ positive pair์ 2N-2์์ negative pair๋ฅผ ๊ตฌ์ฑํ ์ ์๊ฒ ๋ฉ๋๋ค. ๋
ผ๋ฌธ์์๋ positive pair ๊ฐ์ similarity๋ ๋์ด๊ณ , negative pair ๊ฐ์ similarity๋ ์ต์ํํ๋ ํํ์ loss function์ ์ ์ํ์ฌ ํ์ต์ ํ์ฉํ์์ต๋๋ค. (์ง๋ฌธ ๋ฃ์. 2N-2์์ด ์๋๋ผ N-1 ์ ์๋๋๊ณ โ ์๋
ํ์ธ์. 1๊ฐ์ query sample์ ๋ํด, positive pair๋ฅผ ์ด๋ฃจ๋ 1๊ฐ์ sample์ ์ ์ธํ ๋๋จธ์ง 2n-2 ๊ฐ์ sample๋ค๊ณผ ๊ฐ๊ฐ negative pair๋ฅผ ๊ตฌ์ฑํ ์ ์์ต๋๋ค. n๊ฐ์ ์ด๋ฏธ์ง ์ค query sample์ด ๋๋ 1๊ฐ์ ์ด๋ฏธ์ง๋ฅผ ์ ์ธํ n-1 ๊ฐ์ ๋ค๋ฅธ ์ด๋ฏธ์ง๋ค์ด augmentation์ ๊ฑฐ์ณ์ 2n-2๊ฐ์ sample์ด ๋๊ณ , ์ด๊ฒ๋ค๊ณผ ๋ชจ๋ negative pair๋ฅผ ์ด๋ฃฌ๋ค๊ณ ์ดํดํ์๋ฉด ๋ ๊ฒ ๊ฐ์ต๋๋ค.)) a ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ 2๋ฒ์ฉ augmentation ์ํค๋ฉด, 2a ๊ฐ์ ๋ฐ์ดํฐ๊ฐ ๋์ค๋๋ฐ, ๊ฐ ์์(anchor)์ ์
์ฅ ์์๋ 1๊ฐ์ positive sample (1 ๊ฐ์ positive pair), 2a-2 ๊ฐ์ negative sample ์ธ๋ฐ ๊ทธ "๊ฐ๊ฐ"์ ๋ํด์ negative pair ๋ก ์ธ ์ ์์ผ๋ฏ๋ก 2a ๊ฐ์ pair ๋ผ๋ ๊ฒ์ด๋ค. positive-negative ๊ฐ 10์์ด๋ฉด ๋ฐ์ดํฐ์ ์๋ 20๊ฐ๊ฐ ๋๋ค. ํ๋์ anchor ์ ๋ํด์ ๋๋จธ์ง 18 ๊ฐ๋ก negative pair ์ ๋ง๋ค ์ ์์ผ๋ ์ผ๋ง๋ ์ข์๊ฐ.
Encoder ๋ฐ projection head๋ batch ๋จ์๋ก ํ์ตํ๊ฒ ๋๋๋ฐ, ๋ง์ฝ N์ batch size๋ฅผ ์ด์ฉํ๊ฒ ๋๋ค๋ฉด ๊ฐ๊ฐ data augmentation์ ๊ฑฐ์ณ์ 2N๊ฐ์ sample์ ์ป์ ์ ์์ต๋๋ค. ์ด๋ ๊ฒ ๋๋ฉด ๊ฐ sample ๋ณ๋ก 1์์ positive pair์ 2N-2์์ negative pair๋ฅผ ๊ตฌ์ฑํ ์ ์๊ฒ ๋ฉ๋๋ค. ๋
ผ๋ฌธ์์๋ positive pair ๊ฐ์ similarity๋ ๋์ด๊ณ , negative pair ๊ฐ์ similarity๋ ์ต์ํํ๋ ํํ์ loss function์ ์ ์ํ์ฌ ํ์ต์ ํ์ฉํ์์ต๋๋ค. (์ง๋ฌธ ๋ฃ์. 2N-2์์ด ์๋๋ผ N-1 ์ ์๋๋๊ณ โ ์๋
ํ์ธ์. 1๊ฐ์ query sample์ ๋ํด, positive pair๋ฅผ ์ด๋ฃจ๋ 1๊ฐ์ sample์ ์ ์ธํ ๋๋จธ์ง 2n-2 ๊ฐ์ sample๋ค๊ณผ ๊ฐ๊ฐ negative pair๋ฅผ ๊ตฌ์ฑํ ์ ์์ต๋๋ค. n๊ฐ์ ์ด๋ฏธ์ง ์ค query sample์ด ๋๋ 1๊ฐ์ ์ด๋ฏธ์ง๋ฅผ ์ ์ธํ n-1 ๊ฐ์ ๋ค๋ฅธ ์ด๋ฏธ์ง๋ค์ด augmentation์ ๊ฑฐ์ณ์ 2n-2๊ฐ์ sample์ด ๋๊ณ , ์ด๊ฒ๋ค๊ณผ ๋ชจ๋ negative pair๋ฅผ ์ด๋ฃฌ๋ค๊ณ ์ดํดํ์๋ฉด ๋ ๊ฒ ๊ฐ์ต๋๋ค.)) a ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ 2๋ฒ์ฉ augmentation ์ํค๋ฉด, 2a ๊ฐ์ ๋ฐ์ดํฐ๊ฐ ๋์ค๋๋ฐ, ๊ฐ ์์(anchor)์ ์
์ฅ ์์๋ 1๊ฐ์ positive sample (1 ๊ฐ์ positive pair), 2a-2 ๊ฐ์ negative sample ์ธ๋ฐ ๊ทธ "๊ฐ๊ฐ"์ ๋ํด์ negative pair ๋ก ์ธ ์ ์์ผ๋ฏ๋ก 2a ๊ฐ์ pair ๋ผ๋ ๊ฒ์ด๋ค. positive-negative ๊ฐ 10์์ด๋ฉด ๋ฐ์ดํฐ์ ์๋ 20๊ฐ๊ฐ ๋๋ค. ํ๋์ anchor ์ ๋ํด์ ๋๋จธ์ง 18 ๊ฐ๋ก negative pair ์ ๋ง๋ค ์ ์์ผ๋ ์ผ๋ง๋ ์ข์๊ฐ.triplet loss ์ ๋ค๋ฅด๊ฒ, loss ๋ฅผ ๊ตฌํ๋ ๋ฐ z_k ๋ฅผ ์ง์ ํด ์ค ํ์๊ฐ ์๋๋ฐ, ์ด๋ค z_i, z_j ๊ฐ input ์ผ๋ก ์ฌ์ฉ๋๋ ๋ชจ๋ neagtive z_k ์ ๋ํด์ ์ ๋ถ ์ฐ์ฐ์ ๊ฑฐ์น๊ธฐ ๋๋ฌธ์ด๋ค.
Triplet loss (left) and its extension (N+1)-triplet loss (right) (from Sohn 2016)
์ดํดํ๊ณ ๋ณด๋ฉด ์ด๊ฒ์ ๊ต์ฅํ ์ ๊ทธ๋ ค์ง ๊ทธ๋ฆผ์ด๋ผ๋ ๊ฒ์ ์ ์ ์๋ค. ์ด๋ค input x ๋ฅผ 3๊ฐ ๋ฃ์๋ค๊ณ ์๊ฐํด ๋ณด์. ๊ณก์ ํ์ดํ๋ ๋ชจ๋ธ์ optimization ์ด์ ๊ณผ ์ดํ์ ๋ณํ๋ฅผ ๋ํ๋ธ๋ค. ์ด ๊ทธ๋ฆผ์ด ๋ณด์ฌ์ฃผ๊ณ ์ถ์ ๊ฒ์, ๊ทธ๋ฅ ์ ์์ triplet loss ์ ์ผ๋ฐํ๋ผ๋ ์ ์ด๋ค.
Default view
Search
์ ์์ ์กฐ๊ธ ๋ ์ ์ ๋ฆฌํ๋ฉด ์๋์ ๊ฐ์ ํํ๊ฐ ๋์จ๋ค.
z_i ๋ ๋ฐ๋์ anchor ์ด๊ณ ์ฌ๊ธฐ์ z_j ๋ ๋ฐ๋์ positive sample ์ด๋ค. (NX-Xent loss ์ฐธ๊ณ Supervised NT-Xent loss (Khosla et al. 2020), let's first look at the self-supervised version of NT-Xent loss. NT-Xent is coined by Chen et al. 2020 in the SimCLR paper and is short for "normalized temperature scaled cross entropy lossโฃโ. It is a modification of the multi-class N-pair loss with addition of the temperature parameter (๐) to scale the cosine similarities. Chen et al. found that an appropriate temperature parameter can help the model learn from hard negatives. ๋ ๋ง์ฐฌ๊ฐ์ง์ด๋ค.)
์ธ๋ป z_j ์ ํด๋นํ๋ ๊ฒ์ negative sample ๋ฅผ ๋ฃ์ด ๋ ์ผ๋ฐํ์์ผ๋ ๋์ง ์์๊น? ํ๋ ์๊ฐ์ด ๋ค ์ ์์ง๋ง,
์ ์๊ฐํด๋ณด๋ฉด z_j ์ posive ๊ฐ ๋ค์ด๊ฐ๋ negative ๊ฐ ๋ค์ด๊ฐ๋ -log(a/b) ์์ ๋ถ๋ชจ b ๋ ๋์ฒด์ ์ผ๋ก ๋๊ฐ์ํ
๊ณ ๋ถ์์ธ a ๋ง ๋ณํ๋๋ฐ, a/b=1 ์ด์ด์ผ loss=0 ์ด๋ฏ๋ก Optimal ์ด๋ค. ์ด ์ํ๋ก ํ๋ จ์ ์ํค๋ ๊ฒ์ด๋ค.
๋น์ด ์ทจ์ง๊ฐ
positive / (positive_all +
negative_all) ๊ฐ 1 ์ ๊ฐ๊น์์ง๋ก ๊ต์ก
*๋ฐฉํฅ
๊ณผ
์ ํด๋น ์์๊ฐ ์ด๋ป๊ฒ ๋ณํํ์ผ๋ฉด ์ข๊ฒ ๋ค๋ ์ฐ๋ฆฌ์ ๋ฐ๋์ด๋ค.
์ธ๋ฐ ๋ง์ฝ negative ๊ณผ anchor ์ ์ ์ฌ๋๊ฐ ๋ถ์์ ๋ค์ด๊ฐ๋ค๋ฉด ์๋์ ๊ฐ์ ๊ต์ก์ ํ๊ฒ ๋๋ค.
negative / (
positive_all +
negative_all) ๊ฐ 1 ์ ๊ฐ๊น์์ง๋๋ก ๊ต์ก
negative ์
์ฆ๊ฐํด์ผ ํ๋๋ฐ negative_all ์
๊ฐ์ํด์ผ ํ๋ค๊ณ ? negative ์ negative_all ์ ๋ถ๋ถ์งํฉ์ด๋ค. ๊ฒ๋ค๊ฐ positive_all ์ ์ฆ๊ฐํ๊ธฐ๋ฅผ ๋ฐ๋ผ์ผํ๋๋ฐ ์ด ์์์๋
๊ฐ์ํ๊ธฐ๋ฅผ ๋ฐ๋ผ๋ฏ๋ก ์ด๊ฑด ๋
ธ์ด์ฆ์ด๋ค.
loss ๋ฅผ ํด์ํ ๋ ์ด๋ ๊ฒ (์ฐธ๊ณ 3) ์์ ์์๋ฅผ ๊ฐ๊ฐ, ๊ท๋ฉ์ ์ผ๋ก(?) ํด์ํด๋ ๋๋๊ฑด๊ฐ? ๋
ผ๋ฌธ (supervised contrastive loss โฃ) ์์ ์ด๊ฑธ ์ญ์ ํ์์ผ๋ก ์ ๋ (์ฐธ๊ณ 1) ํ๋ ๊ฒ์ด ๋์ค๋๋ฐ, ์ด๋ฐ ์ง๊ด๊ณผ ํ์ค์ด ์กฐ๊ธ ๋ค๋ฅธ ๊ฒฝ์ฐ๋ค์ด ์์ ์กด์ฌํ๋ ๊ฒ ๊ฐ๋ค. ํด๋น ๋
ผ๋ฌธ (์ฐธ๊ณ 2) ์ ์ฐธ๊ณ ํด์ ๊ฐ๋
์ ํ์คํ ํ๋๊ฑด ์ด๋จ๊น? ๊ด๋ จ ์๋ฌธ (์ฐธ๊ณ 4)
positive / (positive_all +
negative_all) ๊ฐ 1 ์ ๊ฐ๊น์์ง๋ก ๊ต์ก
*๋ฐฉํฅ
๊ณผ
์ ํด๋น ์์๊ฐ ์ด๋ป๊ฒ ๋ณํํ์ผ๋ฉด ์ข๊ฒ ๋ค๋ ์ฐ๋ฆฌ์ ๋ฐ๋์ด๋ค.
์ธ๋ฐ ๋ง์ฝ negative ๊ณผ anchor ์ ์ ์ฌ๋๊ฐ ๋ถ์์ ๋ค์ด๊ฐ๋ค๋ฉด ์๋์ ๊ฐ์ ๊ต์ก์ ํ๊ฒ ๋๋ค.
negative / (
positive_all +
negative_all) ๊ฐ 1 ์ ๊ฐ๊น์์ง๋๋ก ๊ต์ก
negative ์
์ฆ๊ฐํด์ผ ํ๋๋ฐ negative_all ์
๊ฐ์ํด์ผ ํ๋ค๊ณ ? negative ์ negative_all ์ ๋ถ๋ถ์งํฉ์ด๋ค. ๊ฒ๋ค๊ฐ positive_all ์ ์ฆ๊ฐํ๊ธฐ๋ฅผ ๋ฐ๋ผ์ผํ๋๋ฐ ์ด ์์์๋
๊ฐ์ํ๊ธฐ๋ฅผ ๋ฐ๋ผ๋ฏ๋ก ์ด๊ฑด ๋
ธ์ด์ฆ์ด๋ค.
loss ๋ฅผ ํด์ํ ๋ ์ด๋ ๊ฒ (์ฐธ๊ณ 3) ์์ ์์๋ฅผ ๊ฐ๊ฐ, ๊ท๋ฉ์ ์ผ๋ก(?) ํด์ํด๋ ๋๋๊ฑด๊ฐ? ๋
ผ๋ฌธ (supervised contrastive loss โฃ) ์์ ์ด๊ฑธ ์ญ์ ํ์์ผ๋ก ์ ๋ (์ฐธ๊ณ 1) ํ๋ ๊ฒ์ด ๋์ค๋๋ฐ, ์ด๋ฐ ์ง๊ด๊ณผ ํ์ค์ด ์กฐ๊ธ ๋ค๋ฅธ ๊ฒฝ์ฐ๋ค์ด ์์ ์กด์ฌํ๋ ๊ฒ ๊ฐ๋ค. ํด๋น ๋
ผ๋ฌธ (์ฐธ๊ณ 2) ์ ์ฐธ๊ณ ํด์ ๊ฐ๋
์ ํ์คํ ํ๋๊ฑด ์ด๋จ๊น? ๊ด๋ จ ์๋ฌธ (์ฐธ๊ณ 4)This form of multi-class N-pair loss helps us introduce the next (Supervised NT-Xent loss (Khosla et al. 2020), let's first look at the self-supervised version of NT-Xent loss. NT-Xent is coined by Chen et al. 2020 in the SimCLR paper and is short for "normalized temperature scaled cross entropy lossโฃโ. It is a modification of the multi-class N-pair loss with addition of the temperature parameter (๐) to scale the cosine similarities. Chen et al. found that an appropriate temperature parameter can help the model learn from hard negatives.) loss function.








