TRF Language Model
main-SA-train.cpp File Reference
#include "trf-model.h"
#include "trf-sa-train.h"
#include <omp.h>

Go to the source code of this file.

Functions

opt Add (wbOPT_STRING, "feat", &cfg_pathFeatStyle, "a feature style file. Set this value will disable -order")
 
opt Add (wbOPT_INT, "order", &cfg_nFeatOrder, "the ngram feature order")
 
opt Add (wbOPT_INT, "len", &cfg_nMaxLen, "the maximum length of TRF")
 
opt Add (wbOPT_STRING, "train", &cfg_pathTrain, "Training corpus (TXT)")
 
opt Add (wbOPT_STRING, "valid", &cfg_pathValid, "valid corpus (TXT)")
 
opt Add (wbOPT_STRING, "test", &cfg_pathTest, "test corpus (TXT)")
 
opt Add (wbOPT_STRING, "read", &cfg_pathModelRead, "Read the init model to train")
 
opt Add (wbOPT_STRING, "write", &cfg_pathModelWrite, "Output model")
 
opt Add (wbOPT_INT, "iter", &cfg_nIterTotalNum, "iter total number")
 
opt Add (wbOPT_INT, "thread", &cfg_nThread, "The thread number")
 
opt Add (wbOPT_INT, "mini-batch", &cfg_nMiniBatch, "mini-batch")
 
opt Add (wbOPT_INT, "t0", &cfg_t0, "t0")
 
opt Add (wbOPT_STRING, "gamma-lambda", &cfg_gamma_lambda, "learning rate of lambda")
 
opt Add (wbOPT_STRING, "gamma-zeta", &cfg_gamma_zeta, "learning rate of zeta")
 
opt Add (wbOPT_TRUE, "unupdate-lambda", &cfg_bUnupdateLambda, "don't update lambda")
 
opt Add (wbOPT_TRUE, "unupdate-zeta", &cfg_bUnupdateZeta, "don't update zeta")
 
opt Add (wbOPT_INT, "tavg", &cfg_nAvgBeg, ">0 then apply averaging")
 
opt Add (wbOPT_FLOAT, "L2", &cfg_fRegL2, "regularization L2")
 
opt Add (wbOPT_FLOAT, "dgap", &cfg_dGap, "the gap for update value at each iteration")
 
opt Add (wbOPT_FALSE, "vgap", &cfg_vGap, "the gap for empirical variance")
 
opt Add (wbOPT_TRUE, "init", &cfg_bInitValue, "Re-init the parameters")
 
opt Add (wbOPT_INT, "print-per-iter", &cfg_nPrintPerIter, "print the LL per iterations")
 
opt Add (wbOPT_STRING, "write-at-iter", &cfg_strWriteAtIter, "write models at iteration, such as [1:100:1000]")
 
opt Add (wbOPT_STRING, "write-mean", &cfg_pathWriteMean, "write the expecataion on training set")
 
opt Add (wbOPT_STRING, "write-var", &cfg_pathWriteVar, "write the variance on training set")
 
opt Add (wbOPT_STRING, "write-train-ll", &cfg_pathWriteLLtrain, "write LL on train")
 
opt Add (wbOPT_STRING, "write-valid-ll", &cfg_pathWriteLLvalid, "write LL on valid")
 
opt Add (wbOPT_STRING, "write-test-ll", &cfg_pathWriteLLtest, "write LL on test")
 
opt Parse (_argc, _argv)
 
 if (cfg_pathModelRead)
 if the model are inputed, then using the input parameters More...
 
 if (bInitWeight)
 
 if (bInitZeta)
 
solve Run (vInitParams.GetBuf())
 
m WriteT (cfg_pathModelWrite)
 
 SAFE_DELETE (pTrain)
 
 SAFE_DELETE (pValid)
 
 SAFE_DELETE (pTest)
 
 SAFE_DELETE (pv)
 

Variables

char * cfg_pathVocab = NULL
 
int cfg_nFeatOrder = 2
 
char * cfg_pathFeatStyle = NULL
 
int cfg_nMaxLen = 0
 
char * cfg_pathTrain = NULL
 
char * cfg_pathValid = NULL
 
char * cfg_pathTest = NULL
 
char * cfg_pathModelRead = NULL
 
char * cfg_pathModelWrite = NULL
 
