-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqd-vis.py
executable file
·80 lines (61 loc) · 2.89 KB
/
qd-vis.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
#!/usr/bin/env python3
import hypertools as hyp
import logging
import matplotlib.pyplot as plt
import numpy as np
import sys
logging.basicConfig(level=logging.INFO, \
format="[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s", \
datefmt="%H:%M:%S")
########################################################################################################################
# 1. Read Input
# 1a. Command line parameters
positionPattern = sys.argv[1]
logFileNames = sys.argv[2:]
# 1b. Parse log files
def parseLogFile(fname):
logging.info('Parsing {}'.format(fname))
lines = [ line for line in open(fname) if positionPattern in line ]
if not lines: logging.warning('No position vectors found in file {}'.format(fname))
lines = [ line[line.find(positionPattern):] for line in lines ]
lines = [ line[len(positionPattern):] for line in lines ]
lines = [ list(map(float, line.split(', '))) for line in lines ]
losses = np.array([ [ line[0] ] for line in lines ])
positions = np.array([ line[1:] for line in lines ])
return { 'name': fname, 'losses': losses, 'positions': positions }
logs = [ parseLogFile(fname) for fname in logFileNames ]
########################################################################################################################
# 2. Reduce dimensions
# We want to apply the same transformation to all points. So:
# 2a. Combine points into single list, and reduce
combinedPositions = np.concatenate([ log['positions'] for log in logs ])
combinedPositionsReduced = hyp.reduce(combinedPositions, ndims=2, reduce='PCA')
# 2b. Separate combined points
separatedPositions = []
for log in logs:
losses = log['losses']
positionsReduced = combinedPositionsReduced[:len(log['positions'])]
combinedPositionsReduced = combinedPositionsReduced[len(log['positions']):]
spa = np.concatenate((positionsReduced, losses), axis=1)
logging.info(spa.shape)
separatedPositions.append({ 'name': log['name'], 'prl': spa })
# 2c. Only keep large log files, and only after they stabilize
large = 200
separatedPositions = [ sp for sp in separatedPositions if len(sp['prl']) > large ]
def stabilize(sp):
name = sp['name']
prl = sp['prl']
return { 'name': name, 'prl': prl[large:] }
separatedPositions = [ stabilize(sp) for sp in separatedPositions ]
logging.info('Preserved {} traces'.format(len(separatedPositions)))
########################################################################################################################
# 3. Draw scatter plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for reducedLog in separatedPositions:
# ax.scatter(reducedLog['prl'][:, 0], reducedLog['prl'][:, 1], reducedLog['prl'][:, 2])
ax.plot(reducedLog['prl'][:, 0], reducedLog['prl'][:, 1], reducedLog['prl'][:, 2])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Loss')
plt.show()