dann特征提取器t-sne降维可视化代码
时间: 2024-02-27 13:52:28 浏览: 168
特征值特征向量java源码-dANN-core:THEN-核心
以下是使用 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 数据集进行测试,可以替换成其他数据集。运行代码后将会显示出降维后的特征向量的散点图。
阅读全文