日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

t-SNE初学

發布時間:2023/12/4 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 t-SNE初学 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://www.datakit.cn/blog/2015/08/06/t_SNE.html

本文主要參考wikipedia,介紹t-SNE算法,以及python下的一些實現可視化應用。

目錄

  • 1、概述
  • 2、原理
    • 2.1基本原理
    • 2.2詳細過程
    • 2.4理由
  • 3、算法流程
  • 4、python試用

1、概述

最開始接觸t-SNE是在kaggle的比賽里,看到很多人提到t-SNE,用于降維和可視化。以前在可視化高維數據的時候,一般是降維到2維里可視化,降維的方法通常選擇PCA,但是PCA是線性的,效果比較一般。這里介紹的t-SNE(t-distributed stochastic neighbor embedding)是用于降維的一種機器學習算法,是由 Laurens van der Maaten 和 Geoffrey Hinton在08年提出來的,論文參見JMLR-Visualizing High-Dimensional Data Using t-SNE。t-SNE 是一種非線性降維算法,非常適用于高維數據降維到2維或者3維,進行可視化。

2、原理

2.1基本原理

t-SNE主要包括兩個步驟:第一、t-SNE構建一個高維對象之間的概率分布,使得相似的對象有更高的概率被選擇,而不相似的對象有較低的概率被選擇。第二,t-SNE在低維空間里在構建這些點的概率分布,使得這兩個概率分布之間盡可能的相似(這里使用KL散度(Kullback–Leibler divergence)來度量兩個分布之間的相似性)。

2.2詳細過程

具體來說,給定一個N個高維的數據x1,...,xNx1,...,xN(注意N不是維度!), t-SNE首先是計算概率pijpij,正比于xixixjxj之間的相似度(這種概率是我們自主構建的),公式如下:

pji=exp(?xi?xj2/(2σ2i))kiexp(?xi?xk2/(2σ2i))pj∣i=exp(?∣∣xi?xj∣∣2/(2σi2))∑k≠iexp(?∣∣xi?xk∣∣2/(2σi2)) pij=pjipij2Npij=pj∣ipi∣j2N

這里看到是用高斯核來構建了概率分布,那么怎么選擇高斯核中的σiσi呢?使用二分搜索得到條件概率分布的perplexity(后面再提到)。

t-SNE的目標是學習一個d維度的映射yi,...,yN,yiRdyi,...,yN,yi∈Rd, 這里定義yiyiyjyj之間的相似度qijqij如下:

qij=(1+yi?yj2)?1kl(1+yk?yl2)?1qij=(1+∣∣yi?yj∣∣2)?1∑k≠l(1+∣∣yk?yl∣∣2)?1

這里使用了學生分布來衡量低維度下點之間的相似度。最后,我們使用KL散度來度量Q和P之間的相似度:

C=KL(P)=ijpi,jlogpijqijC=KL(P∣∣)=∑i≠jpi,jlog?pijqij

之后使用梯度下降來最小化KL散度,梯度值如下:

dCdyi=4j(pij?qij)(yi?yj)(1+yi?yj2)?1dCdyi=4∑j(pij?qij)(yi?yj)(1+∣∣yi?yj∣∣2)?1

t-SNE幾乎在所有論文中的數據集上效果比 Sammon mapping, Isomap, and Locally Linear Embedding 要好。

2.4理由

  • 為什么選擇這樣的分布 論文中,開始使用了高斯核,之后改用了heavy-tailed t分布,因為這種t分布中?(1+yi?yj2)?1(1+∣∣yi?yj∣∣2)?1與低維空間里yi?yj∣∣yi?yj∣∣的二次成反比,能夠使得不相似的兩個對象被更好的分割
  • 高斯核中σiσi的選擇 高斯核中σiσi的選擇, 不同的i是對應了不同的σiσi,取值是用perplexity,當然可以直接看wiki和論文了,這里簡單的敘述下perplexity定義為:?Perp(Pi)=2H(Pi)Perp(Pi)=2H(Pi)?,其中,H(Pi)H(Pi)PiPi的信息熵,即H(Pi)=?jpjilog2p(ji)H(Pi)=?∑jpj∣ilog2?p(j∣i), 可以解釋為實際有效近鄰數。

3、算法流程

