對比損失(Contrastive Loss)與大模型:Contrastive Loss and Large Models (中英雙語)

對比損失(Contrastive Loss)與大模型:從原理到實踐

在現代深度學習中,對比損失(Contrastive Loss)是一種核心技術,尤其是在對比學習(Contrastive Learning)中被廣泛使用。通過最大化正樣本之間的相似度、最小化負樣本之間的相似度,對比損失有效地增強了表示學習的能力。在大模型時代,例如 CLIP、SimCLR、DINO 等,都依賴對比損失來推動模型性能的提升。


1. 什么是對比損失?

對比損失的定義

對比損失的目標是學習一個嵌入空間,在該空間中:

  • 正樣本對(positive pairs)(例如同一圖像的不同視角,或一張圖像和其對應的文本描述)距離盡可能近;
  • 負樣本對(negative pairs)(例如不相關的圖像和文本)距離盡可能遠。

公式化描述如下,以批量大小 ( N N N ) 的樣本為例:
L contrastive = ? 1 N ∑ i = 1 N log ? exp ? ( sim ( z i , z i + ) / τ ) ∑ j = 1 N exp ? ( sim ( z i , z j ) / τ ) \mathcal{L}_{\text{contrastive}} = -\frac{1}{N}\sum_{i=1}^N \log \frac{\exp(\text{sim}(z_i, z_i^+) / \tau)}{\sum_{j=1}^N \exp(\text{sim}(z_i, z_j) / \tau)} Lcontrastive?=?N1?i=1N?logj=1N?exp(sim(zi?,zj?)/τ)exp(sim(zi?,zi+?)/τ)?

  • ( z i z_i zi? ) 和 ( z i + z_i^+ zi+? ) 是正樣本對的嵌入向量。
  • ( sim ( ? , ? ) \text{sim}(\cdot, \cdot) sim(?,?) ) 通常是余弦相似度。
  • ( τ \tau τ ) 是溫度參數,控制分布的“平滑”程度。

2. 對比損失與 Logits 的關系

在實現對比損失時,Logits 是計算相似度和分布的核心中間變量。

  1. Logits 的構成

    • 對于每對樣本 ( ( z i , z j ) (z_i, z_j) (zi?,zj?) ),通過相似度函數(如點積或余弦相似度)得到 logits 值:
      logits i j = sim ( z i , z j ) \text{logits}_{ij} = \text{sim}(z_i, z_j) logitsij?=sim(zi?,zj?)
    • Logits 是未歸一化的相似性分數,直接參與 Softmax 分布的計算。
  2. Softmax 正規化

    • Logits 被用來計算每個正樣本對的條件概率:
      P ( i + ∣ i ) = exp ? ( logits i i + / τ ) ∑ j = 1 N exp ? ( logits i j / τ ) P(i^+|i) = \frac{\exp(\text{logits}_{ii^+} / \tau)}{\sum_{j=1}^N \exp(\text{logits}_{ij} / \tau)} P(i+i)=j=1N?exp(logitsij?/τ)exp(logitsii+?/τ)?
    • 這種概率分布直接反映正樣本與其他樣本的區分度。

3. 對比損失的典型應用:大模型中的案例

(1) CLIP:圖文對齊的典范

CLIP(Contrastive Language–Image Pretraining)是 OpenAI 提出的一個多模態模型,通過對比損失學習圖像和文本的對齊關系。

  • 用 Logits 做什么?
    • CLIP 將圖像和文本分別編碼為嵌入向量 ( z image z_{\text{image}} zimage? ) 和 ( z text z_{\text{text}} ztext? )。
    • 通過點積計算圖像和文本之間的相似度,得到一個 ( N × N N \times N N×N ) 的 logits 矩陣,其中每一行表示一個圖像與所有文本的相似度:
      logits i j = z image , i ? z text , j ∥ z image , i ∥ ∥ z text , j ∥ \text{logits}_{ij} = \frac{z_{\text{image}, i} \cdot z_{\text{text}, j}}{\|z_{\text{image}, i}\| \|z_{\text{text}, j}\|} logitsij?=zimage,i?∥∥ztext,j?zimage,i??ztext,j??
    • 使用對比損失,最大化正確圖文對的概率,同時最小化錯誤配對的概率。
(2) SimCLR:無監督表征學習

SimCLR 是一種經典的無監督對比學習方法,使用數據增強生成正樣本對。

  • 用 Logits 做什么?
    • SimCLR 將增強后的圖像編碼為嵌入,計算每對樣本之間的余弦相似度作為 logits。
    • 對每個樣本,計算其增強版本與其他樣本的對比分布,用對比損失優化模型。
