def __init__(self, b_L, W_L, group=NoGroup(), var_mask=None, input_node_type='linear', hidden_node_type='linear'): self.b_L, self.W_L = b_L, W_L self.group = group if var_mask is None: var_mask = [True] * (len(b_L) + len(W_L)) else: if len(var_mask) != len(b_L) + len(W_L): raise ValueError('number of variable mask not match.') self.var_mask = var_mask self.input_node_type, self.hidden_node_type = input_node_type, hidden_node_type #check data for i in xrange(len(W_L)): w = W_L[i] bl = b_L[i] br = b_L[i + 1] if w.shape != (len(bl), len(br)): raise ValueError('Matrix-bias shape mismatch.') if not len(self.b_L) == len(self.W_L) + 1: raise ValueError('# of layer weights and biases not match.')
def random_rbm(nin, nhid, group=NoGroup(), dtype='complex128', **kwargs): '''Get a random Restricted Boltzmann Machine''' if nhid % group.ng != 0: raise ValueError() nb = nhid / group.ng #data=(random.random(nin+nhid+nin*nhid/group.ng)-0.5)/2**nhid+1j*random.random(nin+nhid+nin*nhid/group.ng)-0.5j if dtype == 'complex128': data = (random.random(nin + nb + nin * nb) - 0.5) + 1j * random.random(nin + nb + nin * nb) - 0.5j data *= 1e-2 / abs(data) elif dtype == 'float64': data = (random.random(nin + nb + nin * nb) - 0.5) * 0.01 else: raise ValueError('unsupported dtype %s' % dtype) a = data[:nin] b = data[nin:nin + nb] W = data[nin + nb:].reshape([nin, nb]) return RBM(a, b, W, group=group, **kwargs)
def random_dbm(dims, group=NoGroup(), dtype='complex128', magnitude=2e-2, **kwargs): '''Get a random Restricted Boltzmann Machine''' num_layers = len(dims) b_L, W_L = [], [] if dtype == 'complex128': rng = lambda shape: random.uniform(-magnitude, magnitude, shape ) + 1j * random.uniform( -magnitude, magnitude, shape) elif dtype == 'float64': rng = lambda shape: random.uniform(-magnitude, magnitude, shape) else: raise ValueError('unsupported dtype %s' % dtype) for i in xrange(num_layers): b_L.append(rng(dims[i])) if i != 0: W_L.append(rng((dims[i - 1], dims[i]))) return DBM(b_L=b_L, W_L=W_L, group=group, **kwargs)
def __init__(self, a, b, W, group=NoGroup(), var_mask=[True, True, True], input_node_type='linear', hidden_node_type='linear', array_order='A'): if array_order == 'C': self.a, self.b, self.W = ascontiguousarray(a), ascontiguousarray( b), ascontiguousarray(W) elif array_order == 'F': self.a, self.b, self.W = asfortranarray(a), asfortranarray( b), asfortranarray(W) else: self.a, self.b, self.W = asarray(a), asarray(b), asarray(W) self.group = group self.var_mask = var_mask self.input_node_type, self.hidden_node_type = input_node_type, hidden_node_type if not len(self.a) * len(self.b) == prod(self.W.shape): raise ValueError()
def test_carleo(self): el=[] fname='data/eng-%s-%s%s.dat'%(self.nsite,self.model,'p' if self.periodic else 'o') #generate a random rbm and the corresponding vector v self.rbm=random_rbm(nin=self.nsite,nhid=self.nsite,group=TIGroup(self.nsite) if self.periodic else NoGroup()) rbm,info=SR(self.h,self.rbm,handler=self.vmc,niter=600,reg_params=('carleo',{'lambda0':100,'b':0.9}),optimizer=lambda f,g,p: -0.1*0.95**p*g) #rbm,info=SR(self.h,self.rbm,handler=self.vmc,niter=200,reg_params=('trunc',{'lambda0':0.2,'eps_trunc':1e-3})) #rbm,info=SR(self.h,self.rbm,handler=self.vmc,niter=200,optimizer=RMSProp(rho=0.9,rate=0.001),reg_params=('trunc',{'lambda0':0.2,'eps_trunc':1e-3})) #rbm,info=SR(self.h,self.rbm,handler=self.vmc,niter=200,optimizer=RMSProp(rho=0.9,rate=0.001),reg_params=('carleo',{'b':0.9,'lambda0':100})) #show error el=asarray(zip(*info['opl'])[1])/self.nsite e_true=-0.443663 err=abs(e_true-el) print 'E = %s, Error/site = %.4f'%(el,err) savetxt(fname,el)
def test_carleo2D(self): el=[] fname='data/eng-%s-%s%s.dat'%(self.nsite,self.model,'p' if self.periodic else 'o') #generate a random rbm and the corresponding vector v group=(TIGroup(self.nsite if not isinstance(self.h,HeisenbergH2D) else 2*[int(sqrt(self.nsite))])) if self.periodic else NoGroup() self.rbm=random_rbm(nin=self.nsite,nhid=self.nsite,group=group) self.rbm.var_mask=[False,True,True] #reg_params=('delta',{'lambda0':1e-4}) #reg_params=('trunc',{'lambda0':0.1,'eps_trunc':1e-5}) reg_params=('carleo',{'lambda0':100,'b':0.9}) #reg_params=('identity',{}) #reg_params=('pinv',{}) sr=SR(self.h,self.rbm,handler=self.vmc,reg_params=reg_params) #optimizer=RmsProp(wrt=self.rbm.dump_arr(),fprime=sr.compute_gradient,step_rate=1e-2,decay=0.9,momentum=0.9) #optimizer=Adam(wrt=self.rbm.dump_arr(),fprime=sr.compute_gradient,step_rate=1e-2) optimizer=GradientDescent(wrt=self.rbm.dump_arr(),fprime=sr.compute_gradient,step_rate=1e-2,momentum=0.5) print 'Running optimizer = %s, regularization = %s, nsite = %s, periodic = %s'%(optimizer,reg_params,self.nsite,self.periodic) self.rbm.a[...]=0 arr_old=self.rbm.dump_arr() for k,info in enumerate(optimizer): #if isinstance(optimizer,GradientDescent): optimizer.step_rate=0.2*0.99**k #optimizer.step_rate*=0.98 print 'Running %s-th Iteration.'%k ei=sr._opq_vals[1]/self.nsite print 'E/site = %s'%ei el.append(ei) #if k>50: #print 'setting momentum!' #optimizer.momentum=0.9 if k>500: break arr=self.rbm.dump_arr() print 'diff rate = %s(norm=%s)'%(norm(arr-arr_old)/norm(arr_old),norm(arr_old)) arr_old=arr savetxt(fname,el) assert_(err<0.05)
def test_sr(self,fakevmc=False): el=[] H=self.fv.get_H() e_true,v_true=eigh(H) #generate a random rbm and the corresponding vector v group=(TIGroup(self.nsite if not isinstance(self.h,HeisenbergH2D) else 2*[int(sqrt(self.nsite))])) if self.periodic else NoGroup() self.rbm=random_rbm(nin=self.nsite,nhid=self.nsite,group=group) #reg_params=('delta',{'lambda0':1e-4}) #reg_params=('trunc',{'lambda0':0.2,'eps_trunc':1e-3}) reg_params=('carleo',{'lambda0':100,'b':0.9}) #reg_params=('identity',{}) #reg_params=('pinv',{}) sr=SR(self.h,self.rbm,handler=self.vmc if not fakevmc else self.fv,reg_params=reg_params) #sr=SD(self.h,self.rbm,handler=self.vmc if not fakevmc else self.fv) optimizer=RmsProp(wrt=self.rbm.dump_arr(),fprime=sr.compute_gradient,step_rate=1e-3,decay=0.9,momentum=0.) #optimizer=Adam(wrt=self.rbm.dump_arr(),fprime=sr.compute_gradient,step_rate=1e-2) #optimizer=GradientDescent(wrt=self.rbm.dump_arr(),fprime=sr.compute_gradient,step_rate=1e-2,momentum=0.) arr_old=self.rbm.dump_arr() self.rbm.a[...]=0 for k,info in enumerate(optimizer): self.rbm.a[...]=0 print 'Running %s-th Iteration.'%k #optimizer.step_rate=0.3*0.96**k v=self.rbm.tovec(self.scfg); v=v/norm(v) ei=sr._opq_vals[1] err=abs(e_true[0]-ei)/(abs(e_true[0])+abs(ei)) print 'E/site = %s (%s), Error = %.4f%%'%(ei/self.nsite,e_true[0]/self.nsite,err*100) el.append(err) #if k>50:optimizer.momentum=0.8 arr=self.rbm.dump_arr() print 'diff rate = %s(norm=%s)'%(norm(arr-arr_old)/norm(arr_old),norm(arr_old)) arr_old=arr if k>500: break savetxt('data/err-%s%s.dat'%(self.nsite,'p' if self.periodic else 'o'),el) assert_(err<0.05)