-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInter_ModChe.py
351 lines (315 loc) · 13.4 KB
/
Inter_ModChe.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import os
import torch
import numpy as np
import matplotlib.pyplot as plt
from utils import readinfo, mymkdir, image2G, image2H, emb_hid_size_wrapper, LossPrepro, dircheck
from ent import updateH, H2G, G2H, constrModH, correlation, mutInfo, RecurGF
from rgfnn import Network, GLinear, GinvLinear, GcorLinear
import warnings
warnings.filterwarnings('ignore')
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
os.environ['OMP_NUM_THREADS'] = '1'
os.environ['OPENBLAS_NUM_THREADS'] = '1'
os.environ['MKL_NUM_THREADS'] = '1'
os.environ['VECLIB_MAXIMUM_THREADS'] = '1'
os.environ['NUMEXPR_NUM_THREADS'] = '1'
torch.set_num_threads(1)
np.random.seed(9)
data = 'Ins_12_d0.2'
Net = 'Simple_inter'
file = 'ENTANGLEMENT'
adj = None
index = np.random.randint(10000, size=1).tolist()
print('index={}'.format(index))
save, show, amount = True, True, None #4001
def labelchange(label, data):
if not data.endswith('MNIST'):
if label == 0:
return 'Normal'
elif label == 1:
return 'Topological'
elif label == 2:
return 'Metal'
else:
return label
else:
return label
def H_magnitude(H):
temp, count = torch.abs(H), torch.count_nonzero(H)
if save: f.write('\nmodel\nmax={}, mean={}\n'.format(torch.max(temp).item(), (torch.sum(temp) / count).item()))
print('model\nmax={}, mean={}'.format(torch.max(temp).item(), (torch.sum(temp) / count).item()))
print('Im(H)={}'.format(torch.sum(torch.abs(H.imag)).item()))
def plotEnergy(E, label, symbol):
plt.figure()
plt.xlim(-1, len(E) + 1)
plt.plot([-1, len(E) + 1], [0, 0], 'ko--', linewidth=1., markersize=0.1)
plt.scatter(np.arange(len(E)), E, c='r', s=10)
plt.xlabel('Count')
plt.ylabel('E')
label = labelchange(label, data)
plt.title('{} / {}'.format(label, symbol))
if save:
path = dircheck(data, file, Net)
plt.savefig('{}/spectrum_{}.jpg'.format(path, label))
if show: plt.show()
plt.close()
def plotOutputVSE(Es, DOS, label, symbol, loc):
plt.figure()
plt.axis([min(Es), max(Es), -np.max(DOS) / 50, np.max(DOS) * 1.02])
plt.plot([0., 0.], [-np.max(DOS) / 50, np.max(DOS) * 1.02], 'ko--', linewidth=0.5, markersize=0.1)
if len(DOS) == 1:
plt.plot(Es, DOS[0], 'bo-', linewidth=0.5, markersize=0.01, label='Topological')
else:
for i, D in enumerate(DOS):
plt.plot(Es, D, 'o-', linewidth=0.5, markersize=0.01, label=i)
plt.legend(loc=loc)
plt.xlabel('E')
plt.ylabel('Output')
label = labelchange(label, data)
plt.title('{} / {}'.format(label, symbol))
if save:
path = dircheck(data, file, Net)
plt.savefig('{}/QNNOutput_{}.jpg'.format(path, label))
if show: plt.show()
plt.close()
def plotAllDOS(E, label, symbol, input_size, embedding_size, hidden_size, output_size):
plt.figure()
plt.xlim(-1, len(E) + 1)
plt.bar(np.arange(input_size), E[:input_size], width=1.0, color='blue')
size = input_size + embedding_size
plt.bar(np.arange(input_size, size), E[input_size:size], width=1.0, color='green')
for hs in hidden_size:
plt.bar(np.arange(size, size + hs), E[size:size + hs], width=1.0)
size += hs
plt.bar(np.arange(size, size + output_size), E[size:], width=1.0, color='red')
plt.xlabel('Site')
plt.ylabel('LDOS')
label = labelchange(label, data)
plt.title('{} / {}'.format(label, symbol))
if save:
path = dircheck(data, file, Net)
plt.savefig('{}/AllLDOS_{}.jpg'.format(path, label))
if show: plt.show()
plt.close()
def EnergyCount(states, values, z, scale, n):
return (-torch.sum(torch.abs(states[:, -n:]) ** 2 / torch.abs(z - values.unsqueeze(1)) ** 2,
dim=2) * z.imag * scale).numpy()
# main functions
def spectrum(model, H0, labels, input_size, embedding_size, hidden_size):
# Compute DOS
with torch.no_grad():
H, modules = torch.zeros(input_size, input_size, dtype=torch.complex128), set()
for k in model.state_dict().keys():
try:
m = k[:k.index('.')]
except:
continue
if m not in modules:
modules.add(m)
module = model.get_submodule(m)
if isinstance(module, GLinear) or isinstance(module, GinvLinear) or isinstance(module, GcorLinear):
# print(m, module)
t, h = module.t_weight().type(H.dtype), module.h_bias().type(H.dtype)
if m == 'out':
t_out, h_out = t, h
else:
# eigs = torch.linalg.eigvalsh(h)
# plotEnergy(eigs, 'Model', m)
H = updateH(H, t, h)
# eigs = torch.linalg.eigvalsh(h_out)
# plotEnergy(eigs, 'Model', 'out')
H = updateH(H, t_out, h_out)
H_magnitude(H)
# eigs = torch.linalg.eigvalsh(H[input_size:, input_size:])
# plotEnergy(eigs, 'Model', 'QNN')
# print(eigs)
# Add H0
H = H.repeat(H0.shape[0], 1, 1)
if data.endswith('MNIST'):
H0 = image2H(H0, input_size)
H[:, :input_size, :input_size] = H0.squeeze(1)
values = torch.linalg.eigvalsh(H)
eigs = [[] for _ in range(len(labels))]
# temp = torch.linalg.eigvalsh(H0.squeeze(1))
# for i in range(len(temp)):
# eigs[i].append(temp[i])
# cur_size = input_size + embedding_size
# temp = torch.linalg.eigvalsh(H[:, :cur_size, :cur_size])
# for i in range(len(temp)):
# eigs[i].append(temp[i])
# for hs in hidden_size:
# cur_size += hs
# temp = torch.linalg.eigvalsh(H[:, :cur_size, :cur_size])
# for i in range(len(temp)):
# eigs[i].append(temp[i])
for i in range(len(values)):
eigs[i].append(values[i])
for e, label in zip(eigs, labels):
for i in range(len(e)):
plotEnergy(e[i], label, i)
if save: np.save('{}/spectrum_{}_{}.npy'.format(dircheck(data, file, Net), label, i), e[i].numpy())
def alldos(model, H0, labels, input_size, embedding_size, hidden_size, output_size, scale, adj=None):
z = model.z.cpu()
# Compute DOS_RGF
if data.endswith('MNIST'):
G0 = image2G(H0, input_size, adj, z=z if Net.endswith('-') else None)
else:
G0 = H2G(H0, z)
G = RecurGF(G0, model).squeeze(1)
G = torch.diagonal(G, dim1=-2, dim2=-1)
DOS_RGF = (G.imag * scale).numpy()
for d, label in zip(DOS_RGF, labels):
plotAllDOS(d, label, "RGF", input_size, embedding_size, hidden_size, output_size)
# # Compute DOS
# H = constrModH(input_size, model, double=double)
# H = H.repeat(H0.shape[0], 1, 1)
# H[:, :input_size, :input_size] = H0.squeeze(1)
# values, states = torch.linalg.eigh(H.type(torch.complex128))
#
# # Compute DOS_EC
# DOS_EC = EnergyCount(states, values, z, scale, H.shape[-1])
# for d, label in zip(DOS_EC, labels):
# plotAllDOS(d, label, 'Energy Count', input_size, embedding_size, hidden_size, output_size)
# # plotAllDOS(d[:input_size + embedding_size + 1], label, 'Energy Count', input_size, embedding_size, hidden_size, output_size)
#
# # Compute DOS_GF
# G = H2G(H, z)
# G = torch.diagonal(G, dim1=-2, dim2=-1)
# DOS_GF = (G.imag * scale).numpy()
# for d, label in zip(DOS_GF, labels):
# plotAllDOS(d, label, "Green's Function", input_size, embedding_size, hidden_size, output_size)
#
# # Compute DOS_NN
# model = model.to(device)
# model.eval()
# DOS_NN = model(H2G(H0, z).to(device))
# if DOS_NN.dim() == 1:
# DOS_NN = DOS_NN.reshape(DOS_GF.shape)
# DOS_NN = DOS_NN.data.cpu().numpy()
# print(DOS_NN, DOS_RGF[:, -2:])
def dos(model, H0, labels, input_size, output_size, scale, loc, device, adj=None):
z = model.z.cpu()
# # Compute DOS
# H = constrModH(input_size, model, double=double)
# H = H.repeat(H0.shape[0], 1, 1)
# if data.endswith('MNIST'):
# H0 = image2H(H0, input_size)
# H[:, :input_size, :input_size] = H0.squeeze(1)
Es = None if amount is None else torch.linspace(-1, 1, amount)
# # Compute DOS_EC
# values, states = torch.linalg.eigh(H)
# DOS_EC = EnergyCount(states, values, z, scale, output_size)
# print('DOS_EC=\n{}'.format(DOS_EC))
# DOS = []
# for E in Es:
# DOS.append(EnergyCount(states, values, E + z.imag * 1j, scale, output_size))
# DOS = np.stack(DOS, axis=2)
# for D, label in zip(DOS, labels):
# plotDOS(Es, D, label, 'Energy Count', loc)
# # Compute DOS_GF
# G = H2G(H, z)
# G = torch.diagonal(G, dim1=-2, dim2=-1)
# G = G.imag * scale
# DOS_GF = G[:, -output_size:].numpy()
# print('DOS_GF=\n{}'.format(DOS_GF))
# DOS = []
# for E in Es:
# G = torch.diagonal(H2G(H, E + z.imag * 1j), dim1=-2, dim2=-1).imag * scale
# DOS.append(G[:, -output_size:].numpy())
# DOS = np.stack(DOS, axis=2)
# for D, label in zip(DOS, labels):
# plotDOS(Es, D, label, "Green's Function", loc)
# Compute DOS_NN
model = model.to(device)
model.eval()
if data.endswith('MNIST'):
G0 = image2G(H0, input_size, adj, z=z if Net.endswith('-') else None).to(device)
else:
G0 = H2G(H0, z).to(device)
DOS_NN_0 = model(G0)
if DOS_NN_0.dim() == 1:
output = LossPrepro(DOS_NN_0, 'BCE', scale=True if scale < 0 else False)
output = torch.vstack((1 - output, output)).T
DOS_NN_0 = DOS_NN_0.unsqueeze(1)
else:
output = torch.softmax(DOS_NN_0, dim=1)
values, indices = torch.max(output, dim=1)
print('Prediction={}'.format(indices.tolist()))
print('Confidence={}'.format(values.tolist()))
if save:
f.write('Prediction={}\n'.format(indices.tolist()))
f.write('Confidence={}\n'.format(values.tolist()))
# print('Distance={}'.format(np.sum(np.abs(DOS_GF - DOS_NN))))
if Es is not None:
DOS = []
for E in Es:
if E == z.real:
DOS.append(DOS_NN_0.data.cpu().numpy())
continue
temp = E + z.imag * 1j
model.z = temp.to(device)
if data.endswith('MNIST'):
G0 = image2G(H0, input_size, adj, z=temp if Net.endswith('-') else None).to(device)
else:
G0 = H2G(H0, temp).to(device)
DOS_NN = model(G0)
if DOS_NN.dim() == 1:
DOS_NN = DOS_NN.unsqueeze(1)
DOS.append(DOS_NN.data.cpu().numpy())
DOS = np.stack(DOS, axis=2)
for D, label in zip(DOS, labels):
plotOutputVSE(Es, D, label, "FNN", loc)
if __name__ == "__main__":
model_path = 'models/{}/{}/{}'.format(data, file, Net)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
state_dict = torch.load('{}/model_best.pth.tar'.format(model_path), map_location="cpu")['state_dict']
if 'z' in state_dict.keys():
del state_dict['z']
input_size, output_size, embedding_size, hidden_size, z, hermi, diago, restr, scale, real, double \
= readinfo('{}/info.txt'.format(model_path))
if data.endswith('MNIST'):
dataset = torch.load('datasets/{}/processed/test.pt'.format(data))
H0 = dataset[0][index].unsqueeze(1) / 255
labels = dataset[1][index].numpy().tolist()
else:
data_path = 'datasets/{}/test'.format(data)
H0 = torch.load('{}/dataset.pt'.format(data_path))
labels = torch.load('{}/labels.pt'.format(data_path))
if data != 'Ins_12':
H0 = torch.cat((H0, torch.load('datasets/Ins_12/test/dataset.pt')), dim=0)
labels = torch.cat((labels, torch.load('datasets/Ins_12/test/labels.pt')), dim=0)
H0, labels = H0[index], labels[index].numpy().tolist()
if type(z) is float:
E = torch.load('{}/Es.pt'.format(data_path))[index]
z = E + z * 1j
if double: H0 = H0.type(torch.complex128)
print('labels={}'.format(labels))
temp = torch.abs(torch.flatten(H0, 1))
count = torch.count_nonzero(temp, dim=1)
print('H0\nmax={}\nmean={}'.format(torch.max(temp, dim=1)[0].numpy(), (torch.sum(temp, dim=1) / count).numpy()))
if save:
f = open('{}/magnitude.txt'.format(dircheck(data, file, Net)), 'w')
f.write('index={}\n'.format(index))
f.write('labels={}\n'.format(labels))
f.write('H0\nmax={}\nmean={}\n'.format(torch.max(temp, dim=1)[0].numpy(), (torch.sum(temp, dim=1) / count).numpy()))
model = Network(Net[:Net.index('_')], input_size, output_size, embedding_size, hidden_size, z, hermi, diago, restr,
real, scale=scale, double=double)
model.load_state_dict(state_dict, strict=False)
model.eval()
print(model)
embedding_size, hidden_size = emb_hid_size_wrapper(model)
loc = 'upper right' if scale else 'lower left'
scale = -1. / torch.pi if scale else 1.
order = 1
if adj is not None: adj = adj * torch.load('datasets/AdjMat.pt')
'''
main functions
'''
if not data.endswith('MNIST') or Net.endswith('-'):
spectrum(model, H0, labels, input_size, embedding_size, hidden_size)
else:
H_magnitude(constrModH(input_size, model, double=double))
alldos(model, H0, labels, input_size, embedding_size, hidden_size, output_size, scale, adj=adj)
dos(model, H0, labels, input_size, output_size, scale, loc, device, adj=adj)
if save: f.close()