(3) DINO:自監督視覺表征

DINO(Self-Distillation with No Labels)是一種自監督學習方法,通過對比損失優化不同視角的相似性。

  • 用 Logits 做什么?
    • DINO 使用教師網絡和學生網絡生成不同視角的嵌入,并通過 logits 計算兩者的相似性分布。
    • 使用對比損失對齊教師和學生的分布。

4. 實際例子:從 Logits 到對比損失

場景:CLIP 圖文對齊

假設有 2 張圖像和 2 條文本,編碼后的嵌入如下:

  • 圖像嵌入:
    z image , 1 = [ 1 , 0 ] , z image , 2 = [ 0 , 1 ] z_{\text{image}, 1} = [1, 0], \quad z_{\text{image}, 2} = [0, 1] zimage,1?=[1,0],zimage,2?=[0,1]
  • 文本嵌入:
    z text , 1 = [ 1 , 0 ] , z text , 2 = [ 0 , 1 ] z_{\text{text}, 1} = [1, 0], \quad z_{\text{text}, 2} = [0, 1] ztext,1?=[1,0],ztext,2?=[0,1]
Step 1: 計算 Logits

通過點積計算 logits:
logits = [ 1 0 0 1 ] \text{logits} = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} logits=[10?01?]

Step 2: Softmax 歸一化

將 logits 歸一化為概率分布(溫度參數 ( τ = 1 \tau = 1 τ=1 )):
P ( image 1 ∣ text 1 ) = exp ? ( 1 ) exp ? ( 1 ) + exp ? ( 0 ) ≈ 0.73 P(\text{image}_1|\text{text}_1) = \frac{\exp(1)}{\exp(1) + \exp(0)} \approx 0.73 P(image1?text1?)=exp(1)+exp(0)exp(1)?0.73
P ( image 1 ∣ text 2 ) = exp ? ( 0 ) exp ? ( 1 ) + exp ? ( 0 ) ≈ 0.27 P(\text{image}_1|\text{text}_2) = \frac{\exp(0)}{\exp(1) + \exp(0)} \approx 0.27 P(image1?text2?)=exp(1)+exp(0)exp(0)?0.27

Step 3: 計算損失

對每個正確的圖文對,計算交叉熵損失:
L = ? 1 2 ( log ? ( 0.73 ) + log ? ( 0.73 ) ) ≈ 0.63 \mathcal{L} = -\frac{1}{2}\big(\log(0.73) + \log(0.73)\big) \approx 0.63 L=?21?(log(0.73)+log(0.73))0.63


5. 洞見與未來方向

洞見
  1. Logits 是表示能力的核心
    Logits 直接反映模型對樣本間關系的編碼能力,其質量決定了對比損失的優化效果。

  2. 對比學習的魯棒性
    對比損失在無監督和多模態任務中表現優異,能夠有效學習出強大的嵌入表示。

未來方向

隨著大模型的發展,對比損失將進一步結合更多模態(例如音頻、視頻),并通過改進訓練策略(如溫度參數調節、自適應權重分配等)提升性能。


總結

對比損失是大模型時代的關鍵技術,從 CLIP 的圖文對齊到 SimCLR 的無監督學習,它通過 Logits 精確建模樣本之間的相似性和差異性。通過對比損失,我們可以在嵌入空間中實現對正樣本的高聚合和負樣本的有效區分,為模型的實際應用提供堅實的基礎。

Contrastive Loss and Large Models: Understanding Logits and Their Applications

Contrastive loss is a fundamental technique in deep learning, especially in contrastive learning. By maximizing the similarity between positive pairs and minimizing it between negative pairs, contrastive loss enhances a model’s ability to learn meaningful representations. In the era of large models, examples like CLIP, SimCLR, and DINO highlight the widespread use of contrastive loss to boost model performance.


1. What Is Contrastive Loss?

Definition

The goal of contrastive loss is to learn an embedding space where:

  • Positive pairs (e.g., different augmentations of the same image, or an image and its corresponding text) are close to each other.
  • Negative pairs (e.g., unrelated images and text) are far apart.

