-
Notifications
You must be signed in to change notification settings - Fork 24
/
utils.py
100 lines (80 loc) · 2.73 KB
/
utils.py
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
"""
Author(s): Wei Chen ([email protected])
"""
import os
import itertools
import time
import numpy as np
from sklearn.decomposition import PCA
from sklearn.metrics import mean_squared_error
import tensorflow as tf
import matplotlib
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def convert_sec(sec):
if sec < 60:
return "%.2f sec" % sec
elif sec < (60 * 60):
return "%.2f min" % (sec / 60)
else:
return "%.2f hr" % (sec / (60 * 60))
class ElapsedTimer(object):
def __init__(self):
self.start_time = time.time()
def elapsed_time(self):
return convert_sec(time.time() - self.start_time)
def gen_grid(d, points_per_axis, lb=0., rb=1.):
''' Generate a grid in a d-dimensional space
within the range [lb, rb] for each axis '''
lincoords = []
for i in range(0, d):
lincoords.append(np.linspace(lb, rb, points_per_axis))
coords = list(itertools.product(*lincoords))
return np.array(coords)
def mean_err(metric_list):
n = len(metric_list)
mean = np.mean(metric_list)
std = np.std(metric_list)
err = 1.96*std/n**.5
return mean, err
def visualize(X):
X = X.reshape((X.shape[0], -1))
pca = PCA(n_components=3)
F = pca.fit_transform(X)
# Reconstruction error
X_rec = pca.inverse_transform(F)
err = mean_squared_error(X, X_rec)
print('Reconstruct error: {}'.format(err))
# 3D Plot
fig3d = plt.figure()
ax3d = fig3d.add_subplot(111, projection = '3d')
# Create cubic bounding box to simulate equal aspect ratio
max_range = np.array([F[:,0].max()-F[:,0].min(), F[:,1].max()-F[:,1].min(), F[:,2].max()-F[:,2].min()]).max()
Xb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][0].flatten() + 0.5*(F[:,0].max()+F[:,0].min())
Yb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][1].flatten() + 0.5*(F[:,1].max()+F[:,1].min())
Zb = 0.5*max_range*np.mgrid[-1:2:2,-1:2:2,-1:2:2][2].flatten() + 0.5*(F[:,2].max()+F[:,2].min())
ax3d.scatter(Xb, Yb, Zb, c='white', alpha=0)
ax3d.scatter(F[:,0], F[:,1], F[:,2])
matplotlib.rcParams.update({'font.size': 22})
# ax3d.set_xticks([])
# ax3d.set_yticks([])
# ax3d.set_zticks([])
plt.show()
def safe_remove(filename):
if os.path.exists(filename):
os.remove(filename)
def create_dir(path):
if not os.path.isdir(path):
os.mkdir(path)
def get_n_vars():
n_vars = 0
for v in tf.global_variables():
n_vars += np.prod(v.get_shape().as_list())
return n_vars
def train_test_plit(X, split=0.8):
# Split training and test data
N = X.shape[0]
split = int(N*split)
X_train = X[:split]
X_test = X[split:]
return X_train, X_test