int cfg_nThread = 1
 
int cfg_nIterTotalNum = 1000
 
int cfg_nMiniBatch = 300
 
int cfg_t0 = 500
 
char * cfg_gamma_lambda = NULL
 
char * cfg_gamma_zeta = NULL
 
bool cfg_bUnupdateLambda = false
 
bool cfg_bUnupdateZeta = false
 
int cfg_nAvgBeg = 0
 
float cfg_fRegL2 = 0
 
float cfg_dGap = 1.0f
 
float cfg_vGap = 1e-5
 
bool cfg_bInitValue = false
 
int cfg_nPrintPerIter = 1
 
char * cfg_strWriteAtIter = NULL
 
char * cfg_pathWriteMean = NULL
 
char * cfg_pathWriteVar = NULL
 
char * cfg_pathWriteLLtrain = NULL
 
char * cfg_pathWriteLLvalid = NULL
 
char * cfg_pathWriteLLtest = NULL
 
Option opt
 
 _wbMain
 
lout<< "*********************************************"<< endl;lout<< " TRF_SAtrain.exe { by Bin Wang } "<< endl;lout<< "\"<< __DATE__<< "\"<< __TIME__<< "\"<< endl;lout<< "**********************************************"<< endl;omp_set_num_threads(cfg_nThread);lout<< "[OMP] omp_thread = "<< omp_get_max_threads()<< endl;trf::omp_rand(cfg_nThread);Title::SetGlobalTitle(String(cfg_pathModelWrite).FileName());Vocab *pv=new Vocab(cfg_pathVocab);Model m(pv, cfg_nMaxLen);if(cfg_pathModelRead) { m.ReadT(cfg_pathModelRead);} else { m.LoadFromCorpus(cfg_pathTrain, cfg_pathFeatStyle, cfg_nFeatOrder);} CorpusTxt *pTrain=(cfg_pathTrain) ? new CorpusTxt(cfg_pathTrain) :NULL;CorpusTxt *pValid=(cfg_pathValid) ? new CorpusTxt(cfg_pathValid) :NULL;CorpusTxt *pTest=(cfg_pathTest) ? new CorpusTxt(cfg_pathTest) :NULL;SAfunc func;func.m_fdbg.Open(String(cfg_pathModelWrite).FileName()+".sadbg", "wt");func.m_fmean.Open(cfg_pathWriteMean, "wt");func.m_fvar.Open(cfg_pathWriteVar, "wt");func.m_ftrainLL.Open(cfg_pathWriteLLtrain, "wt");func.m_fvallidLL.Open(cfg_pathWriteLLvalid, "wt");func.m_ftestLL.Open(cfg_pathWriteLLtest, "wt");func.m_pathOutputModel=cfg_pathModelWrite;func.Reset(&m, pTrain, pValid, pTest, cfg_nMiniBatch);func.m_fRegL2=cfg_fRegL2;func.m_var_gap=cfg_vGap;func.PrintInfo();SAtrain solve(&func);solve.m_nIterMax=cfg_nIterTotalNum;solve.m_gain_lambda.Reset(cfg_gamma_lambda ? cfg_gamma_lambda :"0,0", cfg_t0);solve.m_gain_zeta.Reset(cfg_gamma_zeta ? cfg_gamma_zeta :"0,0.6", cfg_t0);solve.m_bUpdate_lambda=!cfg_bUnupdateLambda;solve.m_bUpdate_zeta=!cfg_bUnupdateZeta;solve.m_nAvgBeg=cfg_nAvgBeg;solve.m_nPrintPerIter=cfg_nPrintPerIter;solve.m_dir_gap=cfg_dGap;VecUnfold(cfg_strWriteAtIter, solve.m_aWriteAtIter);solve.PrintInfo();bool bInitWeight=(!cfg_pathModelRead)||(cfg_bInitValue &&!cfg_bUnupdateLambda);bool bInitZeta=(!cfg_pathModelRead)||(cfg_bInitValue &&!cfg_bUnupdateZeta);Vec< double > vInitParams (func.GetParamNum())
 
 return
 

Function Documentation

§ Add() [1/28]

opt Add ( wbOPT_STRING  ,
"feat"  ,
cfg_pathFeatStyle,
"a feature style file. Set this value will disable -order"   
)

