Giter VIP home page Giter VIP logo

dimensionality-reduction's Introduction

Effective Dimensionality Reduction for Word Embeddings (Raunak et al., RepL4NLP 2019) の実装.

※ ライブラリとして整備する予定はないので,適宜コードを流用して使用してください.

環境設定

  1. WorksApplications/chiVev1.1 mc5 aunit (gensim) をダウンロードする.
  2. 必要なパッケージをインストールする.
    poetry env use YOUR_PYTHON_PATH  # 3.11
    poetry install
  3. 下記のコマンドによってテストを行う.
    poetry run pytest -v
    # OR
    poetry shell
    pytest -v

PPA

All-but-the-Top: Simple and Effective Postprocessing for Word Representations (Mu et al., ICLR 2018) に基づく.以下の説明は numpy に合わせて横ベクトルを中心に記述してある.

パラメーター

  • $V = \begin{pmatrix} v^{(1)}{}^\mathsf{T} \\ \vdots \\ v^{(n)}{}^\mathsf{T} \end{pmatrix}$:単語ベクトル $v^{(1)},\, \ldots,\, v^{(n)} \in \mathbb{R}^p$ をそれぞれ転置して積み重ねた行列.
  • $K$:PCA において固有ベクトルを分散を大きくする順に上位何位まで採用するか.

アルゴリズム

  1. $\displaystyle \overline{v} \leftarrow \frac{1}{n}\sum_{i = 1}^n v^{(i)}$.
  2. FOR $i \in \{1,\, \ldots,\, n\}$
    DO
         $\widetilde{v}^{(i)} \leftarrow v^{(i)} - \overline{v}$
    END DO
  3. $ \widetilde{V} \leftarrow \begin{pmatrix} \widetilde{v}^{(1)}{}^\mathsf{T} \\ \vdots \\ \widetilde{v}^{(n)}{}^\mathsf{T} \end{pmatrix}$.
  4. $W \leftarrow \mathrm{PCAeigenvectors}(\widetilde{V},\, K)$,ただし $W = \begin{pmatrix} w^{(1)}{}^\mathsf{T} \\ \vdots \\ w^{(K)}{}^\mathsf{T} \end{pmatrix}$, $w^{(i)}{}^\mathsf{T} \in \mathbb{R}^{1 \times p}$ for $i \in \{1,\, \ldots,\, K\}$ である.
  5. FOR $i \in \{1, \ldots, n\}$
    DO
         FOR $K \in \{1,\, \ldots,\, K\}$
         DO
             $a_{iK} \leftarrow w^{(K)}{}^\mathsf{T} \widetilde{v}^{(i)}$
         END DO
         $\hat{v}^{(i)}{}^\mathsf{T} \leftarrow \widetilde{v}^{(i)}{}^\mathsf{T} - (a_{i1} w^{(1)}{}^\mathsf{T} + \cdots + a_{iK} w^{(K)}{}^\mathsf{T})$
    END DO
  6. $\hat{V} \leftarrow \begin{pmatrix} \hat{v}^{(1)}{}^\mathsf{T} \\ \vdots \\ \hat{v}^{(n)}{}^\mathsf{T} \end{pmatrix}$.
  7. Return $\hat{V}$.

アルゴリズムに関する補足

ステップ 5 は $ \widetilde{V} = \begin{pmatrix} \widetilde{v}^{(1)}{}^\mathsf{T} \\ \vdots \\ \widetilde{v}^{(n)}{}^\mathsf{T} \end{pmatrix}$ とすると,行列を用いて

$$\begin{align} \hat{V} &= \widetilde{V} - \begin{pmatrix} a_{11} & \cdots & a_{1K} \\\ \vdots & \ddots & \vdots \\\ a_{n1} & \cdots & a_{nK} \end{pmatrix} W \\\ &= \widetilde{V} - \begin{pmatrix} w^{(1)}{}^\mathsf{T} \widetilde{v}^{(1)} & \cdots & w^{(K)}{}^\mathsf{T} \widetilde{v}^{(1)} \\\ \vdots & \ddots & \vdots \\\ w^{(1)}{}^\mathsf{T} \widetilde{v}^{(n)} & \cdots & w^{(K)}{}^\mathsf{T} \widetilde{v}^{(n)} \end{pmatrix} W \\\ &= \widetilde{V} - \begin{pmatrix} v^{(1)}{}^\mathsf{T} \\\ \vdots \\\ v^{(n)}{}^\mathsf{T} \end{pmatrix} \begin{pmatrix} w^{(1)} & \cdots & w^{(K)} \end{pmatrix} W \\\ &= \widetilde{V} - \widetilde{V} W^\mathsf{T} W \end{align}$$

のように計算できる.Python では

V_hat = V_tilde - V_tilde @ W.T @ W

のように書く.Mu et al. (2018) の記述にしたがうと

V_hat = V_tilde - V @ W.T @ W

とするのが正しいが,次元削減の実装 vyraun/Half-Size を見ると前者で計算しているようなので,それに合わせている.

次元削減

パラメーター

  • $V = \begin{pmatrix} v^{(1)}{}^\mathsf{T} \\ \vdots \\ v^{(n)}{}^\mathsf{T} \end{pmatrix}$:単語ベクトル $v^{(1)},\, \ldots,\, v^{(n)} \in \mathbb{R}^p$ をそれぞれ転置して積み重ねた行列.
  • $K$:PCA において固有ベクトルを分散を大きくする順に上位何位まで採用するか.
  • $d$:新しい次元.

アルゴリズム

  1. $X \leftarrow \mathrm{PPA}(V,\, K)$.
  2. $Y \leftarrow \mathrm{PCA}(X,\, d)$.
    ただし $Y$$w^{(1)},\, \ldots,\, w^{(d)} \in \mathbb{R}^{1 \times p}$ をPCAによって得られた固有ベクトルとしたとき, 次のように表される行列である:$Y = \begin{pmatrix} w^{(1)}{}^\top x^{(1)} & \cdots &w^{(d)}{}^\top x^{(1)} \\ \vdots & \ddots & \vdots \\ w^{(n)}{}^\top x^{(n)} & \cdots &w^{(d)}{}^\top x^{(n)} \end{pmatrix}.$
  3. $V \leftarrow \mathrm{PPA}(Y,\, K)$.
  4. Return $V$.

dimensionality-reduction's People

Contributors

pecorarista avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.