Commit d5269725 by xiaotong

bug fixes

parent 9eda6d83
...@@ -74,6 +74,7 @@ void TestTrain() ...@@ -74,6 +74,7 @@ void TestTrain()
XConfig config; XConfig config;
config.Add("dev", -1); config.Add("dev", -1);
config.Add("lrate", 0.1F);
TTDataLoader loader; TTDataLoader loader;
loader.SetFileName("ttrain.txt"); loader.SetFileName("ttrain.txt");
...@@ -217,9 +218,14 @@ void TTModel::SetConfig(XConfig &myConfig) ...@@ -217,9 +218,14 @@ void TTModel::SetConfig(XConfig &myConfig)
config.CreateFromMe(myConfig); config.CreateFromMe(myConfig);
} }
/* initialize the model */ /*
initialize the model
>> myConfig - configuration
>> devID - device id
*/
void TTModel::Init(XConfig &myConfig, int devID) void TTModel::Init(XConfig &myConfig, int devID)
{ {
Clear();
SetConfig(myConfig); SetConfig(myConfig);
vSize = MAX_INT_IN_TTRAIN + 1; vSize = MAX_INT_IN_TTRAIN + 1;
...@@ -230,12 +236,25 @@ void TTModel::Init(XConfig &myConfig, int devID) ...@@ -230,12 +236,25 @@ void TTModel::Init(XConfig &myConfig, int devID)
InitTensor2D(&hiddenW, 3 * eSize, hSize, X_FLOAT, devID); InitTensor2D(&hiddenW, 3 * eSize, hSize, X_FLOAT, devID);
InitTensor2D(&outputW, hSize, vSize, X_FLOAT, devID); InitTensor2D(&outputW, hSize, vSize, X_FLOAT, devID);
embeddingW.SetName("embeddingw");
hiddenW.SetName("hiddenw");
outputW.SetName("outputw");
embeddingW.SetDataRand(-0.1F, 0.1F); embeddingW.SetDataRand(-0.1F, 0.1F);
hiddenW.SetDataRand(-0.1F, 0.1F); hiddenW.SetDataRand(-0.1F, 0.1F);
outputW.SetDataRand(-0.1F, 0.1F); outputW.SetDataRand(-0.1F, 0.1F);
AddParam(&embeddingW);
AddParam(&hiddenW);
AddParam(&outputW);
} }
/* create the model */ /*
create the model
>> devID - device id
>> input - as it is
>> output - as it is
*/
void TTModel::Forward(int devID, XTensor * input, XTensor * output) void TTModel::Forward(int devID, XTensor * input, XTensor * output)
{ {
XTensor embedding; XTensor embedding;
...@@ -261,7 +280,10 @@ void TTModel::Clear() ...@@ -261,7 +280,10 @@ void TTModel::Clear()
config.Clear(); config.Clear();
} }
/* clone the model */ /*
clone the model
>> devID - device id
*/
XModel * TTModel::Clone(int devID) XModel * TTModel::Clone(int devID)
{ {
TTModel * model = new TTModel(); TTModel * model = new TTModel();
...@@ -293,8 +315,10 @@ bool TTModel::RunSimple(XList * inputs, XList * outputs, XList * golds, XList* l ...@@ -293,8 +315,10 @@ bool TTModel::RunSimple(XList * inputs, XList * outputs, XList * golds, XList* l
XNet net; XNet net;
/* create the neural network and run it */
Forward(devID, input, output); Forward(devID, input, output);
/* gold standard in ong-hot representaiton */
goldOneHot = IndexToOnehot(*gold, vSize, 0.0F); goldOneHot = IndexToOnehot(*gold, vSize, 0.0F);
int* dims = new int[goldOneHot.order]; int* dims = new int[goldOneHot.order];
...@@ -303,8 +327,10 @@ bool TTModel::RunSimple(XList * inputs, XList * outputs, XList * golds, XList* l ...@@ -303,8 +327,10 @@ bool TTModel::RunSimple(XList * inputs, XList * outputs, XList * golds, XList* l
dims[goldOneHot.order - 2] = goldOneHot.GetDim(goldOneHot.order - 1); dims[goldOneHot.order - 2] = goldOneHot.GetDim(goldOneHot.order - 1);
goldOneHot.Reshape(goldOneHot.order - 1, dims); goldOneHot.Reshape(goldOneHot.order - 1, dims);
/* loss */
*loss = CrossEntropy(*output, goldOneHot); *loss = CrossEntropy(*output, goldOneHot);
/* back-propagation */
net.Backward(*loss); net.Backward(*loss);
delete[] dims; delete[] dims;
......
...@@ -90,8 +90,11 @@ Set the server model. It distributes the server-side parameters on different dev ...@@ -90,8 +90,11 @@ Set the server model. It distributes the server-side parameters on different dev
*/ */
void XLeader::SetServerModel(XConfig * config, XModel * model, XList * memberModels) void XLeader::SetServerModel(XConfig * config, XModel * model, XList * memberModels)
{ {
serverModel.params.Clear(); serverModel.Clear();
serverModel.params.AddList(&model->params); for (int i = 0; i < model->params.count; i++) {
XTensor * param = (XTensor*)model->params[i];
serverModel.AddParam(param);
}
/* TODO: we can place parameters on different devices */ /* TODO: we can place parameters on different devices */
} }
......
...@@ -97,6 +97,24 @@ bool XModel::RunMe(XList * args) ...@@ -97,6 +97,24 @@ bool XModel::RunMe(XList * args)
return false; return false;
} }
/*
add a parameter tensor
>> param - add a
*/
void XModel::AddParam(XTensor* param)
{
//param->SetVarFlag();
params.Add(param);
PARAM_STATE * newFlags = new PARAM_STATE[params.count];
memcpy(newFlags, flags, sizeof(PARAM_STATE) * (params.count - 1));
newFlags[params.count - 1] = PARAM_STATE_NOT_READY;
delete[] flags;
flags = newFlags;
}
/* refresh the model */ /* refresh the model */
void XModel::RefreshMe() void XModel::RefreshMe()
{ {
......
...@@ -89,6 +89,8 @@ protected: ...@@ -89,6 +89,8 @@ protected:
bool RunMe(XList * args); bool RunMe(XList * args);
public: public:
/* add a parameter tensor */
void AddParam(XTensor* param);
/* refresh the model */ /* refresh the model */
void RefreshMe(); void RefreshMe();
...@@ -100,6 +102,7 @@ public: ...@@ -100,6 +102,7 @@ public:
/* wrapper of Run() */ /* wrapper of Run() */
static static
bool Run(XList * args); bool Run(XList * args);
}; };
} }
......
...@@ -47,6 +47,9 @@ initialize the optimizer ...@@ -47,6 +47,9 @@ initialize the optimizer
*/ */
void XOptimizer::Init(XConfig &config) void XOptimizer::Init(XConfig &config)
{ {
nstep = config.GetInt("nstep", 100000);
nepoch = config.GetInt("nepoch", 50);
lrate = config.GetFloat("lrate", 0.1F);
} }
/* clear the optimizer */ /* clear the optimizer */
......
...@@ -76,7 +76,7 @@ void XWorkerCollect::CollectData(XList * sourceList, XModel * target, long sleep ...@@ -76,7 +76,7 @@ void XWorkerCollect::CollectData(XList * sourceList, XModel * target, long sleep
if (collectMode == DATA_COLLECT_P2P) { if (collectMode == DATA_COLLECT_P2P) {
for (int j = 0; j < tp.count; j++) { for (int j = 0; j < tp.count; j++) {
/* tp[j]->isGradFinished is true only if the model finishes the computation /* tp[j]->isGradFinished is true only if the model finishes theA computation
(in another process) */ (in another process) */
if (target->flags[j] == PARAM_STATE_COLLECTED || !tp[j]->isGradFinished) if (target->flags[j] == PARAM_STATE_COLLECTED || !tp[j]->isGradFinished)
continue; continue;
...@@ -91,7 +91,7 @@ void XWorkerCollect::CollectData(XList * sourceList, XModel * target, long sleep ...@@ -91,7 +91,7 @@ void XWorkerCollect::CollectData(XList * sourceList, XModel * target, long sleep
if (source->flags[j] != PARAM_STATE_COLLECTED && sp[j]->isGradFinished) { if (source->flags[j] != PARAM_STATE_COLLECTED && sp[j]->isGradFinished) {
/* data transmit */ /* data transmit */
CollectP2P(sp.GetItem(j)->grad, tp.GetItem(j)->grad); CollectP2P(sp[j]->grad, tp[j]->grad);
/* reset the flag */ /* reset the flag */
source->flags[j] = PARAM_STATE_COLLECTED; source->flags[j] = PARAM_STATE_COLLECTED;
...@@ -158,16 +158,16 @@ void XWorkerCollect::CollectData(XList * sourceList, XModel * target, long sleep ...@@ -158,16 +158,16 @@ void XWorkerCollect::CollectData(XList * sourceList, XModel * target, long sleep
if (finished == tp.count * sourceList->count) if (finished == tp.count * sourceList->count)
break; break;
/* reset the flags */
for (int j = 0; j < tp.count; j++)
target->flags[j] = PARAM_STATE_COLLECTED;
#ifdef _WIN32 #ifdef _WIN32
Sleep((DWORD)sleepTime); Sleep((DWORD)sleepTime);
#else #else
sleep((unsigned)sleepTime / 1000); sleep((unsigned)sleepTime / 1000);
#endif #endif
} }
/* reset the flags */
for (int j = 0; j < tp.count; j++)
target->flags[j] = PARAM_STATE_COLLECTED;
} }
/* wrapper of CollectData */ /* wrapper of CollectData */
...@@ -203,7 +203,8 @@ void XWorkerCollect::CollectP2P(XTensor * source, XTensor * target) ...@@ -203,7 +203,8 @@ void XWorkerCollect::CollectP2P(XTensor * source, XTensor * target)
CheckNTErrors(IsSameShaped(source, target), "The two tensors should be of the same shape!"); CheckNTErrors(IsSameShaped(source, target), "The two tensors should be of the same shape!");
/* target += source */ /* target += source */
Sum(*source, *target, *source); if(source != target)
Sum(*source, *target, *source);
} }
/* /*
......
...@@ -72,6 +72,8 @@ void XWorkerUpdate::UpdateModel(XModel * model, XOptimizer * optimizer, long sle ...@@ -72,6 +72,8 @@ void XWorkerUpdate::UpdateModel(XModel * model, XOptimizer * optimizer, long sle
XTensor * param = params.GetItem(i); XTensor * param = params.GetItem(i);
XTensor * grad = param->grad; XTensor * grad = param->grad;
CheckNTErrors(grad != NULL, "No gradient!");
/* update the parameter */ /* update the parameter */
optimizer->UpdateParam(param, grad, i); optimizer->UpdateParam(param, grad, i);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论