Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
N
NiuTrans.Tensor
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
杨迪
NiuTrans.Tensor
Commits
6a4741bc
Commit
6a4741bc
authored
Oct 02, 2018
by
xiaotong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
clean the code
parent
d992a0e9
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
47 行增加
和
58 行删除
+47
-58
source/tensor/core/movement/Gather.h
+2
-3
source/tensor/core/movement/Spread.cpp
+1
-4
source/tensor/function/CrossEntropy.cpp
+25
-29
source/tensor/function/CrossEntropy.cu
+5
-6
source/tensor/function/CrossEntropy.cuh
+5
-6
source/tensor/function/CrossEntropy.h
+9
-10
没有找到文件。
source/tensor/core/movement/Gather.h
查看文件 @
6a4741bc
...
...
@@ -30,9 +30,9 @@ namespace nts { // namespace nts(NiuTrans.Tensor)
void
_Gather
(
const
XTensor
*
s
,
XTensor
*
t
,
int
dim
,
int
*
srcIndex
,
int
indexSize
);
/* gather selected sub-tensors (return a XTensor structure)
make a new tensor to keep the result and return it */
make a new tensor to keep the result and return it */
XTensor
Gather
(
const
XTensor
&
s
,
int
dim
,
int
*
srcIndex
,
int
indexSize
);
}
// namespace nts(NiuTrans.Tensor)
#endif // __GATHER_H__
\ No newline at end of file
#endif // __GATHER_H__
source/tensor/core/movement/Spread.cpp
查看文件 @
6a4741bc
...
...
@@ -62,7 +62,6 @@ void _Spread(XTensor * source, XTensor * collection, int dim,
int
*
srcIndex
,
int
indexSize
,
int
*
collIndex
)
{
int
order
=
source
->
order
;
int
size
=
source
->
GetDim
(
dim
);
CheckNTErrors
(
source
->
dataType
==
DEFAULT_DTYPE
,
"TODO!"
);
CheckNTErrors
(
dim
>=
0
&&
dim
<
order
,
"Illegal dimension!"
);
...
...
@@ -150,7 +149,6 @@ void _SpreadForGather(XTensor * source, XTensor * collection, int dim,
int
*
srcIndex
,
int
indexSize
,
int
*
collIndex
)
{
int
order
=
source
->
order
;
int
size
=
source
->
GetDim
(
dim
);
CheckNTErrors
(
source
->
dataType
==
DEFAULT_DTYPE
,
"TODO!"
);
CheckNTErrors
(
dim
>=
0
&&
dim
<
order
,
"Illegal dimension!"
);
...
...
@@ -199,4 +197,4 @@ void _SpreadForGather(XTensor * source, XTensor * collection, int dim,
}
}
}
//
namespace
nts
(
NiuTrans
.
Tensor
)
\ No newline at end of file
}
// namespace nts(NiuTrans.Tensor)
source/tensor/function/CrossEntropy.cpp
查看文件 @
6a4741bc
...
...
@@ -61,42 +61,39 @@ void _CrossEntropy(const XTensor * output, const XTensor * gold,
CheckNTErrors
(
loss
->
order
==
output
->
order
-
1
,
"Wrong loss dimension!"
);
CheckNTErrors
(
gold
->
dataType
==
DEFAULT_DTYPE
&&
output
->
dataType
==
DEFAULT_DTYPE
,
"TODO!"
);
XTensor
*
logInter
=
NewTensorBuf
(
output
,
output
->
devID
,
output
->
mem
);
XTensor
*
mulInter
=
NewTensorBuf
(
output
,
output
->
devID
,
output
->
mem
);
XTensor
*
negInter
=
NewTensorBuf
(
output
,
output
->
devID
,
output
->
mem
);
XTensor
*
logBuf
=
NewTensorBuf
(
output
,
output
->
devID
,
output
->
mem
);
XTensor
*
mulBuf
=
NewTensorBuf
(
output
,
output
->
devID
,
output
->
mem
);
XTensor
*
negBuf
=
NewTensorBuf
(
output
,
output
->
devID
,
output
->
mem
);
/* l = log(output) */
_Log
(
output
,
logBuf
);
if
(
weight
!=
NULL
){
XTensor
*
weightBuf
=
NewTensorBuf
(
output
,
output
->
devID
,
output
->
mem
);
/* multiply gold and weight by broadcast wg = mulDim(g * w) */
/* multiply gold with weight by broadcast wg = mulDim(g * w) */
_MultiplyDim
(
gold
,
weight
,
weightBuf
,
n
,
0
);
/* multiply weighted gold and log(output) wgl = mul(wg, l) */
/* multiply weighted gold with log(output) wgl = mul(wg, l) */
_Multiply
(
weightBuf
,
logBuf
,
mulBuf
,
0
);
DelTensorBuf
(
weightBuf
);
}
else
{
/* multiply gold
and
log(output) gl = mul(g, l) */
/* multiply gold
with
log(output) gl = mul(g, l) */
_Multiply
(
gold
,
logBuf
,
mulBuf
,
0
);
}
/* negate
multiply
result n = negate(mul) */
/* negate result n = negate(mul) */
_NegateMe
(
mulBuf
);
_ReduceSum
(
mulBuf
,
loss
,
n
);
DelTensorBuf
(
negInter
);
DelTensorBuf
(
mulInter
);
DelTensorBuf
(
logInter
);
DelTensorBuf
(
mulBuf
);
DelTensorBuf
(
logBuf
);
}
/*
compute the cross entropy loss (
implementation manually
)
compute the cross entropy loss (
faster implementation with optimized code
)
loss = sum_{i} (-gold_i * log(output_i))
where gold and output are distributions
...
...
@@ -108,9 +105,9 @@ where gold and output are distributions
>> padding - specify a target value that is ignored and does not contribute to the loss computation
>> leadingDim - the leading dimension for the output
*/
void
_CrossEntropy
Manual
(
const
XTensor
*
output
,
const
XTensor
*
gold
,
XTensor
*
loss
,
const
XTensor
*
weight
,
const
XTensor
*
padding
,
int
leadingDim
)
void
_CrossEntropy
Fast
(
const
XTensor
*
output
,
const
XTensor
*
gold
,
XTensor
*
loss
,
const
XTensor
*
weight
,
const
XTensor
*
padding
,
int
leadingDim
)
{
#ifdef USE_CUDA
if
(
output
->
devID
>=
0
)
{
...
...
@@ -263,21 +260,22 @@ DTYPE _CrossEntropy(const XTensor * output, const XTensor * gold,
XTensor
*
logBuf
=
NewTensorBuf
(
output
,
output
->
devID
,
output
->
mem
);
XTensor
*
mulBuf
=
NewTensorBuf
(
output
,
output
->
devID
,
output
->
mem
);
XTensor
*
negBuf
=
NewTensorBuf
(
output
,
output
->
devID
,
output
->
mem
);
/* l = log(output) */
_Log
(
output
,
logBuf
);
if
(
weight
!=
NULL
){
XTensor
*
weightBuf
=
NewTensorBuf
(
output
,
output
->
devID
,
output
->
mem
);
/* multiply gold and weight by broadcast wg = mulDim(g * w) */
/* multiply gold with weight by broadcast wg = mulDim(g * w) */
_MultiplyDim
(
gold
,
weight
,
weightBuf
,
n
,
0
);
/* multiply weighted gold and log(output) wgl = mul(wg, l) */
/* multiply weighted gold with log(output) wgl = mul(wg, l) */
_Multiply
(
weightBuf
,
logBuf
,
mulBuf
,
0
);
DelTensorBuf
(
weightBuf
);
}
else
{
/* multiply gold
and
log(output) gl = mul(g, l) */
/* multiply gold
with
log(output) gl = mul(g, l) */
_Multiply
(
gold
,
logBuf
,
mulBuf
,
0
);
}
...
...
@@ -291,7 +289,6 @@ DTYPE _CrossEntropy(const XTensor * output, const XTensor * gold,
/* reduce sum all classes */
_ReduceSum
(
mulBuf
,
lossInter
,
n
);
DelTensorBuf
(
negBuf
);
DelTensorBuf
(
mulBuf
);
DelTensorBuf
(
logBuf
);
...
...
@@ -334,7 +331,7 @@ DTYPE _CrossEntropy(const XTensor * output, const XTensor * gold,
}
/*
compute the cross entropy loss (
implementation manually
)
compute the cross entropy loss (
faster implementation with optimized code
)
loss = sum_{i} (-gold_i * log(output_i))
where gold and output are distributions
...
...
@@ -347,9 +344,9 @@ where gold and output are distributions
>> leadingDim - the leading dimension for the output
<< return - the cross entropy loss that is a scalar
*/
DTYPE
_CrossEntropy
Manual
(
const
XTensor
*
output
,
const
XTensor
*
gold
,
LOSS_COMPUTE_WAY
reduceWay
,
const
XTensor
*
weight
,
const
XTensor
*
padding
,
int
leadingDim
)
DTYPE
_CrossEntropy
Fast
(
const
XTensor
*
output
,
const
XTensor
*
gold
,
LOSS_COMPUTE_WAY
reduceWay
,
const
XTensor
*
weight
,
const
XTensor
*
padding
,
int
leadingDim
)
{
#ifdef USE_CUDA
if
(
output
->
devID
>=
0
)
{
...
...
@@ -459,7 +456,7 @@ DTYPE _CrossEntropyManual(const XTensor * output, const XTensor * gold,
}
/*
backward compuation for cross entropy function
(tensor version)
backward compuation for cross entropy function
loss = sum_{i} (-t_i * log(y_i))
dE/dy_i = -t_i / y_i
...
...
@@ -575,4 +572,4 @@ void _CrossEntropyBackward(XTensor * dedy, const XTensor * output, const XTensor
}
}
}
//
namespace
nts
(
NiuTrans
.
Tensor
)
\ No newline at end of file
}
// namespace nts(NiuTrans.Tensor)
source/tensor/function/CrossEntropy.cu
查看文件 @
6a4741bc
...
...
@@ -111,7 +111,7 @@ where gold and output are distributions
>> padding - specify a target value that is ignored and does not contribute to the loss computation
>> leadingDim - the leading dimension for the output
*/
void _CudaCrossEntropy
Manual
(const XTensor * output, const XTensor * gold,
void _CudaCrossEntropy
Fast
(const XTensor * output, const XTensor * gold,
XTensor * loss, const XTensor * weight,
const XTensor * padding, int leadingDim)
{
...
...
@@ -201,9 +201,9 @@ where gold and output are distributions
>> leadingDim - the leading dimension for the output
<< return - the cross entropy loss that is a scalar
*/
DTYPE _CudaCrossEntropy
Manual
(const XTensor * output, const XTensor * gold,
LOSS_COMPUTE_WAY reduceWay, const XTensor * weight,
const XTensor * padding, int leadingDim)
DTYPE _CudaCrossEntropy
Fast
(const XTensor * output, const XTensor * gold,
LOSS_COMPUTE_WAY reduceWay, const XTensor * weight,
const XTensor * padding, int leadingDim)
{
DTYPE loss = 0;
...
...
@@ -414,4 +414,4 @@ void _CudaCrossEntropyBackward(XTensor * dedy, const XTensor * output, const XTe
} // namespace nts(NiuTrans.Tensor)
#endif // __CROSSENTROPY_CUH__
\ No newline at end of file
#endif // __CROSSENTROPY_CUH__
source/tensor/function/CrossEntropy.cuh
查看文件 @
6a4741bc
...
...
@@ -27,13 +27,13 @@
namespace nts{ // namespace nts(NiuTrans.Tensor)
/* compute the cross entropy loss
(tensor version)
*/
void _CudaCrossEntropy
Manual
(const XTensor * output, const XTensor * gold,
/* compute the cross entropy loss */
void _CudaCrossEntropy
Fast
(const XTensor * output, const XTensor * gold,
XTensor * loss, const XTensor * weight = NULL,
const XTensor * padding = NULL, int leadingDim = -1);
/* compute the cross entropy loss
(scalar version)
*/
DTYPE _CudaCrossEntropy
Manual
(const XTensor * output, const XTensor * gold,
/* compute the cross entropy loss */
DTYPE _CudaCrossEntropy
Fast
(const XTensor * output, const XTensor * gold,
LOSS_COMPUTE_WAY reduceWay, const XTensor * weight = NULL,
const XTensor * padding = NULL, int leadingDim = -1);
...
...
@@ -45,4 +45,4 @@ void _CudaCrossEntropyBackward(XTensor * dedy, const XTensor * output, const XTe
} // namespace nts(NiuTrans.Tensor)
#endif // __CROSSENTROPY_CUH__
\ No newline at end of file
#endif // __CROSSENTROPY_CUH__
source/tensor/function/CrossEntropy.h
查看文件 @
6a4741bc
...
...
@@ -31,25 +31,25 @@ REDUCE_SUM,
REDUCE_MEAN
};
/* compute the cross entropy loss
(tensor version)
*/
/* compute the cross entropy loss */
void
_CrossEntropy
(
const
XTensor
*
output
,
const
XTensor
*
gold
,
XTensor
*
loss
,
const
XTensor
*
weight
=
NULL
,
const
XTensor
*
padding
=
NULL
,
int
leadingDim
=
-
1
);
/* compute the cross entropy loss
(tensor version)
*/
void
_CrossEntropy
Manual
(
const
XTensor
*
output
,
const
XTensor
*
gold
,
/* compute the cross entropy loss */
void
_CrossEntropy
Fast
(
const
XTensor
*
output
,
const
XTensor
*
gold
,
XTensor
*
loss
,
const
XTensor
*
weight
=
NULL
,
const
XTensor
*
padding
=
NULL
,
int
leadingDim
=
-
1
);
/* compute the cross entropy loss (
scalar version
) */
/* compute the cross entropy loss (
return the loss
) */
DTYPE
_CrossEntropy
(
const
XTensor
*
output
,
const
XTensor
*
gold
,
LOSS_COMPUTE_WAY
reduceWay
,
const
XTensor
*
weight
=
NULL
,
const
XTensor
*
padding
=
NULL
,
int
leadingDim
=
-
1
);
/* compute the cross entropy loss (
scalar version
) */
DTYPE
_CrossEntropy
Manual
(
const
XTensor
*
output
,
const
XTensor
*
gold
,
LOSS_COMPUTE_WAY
reduceWay
=
REDUCE_MEAN
,
const
XTensor
*
weight
=
NULL
,
const
XTensor
*
padding
=
NULL
,
int
leadingDim
=
-
1
);
/* compute the cross entropy loss (
return the loss
) */
DTYPE
_CrossEntropy
Fast
(
const
XTensor
*
output
,
const
XTensor
*
gold
,
LOSS_COMPUTE_WAY
reduceWay
=
REDUCE_MEAN
,
const
XTensor
*
weight
=
NULL
,
const
XTensor
*
padding
=
NULL
,
int
leadingDim
=
-
1
);
/* backward computation of cross entropy function */
void
_CrossEntropyBackward
(
XTensor
*
dedy
,
const
XTensor
*
output
,
const
XTensor
*
gold
,
...
...
@@ -58,4 +58,4 @@ void _CrossEntropyBackward(XTensor * dedy, const XTensor * output, const XTensor
}
// namespace nts(NiuTrans.Tensor)
#endif // __CROSSENTROPY_H__
\ No newline at end of file
#endif // __CROSSENTROPY_H__
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论