本文整理汇总了C++中TIntFltH::Gen方法的典型用法代码示例。如果您正苦于以下问题:C++ TIntFltH::Gen方法的具体用法?C++ TIntFltH::Gen怎么用?C++ TIntFltH::Gen使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TIntFltH
的用法示例。
在下文中一共展示了TIntFltH::Gen方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: GetEigenVectorCentr
void GetEigenVectorCentr(const PUNGraph& Graph, TIntFltH& EigenH, const double& Eps, const int& MaxIter) {
const int NNodes = Graph->GetNodes();
EigenH.Gen(NNodes);
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
EigenH.AddDat(NI.GetId(), 1.0/NNodes);
IAssert(NI.GetId() == EigenH.GetKey(EigenH.Len()-1));
}
TFltV TmpV(NNodes);
double diff = TFlt::Mx;
for (int iter = 0; iter < MaxIter; iter++) {
int j = 0;
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
TmpV[j] = 0;
for (int e = 0; e < NI.GetOutDeg(); e++) {
TmpV[j] += EigenH.GetDat(NI.GetOutNId(e)); }
}
double sum = 0;
for (int i = 0; i < TmpV.Len(); i++) {
EigenH[i] = TmpV[i];
sum += EigenH[i];
}
for (int i = 0; i < EigenH.Len(); i++) {
EigenH[i] /= sum; }
if (fabs(diff-sum) < Eps) { break; }
//printf("\tdiff:%f\tsum:%f\n", fabs(diff-sum), sum);
diff = sum;
}
}
示例2: GetWeightedPageRankMP1
int GetWeightedPageRankMP1(const PNEANet Graph, TIntFltH& PRankH, const TStr& Attr, const double& C, const double& Eps, const int& MaxIter) {
if (!Graph->IsFltAttrE(Attr)) return -1;
TFltV Weights = Graph->GetFltAttrVecE(Attr);
int mxid = Graph->GetMxNId();
TFltV OutWeights(mxid);
Graph->GetWeightOutEdgesV(OutWeights, Weights);
/*for (TNEANet::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
OutWeights[NI.GetId()] = Graph->GetWeightOutEdges(NI, Attr);
}*/
/*TIntFltH Weights;
for (TNEANet::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
Weights.AddDat(NI.GetId(), Graph->GetWeightOutEdges(NI, Attr));
}*/
const int NNodes = Graph->GetNodes();
TVec<TNEANet::TNodeI> NV;
//const double OneOver = 1.0/double(NNodes);
PRankH.Gen(NNodes);
for (TNEANet::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
NV.Add(NI);
PRankH.AddDat(NI.GetId(), 1.0/NNodes);
//IAssert(NI.GetId() == PRankH.GetKey(PRankH.Len()-1));
}
TFltV TmpV(NNodes);
for (int iter = 0; iter < MaxIter; iter++) {
#pragma omp parallel for schedule(dynamic,10000)
for (int j = 0; j < NNodes; j++) {
TNEANet::TNodeI NI = NV[j];
TmpV[j] = 0;
for (int e = 0; e < NI.GetInDeg(); e++) {
const int InNId = NI.GetInNId(e);
const TFlt OutWeight = OutWeights[InNId];
int EId = Graph->GetEId(InNId, NI.GetId());
const TFlt Weight = Weights[Graph->GetFltKeyIdE(EId)];
if (OutWeight > 0) {
TmpV[j] += PRankH.GetDat(InNId) * Weight / OutWeight; }
}
TmpV[j] = C*TmpV[j]; // Berkhin (the correct way of doing it)
//TmpV[j] = C*TmpV[j] + (1.0-C)*OneOver; // iGraph
}
double diff=0, sum=0, NewVal;
#pragma omp parallel for reduction(+:sum) schedule(dynamic,10000)
for (int i = 0; i < TmpV.Len(); i++) { sum += TmpV[i]; }
const double Leaked = (1.0-sum) / double(NNodes);
#pragma omp parallel for reduction(+:diff) schedule(dynamic,10000)
for (int i = 0; i < PRankH.Len(); i++) { // re-instert leaked PageRank
NewVal = TmpV[i] + Leaked; // Berkhin
//NewVal = TmpV[i] / sum; // iGraph
diff += fabs(NewVal-PRankH[i]);
PRankH[i] = NewVal;
}
if (diff < Eps) { break; }
}
return 0;
}
示例3: GetEigenVectorCentr
void GetEigenVectorCentr(const PUNGraph& Graph, TIntFltH& NIdEigenH, const double& Eps, const int& MaxIter) {
const int NNodes = Graph->GetNodes();
NIdEigenH.Gen(NNodes);
// initialize vector values
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
NIdEigenH.AddDat(NI.GetId(), 1.0 / NNodes);
IAssert(NI.GetId() == NIdEigenH.GetKey(NIdEigenH.Len() - 1));
}
TFltV TmpV(NNodes);
for (int iter = 0; iter < MaxIter; iter++) {
int j = 0;
// add neighbor values
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
TmpV[j] = 0;
for (int e = 0; e < NI.GetOutDeg(); e++) {
TmpV[j] += NIdEigenH.GetDat(NI.GetOutNId(e));
}
}
// normalize
double sum = 0;
for (int i = 0; i < TmpV.Len(); i++) {
sum += (TmpV[i] * TmpV[i]);
}
sum = sqrt(sum);
for (int i = 0; i < TmpV.Len(); i++) {
TmpV[i] /= sum;
}
// compute difference
double diff = 0.0;
j = 0;
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
diff += fabs(NIdEigenH.GetDat(NI.GetId()) - TmpV[j]);
}
// set new values
j = 0;
for (TUNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
NIdEigenH.AddDat(NI.GetId(), TmpV[j]);
}
if (diff < Eps) {
break;
}
}
}
示例4: main
int main(int argc, char* argv[]) {
Env = TEnv(argc, argv, TNotify::StdNotify);
Env.PrepArgs(TStr::Fmt("Inverse PageRank. Build: %s, %s. Time: %s", __TIME__, __DATE__, TExeTm::GetCurTm()));
TExeTm ExeTm;
Try
const TStr Iput = Env.GetIfArgPrefixStr("-i:", "Input.txt", "Input File" );
const TStr Oput = Env.GetIfArgPrefixStr("-o:", "Output.txt", "Output File");
FILE* fpI = fopen(Iput.CStr(), "r");
FILE* fpO = fopen(Oput.CStr(), "w");
const double C = 0.85;
const int MaxIter = 50;
const double Eps = 1e-9;
PNGraph Graph = TSnap::LoadEdgeList< PNGraph > (Iput);
fprintf(fpO, "\nNodes: %d, Edges: %d\n\n", Graph->GetNodes(), Graph->GetEdges());
const int NNodes = Graph->GetNodes();
const double OneOver = (double) 1.0 / (double) NNodes;
TIntFltH PRankH;
PRankH.Gen(NNodes);
for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++)
PRankH.AddDat(NI.GetId(), OneOver);
TFltV TmpV(NNodes);
for (int iter = 0; iter < MaxIter; iter++) {
int j = 0;
for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
TmpV[j] = 0;
for (int e = 0; e < NI.GetOutDeg(); e++) {
const int OutNId = NI.GetOutNId(e);
const int InDeg = Graph->GetNI(OutNId).GetInDeg();
if (InDeg > 0)
TmpV[j] += PRankH.GetDat(OutNId) / InDeg;
}
TmpV[j] = C * TmpV[j];
}
for (int i = 0; i < PRankH.Len(); i++)
PRankH[i] = TmpV[i];
/*
double diff = 0, sum = 0, NewVal;
for (int i = 0; i < TmpV.Len(); i++)
sum += TmpV[i];
const double Leaked = (double) (1.0 - sum) / (double) NNodes;
for (int i = 0; i < PRankH.Len(); i++) {
NewVal = TmpV[i] + Leaked;
diff += fabs(NewVal - PRankH[i]);
PRankH[i] = NewVal;
}
if (diff < Eps)
break;
*/
}
fprintf(fpO, "Node ID\t\tInverse PageRank\n");
for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
int Id = NI.GetId();
double ipr = PRankH.GetDat(Id);
fprintf(fpO, "%d\t\t\t%.5lf\n", Id, ipr);
}
Catch
printf("\nRun Time: %s (%s)\n", ExeTm.GetTmStr(), TSecTm::GetCurTm().GetTmStr().CStr());
return 0;
}
示例5: GetWeightedPageRankMP2
int GetWeightedPageRankMP2(const PNEANet Graph, TIntFltH& PRankH, const TStr& Attr, const double& C, const double& Eps, const int& MaxIter) {
if (!Graph->IsFltAttrE(Attr)) return -1;
const int NNodes = Graph->GetNodes();
TVec<TNEANet::TNodeI> NV;
//const double OneOver = 1.0/double(NNodes);
PRankH.Gen(NNodes);
int MxId;
for (TNEANet::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++) {
NV.Add(NI);
PRankH.AddDat(NI.GetId(), 1.0/NNodes);
int Id = NI.GetId();
if (Id > MxId) {
MxId = Id;
}
}
TFltV PRankV(MxId+1);
TFltV OutWeights(MxId+1);
TFltV Weights = Graph->GetFltAttrVecE(Attr);
#pragma omp parallel for schedule(dynamic,10000)
for (int j = 0; j < NNodes; j++) {
TNEANet::TNodeI NI = NV[j];
int Id = NI.GetId();
OutWeights[Id] = Graph->GetWeightOutEdges(NI, Attr);
PRankV[Id] = 1/NNodes;
}
TFltV TmpV(NNodes);
for (int iter = 0; iter < MaxIter; iter++) {
#pragma omp parallel for schedule(dynamic,10000)
for (int j = 0; j < NNodes; j++) {
TNEANet::TNodeI NI = NV[j];
TFlt Tmp = 0;
for (int e = 0; e < NI.GetInDeg(); e++) {
const int InNId = NI.GetInNId(e);
const TFlt OutWeight = OutWeights[InNId];
int EId = Graph->GetEId(InNId, NI.GetId());
const TFlt Weight = Weights[Graph->GetFltKeyIdE(EId)];
if (OutWeight > 0) {
Tmp += PRankH.GetDat(InNId) * Weight / OutWeight;
}
}
TmpV[j] = C*Tmp; // Berkhin (the correct way of doing it)
//TmpV[j] = C*TmpV[j] + (1.0-C)*OneOver; // iGraph
}
double sum = 0;
#pragma omp parallel for reduction(+:sum) schedule(dynamic,10000)
for (int i = 0; i < TmpV.Len(); i++) { sum += TmpV[i]; }
const double Leaked = (1.0-sum) / double(NNodes);
double diff = 0;
#pragma omp parallel for reduction(+:diff) schedule(dynamic,10000)
for (int i = 0; i < NNodes; i++) {
TNEANet::TNodeI NI = NV[i];
double NewVal = TmpV[i] + Leaked; // Berkhin
//NewVal = TmpV[i] / sum; // iGraph
int Id = NI.GetId();
diff += fabs(NewVal-PRankV[Id]);
PRankV[Id] = NewVal;
}
if (diff < Eps) { break; }
}
#pragma omp parallel for schedule(dynamic,10000)
for (int i = 0; i < NNodes; i++) {
TNEANet::TNodeI NI = NV[i];
PRankH[i] = PRankV[NI.GetId()];
}
return 0;
}
示例6: main
int main(int argc, char* argv[]) {
Env = TEnv(argc, argv, TNotify::StdNotify);
Env.PrepArgs(TStr::Fmt("Trust Rank. Build: %s, %s. Time: %s", __TIME__, __DATE__, TExeTm::GetCurTm()));
TExeTm ExeTm;
Try
const TStr Gnod = Env.GetIfArgPrefixStr("-g:", "Gnode.txt", "Good Nodes");
const TStr Bnod = Env.GetIfArgPrefixStr("-b:", "Bnode.txt", "Bad Nodes" );
const TStr Iput = Env.GetIfArgPrefixStr("-i:", "Input.txt", "Input File");
const TStr Oput = Env.GetIfArgPrefixStr("-o:", "Output.txt", "Output File");
const double C = 0.85;
const int MaxIter = 50;
const double Eps = 1e-9;
FILE* fpO = fopen(Oput.CStr(), "w");
PNGraph Graph = TSnap::LoadEdgeList< PNGraph > (Iput);
fprintf(fpO, "\nNodes: %d, Edges: %d\n\n", Graph->GetNodes(), Graph->GetEdges());
const int NNodes = Graph->GetNodes();
TIntFltH TRankH;
TRankH.Gen(NNodes);
int maxNId = 0, NId = 0, ret = 0;
for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++)
maxNId = max(maxNId, NI.GetId());
TFltV initialTrustScore(maxNId + 1);
for (int i = 0; i < initialTrustScore.Len(); i++)
initialTrustScore[i] = 0.5;
FILE* fpI = fopen(Gnod.CStr(), "r");
while (true) {
ret = fscanf(fpI, "%d", &NId);
if (ret == EOF) break;
if (Graph->IsNode(NId))
initialTrustScore[NId] = 1.0;
}
fclose(fpI);
fpI = fopen(Bnod.CStr(), "r");
while (true) {
ret = fscanf(fpI, "%d", &NId);
if (ret == EOF) break;
if (Graph->IsNode(NId))
initialTrustScore[NId] = 0.0;
}
fclose(fpI);
double Tot = 0.0;
for(int i = 0; i < initialTrustScore.Len(); i++)
Tot += initialTrustScore[i];
for(int i = 0; i < initialTrustScore.Len(); i++)
initialTrustScore[i] /= Tot;
for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++)
TRankH.AddDat( NI.GetId(), initialTrustScore[NI.GetId()] );
TFltV TmpV(NNodes);
for (int iter = 0; iter < MaxIter; iter++) {
int j = 0;
for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++, j++) {
TmpV[j] = 0;
for (int e = 0; e < NI.GetOutDeg(); e++) {
const int OutNId = NI.GetOutNId(e);
const int InDeg = Graph->GetNI(InNId).GetInDeg();
if (InDeg > 0)
TmpV[j] += (double) TRankH.GetDat(OutNId) / (double) InDeg;
}
TmpV[j] = C * TmpV[j] + (1.0 - C) * initialTrustScore[NI.GetId()];
}
for (int i = 0; i < TRankH.Len(); i++)
TRankH[i] = TmpV[i];
}
fprintf(fpO, "Node ID\t\tTrustRank\n");
for (TNGraph::TNodeI NI = Graph->BegNI(); NI < Graph->EndNI(); NI++){
int Id = NI.GetId();
double tr = TRankH.GetDat(Id);
fprintf(fpO, "%d\t\t\t%.5lf\n", Id, tr);
}
fclose(fpO);
Catch
printf("\nRun Time: %s (%s)\n", ExeTm.GetTmStr(), TSecTm::GetCurTm().GetTmStr().CStr());
return 0;
}
示例7: GradientForRow
void TAGMFast::GradientForRow(const int UID, TIntFltH& GradU, const TIntSet& CIDSet) {
GradU.Gen(CIDSet.Len());
TFltV HOSumFV; //adjust for Fv of v hold out
if (HOVIDSV[UID].Len() > 0) {
HOSumFV.Gen(SumFV.Len());
for (int e = 0; e < HOVIDSV[UID].Len(); e++) {
for (int c = 0; c < SumFV.Len(); c++) {
HOSumFV[c] += GetCom(HOVIDSV[UID][e], c);
}
}
}
TUNGraph::TNodeI NI = G->GetNI(UID);
int Deg = NI.GetDeg();
TFltV PredV(Deg), GradV(CIDSet.Len());
TIntV CIDV(CIDSet.Len());
if (DoParallel && Deg + CIDSet.Len() > 10) {
#pragma omp parallel for schedule(static, 1)
for (int e = 0; e < Deg; e++) {
if (NI.GetNbrNId(e) == UID) { continue; }
if (HOVIDSV[UID].IsKey(NI.GetNbrNId(e))) { continue; }
PredV[e] = Prediction(UID, NI.GetNbrNId(e));
}
#pragma omp parallel for schedule(static, 1)
for (int c = 0; c < CIDSet.Len(); c++) {
int CID = CIDSet.GetKey(c);
double Val = 0.0;
for (int e = 0; e < Deg; e++) {
int VID = NI.GetNbrNId(e);
if (VID == UID) { continue; }
if (HOVIDSV[UID].IsKey(VID)) { continue; }
Val += PredV[e] * GetCom(VID, CID) / (1.0 - PredV[e]) + NegWgt * GetCom(VID, CID);
}
double HOSum = HOVIDSV[UID].Len() > 0? HOSumFV[CID].Val: 0.0;//subtract Hold out pairs only if hold out pairs exist
Val -= NegWgt * (SumFV[CID] - HOSum - GetCom(UID, CID));
CIDV[c] = CID;
GradV[c] = Val;
}
}
else {
for (int e = 0; e < Deg; e++) {
if (NI.GetNbrNId(e) == UID) { continue; }
if (HOVIDSV[UID].IsKey(NI.GetNbrNId(e))) { continue; }
PredV[e] = Prediction(UID, NI.GetNbrNId(e));
}
for (int c = 0; c < CIDSet.Len(); c++) {
int CID = CIDSet.GetKey(c);
double Val = 0.0;
for (int e = 0; e < Deg; e++) {
int VID = NI.GetNbrNId(e);
if (VID == UID) { continue; }
if (HOVIDSV[UID].IsKey(VID)) { continue; }
Val += PredV[e] * GetCom(VID, CID) / (1.0 - PredV[e]) + NegWgt * GetCom(VID, CID);
}
double HOSum = HOVIDSV[UID].Len() > 0? HOSumFV[CID].Val: 0.0;//subtract Hold out pairs only if hold out pairs exist
Val -= NegWgt * (SumFV[CID] - HOSum - GetCom(UID, CID));
CIDV[c] = CID;
GradV[c] = Val;
}
}
//add regularization
if (RegCoef > 0.0) { //L1
for (int c = 0; c < GradV.Len(); c++) {
GradV[c] -= RegCoef;
}
}
if (RegCoef < 0.0) { //L2
for (int c = 0; c < GradV.Len(); c++) {
GradV[c] += 2 * RegCoef * GetCom(UID, CIDV[c]);
}
}
for (int c = 0; c < GradV.Len(); c++) {
if (GetCom(UID, CIDV[c]) == 0.0 && GradV[c] < 0.0) { continue; }
if (fabs(GradV[c]) < 0.0001) { continue; }
GradU.AddDat(CIDV[c], GradV[c]);
}
for (int c = 0; c < GradU.Len(); c++) {
if (GradU[c] >= 10) { GradU[c] = 10; }
if (GradU[c] <= -10) { GradU[c] = -10; }
IAssert(GradU[c] >= -10);
}
}