Exemple #1
0
void TLSHash::Remove(TFltV Datum) {
  int Id = DataV.SearchForw(Datum);
  if (Id == -1) { return; }
  DataV[Id] = TFltV();
  for (int i=0; i<SigBucketVHV.Len(); i++) {
    THash<TInt, TIntV> &SigBucketVH = SigBucketVHV[i];
    TVec<TInt> SigV;
    SigBucketVH.GetKeyV(SigV);

    for (int j=0; j<SigV.Len(); j++) {
      TIntV &BucketV = SigBucketVH.GetDat(SigV[j]);
      int Ind = BucketV.SearchBin(Id);
      if (Ind != -1) {
        BucketV.Del(Ind);
      }
    }
  }
}
Exemple #2
0
void em_multi(ExamMgr& ExM) {
	TExeTm tm;
	TFltV Alphas(ExM.CPU), ThVs[ExM.CPU];
	for (int i=0; i<ExM.CPU; i++) ThVs[i] = TFltV(ExM.W+1);
	std::vector<std::thread> threads;
	for (int i=0; i<ExM.CPU; i++) threads.emplace_back([i, &ExM, &Alphas, &ThVs] { em_sub(i, ExM, Alphas[i], ThVs[i]); });
	for(std::thread& t: threads) t.join();
	for (int n=1; n<ExM.CPU; n++) Alphas[0] += Alphas[n];
	Alphas[0] /= ExM.CPU;
	for (int i=0; i<=ExM.W; i++) {
		for (int n=1; n<ExM.CPU; n++) ThVs[0][i] += ThVs[n][i];
		ThVs[0][i] /= ExM.CPU;
	}
	if (ExM.TrimTail) ExM.TrimTailNTh(ThVs[0], Alphas[0]);
	const TStr OFnm = ExM.GetBNTHFNm();
	BIO::SaveFltVWithIdx(ThVs[0], OFnm, TStr::Fmt("# Nodes: %d\n# Repeated: %d\n# Avg time cost: %.2f secs.\n# Alpha: %.6e",
			ExM.N, ExM.GetRpt(), tm.GetSecs()/ExM.GetRpt(), Alphas[0].Val));
	printf("Saved to %s\n", OFnm.CStr());
}
//////////////////////////////////////////////////////////////////////////
// Partial-Gram-Schmidt
TPartialGS::TPartialGS(PSVMTrainSet BigSet, const int& Dim, const double& Eps) {
    IAssert(Dim <= BigSet->Len() && 0.0 <= Eps && Eps < 1.0);
    int Len = BigSet->Len();

    TVec<TKeyDat<TFlt, TBool> > NiV(Len);
    for (int i = 0; i < Len; i++) {
        //NiV[i].Key = BigSet->DotProduct(i, i);
        NiV[i].Key = BigSet->GetNorm2(i);
        NiV[i].Dat = false;
        IAssertR(NiV[i].Key.Val > 0.0 && _isnan(NiV[i].Key.Val) == 0, 
                 TInt::GetStr(i) + TStr(":") + TFlt::GetStr(NiV[i].Key));
    }
    R.Gen(Dim, 0);
    //for (i = 0; i < Dim; i++) R[i].Gen(Len-i);
    IdV.Gen(Len);
    for (int i = 0; i < Len; i++) IdV[i] = i;

    TFltV BlufV(Dim, 0); int max = -1;
    for (int j = 0; j < Dim; j++) {
        // find element with bigest residual norm
        max = -1;
        for (int t = 0, l = Len; t < l; t++)
            if (!NiV[t].Dat && (max == -1 || NiV[t].Key > NiV[max].Key)) max = t;

        // if max residual norm is reached
        if (NiV[max].Key.Val < Eps) break;
        //printf("(%.2f)", NiV[max].Key.Val);

        // permute j-th and max-th column of R
        NiV[max].Dat = true;
        int mid = IdV.SearchForw(max, j);
        { int tmp = IdV[j]; IdV[j] = max; IdV[mid] = tmp; }
        for (int t = 0; t < j; t++) {
            double tmp = R[t][j-t];
            R[t][j-t] = R[t][mid-t];
            R[t][mid-t] = tmp;
        }

        // calculate j-th row of R and update NiV (residual norms)
        if (-0.001 < NiV[max].Key.Val && NiV[max].Key.Val < 0) NiV[max].Key.Val = 0.0;
        IAssertR(NiV[max].Key.Val >= 0.0, TInt::GetStr(j) + TStr(":") + TFlt::GetStr(NiV[max].Key.Val));
        IAssert(R.Len() == j);
        R.Add(TFltV()); R[j].Gen(Len-j); // NEW
        R[j][0] = sqrt(NiV[max].Key.Val);
        BlufV.Add(NiV[IdV[j]].Key.Val);
        for (int i = j+1; i < Len; i++) {
            double RR = BigSet->DotProduct(IdV[i], IdV[j]);
            for (int t = 0; t < j; t++)
                RR -= R[t][j-t] * R[t][i-t];
            IAssertR(NiV[IdV[j]].Key.Val>0, TInt::GetStr(i));
            RR /= sqrt(NiV[IdV[j]].Key.Val);
            IAssertR(_isnan(RR) == 0, TInt::GetStr(IdV[j]) + TStr(":") + TFlt::GetStr(NiV[IdV[j]].Key.Val));
            R[j][i-j] = RR;
            NiV[IdV[i]].Key -= RR*RR;
        }
    }

    if (max == -1) max = 0;
    printf("stoped at %d/%d with residual norm %.3f\n", R.Len(), BigSet->Len(), NiV[max].Key.Val);

    NormV.Gen(Len);
    VecNormV.Gen(Len);
    for (int i = 0; i < Len; i++) {
        NormV[i] = NiV[IdV[i]].Key;
        VecNormV[i] = GetKernel(i,i);
    }
}