§ Add() [2/28]

opt Add ( wbOPT_INT  ,
"order"  ,
cfg_nFeatOrder,
"the ngram feature order"   
)

§ Add() [3/28]

opt Add ( wbOPT_INT  ,
"len"  ,
cfg_nMaxLen,
"the maximum length of TRF"   
)

§ Add() [4/28]

opt Add ( wbOPT_STRING  ,
"train"  ,
cfg_pathTrain,
"Training corpus (TXT)"   
)

§ Add() [5/28]

opt Add ( wbOPT_STRING  ,
"valid"  ,
cfg_pathValid,
"valid corpus (TXT)"   
)

§ Add() [6/28]

opt Add ( wbOPT_STRING  ,
"test"  ,
cfg_pathTest,
"test corpus (TXT)"   
)

§ Add() [7/28]

opt Add ( wbOPT_STRING  ,
"read"  ,
cfg_pathModelRead,
"Read the init model to train"   
)

§ Add() [8/28]

opt Add ( wbOPT_STRING  ,
"write"  ,
cfg_pathModelWrite,
"Output model"   
)

§ Add() [9/28]

opt Add ( wbOPT_INT  ,
"iter"  ,
cfg_nIterTotalNum,
"iter total number"   
)

§ Add() [10/28]

opt Add ( wbOPT_INT  ,
"thread"  ,
cfg_nThread,
"The thread number"   
)

§ Add() [11/28]

opt Add ( wbOPT_INT  ,
"mini-batch"  ,
cfg_nMiniBatch,
"mini-batch"   
)

§ Add() [12/28]

opt Add ( wbOPT_INT  ,
"t0"  ,
cfg_t0,
"t0"   
)

§ Add() [13/28]

opt Add ( wbOPT_STRING  ,
"gamma-lambda"  ,
cfg_gamma_lambda,
"learning rate of lambda"   
)

§ Add() [14/28]

opt Add ( wbOPT_STRING  ,
"gamma-zeta"  ,
cfg_gamma_zeta,
"learning rate of zeta"   
)

§ Add() [15/28]

opt Add ( wbOPT_TRUE  ,
"unupdate-lambda"  ,
cfg_bUnupdateLambda,
"don't update lambda"   
)

§ Add() [16/28]

opt Add ( wbOPT_TRUE  ,
"unupdate-zeta"  ,
cfg_bUnupdateZeta,
"don't update zeta"   
)

§ Add() [17/28]

opt Add ( wbOPT_INT  ,
"tavg"  ,
cfg_nAvgBeg,
,
0 then apply averaging"   
)

§ Add() [18/28]

opt Add ( wbOPT_FLOAT  ,
"L2"  ,
cfg_fRegL2,
"regularization L2"   
)

§ Add() [19/28]

opt Add ( wbOPT_FLOAT  ,
"dgap"  ,
cfg_dGap,
"the gap for update value at each iteration"   
)

§ Add() [20/28]

opt Add ( wbOPT_FALSE  ,
"vgap"  ,
cfg_vGap,
"the gap for empirical variance"   
)

§ Add() [21/28]

opt Add ( wbOPT_TRUE  ,
"init"  ,
cfg_bInitValue,
"Re-init the parameters"   
)

§ Add() [22/28]

opt Add ( wbOPT_INT  ,
"print-per-iter"  ,
cfg_nPrintPerIter,
"print the LL per iterations"   
)

§ Add() [23/28]

opt Add ( wbOPT_STRING  ,
"write-at-iter"  ,
cfg_strWriteAtIter,
"write models at  iteration,
such as "  [1:100:1000] 
)

§ Add() [24/28]

opt Add ( wbOPT_STRING  ,
"write-mean"  ,
cfg_pathWriteMean,
"write the expecataion on training set"   
)

§ Add() [25/28]

opt Add ( wbOPT_STRING  ,
"write-var"  ,
cfg_pathWriteVar,
"write the variance on training set"   
)

§ Add() [26/28]

opt Add ( wbOPT_STRING  ,
"write-train-ll"  ,
cfg_pathWriteLLtrain,
"write LL on train"   
)

§ Add() [27/28]

opt Add ( wbOPT_STRING  ,
"write-valid-ll"  ,
cfg_pathWriteLLvalid,
"write LL on valid"   
)

