def check_rbm_numdiff(rbm, num_check=10): '''Check back propagation of RBM.''' print('Overall Test') diff = check_numdiff(rbm, num_check=num_check, eta=1e-3) for layer in rbm.layers: print('Testing %s' % layer) diff = check_numdiff(layer, num_check=num_check, eta=1e-3)
def __init__(self, sign_func, input_shape, nonlinear_list, powerlist=None, num_features=[4,4,4], eta0=0.2, eta1=0.2, NP=1, NC=1, K=None,\ itype='complex128',dtype0='complex128', dtype1='complex128', momentum=0., isym = False, usesum=False,poly_order=10, do_BN=False,is_unitary=False, **kwargs): if K is None: K=np.prod(input_shape) self._sign_func = sign_func self.num_features = num_features self.do_BN = do_BN self.poly_order = poly_order self.eta0, self.eta1 = eta0, eta1 self.isym = isym nsite=np.prod(input_shape) D = len(input_shape) ishape = input_shape def _is_unitary(inl): if is_unitary is True: return True elif is_unitary is False: return False else: return is_unitary[inl] super(WangLei6, self).__init__(**kwargs) # preprocessing if powerlist is not None: plnn = ParallelNN(axis=0) for power in powerlist: plnn.layers.append(functions.ConvProd(ishape, itype, powers=power, boundary='P', strides=(1,)*D)) if isym: plnn.layers.append(ANN(layers=[functions.Reverse(ishape, itype, axis=-1), functions.ConvProd(ishape, itype, powers=power, boundary='P', strides=(1,)*D)])) self.layers.append(plnn) nfo = len(plnn.layers) else: nfo = 1 inl = -1 # product layers. dtype = dtype0 eta=eta0 if NP!=0: self.add_layer(functions.Log,otype='complex128') for inl in range(NP): nfi, nfo = nfo, num_features[inl] self.add_layer(SPConv, weight=eta*typed_uniform(dtype, (nfo, nfi)+(K,)*D), bias=eta*typed_uniform(dtype, (nfo,)), boundary='P', strides=(1,)*D,is_unitary=_is_unitary(inl)) self.use_nonlinear(nonlinear_list[inl]) if NP!=0: self.add_layer(functions.Exp) # convolution layers. eta=eta1 dtype = self.layers[-1].otype for nfi, nfo in zip([nfo]+num_features[NP:NP+NC-1], num_features[NP:NP+NC]): inl = inl+1 self.add_layer(SPConv, weight=eta*typed_uniform(dtype, (nfo, nfi)+input_shape), bias=eta*typed_uniform(dtype, (nfo,)), boundary='P', strides=(1,)*D,is_unitary=_is_unitary(inl)) self.use_nonlinear(nonlinear_list[inl]) self.add_layer(functions.Filter, axes=(-1,), momentum=momentum) inl=inl+1 self.use_nonlinear(nonlinear_list[inl]) # linear layers. if usesum: self.add_layer(functions.Mean, axis=-1) inl=inl+1 self.use_nonlinear(nonlinear_list[inl]) else: for i,(nfi, nfo) in enumerate(zip(num_features[NP+NC-1:], num_features[NP+NC:]+[1])): self.add_layer(Linear, weight=eta*typed_uniform(dtype, (nfo, nfi)), bias=eta*typed_uniform(dtype, (nfo,)),var_mask=(1,1),is_unitary=_is_unitary(inl)) if do_BN: self.add_layer(functions.BatchNorm, axis=None, label='BN-%s'%i) self.add_layer(pfunctions.Poly, params=np.array([0,1.],dtype=dtype1), kernel='polynomial', factorial_rescale=True) inl=inl+1 self.use_nonlinear(nonlinear_list[inl]) print(check_numdiff(self))
def __init__(self, input_shape, num_features1=[12], num_features2=[], itype='float64', version='basic', eta=0.2, use_conv=False, preprocessing=False): self.num_features1, self.num_features2 = num_features1, num_features2 nsite = np.prod(input_shape) super(CaiZi, self).__init__() # create amplitude network if not preprocessing: net1 = ANN(layers=[ functions.Reshape( input_shape, itype=itype, output_shape=(1, ) + input_shape) ]) NF = 1 else: # preprocessing plnn = ParallelNN(axis=0) for power in [[1, 1], [1, 0, 1]]: plnn.layers.append( functions.ConvProd(input_shape, itype, powers=power, boundary='P', strides=(1, ))) NF = 2 net1 = ANN(layers=[plnn]) for i, (nfi, nfo) in enumerate( zip([np.prod(input_shape)] + num_features1, num_features1 + [1])): if use_conv[0] and i == 0: net1.add_layer(SPConv, weight=eta * typed_randn(self.itype, (nfo, NF, nsite)), bias=eta * typed_randn(self.itype, (nfo, ))) net1.add_layer(functions.Transpose, axes=(1, 0)) else: net1.add_layer(Linear, weight=eta * typed_randn(self.itype, (nfo, nfi)), bias=eta * typed_randn(self.itype, (nfo, ))) if version == 'basic': net1.add_layer(functions.Tanh) elif version == 'sigmoid': net1.add_layer(functions.Sigmoid) else: raise if use_conv[0]: net1.add_layer(functions.Mean, axis=0) net1.add_layer(functions.Reshape, output_shape=()) # create sign network net2 = ANN(layers=[ functions.Reshape( input_shape, itype=itype, output_shape=(1, ) + input_shape) ]) for i, (nfi, nfo) in enumerate( zip([np.prod(input_shape)] + num_features2, num_features2 + [1])): if use_conv[1] and i == 0: net2.add_layer(SPConv, weight=eta * typed_randn(self.itype, (nfo, 1, nsite)), bias=eta * typed_randn(self.itype, (nfo, ))) net2.add_layer(functions.Transpose, axes=(1, 0)) else: net2.add_layer(Linear, weight=eta * typed_randn(self.itype, (nfo, nfi)), bias=eta * typed_randn(self.itype, (nfo, ))) net2.add_layer(functions.Mul, alpha=np.pi) net2.add_layer(functions.Cos) if use_conv[1]: net2.add_layer(functions.Mean, axis=0) net2.add_layer(functions.Reshape, output_shape=()) # construct whole network self.layers.append(ParallelNN(layers=[net1, net2])) self.add_layer(functions.Prod, axis=0) print(check_numdiff(self))
I1, I2 = 28, 28 eta = 0.1 dtype = 'float32' W_fc1 = typed_randn(dtype, (F1, I1 * I2)) * eta b_fc1 = typed_randn(dtype, (F1, )) * eta # create an empty vertical network. ann = ANN() linear1 = Linear((-1, I1 * I2), dtype, W_fc1, b_fc1) ann.layers.append(linear1) ann.add_layer(functions.SoftMaxCrossEntropy, axis=1) ann.add_layer(functions.Mean, axis=0) return ann # build and print it ann = build_ann() print(ann) # random numerical differenciation check. # prepair a one-hot target. y_true = np.zeros(10, dtype='float32') y_true[3] = 1 assert (all(check_numdiff(ann, var_dict={'y_true': y_true}))) # graphviz support from poornn.visualize import viznn viznn(ann, filename='./mnist_simple.png')