Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
48 lines (43 loc) · 1.93 KB
/
Copy pathutils.py
File metadata and controls
48 lines (43 loc) · 1.93 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
import numpy as np
def load_data():
x_train = np.load('data/x_train.npy')
y_train = np.load('data/y_train.npy')
x_test = np.load('data/x_test.npy')
y_test = np.load('data/y_test.npy')
x_advs = np.load('data/adversarials.npy')
return x_train, y_train, x_test, y_test, x_advs
from matplotlib import pylab as plt
# %matplotlib inline
# set display defaults
plt.rcParams['figure.figsize'] = (10, 10) # large images
plt.rcParams['image.interpolation'] = 'nearest' # don't interpolate: show square pixels
plt.rcParams['image.cmap'] = 'gray' # use grayscale output rather than a (potentially misleading) color heatmap
# Plotting function
# plt.rc('font', **{'family' : 'sans-serif',
# 'weight' : 'bold',
# 'size' : 18})
# plt.rc('text', **{'usetex' : True})
def plot_mnist(X, y, X_embedded, name, min_dist=10.0):
fig = plt.figure(figsize=(10, 10))
ax = plt.axes(frameon=False)
plt.title("\\textbf{MNIST dataset} -- Two-dimensional "
"embedding of 70,000 handwritten digits with %s" % name)
plt.setp(ax, xticks=(), yticks=())
plt.subplots_adjust(left=0.0, bottom=0.0, right=1.0, top=0.9,
wspace=0.0, hspace=0.0)
plt.scatter(X_embedded[:, 0], X_embedded[:, 1],
c=y, marker="x")
if min_dist is not None:
from matplotlib import offsetbox
shown_images = np.array([[15., 15.]])
indices = np.arange(X_embedded.shape[0])
np.random.shuffle(indices)
for i in indices[:5000]:
dist = np.sum((X_embedded[i] - shown_images) ** 2, 1)
if np.min(dist) < min_dist:
continue
shown_images = np.r_[shown_images, [X_embedded[i]]]
imagebox = offsetbox.AnnotationBbox(
offsetbox.OffsetImage(X[i].reshape(28, 28),
cmap=plt.cm.gray_r), X_embedded[i])
ax.add_artist(imagebox)