For a batch size ( N N N ), contrastive loss is defined as:
L contrastive = ? 1 N ∑ i = 1 N log ? exp ? ( sim ( z i , z i + ) / τ ) ∑ j = 1 N exp ? ( sim ( z i , z j ) / τ ) \mathcal{L}_{\text{contrastive}} = -\frac{1}{N}\sum_{i=1}^N \log \frac{\exp(\text{sim}(z_i, z_i^+) / \tau)}{\sum_{j=1}^N \exp(\text{sim}(z_i, z_j) / \tau)} Lcontrastive?=?N1?i=1N?logj=1N?exp(sim(zi?,zj?)/τ)exp(sim(zi?,zi+?)/τ)?

  • ( z i z_i zi? ) and ( z i + z_i^+ zi+? ) are embeddings of positive pairs.
  • ( sim ( ? , ? ) \text{sim}(\cdot, \cdot) sim(?,?) ) is a similarity function, often cosine similarity.
  • ( τ \tau τ ) is a temperature parameter controlling the sharpness of the distribution.

2. The Role of Logits in Contrastive Loss

Logits serve as the core intermediate variable in the computation of contrastive loss.

  1. Logits Construction:

    • For each sample pair ( ( z i , z j ) (z_i, z_j) (zi?,zj?) ), logits are derived from a similarity measure:
      logits i j = sim ( z i , z j ) \text{logits}_{ij} = \text{sim}(z_i, z_j) logitsij?=sim(zi?,zj?)
    • These logits represent unnormalized similarity scores.
  2. Softmax Normalization:

    • Logits are transformed into probabilities to compute the likelihood of positive pairs:
      P ( i + ∣ i ) = exp ? ( logits i i + / τ ) ∑ j = 1 N exp ? ( logits i j / τ ) P(i^+|i) = \frac{\exp(\text{logits}_{ii^+} / \tau)}{\sum_{j=1}^N \exp(\text{logits}_{ij} / \tau)} P(i+i)=j=1N?exp(logitsij?/τ)exp(logitsii+?/τ)?
    • This normalization helps the model focus on distinguishing between positive and negative pairs.

3. Applications of Contrastive Loss in Large Models

(1) CLIP: Aligning Images and Text

CLIP (Contrastive Language–Image Pretraining) is a multimodal model by OpenAI that uses contrastive loss to align images and text.

  • How CLIP Uses Logits:
    • CLIP encodes images and text into embeddings, ( z image z_{\text{image}} zimage? ) and ( z text z_{\text{text}} ztext? ).
    • Logits are computed via dot product similarity, producing a ( N × N N \times N N×N ) logits matrix where each row represents an image-text similarity distribution:
      logits i j = z image , i ? z text , j ∥ z image , i ∥ ∥ z text , j ∥ \text{logits}_{ij} = \frac{z_{\text{image}, i} \cdot z_{\text{text}, j}}{\|z_{\text{image}, i}\| \|z_{\text{text}, j}\|} logitsij?=zimage,i?∥∥ztext,j?zimage,i??ztext,j??
    • Contrastive loss maximizes the probability of the correct image-text pair while minimizing others.
(2) SimCLR: Self-Supervised Representation Learning

SimCLR is a self-supervised method that learns representations by maximizing agreement between augmentations of the same image.

  • How SimCLR Uses Logits:
    • Augmented image embeddings are compared using cosine similarity to compute logits.
    • Contrastive loss ensures that embeddings of the same image augmentation are close while embeddings of different images are far apart.
(3) DINO: Self-Distillation for Vision

DINO (Self-Distillation with No Labels) uses contrastive loss to align representations between teacher and student networks.

  • How DINO Uses Logits:
    • DINO computes logits for embeddings generated by the teacher and student networks.
    • Contrastive loss aligns the similarity distributions of the two networks across different augmentations of the same input.

4. Practical Example: From Logits to Contrastive Loss

Scenario: CLIP for Image-Text Alignment

Suppose we have two images and two texts. The embeddings are as follows:

  • Image embeddings:
    z image , 1 = [ 1 , 0 ] , z image , 2 = [ 0 , 1 ] z_{\text{image}, 1} = [1, 0], \quad z_{\text{image}, 2} = [0, 1] zimage,1?=[1,0],zimage,2?=[0,1]
  • Text embeddings:
    z text , 1 = [ 1 , 0 ] , z text , 2 = [ 0 , 1 ] z_{\text{text}, 1} = [1, 0], \quad z_{\text{text}, 2} = [0, 1] ztext,1?=[1,0],ztext,2?=[0,1]
Step 1: Compute Logits

Using dot product similarity:
logits = [ 1 0 0 1 ] \text{logits} = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix} logits=[10?01?]

Step 2: Apply Softmax

