-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.py
58 lines (47 loc) · 1.55 KB
/
test.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
import numpy as np
import scipy.io as sio
class Dict(dict):
"""
Example:
m = Dict({'first_name': 'Eduardo'}, last_name='Pool', age=24, sports=['Soccer'])
"""
def __init__(self, *args, **kwargs):
super(Dict, self).__init__(*args, **kwargs)
for arg in args:
if isinstance(arg, dict):
for k, v in arg.iteritems():
self[k] = v
if kwargs:
for k, v in kwargs.iteritems():
self[k] = v
def __getattr__(self, attr):
return self[attr]
def __setattr__(self, key, value):
self.__setitem__(key, value)
def __setitem__(self, key, value):
super(Dict, self).__setitem__(key, value)
self.__dict__.update({key: value})
def __delattr__(self, item):
self.__delitem__(item)
def __delitem__(self, key):
super(Dict, self).__delitem__(key)
del self.__dict__[key]
def visualize(x, pred, label):
import matplotlib.pyplot as plt
assert len(x) == len(pred)
xs = np.arange(len(x))
pred_top = np.argsort(pred)[::-1][:len(label)]
pred = np.exp(pred) / np.sum(np.exp(pred), axis=0) # softmax
plt.figure(0)
plt.plot(xs, x, 'r')
plt.plot(xs, pred, 'b')
plt.scatter(label - 1, np.ones(label.shape) * 0.5, alpha = 0.5)
plt.scatter(pred_top, pred[pred_top], alpha = 0.5)
plt.show()
# plt.savefig('vis.png')
result = sio.loadmat('./test_samples.mat')
pred = result["predict"]
label = result["label"]
X = result["X"]
for x, p, l, in zip(X, pred, label):
visualize(x,p,l)