Simple version of t-Distributed Stochastic Neighbor Embedding

  • Data:?X=x1,...,xnX=x1,...,xn
  • 計算cost function的參數: perplexity Perp
  • 優化參數: 設置迭代次數T, 學習速率ηη, 動量α(t)α(t)
  • 目標結果是低維數據表示?YT=y1,...,ynYT=y1,...,yn
  • 開始優化
    • 計算在給定Perp下的條件概率pjipj∣i(參見上面公式)
    • 令?pij=pji+pij2npij=pj∣i+pi∣j2n
    • 用?N(0,10?4I)N(0,10?4I)?隨機初始化 Y
    • 迭代,從 t = 1 到 T, 做如下操作:
      • 計算低維度下的?qijqij(參見上面的公式)
      • 計算梯度(參見上面的公式)
      • 更新?Yt=Yt?1+ηdCdY+α(t)(Yt?1?Yt?2)Yt=Yt?1+ηdCdY+α(t)(Yt?1?Yt?2)
    • 結束
  • 結束

4、python試用

# Authors: Fabian Pedregosa <fabian.pedregosa@inria.fr> # Olivier Grisel <olivier.grisel@ensta.org> # Mathieu Blondel <mathieu@mblondel.org> # Gael Varoquaux # License: BSD 3 clause (C) INRIA 2011print(__doc__) from time import timeimport numpy as np import matplotlib.pyplot as plt from matplotlib import offsetbox from sklearn import (manifold, datasets, decomposition, ensemble, lda,random_projection)digits = datasets.load_digits(n_class=6) X = digits.data y = digits.target n_samples, n_features = X.shape n_neighbors = 30#---------------------------------------------------------------------- # Scale and visualize the embedding vectors def plot_embedding(X, title=None):x_min, x_max = np.min(X, 0), np.max(X, 0)X = (X - x_min) / (x_max - x_min)plt.figure()ax = plt.subplot(111)for i in range(X.shape[0]):plt.text(X[i, 0], X[i, 1], str(digits.target[i]),color=plt.cm.Set1(y[i] / 10.),fontdict={'weight': 'bold', 'size': 9})if hasattr(offsetbox, 'AnnotationBbox'):# only print thumbnails with matplotlib > 1.0shown_images = np.array([[1., 1.]]) # just something bigfor i in range(digits.data.shape[0]):dist = np.sum((X[i] - shown_images) ** 2, 1)if np.min(dist) < 4e-3:# don't show points that are too closecontinueshown_images = np.r_[shown_images, [X[i]]]imagebox = offsetbox.AnnotationBbox(offsetbox.OffsetImage(digits.images[i], cmap=plt.cm.gray_r),X[i])ax.add_artist(imagebox)plt.xticks([]), plt.yticks([])if title is not None:plt.title(title)#---------------------------------------------------------------------- # Plot images of the digits n_img_per_row = 20 img = np.zeros((10 * n_img_per_row, 10 * n_img_per_row)) for i in range(n_img_per_row):ix = 10 * i + 1for j in range(n_img_per_row):iy = 10 * j + 1img[ix:ix + 8, iy:iy + 8] = X[i * n_img_per_row + j].reshape((8, 8))plt.imshow(img, cmap=plt.cm.binary) plt.xticks([]) plt.yticks([]) plt.title('A selection from the 64-dimensional digits dataset')#---------------------------------------------------------------------- # Projection on to the first 2 principal componentsprint("Computing PCA projection") t0 = time() X_pca = decomposition.TruncatedSVD(n_components=2).fit_transform(X) plot_embedding(X_pca,"Principal Components projection of the digits (time %.2fs)" %(time() - t0))#---------------------------------------------------------------------- # Projection on to the first 2 linear discriminant componentsprint("Computing LDA projection") X2 = X.copy() X2.flat[::X.shape[1] + 1] += 0.01 # Make X invertible t0 = time() X_lda = lda.LDA(n_components=2).fit_transform(X2, y) plot_embedding(X_lda,"Linear Discriminant projection of the digits (time %.2fs)" %(time() - t0))#---------------------------------------------------------------------- # t-SNE embedding of the digits dataset print("Computing t-SNE embedding") tsne = manifold.TSNE(n_components=2, init='pca', random_state=0) t0 = time() X_tsne = tsne.fit_transform(X)plot_embedding(X_tsne,"t-SNE embedding of the digits (time %.2fs)" %(time() - t0))plt.show()

附錄:Manifold Learning 可以參考sklearn的文檔

總結

以上是生活随笔為你收集整理的t-SNE初学的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。