Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
N
NiuTrans.Tensor
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
8
Issues
8
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
NiuTrans
NiuTrans.Tensor
Commits
d5269725
Commit
d5269725
authored
Mar 07, 2021
by
xiaotong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bug fixes
parent
9eda6d83
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
68 行增加
和
12 行删除
+68
-12
source/train/TTrain.cpp
+29
-3
source/train/XLeader.cpp
+5
-2
source/train/XModel.cpp
+18
-0
source/train/XModel.h
+3
-0
source/train/XOptimizer.cpp
+3
-0
source/train/XWorkerCollect.cpp
+8
-7
source/train/XWorkerUpdate.cpp
+2
-0
没有找到文件。
source/train/TTrain.cpp
查看文件 @
d5269725
...
...
@@ -74,6 +74,7 @@ void TestTrain()
XConfig
config
;
config
.
Add
(
"dev"
,
-
1
);
config
.
Add
(
"lrate"
,
0.1
F
);
TTDataLoader
loader
;
loader
.
SetFileName
(
"ttrain.txt"
);
...
...
@@ -217,9 +218,14 @@ void TTModel::SetConfig(XConfig &myConfig)
config
.
CreateFromMe
(
myConfig
);
}
/* initialize the model */
/*
initialize the model
>> myConfig - configuration
>> devID - device id
*/
void
TTModel
::
Init
(
XConfig
&
myConfig
,
int
devID
)
{
Clear
();
SetConfig
(
myConfig
);
vSize
=
MAX_INT_IN_TTRAIN
+
1
;
...
...
@@ -230,12 +236,25 @@ void TTModel::Init(XConfig &myConfig, int devID)
InitTensor2D
(
&
hiddenW
,
3
*
eSize
,
hSize
,
X_FLOAT
,
devID
);
InitTensor2D
(
&
outputW
,
hSize
,
vSize
,
X_FLOAT
,
devID
);
embeddingW
.
SetName
(
"embeddingw"
);
hiddenW
.
SetName
(
"hiddenw"
);
outputW
.
SetName
(
"outputw"
);
embeddingW
.
SetDataRand
(
-
0.1
F
,
0.1
F
);
hiddenW
.
SetDataRand
(
-
0.1
F
,
0.1
F
);
outputW
.
SetDataRand
(
-
0.1
F
,
0.1
F
);
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
)
{
XTensor
embedding
;
...
...
@@ -261,7 +280,10 @@ void TTModel::Clear()
config
.
Clear
();
}
/* clone the model */
/*
clone the model
>> devID - device id
*/
XModel
*
TTModel
::
Clone
(
int
devID
)
{
TTModel
*
model
=
new
TTModel
();
...
...
@@ -293,8 +315,10 @@ bool TTModel::RunSimple(XList * inputs, XList * outputs, XList * golds, XList* l
XNet
net
;
/* create the neural network and run it */
Forward
(
devID
,
input
,
output
);
/* gold standard in ong-hot representaiton */
goldOneHot
=
IndexToOnehot
(
*
gold
,
vSize
,
0.0
F
);
int
*
dims
=
new
int
[
goldOneHot
.
order
];
...
...
@@ -303,8 +327,10 @@ bool TTModel::RunSimple(XList * inputs, XList * outputs, XList * golds, XList* l
dims
[
goldOneHot
.
order
-
2
]
=
goldOneHot
.
GetDim
(
goldOneHot
.
order
-
1
);
goldOneHot
.
Reshape
(
goldOneHot
.
order
-
1
,
dims
);
/* loss */
*
loss
=
CrossEntropy
(
*
output
,
goldOneHot
);
/* back-propagation */
net
.
Backward
(
*
loss
);
delete
[]
dims
;
...
...
source/train/XLeader.cpp
查看文件 @
d5269725
...
...
@@ -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
)
{
serverModel
.
params
.
Clear
();
serverModel
.
params
.
AddList
(
&
model
->
params
);
serverModel
.
Clear
();
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 */
}
...
...
source/train/XModel.cpp
查看文件 @
d5269725
...
...
@@ -97,6 +97,24 @@ bool XModel::RunMe(XList * args)
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 */
void
XModel
::
RefreshMe
()
{
...
...
source/train/XModel.h
查看文件 @
d5269725
...
...
@@ -89,6 +89,8 @@ protected:
bool
RunMe
(
XList
*
args
);
public
:
/* add a parameter tensor */
void
AddParam
(
XTensor
*
param
);
/* refresh the model */
void
RefreshMe
();
...
...
@@ -100,6 +102,7 @@ public:
/* wrapper of Run() */
static
bool
Run
(
XList
*
args
);
};
}
...
...
source/train/XOptimizer.cpp
查看文件 @
d5269725
...
...
@@ -47,6 +47,9 @@ initialize the optimizer
*/
void
XOptimizer
::
Init
(
XConfig
&
config
)
{
nstep
=
config
.
GetInt
(
"nstep"
,
100000
);
nepoch
=
config
.
GetInt
(
"nepoch"
,
50
);
lrate
=
config
.
GetFloat
(
"lrate"
,
0.1
F
);
}
/* clear the optimizer */
...
...
source/train/XWorkerCollect.cpp
查看文件 @
d5269725
...
...
@@ -76,7 +76,7 @@ void XWorkerCollect::CollectData(XList * sourceList, XModel * target, long sleep
if
(
collectMode
==
DATA_COLLECT_P2P
)
{
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 the
A
computation
(in another process) */
if
(
target
->
flags
[
j
]
==
PARAM_STATE_COLLECTED
||
!
tp
[
j
]
->
isGradFinished
)
continue
;
...
...
@@ -91,7 +91,7 @@ void XWorkerCollect::CollectData(XList * sourceList, XModel * target, long sleep
if
(
source
->
flags
[
j
]
!=
PARAM_STATE_COLLECTED
&&
sp
[
j
]
->
isGradFinished
)
{
/* data transmit */
CollectP2P
(
sp
.
GetItem
(
j
)
->
grad
,
tp
.
GetItem
(
j
)
->
grad
);
CollectP2P
(
sp
[
j
]
->
grad
,
tp
[
j
]
->
grad
);
/* reset the flag */
source
->
flags
[
j
]
=
PARAM_STATE_COLLECTED
;
...
...
@@ -158,16 +158,16 @@ void XWorkerCollect::CollectData(XList * sourceList, XModel * target, long sleep
if
(
finished
==
tp
.
count
*
sourceList
->
count
)
break
;
/* reset the flags */
for
(
int
j
=
0
;
j
<
tp
.
count
;
j
++
)
target
->
flags
[
j
]
=
PARAM_STATE_COLLECTED
;
#ifdef _WIN32
Sleep
((
DWORD
)
sleepTime
);
#else
sleep
((
unsigned
)
sleepTime
/
1000
);
#endif
}
/* reset the flags */
for
(
int
j
=
0
;
j
<
tp
.
count
;
j
++
)
target
->
flags
[
j
]
=
PARAM_STATE_COLLECTED
;
}
/* wrapper of CollectData */
...
...
@@ -203,7 +203,8 @@ void XWorkerCollect::CollectP2P(XTensor * source, XTensor * target)
CheckNTErrors
(
IsSameShaped
(
source
,
target
),
"The two tensors should be of the same shape!"
);
/* target += source */
Sum
(
*
source
,
*
target
,
*
source
);
if
(
source
!=
target
)
Sum
(
*
source
,
*
target
,
*
source
);
}
/*
...
...
source/train/XWorkerUpdate.cpp
查看文件 @
d5269725
...
...
@@ -72,6 +72,8 @@ void XWorkerUpdate::UpdateModel(XModel * model, XOptimizer * optimizer, long sle
XTensor
*
param
=
params
.
GetItem
(
i
);
XTensor
*
grad
=
param
->
grad
;
CheckNTErrors
(
grad
!=
NULL
,
"No gradient!"
);
/* update the parameter */
optimizer
->
UpdateParam
(
param
,
grad
,
i
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论