Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
1f9426fd
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
1f9426fd
编写于
11月 29, 2017
作者:
Y
Yancey1989
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add backward
上级
2ce56940
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
144 addition
and
15 deletion
+144
-15
paddle/operators/hierarchical_sigmoid_op.h
paddle/operators/hierarchical_sigmoid_op.h
+47
-5
paddle/operators/math/matrix_bit_code.cc
paddle/operators/math/matrix_bit_code.cc
+64
-7
paddle/operators/math/matrix_bit_code.h
paddle/operators/math/matrix_bit_code.h
+33
-3
未找到文件。
paddle/operators/hierarchical_sigmoid_op.h
浏览文件 @
1f9426fd
...
...
@@ -44,9 +44,9 @@ class HierarchicalSigmoidOpKernel : public framework::OpKernel<T> {
auto
pre_out_mat
=
EigenMatrix
<
T
>::
From
(
pre_out
);
int64_t
batch_size
=
ins
[
0
]
->
dims
()[
0
];
int64_t
size
=
ins
.
size
(
);
int64_t
code_length
=
math
::
FindLastSet
(
num_classes
-
1
);
std
::
vector
<
int64_t
>
pre_out_dims
({
batch_size
,
size
});
std
::
vector
<
int64_t
>
pre_out_dims
({
batch_size
,
code_length
});
pre_out
.
mutable_data
<
T
>
(
framework
::
make_ddim
(
pre_out_dims
),
ctx
.
GetPlace
());
std
::
vector
<
int64_t
>
sum_dims
({
batch_size
,
1UL
});
sum
.
mutable_data
<
T
>
(
framework
::
make_ddim
(
sum_dims
),
ctx
.
GetPlace
());
...
...
@@ -64,8 +64,11 @@ class HierarchicalSigmoidOpKernel : public framework::OpKernel<T> {
pre_out_mat
.
abs
().
cwiseMax
(
static_cast
<
T
>
(
40.0
));
math
::
SumByBitCode
<
T
>
(
num_classes
,
*
label
,
*
out
,
pre_out
,
static_cast
<
T
>
(
-
1
));
// softrelu
pre_out_mat
.
device
(
place
)
=
(
static_cast
<
T
>
(
1
)
+
pre_out_mat
.
exp
()).
log
();
// softrelu with threshold is 40.0
pre_out_mat
.
device
(
place
)
=
pre_out_mat
.
abs
().
cwiseMax
(
static_cast
<
T
>
(
40.0
));
pre_out_mat
.
device
(
place
)
=
(
static_cast
<
T
>
(
1.0
)
+
pre_out_mat
.
exp
()).
log
();
row_sum
(
device_ctx
,
pre_out
,
&
sum
);
col_sum
(
device_ctx
,
*
out
,
&
sum
);
...
...
@@ -75,7 +78,46 @@ class HierarchicalSigmoidOpKernel : public framework::OpKernel<T> {
template
<
typename
Place
,
typename
T
>
class
HierarchicalSigmoidGradOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{}
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
ins
=
ctx
.
MultiInput
<
framework
::
Tensor
>
(
"X"
);
auto
ins_grad
=
ctx
.
MultiOutput
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
params
=
ctx
.
MultiOutput
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Parameters"
));
auto
*
bias
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Bias"
));
auto
*
label
=
ctx
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Label"
));
size_t
num_classes
=
static_cast
<
size_t
>
(
ctx
.
Attr
<
int
>
(
"num_classes"
));
framework
::
Tensor
pre_out
;
auto
place
=
ctx
.
GetEigenDevice
<
Place
>
();
auto
&
dev_ctx
=
ctx
.
device_context
();
int64_t
batch_size
=
ins_grad
.
size
();
int64_t
code_length
=
math
::
FindLastSet
(
num_classes
-
1
);
auto
pre_out_mat
=
EigenMatrix
<
T
>::
From
(
pre_out
);
// init pre_out matrix with {1.0}
std
::
vector
<
int64_t
>
pre_out_dims
({
batch_size
,
code_length
});
pre_out
.
mutable_data
<
T
>
(
framework
::
make_ddim
(
pre_out_dims
),
ctx
.
GetPlace
());
math
::
SetConstant
<
Place
,
T
>
set
;
set
(
dev_ctx
,
&
pre_out
,
static_cast
<
T
>
(
1.0
));
// softrelu derivative
pre_out_mat
.
device
(
place
)
=
pre_out_mat
*
(
static_cast
<
T
>
(
1.0
)
-
static_cast
<
T
>
(
1.0
)
/
pre_out_mat
);
math
::
SubByBitCode
<
T
>
(
num_classes
,
*
label
,
pre_out
);
if
(
bias
)
{
math
::
AddByBitCodeGrad
<
T
>
(
num_classes
,
*
label
,
pre_out
,
*
bias
);
}
for
(
size_t
i
=
0
;
i
<
ins_grad
.
size
();
++
i
)
{
math
::
MulByBitCodeGradWeight
<
T
>
(
num_classes
,
*
label
,
pre_out
,
*
params
[
i
],
*
ins
[
i
]);
math
::
MulByBitCodeGradError
<
T
>
(
num_classes
,
*
label
,
pre_out
,
*
params
[
i
],
*
ins_grad
[
i
]);
}
}
};
}
// namespace operators
...
...
paddle/operators/math/matrix_bit_code.cc
浏览文件 @
1f9426fd
...
...
@@ -69,19 +69,23 @@ static void AddByBitCodeT(Op op, CodeTable code_table,
}
}
/* For j < codeLength:
a(i, j) += b(0, index(i, j))
*/
template
<
typename
T
>
void
AddByBitCode
(
size_t
num_classes
,
const
framework
::
Tensor
&
codes
,
framework
::
Tensor
&
a
,
const
framework
::
Tensor
&
b
)
{
framework
::
Tensor
&
tmat
,
const
framework
::
Tensor
&
vec
)
{
auto
op
=
[](
T
&
t
,
T
&
v
)
{
t
+=
v
;
};
AddByBitCodeT
<
T
>
(
op
,
SimpleCodeTable
(
num_classes
),
codes
,
a
,
b
);
AddByBitCodeT
<
T
>
(
op
,
SimpleCodeTable
(
num_classes
),
codes
,
tmat
,
vec
);
}
template
<
typename
T
>
void
AddByBitCodeGrad
(
size_t
num_classes
,
const
framework
::
Tensor
&
codes
,
const
framework
::
Tensor
&
tmat
,
framework
::
Tensor
&
vec
)
{
auto
op
=
[](
T
&
t
,
T
&
v
)
{
v
+=
t
;
};
AddByBitCode
<
T
>
(
op
,
SimpleCodeTable
(
num_classes
),
codes
,
tmat
,
vec
);
}
template
<
class
CodeTable
,
typename
T
>
void
SumByBitCodeT
(
CodeTable
code_table
,
const
framework
::
Tensor
&
codes
,
framework
::
Tensor
&
tmat
,
framework
::
Tensor
&
sum
,
framework
::
Tensor
&
tmat
,
const
framework
::
Tensor
&
sum
,
const
T
&
scale_sum
)
{
size_t
max_code_length
=
code_table
.
get_max_code_length
();
size_t
num_samples
=
tmat
.
dims
()[
0
];
...
...
@@ -142,8 +146,61 @@ void MulByBitCode(size_t num_classes, const framework::Tensor& codes,
}
t
+=
sum
;
};
MulByBitCode
(
op
,
SimpleCodeTable
(
num_classes
),
codes
,
tmat
,
weight
,
input
);
MulByBitCodeT
<
T
>
(
op
,
SimpleCodeTable
(
num_classes
),
codes
,
tmat
,
weight
,
input
);
}
template
<
typename
T
>
void
MulByBitCodeGradWeight
(
size_t
num_classes
,
const
framework
::
Tensor
&
codes
,
const
framework
::
Tensor
&
tmat
,
framework
::
Tensor
&
weight
,
const
framework
::
Tensor
&
input
)
{
auto
op
=
[](
const
T
t
,
T
*
weight_row
,
const
T
*
input_row
,
size_t
input_dim
)
{
for
(
size_t
k
=
0
;
k
<
input_dim
;
++
k
)
{
weight_row
[
k
]
+=
t
*
input_row
[
k
];
}
};
MulByBitCodeT
<
T
>
(
op
,
SimpleCodeTable
(
num_classes
),
codes
,
tmat
,
weight
,
input
);
}
template
<
typename
T
>
void
MulByBitCodeGradError
(
size_t
num_classes
,
const
framework
::
Tensor
&
codes
,
const
framework
::
Tensor
&
tmat
,
const
framework
::
Tensor
&
weight
,
framework
::
Tensor
&
input
)
{
auto
op
=
[](
const
T
t
,
const
T
*
weight_row
,
T
*
input_row
,
size_t
input_dim
)
{
for
(
size_t
k
=
0
;
k
<
input_dim
;
++
k
)
{
input_row
[
k
]
+=
t
*
weight_row
[
k
];
}
};
MulByBitCodeT
<
T
>
(
op
,
SimpleCodeTable
(
num_classes
),
codes
,
tmat
,
weight
,
input
);
}
template
<
class
CodeTable
,
typename
T
>
void
SubByBitCodeT
(
CodeTable
code_table
,
const
framework
::
Tensor
&
codes
,
framework
::
Tensor
&
tmat
)
{
size_t
max_code_length
=
code_table
.
get_max_code_length
();
size_t
num_samples
=
tmat
.
dims
()[
0
];
size_t
o_width
=
tmat
.
dims
()[
1
];
for
(
size_t
i
=
0
;
i
<
num_samples
;
++
i
)
{
auto
code
=
code_table
(
codes
.
data
<
T
>
()[
i
]);
int
code_length
=
code
.
get_length
();
for
(
int
j
=
0
;
j
<
code_length
;
++
j
)
{
if
(
code
.
calc_bit
(
j
))
{
tmat
.
data
<
T
>
()[
i
*
o_width
+
j
]
-=
1
;
}
}
}
}
template
<
typename
T
>
void
SubByBitCode
(
size_t
num_classes
,
const
framework
::
Tensor
&
codes
,
framework
::
Tensor
&
tmat
)
{
SubByBitCodeT
<
T
>
(
SimpleCodeTable
(
num_classes
),
codes
,
tmat
);
}
}
// namespace math
}
// namespace operators
}
// namespace paddle
paddle/operators/math/matrix_bit_code.h
浏览文件 @
1f9426fd
...
...
@@ -59,27 +59,57 @@ struct SimpleCodeTable {
int
max_code_length_
;
};
/* For j < code
L
ength
/* For j < code
_l
ength
tmat(i, j) += vec(0, index(i, j))
*/
template
<
typename
T
>
void
AddByBitCode
(
size_t
num_classes
,
const
framework
::
Tensor
&
codes
,
framework
::
Tensor
&
tmat
,
const
framework
::
Tensor
&
vec
);
/* For j < codeLength
/* For j < code_length
vec(0, index(i, j)) += tmat(i, j)
*/
template
<
typename
T
>
void
AddByBitCodeGrad
(
size_t
num_classes
,
const
framework
::
Tensor
&
codes
,
const
framework
::
Tensor
&
tmat
,
framework
::
Tensor
&
vec
);
/* For j < code_length
sum(i, 0) = \sum_j bit(i, j) * tmat(i, j)
*/
template
<
typename
T
>
void
SumByBitCode
(
size_t
num_classes
,
const
framework
::
Tensor
&
codes
,
framework
::
Tensor
&
tmat
,
framework
::
Tensor
&
sum
,
T
scale_sum
);
/* For j < code
L
ength
/* For j < code
_l
ength
input.row(i) += tmat(i, j) * weight.row(index(i, j))
*/
template
<
typename
T
>
void
MulByBitCode
(
size_t
num_classes
,
const
framework
::
Tensor
&
codes
,
framework
::
Tensor
&
tmat
,
const
framework
::
Tensor
&
weight
,
const
framework
::
Tensor
&
input
);
/* For index(i, j) >= 0:
weight.row(index(i, j)) += tmat(i, j) * input.row(i)
*/
template
<
typename
T
>
void
MulByBitCodeGradWeight
(
size_t
num_classes
,
const
framework
::
Tensor
&
codes
,
const
framework
::
Tensor
&
tmat
,
framework
::
Tensor
&
weight
,
const
framework
::
Tensor
&
input
);
/* For j < code_length
input.row(i) += tmat(i, j) * weight.row(index(i, j))
*/
template
<
typename
T
>
void
MulByBitCodeGradError
(
size_t
num_classes
,
const
framework
::
Tensor
&
codes
,
const
framework
::
Tensor
&
tmat
,
const
framework
::
Tensor
&
weight
,
framework
::
Tensor
&
input
);
/* For j < code_length
tmat(i, j) -= bit(i, j)
*/
template
<
typename
T
>
void
SubByBitCode
(
size_t
num_classes
,
const
framework
::
Tensor
&
codes
,
framework
::
Tensor
&
tmat
);
}
// namespace math
}
// namespace operators
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录