void CTRNN::RandomizeCircuitOutput(double lb, double ub, RandomState &rs) { for (int i = 1; i <= size; i++) SetNeuronOutput(i, rs.UniformRandom(lb, ub)); }
// 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); }
void RandomUnitVector(TVector<double> &v) {GRS.RandomUnitVector(v);};
int ProbabilisticChoice(double prob) {return GRS.ProbabilisticChoice(prob);};
int UniformRandomInteger(int min,int max) {return GRS.UniformRandomInteger(min, max);};
double GaussianRandom(double mean, double variance) {return GRS.GaussianRandom(mean, variance);};
void BinaryReadRandomState(ifstream& bifs) {GRS.BinaryReadRandomState(bifs);};
double UniformRandom(double min,double max) { return GRS.UniformRandom(min, max);};
void BinaryWriteRandomState(ofstream& bofs) {GRS.BinaryWriteRandomState(bofs);};
void ReadRandomState(istream& is) {GRS.ReadRandomState(is);};
void WriteRandomState(ostream& os) {GRS.WriteRandomState(os);};
long GetRandomSeed(void) {return GRS.GetRandomSeed();};
void SetRandomSeed(long seed) {GRS.SetRandomSeed(seed);};
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)); }
void CTRNN::randomizeOutput(double lb, double ub, RandomState &rs) { for (int i = 0; i < size; i++) setOutput(i, rs.UniformRandom(lb, ub)); }