-
Notifications
You must be signed in to change notification settings - Fork 13
/
exatn_demo.py
314 lines (293 loc) · 6.88 KB
/
exatn_demo.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
# Exatn tutorial
# %%
import sys
sys.path.append("/home/plate/.local/lib")
import exatn
import numpy as np
#%%
def S(x):
""" Flatten array,
then reshape to inverse shape,
then transpose to match the original shape
"""
x = x.reshape(*reversed(x.shape))
x = x.transpose() # reverses order of axis by default
return x
def inv_S(x):
""" Flatten array,
then reshape to inverse shape,
then transpose to match the original shape
"""
x = x.transpose() # reverses order of axis by default
x = x.reshape(*reversed(x.shape))
return x
def idempotent_tensor_create(name, value: np.array):
""" Clears existing tensors before creation,
protects original arrays from exatn's modification
"""
if hasattr(exatn, 'tensorAllocated'):
# tensorAllocated is not included in official bindings, but you can use my fork
tensor_exists = exatn.tensorAllocated(name)
if tensor_exists:
exatn.destroyTensor(name)
else:
# will produce a warning on non-existent tensor
exatn.destroyTensor(name)
return exatn.createTensor(name, (np.array(value, copy=True)))
# %% [markdown]
"""
## Try to run simplest thing, d=1
"""
#%%
exatn.createTensor('R1', np.array([0.1, 0.2]))
exatn.createTensor('R2', np.array([0., 1. ]))
exatn.createTensor('SR1')
#%%
exatn.evaluateTensorNetwork('sum', 'SR1() = R1(a)*R2(a)')
#%%
exatn.getLocalTensor('SR1')
#%%
exatn.destroyTensor('R1')
exatn.destroyTensor('R2')
exatn.destroyTensor('SR1')
#%%
# %% [markdown]
"""
## Try to run next to simplest thing, d=2
"""
#%%
x, y = np.array([[0.2, 0.3], [.12, .67]]), np.array([0., 1.])
#%%
idempotent_tensor_create('R1', x)
idempotent_tensor_create('R2', y)
#%%
exatn.createTensor('SR1', [2], 0.)
#%%
exatn.evaluateTensorNetwork('sum', 'SR1(a) = R1(a,b)*R2(b)')
#%%
r_ex = exatn.getTensorData('SR1')
r_ex
#%%
expr1 = 'ij, j -> i'
r1 = np.einsum(expr1, x, y)
if not np.allclose(r1, r_ex):
print(f"Expression: `{expr1}` is not equivalent to exatn's")
else:
print( f"Expression: `{expr1}` is OK")
#%%
expr1 = 'ji, j -> i'
r1 = np.einsum(expr1, x, y)
if not np.allclose(r1, r_ex):
print( f"Expression: `{expr1}` is not equivalent to exatn's")
else:
print( f"Expression: `{expr1}` is OK")
# %% [markdown]
"""
## Try to check matrix multiplication
Use different dimensions for each axis to be sure that transpositions are correct.
"""
# %%
#N = 2
#a, b = np.random.randn(N, N+1), np.random.randn(N+1, N+2)
#a, b = [[1., 0], [0., 2.]], [[1., 2.,], [3., 4.]]
#%%
a = np.array([
[1., 0, 0],
[0., 1, 1]
], order='F')
b = np.array([
[1., 0, 3, 0],
[1, 1, 2, 2],
[-1, 1, -2, 2],
], order='F')
exatn.createTensor('C1', [2, 4], 0.)
#%%
idempotent_tensor_create('A1', a)
idempotent_tensor_create('B1', b)
# %%
exatn.evaluateTensorNetwork('test3', 'C1(a, c) = A1(a, b) * B1(b, c)')
# %%
c1_exatn = exatn.getLocalTensor('C1')
# %%
try:
assert np.allclose(c1_exatn, np.dot(a, b)), "direct multiple is not equivalent"
except AssertionError:
pass
# %%
try:
assert np.allclose(S(c1_exatn), np.dot(a, b)), "Transformed multiple is not equivalent"
except AssertionError:
pass
#%%
print('c = a*b\n', np.dot(a, b))
# %%
print('c exatn\n', c1_exatn)
#%%
exatn.destroyTensor('C1')
# %%
# %% [markdown]
"""
## Test more complex tensor network contraction
### Use tn.appendTensor
"""
#%%
x, y, z = [np.random.randn(*sh) for sh in [
(2, 3),
(3, 2, 2),
(2, 2, 2)
]]
# %%
idempotent_tensor_create('X', x)
idempotent_tensor_create('Y', y)
idempotent_tensor_create('Z', z)
# %%
tn = exatn.TensorNetwork('test')
tn.appendTensor(1, 'X')
tn.appendTensor(2, 'Y', [(1, 0)])
tn.appendTensor(3, 'Z', [(1, 0), (2,1)])
#%%
#tn.printItstd()
# %%
exatn.evaluate(tn)
# %%
result = tn.getTensor(0)
# %%
result_name = result.getName()
result_data = exatn.getTensorData(result_name)
# %%
einsum_data = np.einsum('ij,jkl,klm->im', x, y, z)
# %% [markdown]
"""
### Compare results to einsum
"""
# %%
assert np.allclose(einsum_data, result_data),\
f"Numpy result:\n {einsum_data} \n != exatn result: \n {result_data}"
# %% [markdown]
"""
### Compare input data
"""
# %%
exatn_x = exatn.getTensorData('X')
assert np.allclose(exatn_x, x), f"numpy: {x} != exatn: {exatn_x}"
# %% [markdown]
"""
### Run using `evaluateTensorNetwork`
"""
#%%
exatn.createTensor('F0', [2, 2], 0.)
# %%
exatn.createTensor('X0', np.array(x, copy=True))
exatn.createTensor('Y0', np.array(y, copy=True))
exatn.createTensor('Z0', np.array(z, copy=True))
# %%
exatn.evaluateTensorNetwork('test2', 'F0(a,b) = X(a,c) * Y(c,d,e) * Z(d,e,b)')
# %%
eval_data = exatn.getLocalTensor('F0')
eval_data
# %%
assert np.allclose(eval_data, result_data)
#%%
einsum_data
# %% [markdown]
"""
### Fix numerics with reshape-transform
"""
# %%
def S(x):
""" Flatten array,
then reshape to inverse shape,
then transpose to match the original shape
"""
x = x.reshape(*reversed(x.shape))
x = x.transpose() # reverses order of axis by default
return x
def inv_S(x):
""" Flatten array,
then reshape to inverse shape,
then transpose to match the original shape
"""
x = x.transpose() # reverses order of axis by default
x = x.reshape(*reversed(x.shape))
return x
#%%
einsum_data_adj = inv_S(np.einsum('ij,jkl,klm->im', S(x), S(y), S(z)))
#%%
assert np.allclose(einsum_data_adj, eval_data)
# %% [markdown]
"""
## Try to check matrix multiplication
Use different dimensions for each axis to be sure that transpositions are correct.
"""
# %%
N = 2
a, b = np.random.randn(N, N+1), np.random.randn(N+1, N+2)
exatn.createTensor('C1', [N, N+2], 0.)
exatn.createTensor('A1', np.array(a, copy=True))
exatn.createTensor('B1', np.array(b, copy=True))
# %%
exatn.evaluateTensorNetwork('test3', 'C1(a, c) = B1(b,c) * A1(a, b)')
# %%
c1_exatn = exatn.getLocalTensor('C1')
# %%
assert np.allclose(c1_exatn, a.dot(b))
# %%
a.dot(b)
# %%
c1_exatn
# %%
exatn.evaluateTensorNetwork('test4', 'C1(a, c) = B1(b,c) * A1(a, b)')
# %%
exatn.getLocalTensor('C1')
#%% [markdown]
"""
## Check tensors generated by exatn
"""
# %%
exatn.createTensor('Xr', [2, 3], 0)
exatn.createTensor('Yr', [3, 4], 0)
exatn.initTensorRnd('Xr')
exatn.initTensorRnd('Yr')
# %%
xr = exatn.getTensorData('Xr')
yr = exatn.getTensorData('Yr')
# %%
zr = np.dot(xr, yr)
# %%
exatn.createTensor('Zr', [2, 4], 0)
# %%
exatn.evaluateTensorNetwork('rnd', 'Zr(a, b) = Xr(a, c) * Yr(c, b)')
# %%
zr_exatn = exatn.getTensorData('Zr')
# %%
if not np.allclose(zr_exatn, zr):
print('Results do not match')
# %% [markdown]
"""
Fix results once again
"""
#%%
t = np.array([[1,0,0],[0, 1, 1]])
S(t)
# %%
inv_S(S(t))
#%%
S(S(S(t)))
#%%
S(np.arange(6).reshape(2, 3))
# %%
adj_zr = inv_S(np.dot(S(xr), S(yr)))
# %%
if not np.allclose(zr_exatn, adj_zr):
print('Adjusted result also doesnt match')
print('adj_zr', adj_zr)
print('exatn', zr_exatn)
else:
print('Adjusted result matches exatn')
# %%
S(np.array([[0, -1], [1, 0]]))
# %%
S(np.array([
[1, 2, 3, 4],
[5, 6, 7, 8]
]))