t-SNE实现

论文里需要用到t-SNE对特征进行可视化,原理还没细看,但总体思路比较清晰,具体实现网上也有现成的代码。

对输出层的前一层特征提取出来,比如某个batch,维度是(b, 300),对应的target也提取出来,维度(b, k),注意这里需要把one-hot转化为类别形式,

注意,需要把整个测试集都过一遍,因此用列表来存每个batch的输出,然后再用torch.stach转化为tensor,最后再转化为numpy格式

x_tsne = []
y_tsne = []
for i, data in enumerate(val_unseen_loader):
#     data_time = time.time() - test_start_time
    val_unseen_inputs, val_unseen_labels = data['image'], data['target']
    ###
    val_unseen_inputs = val_unseen_inputs.to(device)
    val_unseen_labels = val_unseen_labels.to(device)
    with torch.no_grad():
        logits1, logits2, x_sem = model_OpenML(val_unseen_inputs, mode='val_zsl')
        targets = val_unseen_labels.clone()
        targets[targets==0] = 1
        targets[targets==-1] = 0
        x_tsne.append(x_sem)
        y_tsne.append(targets)

因为我是多标签数据,所以这里采取的思路是把多标签转化为多个单标签样本,也就是多个one-hot标签

# 把多标签转化为单标签,图像特征直接复制
t_features = []
t_labels = []
for i in range(len(x_tsne)):
    for j in range(x_tsne[i].shape[0]):
        label_index = torch.where(y_tsne[i][j]>0)[0]
        for lab in label_index:
            t_features.append(x_tsne[i][j].unsqueeze(0))
            t_labels.append(lab)

input_tsne = torch.cat(t_features, dim=0).cpu().numpy()

label_tsne = np.zeros(len(t_labels))
for i in range(len(t_labels)):
    label_tsne[i] = t_labels[i].cpu().numpy()

然后可以把input_tsne和label_tsne作为tSNE模型的输入进行训练

import numpy as np
import matplotlib.pyplot as plt
from sklearn import manifold,datasets

tsne = manifold.TSNE(n_components=2, init='pca', random_state=501, n_iter=500)
X_tsne = tsne.fit_transform(input_tsne)

这里参数再根据实际效果调节下

训练完开始画图:

import numpy as np
from sklearn.datasets import load_digits
from scipy.spatial.distance import pdist
# from sklearn.manifold.t_sne import _joint_probabilities
from scipy import linalg
from sklearn.metrics import pairwise_distances
from scipy.spatial.distance import squareform
from sklearn.manifold import TSNE
from matplotlib import pyplot as plt
import seaborn as sns
sns.set(rc={'figure.figsize':(11.7,8.27)})
# palette = sns.color_palette("bright", 17)
palette = sns.color_palette('tab20b_r', n_colors=17)
# palette = sns.color_palette('tab20b', n_colors=17)

sns.scatterplot(X_tsne[:,0], X_tsne[:,1], hue=label_tsne, legend='full', palette=palette)

参考:

https://www.cntofu.com/book/172/docs/54.md seaborn中文文档

https://blog.csdn.net/qq_42554007/article/details/82624418 seaborn调色

https://blog.csdn.net/weixin_41391619/article/details/115719116 seaborn调色

https://scikit-learn.org/stable/modules/generated/sklearn.manifold.TSNE.html sklearn t-SNE具体参数

https://zhuanlan.zhihu.com/p/148170862 t-SNE原理

posted @ 2022-03-30 11:25  嶙羽  阅读(651)  评论(0编辑  收藏  举报