使用python绘制t-sne图,并保存
一下是一个将que_im_features向量可视化的例子:
def emb_save(que_im_features,i):# 向量[75, 640, 11, 11], episode
import numpy as np
import pandas as pd
from sklearn import manifold
import matplotlib.pyplot as plt
que_im_features = que_im_features.mean(-1) #[75, 640,11,11]-> [75, 640]变成二维
que_im_features = que_im_features.mean(-1)
vec_list = que_im_features.cpu().numpy()
vec_array = np.array(vec_list)
tsne = manifold.TSNE(n_components=2, init='pca', random_state=42,perplexity=8,learning_rate=515,n_iter=50000).fit_transform(vec_array)
# print("tsne:",tsne)
# tsne 归一化, 这一步可做可不做
x_min, x_max = tsne.min(0), tsne.max(0)
tsne_norm = (tsne - x_min) / (x_max - x_min)
tsne_1 = tsne_norm[0:50]#每类50个
tsne_2 = tsne_norm[50:100]
plt.figure(figsize=(7, 7))#图大小
plt.scatter(tsne_1[:, 0], tsne_1[:, 1], 30, label='Brown Creeper')
# tsne_normal[i, 0]为横坐标,X_norm[i, 1]为纵坐标,1为散点图的面积, color给每个类别设定颜色
plt.scatter(tsne_2[:, 0], tsne_2[:, 1], 30, label='Rusty Blackbird')
plt.legend(loc="lower right",prop={'size': 14})#图例设置,位置右下,大小为14号
plt.xticks( size=16)#横轴字大小
plt.yticks( size=16)
plt.savefig("./Picture"+str(i)+".pdf",bbox_inches='tight')#保存为pdf
# bbox_inches='tight'输出图像周围空白比边距减小