T_SNE降维与可视化

在http://lvdmaaten.github.io/tsne/可以下载到matlab,python等多个版本的实现
我用matlab比较熟悉,所以采用的是matlab版本

T_SNE的主要思想是:

  1. 将高维数据转化为K-means data network
  2. 投影为二维或者三维数据,达到可视化的效果

可以看到经过分类训练过后的特征,聚类非常明显

这里写图片描述

wiki_image_ori

这里写图片描述

wiki_text_ori

这里写图片描述

wiki_image

这里写图片描述

wiki_text

这里写图片描述

3D example

这里写图片描述

% Load data
clear;
load('./WIKI/test_img.mat');
load('./WIKI/test_txt.mat');
load('./WIKI/test_lab.mat');
ind = randperm(size(test_img, 1));
train_image = test_img(ind(1:693),:);
train_text = test_txt(ind(1:693),:);
train_labels_text = test_lab(ind(1:693));
train_labels_image = test_lab(ind(1:693))+10;
% Set parameters
no_dims = 2;
initial_dims = 200;
perplexity = 30;

mappedimage = tsne(train_image, train_labels_image, 300, initial_dims, perplexity);
mappedtext = train_text;
train_X=[mappedimage;mappedtext];
train_labels=[train_labels_text;train_labels_image];
% Run t_SNE
mappedX = tsne(train_X, train_labels, no_dims, initial_dims, perplexity);
% Plot results
gscatter(mappedX(:,1), mappedX(:,2),train_labels,[0 0 0;1 1 0; 1 0 0;0 0 1;0 1 1;0 1 0;0.8 0.4 0.1;1 0.7 0.8;1 0.9 0.8;0.4 0.35 0.8;0 0 0;1 1 0; 1 0 0;0 0 1;0 1 1;0 1 0;0.8 0.4 0.1;1 0.7 0.8;1 0.9 0.8;0.4 0.35 0.8],'DDDDDDDDDD..........',10);

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用 PyTorch 和 scikit-learn 实现的 DANN 特征提取器 t-SNE 降维可视化代码: ```python import torch import torch.nn.functional as F import matplotlib.pyplot as plt from sklearn.manifold import TSNE # 加载数据集 train_loader = torch.utils.data.DataLoader( torchvision.datasets.MNIST('/files/', train=True, download=True, transform=torchvision.transforms.Compose([ torchvision.transforms.ToTensor(), torchvision.transforms.Normalize( (0.1307,), (0.3081,)) ])), batch_size=64, shuffle=True) # 定义 DANN 特征提取器 class FeatureExtractor(torch.nn.Module): def __init__(self): super(FeatureExtractor, self).__init__() self.conv1 = torch.nn.Conv2d(1, 64, kernel_size=5) self.conv2 = torch.nn.Conv2d(64, 50, kernel_size=5) self.conv2_drop = torch.nn.Dropout2d() self.fc1 = torch.nn.Linear(50*4*4, 100) self.fc2 = torch.nn.Linear(100, 10) def forward(self, x): x = F.relu(F.max_pool2d(self.conv1(x), 2)) x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2)) x = x.view(-1, 50*4*4) x = F.relu(self.fc1(x)) x = F.dropout(x, training=self.training) x = self.fc2(x) return x # 加载模型参数 model = FeatureExtractor() model.load_state_dict(torch.load('dann_feature_extractor.pth')) # 提取特征向量 features = [] labels = [] for data, target in train_loader: features.append(model(data).detach().numpy()) labels.append(target.detach().numpy()) features = np.concatenate(features, axis=0) labels = np.concatenate(labels, axis=0) # 使用 t-SNE 进行特征降维 tsne = TSNE(n_components=2, random_state=0) features_tsne = tsne.fit_transform(features) # 可视化降维后的特征向量 for i in range(10): plt.scatter(features_tsne[labels==i, 0], features_tsne[labels==i, 1], label=str(i)) plt.legend() plt.show() ``` 这里使用了 MNIST 数据集进行测试,可以替换成其他数据集。运行代码后将会显示出降维后的特征向量的散点图。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猴猴猪猪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值