Convert logits to probabilities (with temperature ( τ = 1 \tau = 1 τ=1 )):
P ( text 1 ∣ image 1 ) = exp ? ( 1 ) exp ? ( 1 ) + exp ? ( 0 ) ≈ 0.73 P(\text{text}_1|\text{image}_1) = \frac{\exp(1)}{\exp(1) + \exp(0)} \approx 0.73 P(text1?image1?)=exp(1)+exp(0)exp(1)?0.73
P ( text 2 ∣ image 1 ) = exp ? ( 0 ) exp ? ( 1 ) + exp ? ( 0 ) ≈ 0.27 P(\text{text}_2|\text{image}_1) = \frac{\exp(0)}{\exp(1) + \exp(0)} \approx 0.27 P(text2?image1?)=exp(1)+exp(0)exp(0)?0.27

Step 3: Compute Loss

For correct image-text pairs, the loss is:
L = ? 1 2 ( log ? ( 0.73 ) + log ? ( 0.73 ) ) ≈ 0.63 \mathcal{L} = -\frac{1}{2}\big(\log(0.73) + \log(0.73)\big) \approx 0.63 L=?21?(log(0.73)+log(0.73))0.63


5. Insights and Future Directions

Insights
  1. Logits Are Critical for Representations:
    Logits reflect the raw similarity between embeddings and directly influence the optimization of contrastive loss.

  2. Robustness of Contrastive Learning:
    Contrastive loss excels in both supervised and unsupervised tasks by leveraging relative relationships between samples.

Future Directions

With the expansion of large models, contrastive loss can further integrate multimodal data (e.g., audio, video) and optimize embeddings for diverse tasks. Techniques like adaptive temperature scaling and hybrid contrastive objectives are promising areas for innovation.


Conclusion

Contrastive loss is a cornerstone of modern large-model training. By leveraging logits to compute similarity distributions, models like CLIP, SimCLR, and DINO achieve remarkable alignment and representation learning. The continued evolution of contrastive techniques will likely play a key role in the development of next-generation AI systems.

后記

2024年12月13日21點18分于上海,在GPT4o大模型輔助下完成。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/62875.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/62875.shtml
英文地址,請注明出處:http://en.pswp.cn/web/62875.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Java基礎學習:java常用啟動命令

一、java -jar 1、系統屬性傳遞 使用形式:java -DpathD:\jacoco -jar 獲取方式:System.getProperties() 2、系統參數傳遞 使用形式:java -jar application.jar --jacocoPathD:\tomcat 獲取方式:通過啟動方法入口main的參數arg…

Linux下SVN客戶端保存賬號密碼

參考文章:解決:Linux上SVN 1.12版本以上無法直接存儲明文密碼_linux svn 保存密碼-CSDN博客新版本svn使用gpg-agent存儲密碼-CSDN博客svn之無法讓 SVN 存儲密碼,即使配置設置為允許_編程設計_ITGUEST 方法一:明文方式保存密碼 首…

負載均衡oj項目:介紹

目錄 項目介紹 項目演示 項目介紹 負載均衡oj是一個基于bs模式的項目。 用戶使用瀏覽器向oj模塊提交代碼,oj模塊會在所有在線的后端主機中選擇一個負載情況最低的主機,將用戶的代碼提交給該主機,該主機進行編譯運行,將結果返回…

gateway 微服務的入口-筆記

本文屬于b站圖靈課堂springcloud筆記系列。講的好還不要錢,值得推薦。 為什么需要API網關? 客戶端多次請求不同的微服務,會增加客戶端代碼和配置的復雜性,維護成本比價高認證復雜,每個微服務可能存在不同的認證方式&…

vue2+element-ui實現多行行內表格編輯

效果圖展示 當在表格中點擊編輯按鈕時:點擊的行變成文本框且數據回顯可以點擊確定按鈕修改數據或者取消修改回退數據: 具體實現步驟 1. 行數據定義編輯標記 行數據定義編輯標記 當在組件中獲取到用于表格展示數據的方法中,針對每一行數據添加一個編輯標記 this.list.f…

安卓主板_MTK聯發科android主板方案

在當前智能設備的發展中,安卓主板的配置靈活性和性能優化顯得尤為重要。安卓主板的聯發科方案,在芯片上,搭載聯發科MTK6761、MT8766、MT6765、MT6762、MT8768、MT8390、MTK8370以及MT8788等型號,均基于64位的四核或八核架構設計。…

如何破解“不會寫作”的煩惱

