-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathplot_contour_nnguide.py
More file actions
139 lines (109 loc) · 3.91 KB
/
plot_contour_nnguide.py
File metadata and controls
139 lines (109 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os
import json
import random
import argparse
import datetime
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from PIL import Image
from tqdm import tqdm
from torchvision import utils, transforms
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import make_moons
seed = 3 # pick any integer you like
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
## parameters to change
grid_size = 100
grid_range = [-2., 3.]
# grid_range = [-20., 21.]
contour_cmap = 'RdBu_r'
inclass_color = 'green'
outclass_color = 'orange'
cmap_level = 11
## sample data part
data_all, label_all = make_moons(n_samples=500, noise=0.1)
df = pd.DataFrame(dict(x=data_all[:,0], y=data_all[:,1], label=label_all))
## train network
dim_data = 2
dim_net = 240
network = nn.Sequential(*[nn.Linear(dim_data, dim_net),
nn.ReLU(),
nn.Linear(dim_net, dim_net),
nn.ReLU(),
nn.Linear(dim_net, 2)])
# nn.Softmax(dim=1)]
optim = optim.SGD(network.parameters(), lr=0.1)
crit = nn.CrossEntropyLoss()
data_all = torch.tensor(data_all).type(torch.float32)
label_all = torch.tensor(label_all).type(torch.LongTensor)
total_iters = 5000
crit.cuda()
network.cuda()
network.train()
for i in range(total_iters):
out = network(data_all.cuda())
loss = crit(out, label_all.cuda())
optim.zero_grad()
loss.backward()
optim.step()
_, _ = torch.max(out, dim=1)
## plot contour using grids
x_list = np.linspace(grid_range[0], grid_range[1], grid_size)
y_list = np.linspace(grid_range[0], grid_range[1], grid_size)
grid_x, grid_y = np.meshgrid(x_list, y_list)
grid_inputs = np.concatenate((grid_x.reshape(-1,1), grid_y.reshape(-1,1)), axis=1)
grid_inputs = torch.tensor(grid_inputs).type(torch.float32)
network.cpu()
network.eval()
feature_extractor = nn.Sequential(*list(network.children())[:-1])
# Evaluation with KNN using cosine distance
network.eval()
with torch.no_grad():
# Extract features from training data and grid points
train_features = feature_extractor(data_all)
grid_features = feature_extractor(grid_inputs)
# Normalize features for cosine similarity calculation
train_features_norm = F.normalize(train_features, p=2, dim=1)
grid_features_norm = F.normalize(grid_features, p=2, dim=1)
# Compute cosine similarity matrix
# (Cosine similarity = dot product of normalized vectors)
similarity = torch.mm(grid_features_norm, train_features_norm.t())
# For each grid point, find the top-k similarity scores and indices
k = 5
sims, indices = torch.topk(similarity, k=k, dim=1)
# Get the k-th similarity score for each grid point
sims = sims[:, :k-1].mean(dim=1) # k-1 because of
# sims = sims[:, k-1]
logits = network(grid_inputs)
probs = F.softmax(logits)
msps, _ = torch.max(probs, dim=1)
scores = msps * sims
# scores = (scores - scores.min()) / (scores.max() - scores.min() + 1e-5)
scores = torch.clamp((scores - torch.quantile(scores, 0.05)) /
(torch.quantile(scores, 0.95) - torch.quantile(scores, 0.05) + 1e-5),
0.0, 1.0)
scores = torch.clip(scores, 0, 1)
scores = scores.reshape(grid_size, grid_size).numpy()
fig, ax = plt.subplots()
levels = np.linspace(scores.min(), 1.0, cmap_level)
contour = ax.contourf(grid_x, grid_y, scores, levels=levels, cmap="RdBu_r", vmax=1, vmin=0.0)
fig.colorbar(contour, ax=[ax])
for data, label in zip(data_all, label_all):
if label == 0:
ax.scatter(data[0], data[1], c=inclass_color)
elif label == 1:
ax.scatter(data[0], data[1], c=outclass_color)
ax.set_title('Contour Plot')
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.savefig('./nnguide.jpg')
plt.close()