§ Add() [28/28]

opt Add ( wbOPT_STRING  ,
"write-test-ll"  ,
cfg_pathWriteLLtest,
"write LL on test"   
)

§ if() [1/3]

if the model are inputed, then using the input parameters

Definition at line 166 of file main-SA-train.cpp.

§ if() [2/3]

if ( bInitWeight  )

Definition at line 169 of file main-SA-train.cpp.

§ if() [3/3]

if ( bInitZeta  )

Definition at line 173 of file main-SA-train.cpp.

§ Parse()

opt Parse ( _argc  ,
_argv   
)

§ Run()

solve Run ( vInitParams.  GetBuf())

§ SAFE_DELETE() [1/4]

SAFE_DELETE ( pTrain  )

§ SAFE_DELETE() [2/4]

SAFE_DELETE ( pValid  )

§ SAFE_DELETE() [3/4]

SAFE_DELETE ( pTest  )

§ SAFE_DELETE() [4/4]

SAFE_DELETE ( pv  )

§ WriteT()

m WriteT ( cfg_pathModelWrite  )

Variable Documentation

§ _wbMain

_wbMain
Initial value:
{
opt.Add(wbOPT_STRING, "vocab", &cfg_pathVocab, "The vocabulary")
char * cfg_pathVocab
void Add(ValueType t, const char *pLabel, void *pAddress, const char *pDocMsg=NULL)
Add a option.
Definition: wb-option.cpp:35
Option opt

Definition at line 65 of file main-SA-train.cpp.

§ cfg_bInitValue

bool cfg_bInitValue = false

Definition at line 52 of file main-SA-train.cpp.

§ cfg_bUnupdateLambda

bool cfg_bUnupdateLambda = false

Definition at line 44 of file main-SA-train.cpp.

§ cfg_bUnupdateZeta

bool cfg_bUnupdateZeta = false

Definition at line 45 of file main-SA-train.cpp.

§ cfg_dGap

float cfg_dGap = 1.0f

Definition at line 49 of file main-SA-train.cpp.

§ cfg_fRegL2

float cfg_fRegL2 = 0

Definition at line 48 of file main-SA-train.cpp.

§ cfg_gamma_lambda

char* cfg_gamma_lambda = NULL

Definition at line 42 of file main-SA-train.cpp.

§ cfg_gamma_zeta

char* cfg_gamma_zeta = NULL

Definition at line 43 of file main-SA-train.cpp.

§ cfg_nAvgBeg

int cfg_nAvgBeg = 0

Definition at line 46 of file main-SA-train.cpp.

§ cfg_nFeatOrder

int cfg_nFeatOrder = 2

Definition at line 26 of file main-SA-train.cpp.

§ cfg_nIterTotalNum

int cfg_nIterTotalNum = 1000

Definition at line 39 of file main-SA-train.cpp.

§ cfg_nMaxLen

int cfg_nMaxLen = 0

Definition at line 28 of file main-SA-train.cpp.

§ cfg_nMiniBatch

int cfg_nMiniBatch = 300

Definition at line 40 of file main-SA-train.cpp.

§ cfg_nPrintPerIter

int cfg_nPrintPerIter = 1

Definition at line 53 of file main-SA-train.cpp.

§ cfg_nThread

int cfg_nThread = 1

Definition at line 37 of file main-SA-train.cpp.

§ cfg_pathFeatStyle

char* cfg_pathFeatStyle = NULL

Definition at line 27 of file main-SA-train.cpp.

§ cfg_pathModelRead

char* cfg_pathModelRead = NULL

Definition at line 34 of file main-SA-train.cpp.

§ cfg_pathModelWrite

char* cfg_pathModelWrite = NULL

Definition at line 35 of file main-SA-train.cpp.

§ cfg_pathTest

char* cfg_pathTest = NULL

Definition at line 32 of file main-SA-train.cpp.

§ cfg_pathTrain

char* cfg_pathTrain = NULL

Definition at line 30 of file main-SA-train.cpp.

§ cfg_pathValid

char* cfg_pathValid = NULL

Definition at line 31 of file main-SA-train.cpp.

§ cfg_pathVocab

char* cfg_pathVocab = NULL

Definition at line 24 of file main-SA-train.cpp.

§ cfg_pathWriteLLtest

char* cfg_pathWriteLLtest = NULL

Definition at line 60 of file main-SA-train.cpp.

§ cfg_pathWriteLLtrain

char* cfg_pathWriteLLtrain = NULL

Definition at line 58 of file main-SA-train.cpp.

§ cfg_pathWriteLLvalid

char* cfg_pathWriteLLvalid = NULL

Definition at line 59 of file main-SA-train.cpp.

§ cfg_pathWriteMean

char* cfg_pathWriteMean = NULL

Definition at line 56 of file main-SA-train.cpp.

§ cfg_pathWriteVar

char* cfg_pathWriteVar = NULL

Definition at line 57 of file main-SA-train.cpp.

§ cfg_strWriteAtIter

char* cfg_strWriteAtIter = NULL

Definition at line 54 of file main-SA-train.cpp.

§ cfg_t0

int cfg_t0 = 500

Definition at line 41 of file main-SA-train.cpp.

§ cfg_vGap

float cfg_vGap = 1e-5

Definition at line 50 of file main-SA-train.cpp.

§ opt

Option opt

Definition at line 62 of file main-SA-train.cpp.

§ return

return

Definition at line 191 of file main-SA-train.cpp.

§ vInitParams

lout<< "*********************************************" << endl; lout << " TRF_SAtrain.exe { by Bin Wang } " << endl; lout << "\t" << __DATE__ << "\t" << __TIME__ << "\t" << endl; lout << "**********************************************" << endl; omp_set_num_threads(cfg_nThread); lout << "[OMP] omp_thread = " << omp_get_max_threads() << endl; trf::omp_rand(cfg_nThread); Title::SetGlobalTitle(String(cfg_pathModelWrite).FileName()); Vocab *pv = new Vocab(cfg_pathVocab); Model m(pv, cfg_nMaxLen); if (cfg_pathModelRead) { m.ReadT(cfg_pathModelRead); } else { m.LoadFromCorpus(cfg_pathTrain, cfg_pathFeatStyle, cfg_nFeatOrder); } CorpusTxt *pTrain = (cfg_pathTrain) ? new CorpusTxt(cfg_pathTrain) : NULL; CorpusTxt *pValid = (cfg_pathValid) ? new CorpusTxt(cfg_pathValid) : NULL; CorpusTxt *pTest = (cfg_pathTest) ? new CorpusTxt(cfg_pathTest) : NULL; SAfunc func; func.m_fdbg.Open(String(cfg_pathModelWrite).FileName() + ".sadbg", "wt"); func.m_fmean.Open(cfg_pathWriteMean, "wt"); func.m_fvar.Open(cfg_pathWriteVar, "wt"); func.m_ftrainLL.Open(cfg_pathWriteLLtrain, "wt"); func.m_fvallidLL.Open(cfg_pathWriteLLvalid, "wt"); func.m_ftestLL.Open(cfg_pathWriteLLtest, "wt"); func.m_pathOutputModel = cfg_pathModelWrite; func.Reset(&m, pTrain, pValid, pTest, cfg_nMiniBatch); func.m_fRegL2 = cfg_fRegL2; func.m_var_gap = cfg_vGap; func.PrintInfo(); SAtrain solve(&func); solve.m_nIterMax = cfg_nIterTotalNum; solve.m_gain_lambda.Reset(cfg_gamma_lambda ? cfg_gamma_lambda : "0,0", cfg_t0); solve.m_gain_zeta.Reset(cfg_gamma_zeta ? cfg_gamma_zeta : "0,0.6", cfg_t0); solve.m_bUpdate_lambda = !cfg_bUnupdateLambda; solve.m_bUpdate_zeta = !cfg_bUnupdateZeta; solve.m_nAvgBeg = cfg_nAvgBeg; solve.m_nPrintPerIter = cfg_nPrintPerIter; solve.m_dir_gap = cfg_dGap; VecUnfold(cfg_strWriteAtIter, solve.m_aWriteAtIter); solve.PrintInfo(); bool bInitWeight = (!cfg_pathModelRead) || (cfg_bInitValue && !cfg_bUnupdateLambda); bool bInitZeta = (!cfg_pathModelRead) || (cfg_bInitValue && !cfg_bUnupdateZeta); Vec<double> vInitParams(func.GetParamNum())

Definition at line 164 of file main-SA-train.cpp.