Пример #1
0
void CTRNN::RandomizeCircuitOutput(double lb, double ub, RandomState &rs)
{
	for (int i = 1; i <= size; i++)
    SetNeuronOutput(i, rs.UniformRandom(lb, ub));
}
Пример #2
0
// Constructor: it is assumed that zms is already set with m>1
// If q == 0, then the current context is used
Cmodulus::Cmodulus(const PAlgebra &zms, long qq, long rt)
{
  assert(zms.getM()>1);
  bool explicitModulus = true;

  if (qq == 0) {
    q = zz_p::modulus();
    explicitModulus = false;
  }
  else
    q = qq;

  zMStar = &zms;
  root = rt;

  long mm;
  mm = zms.getM();
  m_inv = InvMod(mm, q);

  zz_pBak bak; 

  if (zms.getPow2()) {
    // special case when m is a power of 2

    assert( explicitModulus );
    bak.save();

    RandomState state;  SetSeed(conv<ZZ>("84547180875373941534287406458029"));
    // DIRT: this ensures the roots are deterministically generated
    //    inside the zz_pContext constructor
    context = zz_pContext(INIT_USER_FFT, q);
    state.restore();

    context.restore();

    powers.set_ptr(new zz_pX);
    ipowers.set_ptr(new zz_pX);


    long k = zms.getPow2();
    long phim = 1L << (k-1); 

    assert(k <= zz_pInfo->MaxRoot); 
    // rootTables get initialized 0..zz_pInfo->Maxroot

#ifdef FHE_OPENCL
    altFFTInfo = MakeSmart<AltFFTPrimeInfo>();
    InitAltFFTPrimeInfo(*altFFTInfo, *zz_pInfo->p_info, k-1);
#endif

    long w0 = zz_pInfo->p_info->RootTable[0][k];
    long w1 = zz_pInfo->p_info->RootTable[1][k];

    powers->rep.SetLength(phim);
    powers_aux.SetLength(phim);
    for (long i = 0, w = 1; i < phim; i++) {
      powers->rep[i] = w;
      powers_aux[i] = PrepMulModPrecon(w, q);
      w = MulMod(w, w0, q);
    }

    ipowers->rep.SetLength(phim);
    ipowers_aux.SetLength(phim);
    for (long i = 0, w = 1; i < phim; i++) {
      ipowers->rep[i] = w;
      ipowers_aux[i] = PrepMulModPrecon(w, q);
      w = MulMod(w, w1, q);
    }

  
    return;
  }

  if (explicitModulus) {
    bak.save(); // backup the current modulus
    context = BuildContext(q, NextPowerOfTwo(zms.getM()) + 1);
    context.restore();       // set NTL's current modulus to q
  }
  else
    context.save();

  if (root==0) { // Find a 2m-th root of unity modulo q, if not given
    zz_p rtp;
    long e = 2*zms.getM();
    FindPrimitiveRoot(rtp,e); // NTL routine, relative to current modulus
    if (rtp==0) // sanity check
      Error("Cmod::compRoots(): no 2m'th roots of unity mod q");
    root = rep(rtp);
  }
  rInv = InvMod(root,q); // set rInv = root^{-1} mod q

  // Allocate memory (relative to current modulus that was defined above).
  // These objects will be initialized when anyone calls FFT/iFFT.

  zz_pX phimx_poly;
  conv(phimx_poly, zms.getPhimX());

  powers.set_ptr(new zz_pX);
  Rb.set_ptr(new fftRep);
  ipowers.set_ptr(new zz_pX);
  iRb.set_ptr(new fftRep);
  phimx.set_ptr(new zz_pXModulus1(zms.getM(), phimx_poly));

  BluesteinInit(mm, conv<zz_p>(root), *powers, powers_aux, *Rb);
  BluesteinInit(mm, conv<zz_p>(rInv), *ipowers, ipowers_aux, *iRb);
}
Пример #3
0
void RandomUnitVector(TVector<double> &v) {GRS.RandomUnitVector(v);};
Пример #4
0
int ProbabilisticChoice(double prob) {return GRS.ProbabilisticChoice(prob);};
Пример #5
0
int UniformRandomInteger(int min,int max) {return GRS.UniformRandomInteger(min, max);};
Пример #6
0
double GaussianRandom(double mean, double variance) {return GRS.GaussianRandom(mean, variance);};
Пример #7
0
void BinaryReadRandomState(ifstream& bifs) {GRS.BinaryReadRandomState(bifs);};
Пример #8
0
double UniformRandom(double min,double max) { return GRS.UniformRandom(min, max);};
Пример #9
0
void BinaryWriteRandomState(ofstream& bofs) {GRS.BinaryWriteRandomState(bofs);};
Пример #10
0
void ReadRandomState(istream& is) {GRS.ReadRandomState(is);};
Пример #11
0
void WriteRandomState(ostream& os) {GRS.WriteRandomState(os);};
Пример #12
0
long GetRandomSeed(void) {return GRS.GetRandomSeed();};
Пример #13
0
void SetRandomSeed(long seed) {GRS.SetRandomSeed(seed);};
Пример #14
0
void CTRNN::randomizeWeights(double lb, double ub, RandomState &rs)
{
	for (int i = 0; i < size; i++)
		for (int j = 0; j < size; j++)
        setWeight(i, j, rs.UniformRandom(lb, ub));
}
Пример #15
0
void CTRNN::randomizeOutput(double lb, double ub, RandomState &rs)
{
	for (int i = 0; i < size; i++)
    setOutput(i, rs.UniformRandom(lb, ub));
}