在人生的諸多煩惱中,有一種煩惱或許不那么顯眼,卻常常如影隨形,讓人倍感困擾——那就是不會寫作的煩惱。這不僅僅是對那些以寫作為生的人而言,對于每一個需要在學習、工作或生活中以文字表達思想、情感的人來說,不會寫…

虛幻5描邊輪廓材質

很多游戲內都有這種描邊效果,挺實用也挺好看的,簡單復刻一下 效果演示: Linethickness可以控制輪廓線條的粗細 這樣連完,然后放到網格體細節的覆層材質上即可 可以自己更改粗細大小和顏色

修改docker源

在/etc/docker/daemon.json文件中寫入 { "registry-mirrors": [ "Welcome to nginx!" ] } 執行 systemctl daemon-reload systemctl restart docker docker info能夠看到源已經被替換 現在國內能夠使用的docker源經過測試只有Welcome to nginx! …

【JavaEE】網絡(2)

一、網絡編程套接字 1.1 基礎概念 【網絡編程】指網絡上的主機,通過不同的進程,以編程的方式實現網絡通信;當然,我們只要滿足進程不同就行,所以即便是同一個主機,只要是不同進程,基于網絡來傳…

RANS(Reynolds-Averaged Navier-Stokes) 湍流模型類型

RANS(Reynolds-Averaged Navier-Stokes) 湍流模型有多種不同的類型,除了標準的 kkk-ω 湍流模型,還有其他一些常用的湍流模型。RANS 模型的核心思想是對 Navier-Stokes 方程進行 雷諾平均,通過將流動場的瞬時變量分解為…

JS中this的值詳細講解以及面試指向練習

this 的值取決于它出現的上下文:函數、類或全局。 在函數內部,this 的值取決于函數如何被調用,this 是語言在函數體被執行時為你創建的綁定 對于典型的函數,this 的值是函數被訪問的對象。換句話說,如果函數調用的形…

2024年河南省高等職業教育技能大賽 大數據分析與應用賽項競賽方案

2024年河南省高等職業教育技能大賽 大數據分析與應用賽項競賽方案 一、賽項名稱 賽項名稱:大數據分析與應用 賽項編號:HN033 賽項組別:專業核心基本技能賽項 專業大類:電子與信息等 競賽形式:學生組(團體賽…

vue繞過rules自定義編寫動態校驗

今天犯了個低級錯誤,雖然走了很多彎路,但這個過程還是值得記錄一下 例子如下,有兩個輸入框: 第一個是套餐選擇下拉框,可以下拉選擇三個內容 第二個要根據上面的套餐選擇三個選項來決定怎么顯示,使用v-if&…

數字化招聘系統如何幫助企業實現招聘效率翻倍提升?

眾所周知,傳統的招聘方式已經難以滿足現代企業對人才的需求,而數字化招聘系統的出現,為企業提供了全新的解決方案。通過數字化招聘系統,企業可以自動化處理繁瑣的招聘流程,快速篩選合適的候選人,從而大幅提…

短視頻矩陣源碼開發部署全流程解析

在當今的數字化時代,短視頻已成為人們娛樂、學習和社交的重要方式。短視頻矩陣系統的開發與部署,對于希望在這一領域脫穎而出的企業和個人而言,至關重要。本文將詳細闡述短視頻矩陣源碼的開發與部署流程,并附上部分源代碼示例&…

華為云云原生中間件DCS DMS 通過中國信通院與全球IPv6測試中心雙重能力檢測

近日,中國信息通信研究院(以下簡稱“中國信通院”)與全球IPv6測試中心相繼宣布,華為云的分布式緩存服務(Distributed Cache Service,簡稱DCS)和分布式消息服務(Distributed Message …

關閉WPS在線功能資源和功能推薦

Kingsoft\WPS Office\12.1.0.18912\office6 選擇 【高級】 點擊 【確定】

Polars數據聚合與旋轉實戰教程

在這篇博文中,我們的目標是解決數據愛好者提出的一個常見問題:如何有效地從Polars DataFrame中創建匯總視圖,以便在不同時間段或類別之間輕松進行比較。我們將使用一個實際的數據集示例來探索實現這一目標的各種方法。 Polars簡介 Polars 是…

2024154讀書筆記|《帶著詩歌上街去》——我不長葉子,不開花,也不必要什么結果

2024154讀書筆記|《帶著詩歌上街去》——我不長葉子🌿,不開花🌼,也不必要什么結果 《帶著詩歌上街去》作者隔花人,作者很有巧思,在拍攝的照片上做詩,詩不是很有感覺,但是在墻上、風景…