-
Notifications
You must be signed in to change notification settings - Fork 53
/
util.h
244 lines (232 loc) · 5.93 KB
/
util.h
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
#pragma once
#include "itensor/util/print_macro.h"
#include "itensor/mps/mps.h"
#include "itensor/mps/sites/spinhalf.h"
#include "mllib/mnist.h"
using namespace itensor;
using std::min;
using std::max;
using std::vector;
using std::array;
using std::move;
using std::min;
using MPSArr = array<vector<MPS>,10>;
const auto Label = itensor::IndexType("Label");
ITensor inline
toverlap(MPS const& psi,
MPS const& img,
long c = 1)
{
auto N = psi.N();
auto W = img.A(N)*psi.A(N);
for(auto j = N-1; j >= c; --j)
{
W *= (img.A(j)*psi.A(j));
}
if(c > 1)
{
auto WL = img.A(1)*psi.A(1);
for(auto j = 2; j < c; ++j)
{
WL *= (img.A(j)*psi.A(j));
}
W *= WL;
}
return W;
}
template<typename C>
long
argmax(C const& c)
{
auto mel = c.front();
auto mn = 0;
for(auto n : range(c.size()))
{
if(c[n] > mel)
{
mel = c[n];
mn = n;
}
}
return mn;
}
template<typename C>
long
argmin(C const& c)
{
auto mel = c.front();
auto mn = 0;
for(auto n : range(c.size()))
{
if(c[n] < mel)
{
mel = c[n];
mn = n;
}
}
return mn;
}
template<typename ImgType, typename Func>
MPS
makeMPS(SiteSet const& sites,
ImgType const& img,
Func const& phi)
{
auto N = sites.N();
if(N != (decltype(N)) img.size()) throw std::runtime_error("Mismatched sizes");
auto d = sites(1).m();
auto psi = MPS(sites);
auto links = vector<Index>(N+4);
for(auto b : range(N+1)) links.at(b) = Index("l",1);
for(auto j : range1(N))
{
auto& ll = links.at(j-1);
auto& rl = links.at(j);
auto A = ITensor(ll,sites(j),rl);
for(auto n : range1(d))
{
A.set(ll(1),sites(j)(n),rl(1),phi(img(j),n));
}
psi.setA(j,A);
}
psi.Anc(N) *= setElt(links.at(N)(1));
psi.Anc(1) *= setElt(links.at(0)(1));
return psi;
}
template<typename ImgType>
ImgType const&
randImg(std::vector<ImgType> const& imgs,
long label)
{
auto max_tries = 1000;
for(auto t = 0; t < max_tries; ++t)
{
auto w = (long)imgs.size()*itensor::Global::random();
if(w < 0) w = 0;
if(w >= imgs.size()) w = imgs.size()-1;
auto& img = imgs.at(w);
if(img.label == label) return img;
}
Error(format("Did not find image with requested label after %d tries",max_tries));
return imgs.front();
}
template<size_t nlabel>
void
fullTest(MPS const& psi,
MPSArr const& set_test,
array<long,nlabel> labels)
{
Index L;
long cent = 0;
for(auto j : range1(psi.N()))
{
L = findtype(psi.A(j),Label);
if(L)
{
cent = j;
break;
}
}
if(!L || cent==0) Error("expected Label index at some site of psi MPS");
auto counts = array<long,10>{};
auto ninc = array<long,10>{};
auto tninc = 0;
auto tncor = 0;
auto nte = 0;
while(true)
{
bool got_state = false;
for(auto l : labels)
{
auto c = counts[l];
auto& testL = set_test.at(l);
if(c >= (long)testL.size()) continue;
counts[l]++;
++nte;
got_state = true;
auto& testimg = testL.at(c);
auto W = toverlap(psi,testimg,cent);
auto weights = array<Real,10>{};
{
//printf("L=%d weights",L);
for(auto k : range(10))
{
//printf(" %.0E",std::fabs(W.real(wi(1+k))));
weights[k] = std::fabs(W.real(L(1+k)));
}
//println();
auto pl = argmax(weights);
if(pl == l)
{
//println("Correct");
++tncor;
}
else
{
//println("Incorrect");
++tninc;
++ninc[l];
}
//PAUSE
}
}
if(!got_state) break;
}
printfln("%d/%d correct (%.2f%%), %d/%d incorrect (%.2f%%)",
tncor,nte,tncor*100./nte,tninc,nte,tninc*100./nte);
auto tot_counts = 0l;
for(auto l : range(10))
{
auto nt = counts[l];
tot_counts += nt;
if(nt == 0) continue;
auto ni = ninc[l];
auto nc = nt-ni;
printfln(" Digit %d %d/%d correct (%.2f%%), %d/%d incorrect (%.2f%%)",
l,nc,nt,nc*100./nt,ni,nt,ni*100./nt);
}
printfln("Total # test images = %d",tot_counts);
}
void inline
movePos(MPS & psi,
long np,
Args const& args = Args::global())
{
auto N = psi.N();
if(np < 1 || np > N) Error("New position out of range");
Index L;
long p = 0;
for(auto j : range1(N))
{
L = findtype(psi.A(j),Label);
if(L)
{
p = j;
break;
}
if(j == N) Error("Expected psi to have Label-type Index");
}
//printfln("Found o.c. at position %d",p);
auto moveBy = [&](long dp)
{
auto AA = psi.A(p)*psi.A(p+dp);
ITensor U,S,V;
if(1 < p && p < N) U = ITensor(findtype(psi.A(p),Site),uniqueIndex(psi.A(p),psi.A(p+dp),Link));
else U = ITensor(findtype(psi.A(p),Site));
svd(AA,U,S,V,args);
psi.setA(p,U);
psi.setA(p+dp,S*V);
psi.Aref(p+dp) *= norm(AA)/norm(psi.A(p+dp));
if(!findtype(psi.A(p+dp),Label)) Error("No Label index on new o.c.");
};
while(np > p)
{
moveBy(+1);
++p;
}
while(np < p)
{
moveBy(-1);
--p;
}
}