Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
ffafc5c9
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 2 年 前同步成功
通知
2325
Star
20933
Fork
5424
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
ffafc5c9
编写于
8月 07, 2017
作者:
C
caoying03
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix the SubNestedSequenceLayer implementations.
上级
29fa73bc
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
1982 addition
and
1932 deletion
+1982
-1932
paddle/gserver/layers/SubNestedSequenceLayer.cpp
paddle/gserver/layers/SubNestedSequenceLayer.cpp
+74
-14
paddle/gserver/tests/test_LayerGrad.cpp
paddle/gserver/tests/test_LayerGrad.cpp
+1904
-1916
python/paddle/trainer_config_helpers/layers.py
python/paddle/trainer_config_helpers/layers.py
+4
-2
未找到文件。
paddle/gserver/layers/SubNestedSequenceLayer.cpp
浏览文件 @
ffafc5c9
...
@@ -31,16 +31,22 @@ public:
...
@@ -31,16 +31,22 @@ public:
void
backward
(
const
UpdateCallback
&
callback
=
nullptr
)
override
;
void
backward
(
const
UpdateCallback
&
callback
=
nullptr
)
override
;
private:
private:
void
calSelectedCols
(
const
MatrixPtr
scores
,
void
reorganizeSeqInfo
(
const
ICpuGpuVectorPtr
seqStartPos
,
const
int
*
seqStartPos
,
const
ICpuGpuVectorPtr
subSeqStartPos
);
const
int
*
subSeqStartPos
);
void
calSelectedCols
(
const
MatrixPtr
selectedIndices
,
const
std
::
vector
<
std
::
vector
<
int
>>
inputSeqInfo
);
void
buildOutputSeqInfo
();
void
buildOutputSeqInfo
();
std
::
vector
<
int
>
outSeqStartInfo_
;
std
::
vector
<
int
>
outSeqStartInfo_
;
std
::
vector
<
int
>
outSubSeqStartInfo_
;
std
::
vector
<
int
>
outSubSeqStartInfo_
;
MatrixPtr
scoreOverInputSeq_
;
// if the second input of this layer is on GPU memory, copy it to CPU memory.
MatrixPtr
selIdsCpu_
;
// reorganize sequenceStartPositions and subSequenceStartPositions altogether
// into a 2d vector to facilitate the sequence selection process.
std
::
vector
<
std
::
vector
<
int
>>
inputSeqInfo_
;
// the final seleted row indices in a batch,
// rowIdx_ and selectedRows_ actually share a same memory.
// rowIdx_ and selectedRows_ actually share a same memory.
IVectorPtr
rowIndice_
;
IVectorPtr
rowIndice_
;
std
::
vector
<
int
>
selectedRows_
;
std
::
vector
<
int
>
selectedRows_
;
...
@@ -57,12 +63,47 @@ bool SubNestedSequenceLayer::init(const LayerMap& layerMap,
...
@@ -57,12 +63,47 @@ bool SubNestedSequenceLayer::init(const LayerMap& layerMap,
return
true
;
return
true
;
}
}
void
SubNestedSequenceLayer
::
calSelectedCols
(
const
MatrixPtr
selected_indices
,
void
SubNestedSequenceLayer
::
reorganizeSeqInfo
(
const
int
*
seqStartPos
,
const
ICpuGpuVectorPtr
seqStartPos
,
const
ICpuGpuVectorPtr
subSeqStartPos
)
{
const
int
*
subSeqStartPos
)
{
int
*
seqStarts
=
seqStartPos
->
getMutableData
(
false
);
int
*
subSeqStarts
=
subSeqStartPos
->
getMutableData
(
false
);
int
seqNum
=
seqStartPos
->
getSize
()
-
1
;
inputSeqInfo_
.
resize
(
seqNum
,
std
::
vector
<
int
>
());
int
seqIdx
=
0
;
for
(
size_t
i
=
0
;
i
<
subSeqStartPos
->
getSize
();
++
i
)
{
inputSeqInfo_
[
seqIdx
].
push_back
(
subSeqStarts
[
i
]);
if
(
subSeqStarts
[
i
]
==
seqStarts
[
seqIdx
+
1
])
{
seqIdx
++
;
if
(
seqIdx
==
seqNum
)
return
;
inputSeqInfo_
[
seqIdx
].
push_back
(
subSeqStarts
[
i
]);
}
}
}
void
SubNestedSequenceLayer
::
calSelectedCols
(
const
MatrixPtr
selectedIndices
,
const
std
::
vector
<
std
::
vector
<
int
>>
inputSeqInfo
)
{
selectedRows_
.
clear
();
selectedRows_
.
clear
();
outSubSeqStartInfo_
.
resize
(
1
,
0
);
outSubSeqStartInfo_
.
resize
(
1
,
0
);
outSeqStartInfo_
.
resize
(
1
,
0
);
outSeqStartInfo_
.
resize
(
1
,
0
);
size_t
seqNum
=
selectedIndices
->
getHeight
();
size_t
beamSize
=
selectedIndices
->
getWidth
();
for
(
size_t
i
=
0
;
i
<
seqNum
;
++
i
)
{
for
(
size_t
j
=
0
;
j
<
beamSize
;
++
j
)
{
if
(
selectedIndices
->
getElement
(
i
,
j
)
==
-
1.
)
break
;
int
selSubSeqIdx
=
selectedIndices
->
getElement
(
i
,
j
);
CHECK_GT
(
inputSeqInfo_
[
i
].
size
()
-
1
,
selSubSeqIdx
);
size_t
subSeqLen
=
inputSeqInfo_
[
i
][
selSubSeqIdx
+
1
]
-
inputSeqInfo_
[
i
][
selSubSeqIdx
];
for
(
size_t
k
=
0
;
k
<
subSeqLen
;
++
k
)
selectedRows_
.
push_back
(
inputSeqInfo_
[
i
][
selSubSeqIdx
]
+
k
);
outSubSeqStartInfo_
.
push_back
(
outSubSeqStartInfo_
.
back
()
+
subSeqLen
);
}
outSeqStartInfo_
.
push_back
(
outSubSeqStartInfo_
.
back
());
}
}
}
void
SubNestedSequenceLayer
::
buildOutputSeqInfo
()
{
void
SubNestedSequenceLayer
::
buildOutputSeqInfo
()
{
...
@@ -83,17 +124,35 @@ void SubNestedSequenceLayer::forward(PassType passType) {
...
@@ -83,17 +124,35 @@ void SubNestedSequenceLayer::forward(PassType passType) {
Layer
::
forward
(
passType
);
Layer
::
forward
(
passType
);
const
Argument
&
inputSeq
=
getInput
(
0
);
const
Argument
&
inputSeq
=
getInput
(
0
);
const
MatrixPtr
selected_indices
=
getInputValue
(
1
);
CHECK
(
inputSeq
.
hasSubseq
())
<<
"The first input of SubNestSequence layer "
CHECK
(
inputSeq
.
hasSubseq
())
<<
"The first input of SubNestSequence layer "
<<
"must be a nested sequence."
;
<<
"must be a nested sequence."
;
CHECK_EQ
(
inputSeq
.
getNumSequences
(),
selected_indices
->
getHeight
());
const
MatrixPtr
selectedIndices
=
getInputValue
(
1
);
CHECK_EQ
(
inputSeq
.
getNumSequences
(),
selectedIndices
->
getHeight
());
calSelectedCols
(
selected_indices
,
inputSeq
.
sequenceStartPositions
->
getMutableData
(
false
),
if
(
dynamic_cast
<
GpuMatrix
*>
(
selectedIndices
.
get
()))
{
inputSeq
.
subSequenceStartPositions
->
getMutableData
(
false
));
/*
* Currently, the second input for this layer generated by
* kmax_sequence_score_layer whose output is always stored on CPU,
* or a data_layer which canbe on GPU.
*
* If the second input is on GPU, copy it to CPU memory, because this
* input always uses very few memory, and operations related to it are
* all logic control, not computations.
*/
Matrix
::
resizeOrCreate
(
selIdsCpu_
,
selectedIndices
->
getHeight
(),
selectedIndices
->
getWidth
(),
false
/* trans */
,
false
/* useGpu */
);
selIdsCpu_
->
copyFrom
(
*
selectedIndices
);
}
else
{
selIdsCpu_
=
selectedIndices
;
}
reorganizeSeqInfo
(
inputSeq
.
sequenceStartPositions
,
inputSeq
.
subSequenceStartPositions
);
calSelectedCols
(
selIdsCpu_
,
inputSeqInfo_
);
resetOutput
(
selectedRows_
.
size
(),
getSize
());
resetOutput
(
selectedRows_
.
size
(),
getSize
());
buildOutputSeqInfo
();
if
(
useGpu_
)
{
if
(
useGpu_
)
{
rowIndice_
=
IVector
::
create
(
selectedRows_
.
size
(),
useGpu_
);
rowIndice_
=
IVector
::
create
(
selectedRows_
.
size
(),
useGpu_
);
...
@@ -103,6 +162,7 @@ void SubNestedSequenceLayer::forward(PassType passType) {
...
@@ -103,6 +162,7 @@ void SubNestedSequenceLayer::forward(PassType passType) {
IVector
::
create
(
selectedRows_
.
data
(),
selectedRows_
.
size
(),
useGpu_
);
IVector
::
create
(
selectedRows_
.
data
(),
selectedRows_
.
size
(),
useGpu_
);
}
}
buildOutputSeqInfo
();
getOutputValue
()
->
selectRows
(
*
getInputValue
(
0
),
*
rowIndice_
);
getOutputValue
()
->
selectRows
(
*
getInputValue
(
0
),
*
rowIndice_
);
}
}
...
...
paddle/gserver/tests/test_LayerGrad.cpp
浏览文件 @
ffafc5c9
...
@@ -32,1887 +32,1872 @@ DECLARE_double(checkgrad_eps);
...
@@ -32,1887 +32,1872 @@ DECLARE_double(checkgrad_eps);
DECLARE_bool
(
thread_local_rand_use_global_seed
);
DECLARE_bool
(
thread_local_rand_use_global_seed
);
DECLARE_bool
(
prev_batch_state
);
DECLARE_bool
(
prev_batch_state
);
// TEST(Operator, dot_mul) {
TEST
(
Operator
,
dot_mul
)
{
// TestConfig config;
TestConfig
config
;
// config.layerConfig.set_size(10);
config
.
layerConfig
.
set_size
(
10
);
//
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 10, 0});
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
10
,
0
});
// config.inputDefs.push_back({INPUT_DATA, "layer_1", 10, 0});
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_1"
,
10
,
0
});
// config.layerConfig.add_inputs();
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.add_inputs();
config
.
layerConfig
.
add_inputs
();
//
// OperatorConfig& operatorConf = *config.layerConfig.add_operator_confs();
OperatorConfig
&
operatorConf
=
*
config
.
layerConfig
.
add_operator_confs
();
// operatorConf.set_type("dot_mul");
operatorConf
.
set_type
(
"dot_mul"
);
// operatorConf.set_dotmul_scale(-1);
operatorConf
.
set_dotmul_scale
(
-
1
);
//
// testOperatorGrad(config, operatorConf, 100, false, false);
testOperatorGrad
(
config
,
operatorConf
,
100
,
false
,
false
);
// }
}
//
// TEST(Projection, context) {
TEST
(
Projection
,
context
)
{
// for (auto contextStart : {-5, -3, -1, 0, 3}) {
for
(
auto
contextStart
:
{
-
5
,
-
3
,
-
1
,
0
,
3
})
{
// for (auto contextLength : {1, 2, 5, 7}) {
for
(
auto
contextLength
:
{
1
,
2
,
5
,
7
})
{
// for (auto batchSize : {1, 2, 5, 20, 50}) {
for
(
auto
batchSize
:
{
1
,
2
,
5
,
20
,
50
})
{
// for (auto trainablePadding : {false, true}) {
for
(
auto
trainablePadding
:
{
false
,
true
})
{
// LOG(INFO) << " contextStart=" << contextStart
LOG
(
INFO
)
<<
" contextStart="
<<
contextStart
// << " contextLength=" << contextLength
<<
" contextLength="
<<
contextLength
// << " batchSize=" << batchSize
<<
" batchSize="
<<
batchSize
// << " trainablePadding=" << trainablePadding;
<<
" trainablePadding="
<<
trainablePadding
;
// ProjectionConfig conf;
ProjectionConfig
conf
;
// conf.set_type("context");
conf
.
set_type
(
"context"
);
// conf.set_input_size(10);
conf
.
set_input_size
(
10
);
// conf.set_context_start(contextStart);
conf
.
set_context_start
(
contextStart
);
// conf.set_context_length(contextLength);
conf
.
set_context_length
(
contextLength
);
// conf.set_trainable_padding(trainablePadding);
conf
.
set_trainable_padding
(
trainablePadding
);
// conf.set_output_size(conf.context_length() * conf.input_size());
conf
.
set_output_size
(
conf
.
context_length
()
*
conf
.
input_size
());
// int pad =
int
pad
=
// std::max(0, -conf.context_start()) +
std
::
max
(
0
,
-
conf
.
context_start
())
+
// std::max(0, conf.context_start() + conf.context_length() - 1);
std
::
max
(
0
,
conf
.
context_start
()
+
conf
.
context_length
()
-
1
);
// for (auto useGpu : {false, true}) {
for
(
auto
useGpu
:
{
false
,
true
})
{
// testProjectionGrad(
testProjectionGrad
(
// conf,
conf
,
// INPUT_SEQUENCE_DATA,
INPUT_SEQUENCE_DATA
,
// trainablePadding ? conf.input_size() * pad : 0,
trainablePadding
?
conf
.
input_size
()
*
pad
:
0
,
// batchSize,
batchSize
,
// useGpu,
useGpu
,
// contextStart + contextLength <= 1); // = testState
contextStart
+
contextLength
<=
1
);
// = testState
// }
}
// }
}
// }
}
// }
}
// }
}
// }
}
//
// TEST(Projection, trans_fc) {
TEST
(
Projection
,
trans_fc
)
{
// ProjectionConfig conf;
ProjectionConfig
conf
;
// conf.set_type("trans_fc");
conf
.
set_type
(
"trans_fc"
);
// conf.set_input_size(50);
conf
.
set_input_size
(
50
);
// conf.set_output_size(20);
conf
.
set_output_size
(
20
);
// for (auto useGpu : {false, true}) {
for
(
auto
useGpu
:
{
false
,
true
})
{
// testProjectionGrad(conf,
testProjectionGrad
(
conf
,
// INPUT_DATA,
INPUT_DATA
,
// /* parameterSize */ 1000,
/* parameterSize */
1000
,
// /* batchSize */ 100,
/* batchSize */
100
,
// useGpu);
useGpu
);
// }
}
// }
}
//
// TEST(Projection, fc) {
TEST
(
Projection
,
fc
)
{
// ProjectionConfig conf;
ProjectionConfig
conf
;
// conf.set_type("fc");
conf
.
set_type
(
"fc"
);
// conf.set_input_size(10);
conf
.
set_input_size
(
10
);
// conf.set_output_size(20);
conf
.
set_output_size
(
20
);
// for (auto useGpu : {false, true}) {
for
(
auto
useGpu
:
{
false
,
true
})
{
// testProjectionGrad(conf,
testProjectionGrad
(
conf
,
// INPUT_DATA,
INPUT_DATA
,
// /* parameterSize */ 200,
/* parameterSize */
200
,
// /* batchSize */ 100,
/* batchSize */
100
,
// useGpu);
useGpu
);
// }
}
// }
}
//
// TEST(Projection, dot_mul) {
TEST
(
Projection
,
dot_mul
)
{
// ProjectionConfig conf;
ProjectionConfig
conf
;
// conf.set_type("dot_mul");
conf
.
set_type
(
"dot_mul"
);
// conf.set_input_size(20);
conf
.
set_input_size
(
20
);
// conf.set_output_size(20);
conf
.
set_output_size
(
20
);
// for (auto useGpu : {false, true}) {
for
(
auto
useGpu
:
{
false
,
true
})
{
// testProjectionGrad(conf,
testProjectionGrad
(
conf
,
// INPUT_DATA,
INPUT_DATA
,
// /* parameterSize */ 20,
/* parameterSize */
20
,
// /* batchSize */ 100,
/* batchSize */
100
,
// useGpu);
useGpu
);
// }
}
// }
}
//
// TEST(Projection, table) {
TEST
(
Projection
,
table
)
{
// ProjectionConfig conf;
ProjectionConfig
conf
;
// conf.set_type("table");
conf
.
set_type
(
"table"
);
// conf.set_input_size(10);
conf
.
set_input_size
(
10
);
// conf.set_output_size(20);
conf
.
set_output_size
(
20
);
// for (auto useGpu : {false, true}) {
for
(
auto
useGpu
:
{
false
,
true
})
{
// testProjectionGrad(conf,
testProjectionGrad
(
conf
,
// INPUT_LABEL,
INPUT_LABEL
,
// /* parameterSize */ 200,
/* parameterSize */
200
,
// /* batchSize */ 100,
/* batchSize */
100
,
// useGpu);
useGpu
);
// }
}
// }
}
//
// TEST(Projection, identity) {
TEST
(
Projection
,
identity
)
{
// ProjectionConfig conf;
ProjectionConfig
conf
;
// conf.set_type("identity");
conf
.
set_type
(
"identity"
);
// conf.set_input_size(10);
conf
.
set_input_size
(
10
);
// conf.set_output_size(10);
conf
.
set_output_size
(
10
);
// for (auto useGpu : {false, true}) {
for
(
auto
useGpu
:
{
false
,
true
})
{
// testProjectionGrad(conf,
testProjectionGrad
(
conf
,
// INPUT_DATA,
INPUT_DATA
,
// /* parameterSize */ 0,
/* parameterSize */
0
,
// /* batchSize */ 100,
/* batchSize */
100
,
// useGpu);
useGpu
);
// }
}
// }
}
//
// TEST(Projection, slice) {
TEST
(
Projection
,
slice
)
{
// ProjectionConfig conf;
ProjectionConfig
conf
;
// conf.set_type("slice");
conf
.
set_type
(
"slice"
);
// conf.set_input_size(100);
conf
.
set_input_size
(
100
);
// SliceConfig& slice1 = *conf.add_slices();
SliceConfig
&
slice1
=
*
conf
.
add_slices
();
// slice1.set_start(10);
slice1
.
set_start
(
10
);
// slice1.set_end(20);
slice1
.
set_end
(
20
);
// SliceConfig& slice2 = *conf.add_slices();
SliceConfig
&
slice2
=
*
conf
.
add_slices
();
// slice2.set_start(50);
slice2
.
set_start
(
50
);
// slice2.set_end(70);
slice2
.
set_end
(
70
);
// conf.set_output_size(30);
conf
.
set_output_size
(
30
);
// for (auto useGpu : {false, true}) {
for
(
auto
useGpu
:
{
false
,
true
})
{
// testProjectionGrad(conf,
testProjectionGrad
(
conf
,
// INPUT_DATA,
INPUT_DATA
,
// /* parameterSize */ 0,
/* parameterSize */
0
,
// /* batchSize */ 10,
/* batchSize */
10
,
// useGpu);
useGpu
);
// }
}
// }
}
//
// TEST(Projection, scaling) {
TEST
(
Projection
,
scaling
)
{
// ProjectionConfig conf;
ProjectionConfig
conf
;
// conf.set_type("scaling");
conf
.
set_type
(
"scaling"
);
// conf.set_input_size(10);
conf
.
set_input_size
(
10
);
// conf.set_output_size(10);
conf
.
set_output_size
(
10
);
// for (auto useGpu : {false}) {
for
(
auto
useGpu
:
{
false
})
{
// testProjectionGrad(conf,
testProjectionGrad
(
conf
,
// INPUT_DATA,
INPUT_DATA
,
// /* parameterSize */ 1,
/* parameterSize */
1
,
// /* batchSize */ 100,
/* batchSize */
100
,
// useGpu);
useGpu
);
// }
}
// }
}
//
// void testProjectionConv(size_t groups, bool isDeconv) {
void
testProjectionConv
(
size_t
groups
,
bool
isDeconv
)
{
// const int NUM_FILTERS = 18;
const
int
NUM_FILTERS
=
18
;
// const int FILTER_SIZE = 2;
const
int
FILTER_SIZE
=
2
;
// const int FILTER_SIZE_Y = 4;
const
int
FILTER_SIZE_Y
=
4
;
// const int CHANNELS = 3;
const
int
CHANNELS
=
3
;
// const int IMAGE_SIZE = 16;
const
int
IMAGE_SIZE
=
16
;
//
// ProjectionConfig conf;
ProjectionConfig
conf
;
// if (isDeconv) {
if
(
isDeconv
)
{
// conf.set_type("convt");
conf
.
set_type
(
"convt"
);
// } else {
}
else
{
// conf.set_type("conv");
conf
.
set_type
(
"conv"
);
// }
}
// conf.set_num_filters(NUM_FILTERS);
conf
.
set_num_filters
(
NUM_FILTERS
);
//
// ConvConfig* conv = conf.mutable_conv_conf();
ConvConfig
*
conv
=
conf
.
mutable_conv_conf
();
// conv->set_filter_size(FILTER_SIZE);
conv
->
set_filter_size
(
FILTER_SIZE
);
// conv->set_filter_size_y(FILTER_SIZE_Y);
conv
->
set_filter_size_y
(
FILTER_SIZE_Y
);
// conv->set_channels(CHANNELS);
conv
->
set_channels
(
CHANNELS
);
// conv->set_padding(0);
conv
->
set_padding
(
0
);
// conv->set_padding_y(1);
conv
->
set_padding_y
(
1
);
// conv->set_stride(2);
conv
->
set_stride
(
2
);
// conv->set_stride_y(2);
conv
->
set_stride_y
(
2
);
// conv->set_groups(groups);
conv
->
set_groups
(
groups
);
// if (isDeconv) {
if
(
isDeconv
)
{
// conv->set_filter_channels(NUM_FILTERS / conv->groups());
conv
->
set_filter_channels
(
NUM_FILTERS
/
conv
->
groups
());
// } else {
}
else
{
// conv->set_filter_channels(conv->channels() / conv->groups());
conv
->
set_filter_channels
(
conv
->
channels
()
/
conv
->
groups
());
// }
}
// conv->set_img_size(IMAGE_SIZE);
conv
->
set_img_size
(
IMAGE_SIZE
);
// int output_x = outputSize(conv->img_size(),
int
output_x
=
outputSize
(
conv
->
img_size
(),
// conv->filter_size(),
conv
->
filter_size
(),
// conv->padding(),
conv
->
padding
(),
// conv->stride(),
conv
->
stride
(),
// /* caffeMode */ true);
/* caffeMode */
true
);
// int output_y = outputSize(conv->img_size(),
int
output_y
=
outputSize
(
conv
->
img_size
(),
// conv->filter_size_y(),
conv
->
filter_size_y
(),
// conv->padding_y(),
conv
->
padding_y
(),
// conv->stride_y(),
conv
->
stride_y
(),
// /* caffeMode */ true);
/* caffeMode */
true
);
// conv->set_output_x(output_x);
conv
->
set_output_x
(
output_x
);
// conv->set_output_y(output_y);
conv
->
set_output_y
(
output_y
);
// if (isDeconv) {
if
(
isDeconv
)
{
// conf.set_input_size(output_x * output_y * CHANNELS);
conf
.
set_input_size
(
output_x
*
output_y
*
CHANNELS
);
// conf.set_output_size(IMAGE_SIZE * IMAGE_SIZE * NUM_FILTERS);
conf
.
set_output_size
(
IMAGE_SIZE
*
IMAGE_SIZE
*
NUM_FILTERS
);
// } else {
}
else
{
// conf.set_input_size(IMAGE_SIZE * IMAGE_SIZE * CHANNELS);
conf
.
set_input_size
(
IMAGE_SIZE
*
IMAGE_SIZE
*
CHANNELS
);
// conf.set_output_size(output_x * output_y * NUM_FILTERS);
conf
.
set_output_size
(
output_x
*
output_y
*
NUM_FILTERS
);
// }
}
//
// testProjectionGrad(conf,
testProjectionGrad
(
conf
,
// INPUT_DATA,
INPUT_DATA
,
// /* parameterSize */ NUM_FILTERS * CHANNELS * FILTER_SIZE
/* parameterSize */
NUM_FILTERS
*
CHANNELS
*
FILTER_SIZE
*
// *
FILTER_SIZE_Y
/
groups
,
// FILTER_SIZE_Y / groups,
/* batchSize */
100
,
// /* batchSize */ 100,
true
,
// true,
false
,
// false,
NUM_FILTERS
,
// NUM_FILTERS,
true
);
// true);
}
// }
//
#ifndef PADDLE_ONLY_CPU
// #ifndef PADDLE_ONLY_CPU
TEST
(
Projection
,
conv
)
{
// TEST(Projection, conv) {
/// test ConvProjection
// /// test ConvProjection
testProjectionConv
(
1
,
false
);
// testProjectionConv(1, false);
testProjectionConv
(
3
,
false
);
// testProjectionConv(3, false);
/// test ConvTransProjection
// /// test ConvTransProjection
testProjectionConv
(
1
,
true
);
// testProjectionConv(1, true);
testProjectionConv
(
3
,
true
);
// testProjectionConv(3, true);
}
// }
#endif
// #endif
//
TEST
(
Layer
,
BilinearInterpLayer
)
{
// TEST(Layer, BilinearInterpLayer) {
TestConfig
config
;
// TestConfig config;
config
.
layerConfig
.
set_type
(
"bilinear_interp"
);
// config.layerConfig.set_type("bilinear_interp");
config
.
biasSize
=
0
;
// config.biasSize = 0;
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
4096
,
0
});
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 4096, 0});
//
LayerInputConfig
*
input
=
config
.
layerConfig
.
add_inputs
();
// LayerInputConfig* input = config.layerConfig.add_inputs();
BilinearInterpConfig
*
bilinear
=
input
->
mutable_bilinear_interp_conf
();
// BilinearInterpConfig* bilinear = input->mutable_bilinear_interp_conf();
ImageConfig
*
image
=
bilinear
->
mutable_image_conf
();
// ImageConfig* image = bilinear->mutable_image_conf();
image
->
set_img_size
(
32
);
// image->set_img_size(32);
image
->
set_img_size_y
(
32
);
// image->set_img_size_y(32);
image
->
set_channels
(
4
);
// image->set_channels(4);
//
for
(
auto
useGpu
:
{
false
,
true
})
{
// for (auto useGpu : {false, true}) {
for
(
auto
outSize
:
{
32
,
64
})
{
// for (auto outSize : {32, 64}) {
bilinear
->
set_out_size_x
(
outSize
);
// bilinear->set_out_size_x(outSize);
bilinear
->
set_out_size_y
(
outSize
);
// bilinear->set_out_size_y(outSize);
testLayerGrad
(
config
,
"bilinear_interp"
,
10
,
false
,
useGpu
);
// testLayerGrad(config, "bilinear_interp", 10, false, useGpu);
}
// }
}
// }
}
// }
//
TEST
(
Layer
,
concat
)
{
// TEST(Layer, concat) {
TestConfig
config
;
// TestConfig config;
config
.
biasSize
=
0
;
// config.biasSize = 0;
config
.
layerConfig
.
set_type
(
"concat"
);
// config.layerConfig.set_type("concat");
config
.
layerConfig
.
set_size
(
15
);
// config.layerConfig.set_size(15);
config
.
layerConfig
.
set_active_type
(
"sigmoid"
);
// config.layerConfig.set_active_type("sigmoid");
//
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
5
,
0
});
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 5, 0});
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.add_inputs();
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_1"
,
10
,
0
});
// config.inputDefs.push_back({INPUT_DATA, "layer_1", 10, 0});
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.add_inputs();
//
for
(
auto
useGpu
:
{
false
,
true
})
{
// for (auto useGpu : {false, true}) {
testLayerGrad
(
config
,
"concat"
,
100
,
false
,
useGpu
);
// testLayerGrad(config, "concat", 100, false, useGpu);
}
// }
}
// }
//
TEST
(
Layer
,
AddtoLayer
)
{
// TEST(Layer, AddtoLayer) {
TestConfig
config
;
// TestConfig config;
config
.
biasSize
=
0
;
// config.biasSize = 0;
config
.
layerConfig
.
set_type
(
"addto"
);
// config.layerConfig.set_type("addto");
config
.
layerConfig
.
set_size
(
10
);
// config.layerConfig.set_size(10);
config
.
layerConfig
.
set_active_type
(
"sigmoid"
);
// config.layerConfig.set_active_type("sigmoid");
//
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
10
,
0
});
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 10, 0});
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.add_inputs();
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_1"
,
10
,
0
});
// config.inputDefs.push_back({INPUT_DATA, "layer_1", 10, 0});
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.add_inputs();
//
for
(
auto
useGpu
:
{
false
,
true
})
{
// for (auto useGpu : {false, true}) {
testLayerGrad
(
config
,
"addto"
,
100
,
false
,
useGpu
);
// testLayerGrad(config, "addto", 100, false, useGpu);
}
// }
}
// }
//
TEST
(
Layer
,
CTCLayer
)
{
// TEST(Layer, CTCLayer) {
TestConfig
config
;
// TestConfig config;
config
.
layerConfig
.
set_type
(
"ctc"
);
// config.layerConfig.set_type("ctc");
config
.
layerConfig
.
set_norm_by_times
(
false
);
// config.layerConfig.set_norm_by_times(false);
config
.
layerConfig
.
set_size
(
10
);
// config.layerConfig.set_size(10);
config
.
biasSize
=
0
;
// config.biasSize = 0;
//
config
.
inputDefs
.
push_back
({
INPUT_SEQUENCE_DATA
,
"layer_0"
,
10
,
0
});
// config.inputDefs.push_back({INPUT_SEQUENCE_DATA, "layer_0", 10, 0});
config
.
inputDefs
.
push_back
({
INPUT_SEQUENCE_LABEL
,
"layer_1"
,
10
,
0
});
// config.inputDefs.push_back({INPUT_SEQUENCE_LABEL, "layer_1", 10, 0});
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.add_inputs();
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.add_inputs();
//
for
(
auto
useGpu
:
{
false
,
true
})
{
// for (auto useGpu : {false, true}) {
testLayerGrad
(
config
,
// testLayerGrad(config,
"ctc"
,
// "ctc",
100
,
// 100,
/* trans */
false
,
/* useGpu */
// /* trans */ false, /* useGpu */
useGpu
);
// useGpu);
}
// }
}
// }
//
TEST
(
Layer
,
cosSimLayer
)
{
// TEST(Layer, cosSimLayer) {
TestConfig
config
;
// TestConfig config;
config
.
layerConfig
.
set_type
(
"cos"
);
// config.layerConfig.set_type("cos");
config
.
layerConfig
.
set_size
(
1
);
// config.layerConfig.set_size(1);
config
.
biasSize
=
0
;
// config.biasSize = 0;
//
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
50
,
0
});
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 50, 0});
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_1"
,
50
,
0
});
// config.inputDefs.push_back({INPUT_DATA, "layer_1", 50, 0});
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.add_inputs();
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.add_inputs();
//
for
(
auto
useGpu
:
{
false
,
true
})
{
// for (auto useGpu : {false, true}) {
testLayerGrad
(
config
,
"cos"
,
100
,
false
,
useGpu
);
// testLayerGrad(config, "cos", 100, false, useGpu);
}
// }
}
// }
//
TEST
(
Layer
,
CosSimVecMatLayer
)
{
// TEST(Layer, CosSimVecMatLayer) {
TestConfig
config
;
// TestConfig config;
config
.
layerConfig
.
set_type
(
"cos_vm"
);
// config.layerConfig.set_type("cos_vm");
config
.
layerConfig
.
set_size
(
5
);
// output size
// config.layerConfig.set_size(5); // output size
config
.
layerConfig
.
set_cos_scale
(
2.0
);
// config.layerConfig.set_cos_scale(2.0);
//
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
20
,
0
});
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 20, 0});
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.add_inputs();
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_1"
,
100
,
0
});
// config.inputDefs.push_back({INPUT_DATA, "layer_1", 100, 0});
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.add_inputs();
//
for
(
auto
useGpu
:
{
false
,
true
})
{
// for (auto useGpu : {false, true}) {
testLayerGrad
(
config
,
"cos_vm"
,
100
,
false
,
useGpu
);
// testLayerGrad(config, "cos_vm", 100, false, useGpu);
}
// }
}
// }
//
void
testDepthwiseConvLayer
(
const
string
&
type
,
bool
useGpu
)
{
// void testDepthwiseConvLayer(const string& type, bool useGpu) {
TestConfig
config
;
// TestConfig config;
config
.
biasSize
=
32
;
// config.biasSize = 32;
config
.
layerConfig
.
set_type
(
type
);
// config.layerConfig.set_type(type);
config
.
layerConfig
.
set_num_filters
(
32
);
// config.layerConfig.set_num_filters(32);
config
.
layerConfig
.
set_partial_sum
(
1
);
// config.layerConfig.set_partial_sum(1);
config
.
layerConfig
.
set_shared_biases
(
true
);
// config.layerConfig.set_shared_biases(true);
//
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
2048
,
192
});
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 2048, 192});
LayerInputConfig
*
input
=
config
.
layerConfig
.
add_inputs
();
// LayerInputConfig* input = config.layerConfig.add_inputs();
ConvConfig
*
conv
=
input
->
mutable_conv_conf
();
// ConvConfig* conv = input->mutable_conv_conf();
conv
->
set_filter_size
(
2
);
// conv->set_filter_size(2);
conv
->
set_filter_size_y
(
3
);
// conv->set_filter_size_y(3);
conv
->
set_channels
(
16
);
// conv->set_channels(16);
conv
->
set_padding
(
0
);
// conv->set_padding(0);
conv
->
set_padding_y
(
1
);
// conv->set_padding_y(1);
conv
->
set_stride
(
2
);
// conv->set_stride(2);
conv
->
set_stride_y
(
2
);
// conv->set_stride_y(2);
conv
->
set_groups
(
16
);
// conv->set_groups(16);
conv
->
set_filter_channels
(
conv
->
channels
()
/
conv
->
groups
());
// conv->set_filter_channels(conv->channels() / conv->groups());
conv
->
set_img_size
(
16
);
// conv->set_img_size(16);
conv
->
set_img_size_y
(
8
);
// conv->set_img_size_y(8);
conv
->
set_output_x
(
outputSize
(
conv
->
img_size
(),
// conv->set_output_x(outputSize(conv->img_size(),
conv
->
filter_size
(),
// conv->filter_size(),
conv
->
padding
(),
// conv->padding(),
conv
->
stride
(),
// conv->stride(),
/* caffeMode */
true
));
// /* caffeMode */ true));
conv
->
set_output_y
(
outputSize
(
conv
->
img_size_y
(),
// conv->set_output_y(outputSize(conv->img_size_y(),
conv
->
filter_size_y
(),
// conv->filter_size_y(),
conv
->
padding_y
(),
// conv->padding_y(),
conv
->
stride_y
(),
// conv->stride_y(),
/* caffeMode */
true
));
// /* caffeMode */ true));
config
.
layerConfig
.
set_size
(
conv
->
output_x
()
*
conv
->
output_y
()
*
// config.layerConfig.set_size(conv->output_x() * conv->output_y() *
config
.
layerConfig
.
num_filters
());
// config.layerConfig.num_filters());
//
testLayerGrad
(
config
,
"depthwise_conv"
,
100
,
false
,
useGpu
);
// testLayerGrad(config, "depthwise_conv", 100, false, useGpu);
// Use small batch_size and useWeight=true to test biasGrad
// // Use small batch_size and useWeight=true to test biasGrad
testLayerGrad
(
config
,
"depthwise_conv"
,
2
,
false
,
useGpu
,
true
,
0.02
);
// testLayerGrad(config, "depthwise_conv", 2, false, useGpu, true, 0.02);
}
// }
//
TEST
(
Layer
,
depthwiseConvLayer
)
{
// TEST(Layer, depthwiseConvLayer) {
// 'depthwise_conv' is a sepecial case of 'exconv' whose
// // 'depthwise_conv' is a sepecial case of 'exconv' whose
// groups size equals to the input channels size.
// // groups size equals to the input channels size.
testDepthwiseConvLayer
(
"exconv"
,
/* useGpu= */
false
);
// testDepthwiseConvLayer("exconv", /* useGpu= */ false);
#ifndef PADDLE_ONLY_CPU
// #ifndef PADDLE_ONLY_CPU
testDepthwiseConvLayer
(
"exconv"
,
/* useGpu= */
true
);
// testDepthwiseConvLayer("exconv", /* useGpu= */ true);
#endif
// #endif
}
// }
//
void
testConvLayer
(
const
string
&
type
,
bool
trans
,
bool
useGpu
)
{
// void testConvLayer(const string& type, bool trans, bool useGpu) {
TestConfig
config
;
// TestConfig config;
config
.
biasSize
=
16
;
// config.biasSize = 16;
config
.
layerConfig
.
set_type
(
type
);
// config.layerConfig.set_type(type);
config
.
layerConfig
.
set_num_filters
(
16
);
// config.layerConfig.set_num_filters(16);
config
.
layerConfig
.
set_partial_sum
(
1
);
// config.layerConfig.set_partial_sum(1);
config
.
layerConfig
.
set_shared_biases
(
true
);
// config.layerConfig.set_shared_biases(true);
//
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
384
,
288
});
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 384, 288});
LayerInputConfig
*
input
=
config
.
layerConfig
.
add_inputs
();
// LayerInputConfig* input = config.layerConfig.add_inputs();
ConvConfig
*
conv
=
input
->
mutable_conv_conf
();
// ConvConfig* conv = input->mutable_conv_conf();
conv
->
set_filter_size
(
2
);
// conv->set_filter_size(2);
conv
->
set_filter_size_y
(
3
);
// conv->set_filter_size_y(3);
conv
->
set_channels
(
3
);
// conv->set_channels(3);
conv
->
set_padding
(
0
);
// conv->set_padding(0);
conv
->
set_padding_y
(
1
);
// conv->set_padding_y(1);
conv
->
set_stride
(
2
);
// conv->set_stride(2);
conv
->
set_stride_y
(
2
);
// conv->set_stride_y(2);
conv
->
set_groups
(
1
);
// conv->set_groups(1);
conv
->
set_filter_channels
(
conv
->
channels
()
/
conv
->
groups
());
// conv->set_filter_channels(conv->channels() / conv->groups());
conv
->
set_img_size
(
16
);
// conv->set_img_size(16);
conv
->
set_img_size_y
(
8
);
// conv->set_img_size_y(8);
conv
->
set_output_x
(
outputSize
(
conv
->
img_size
(),
// conv->set_output_x(outputSize(conv->img_size(),
conv
->
filter_size
(),
// conv->filter_size(),
conv
->
padding
(),
// conv->padding(),
conv
->
stride
(),
// conv->stride(),
/* caffeMode */
true
));
// /* caffeMode */ true));
conv
->
set_output_y
(
outputSize
(
conv
->
img_size_y
(),
// conv->set_output_y(outputSize(conv->img_size_y(),
conv
->
filter_size_y
(),
// conv->filter_size_y(),
conv
->
padding_y
(),
// conv->padding_y(),
conv
->
stride_y
(),
// conv->stride_y(),
/* caffeMode */
true
));
// /* caffeMode */ true));
config
.
layerConfig
.
set_size
(
conv
->
output_x
()
*
conv
->
output_y
()
*
// config.layerConfig.set_size(conv->output_x() * conv->output_y() *
config
.
layerConfig
.
num_filters
());
// config.layerConfig.num_filters());
//
testLayerGrad
(
config
,
"conv"
,
100
,
trans
,
useGpu
);
// testLayerGrad(config, "conv", 100, trans, useGpu);
// Use small batch_size and useWeight=true to test biasGrad
// // Use small batch_size and useWeight=true to test biasGrad
testLayerGrad
(
config
,
"conv"
,
2
,
trans
,
useGpu
,
true
,
0.02
);
// testLayerGrad(config, "conv", 2, trans, useGpu, true, 0.02);
}
// }
//
TEST
(
Layer
,
convLayer
)
{
// TEST(Layer, convLayer) {
testConvLayer
(
"exconv"
,
/* trans= */
false
,
/* useGpu= */
false
);
// testConvLayer("exconv", /* trans= */ false, /* useGpu= */ false);
#ifndef PADDLE_ONLY_CPU
// #ifndef PADDLE_ONLY_CPU
testConvLayer
(
"exconv"
,
/* trans= */
false
,
/* useGpu= */
true
);
// testConvLayer("exconv", /* trans= */ false, /* useGpu= */ true);
testConvLayer
(
"cudnn_conv"
,
/* trans= */
false
,
/* useGpu= */
true
);
// testConvLayer("cudnn_conv", /* trans= */ false, /* useGpu= */ true);
#endif
// #endif
}
// }
//
void
testConvTransLayer
(
const
string
&
type
,
bool
trans
,
bool
useGpu
)
{
// void testConvTransLayer(const string& type, bool trans, bool useGpu) {
TestConfig
config
;
// TestConfig config;
config
.
biasSize
=
3
;
// config.biasSize = 3;
config
.
layerConfig
.
set_type
(
type
);
// config.layerConfig.set_type(type);
config
.
layerConfig
.
set_num_filters
(
3
);
// config.layerConfig.set_num_filters(3);
config
.
layerConfig
.
set_partial_sum
(
1
);
// config.layerConfig.set_partial_sum(1);
config
.
layerConfig
.
set_shared_biases
(
true
);
// config.layerConfig.set_shared_biases(true);
//
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
1024
,
384
});
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 1024, 384});
LayerInputConfig
*
input
=
config
.
layerConfig
.
add_inputs
();
// LayerInputConfig* input = config.layerConfig.add_inputs();
ConvConfig
*
conv
=
input
->
mutable_conv_conf
();
// ConvConfig* conv = input->mutable_conv_conf();
conv
->
set_filter_size
(
2
);
// conv->set_filter_size(2);
conv
->
set_filter_size_y
(
4
);
// conv->set_filter_size_y(4);
conv
->
set_channels
(
16
);
// conv->set_channels(16);
conv
->
set_padding
(
0
);
// conv->set_padding(0);
conv
->
set_padding_y
(
1
);
// conv->set_padding_y(1);
conv
->
set_stride
(
2
);
// conv->set_stride(2);
conv
->
set_stride_y
(
2
);
// conv->set_stride_y(2);
conv
->
set_groups
(
1
);
// conv->set_groups(1);
conv
->
set_filter_channels
(
3
/
conv
->
groups
());
// conv->set_filter_channels(3 / conv->groups());
conv
->
set_img_size
(
16
);
// conv->set_img_size(16);
conv
->
set_output_x
(
outputSize
(
conv
->
img_size
(),
// conv->set_output_x(outputSize(conv->img_size(),
conv
->
filter_size
(),
// conv->filter_size(),
conv
->
padding
(),
// conv->padding(),
conv
->
stride
(),
// conv->stride(),
/* caffeMode */
true
));
// /* caffeMode */ true));
//
config
.
layerConfig
.
set_size
(
conv
->
img_size
()
*
conv
->
img_size
()
*
// config.layerConfig.set_size(conv->img_size() * conv->img_size() *
config
.
layerConfig
.
num_filters
());
// config.layerConfig.num_filters());
//
testLayerGrad
(
config
,
"convTrans"
,
100
,
trans
,
useGpu
);
// testLayerGrad(config, "convTrans", 100, trans, useGpu);
// Use small batch_size and useWeight=true to test biasGrad
// // Use small batch_size and useWeight=true to test biasGrad
testLayerGrad
(
config
,
"convTrans"
,
2
,
trans
,
useGpu
,
true
,
0.02
);
// testLayerGrad(config, "convTrans", 2, trans, useGpu, true, 0.02);
}
// }
//
TEST
(
Layer
,
convTransLayer
)
{
// TEST(Layer, convTransLayer) {
for
(
auto
useGpu
:
{
false
,
true
})
{
// for (auto useGpu : {false, true}) {
testConvTransLayer
(
"exconvt"
,
/* trans= */
false
,
/* useGpu= */
useGpu
);
// testConvTransLayer("exconvt", /* trans= */ false, /* useGpu= */ useGpu);
}
// }
#ifndef PADDLE_ONLY_CPU
// #ifndef PADDLE_ONLY_CPU
testConvTransLayer
(
"cudnn_convt"
,
/* trans= */
false
,
/* useGpu= */
true
);
// testConvTransLayer("cudnn_convt", /* trans= */ false, /* useGpu= */ true);
#endif
// #endif
}
// }
//
TEST
(
Layer
,
blockExpandLayer
)
{
// TEST(Layer, blockExpandLayer) {
TestConfig
config
;
// TestConfig config;
config
.
biasSize
=
0
;
// config.biasSize = 0;
config
.
layerConfig
.
set_type
(
"blockexpand"
);
// config.layerConfig.set_type("blockexpand");
//
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
6144
,
0
});
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 6144, 0});
LayerInputConfig
*
input
=
config
.
layerConfig
.
add_inputs
();
// LayerInputConfig* input = config.layerConfig.add_inputs();
BlockExpandConfig
*
blockExpand
=
input
->
mutable_block_expand_conf
();
// BlockExpandConfig* blockExpand = input->mutable_block_expand_conf();
blockExpand
->
set_img_size_x
(
64
);
// blockExpand->set_img_size_x(64);
blockExpand
->
set_img_size_y
(
32
);
// blockExpand->set_img_size_y(32);
blockExpand
->
set_channels
(
3
);
// blockExpand->set_channels(3);
blockExpand
->
set_padding_x
(
0
);
// blockExpand->set_padding_x(0);
blockExpand
->
set_padding_y
(
0
);
// blockExpand->set_padding_y(0);
blockExpand
->
set_block_x
(
4
);
// blockExpand->set_block_x(4);
blockExpand
->
set_block_y
(
32
);
// blockExpand->set_block_y(32);
blockExpand
->
set_stride_x
(
2
);
// blockExpand->set_stride_x(2);
blockExpand
->
set_stride_y
(
2
);
// blockExpand->set_stride_y(2);
blockExpand
->
set_output_x
(
outputSize
(
blockExpand
->
img_size_x
(),
// blockExpand->set_output_x(outputSize(blockExpand->img_size_x(),
blockExpand
->
block_x
(),
// blockExpand->block_x(),
blockExpand
->
padding_x
(),
// blockExpand->padding_x(),
blockExpand
->
stride_x
(),
// blockExpand->stride_x(),
/* caffeMode */
false
));
// /* caffeMode */ false));
blockExpand
->
set_output_y
(
outputSize
(
blockExpand
->
img_size_y
(),
// blockExpand->set_output_y(outputSize(blockExpand->img_size_y(),
blockExpand
->
block_y
(),
// blockExpand->block_y(),
blockExpand
->
padding_y
(),
// blockExpand->padding_y(),
blockExpand
->
stride_y
(),
// blockExpand->stride_y(),
/* caffeMode */
false
));
// /* caffeMode */ false));
config
.
layerConfig
.
set_size
(
blockExpand
->
block_x
()
*
blockExpand
->
block_y
()
*
// config.layerConfig.set_size(blockExpand->block_x() * blockExpand->block_y()
blockExpand
->
channels
());
// *
// blockExpand->channels());
for
(
auto
useGpu
:
{
false
,
true
})
{
//
testLayerGrad
(
config
,
"blockexpand"
,
100
,
false
,
useGpu
);
// for (auto useGpu : {false, true}) {
}
// testLayerGrad(config, "blockexpand", 100, false, useGpu);
}
// }
// }
TEST
(
Layer
,
maxoutLayer
)
{
//
TestConfig
config
;
// TEST(Layer, maxoutLayer) {
config
.
biasSize
=
0
;
// TestConfig config;
config
.
layerConfig
.
set_type
(
"maxout"
);
// config.biasSize = 0;
// config.layerConfig.set_type("maxout");
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
4096
,
0
});
//
LayerInputConfig
*
input
=
config
.
layerConfig
.
add_inputs
();
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 4096, 0});
MaxOutConfig
*
maxout
=
input
->
mutable_maxout_conf
();
// LayerInputConfig* input = config.layerConfig.add_inputs();
ImageConfig
*
image
=
maxout
->
mutable_image_conf
();
// MaxOutConfig* maxout = input->mutable_maxout_conf();
// ImageConfig* image = maxout->mutable_image_conf();
image
->
set_img_size
(
32
);
//
image
->
set_img_size_y
(
32
);
// image->set_img_size(32);
image
->
set_channels
(
4
);
// image->set_img_size_y(32);
maxout
->
set_groups
(
2
);
// image->set_channels(4);
// maxout->set_groups(2);
for
(
auto
useGpu
:
{
false
,
true
})
{
//
testLayerGrad
(
config
,
"maxout"
,
10
,
false
,
useGpu
);
// for (auto useGpu : {false, true}) {
}
// testLayerGrad(config, "maxout", 10, false, useGpu);
}
// }
void
testFcLayer
(
string
format
,
size_t
nnz
)
{
// }
TestConfig
config
;
// void testFcLayer(string format, size_t nnz) {
config
.
biasSize
=
4096
;
// TestConfig config;
config
.
layerConfig
.
set_type
(
"fc"
);
// config.biasSize = 4096;
config
.
layerConfig
.
set_size
(
4096
);
// config.layerConfig.set_type("fc");
config
.
layerConfig
.
set_active_type
(
"sigmoid"
);
// config.layerConfig.set_size(4096);
config
.
layerConfig
.
set_drop_rate
(
0.1
);
// config.layerConfig.set_active_type("sigmoid");
// config.layerConfig.set_drop_rate(0.1);
config
.
inputDefs
.
push_back
(
//
{
INPUT_DATA
,
"layer_0"
,
8192
,
nnz
,
ParaSparse
(
format
)});
// config.inputDefs.push_back(
config
.
layerConfig
.
add_inputs
();
// {INPUT_DATA, "layer_0", 8192, nnz, ParaSparse(format)});
// config.layerConfig.add_inputs();
LOG
(
INFO
)
<<
config
.
inputDefs
[
0
].
sparse
.
sparse
<<
" "
//
<<
config
.
inputDefs
[
0
].
sparse
.
format
;
// LOG(INFO) << config.inputDefs[0].sparse.sparse << " "
// << config.inputDefs[0].sparse.format;
for
(
auto
useGpu
:
{
false
,
true
})
{
//
testLayerGrad
(
config
,
// for (auto useGpu : {false, true}) {
"fc"
,
// testLayerGrad(config,
100
,
// "fc",
/* trans */
false
,
// 100,
useGpu
,
// /* trans */ false,
/* weight */
true
);
// useGpu,
}
// /* weight */ true);
}
// }
// }
TEST
(
Layer
,
fcLayer
)
{
//
testFcLayer
(
""
,
4096
*
4096
*
2
);
// TEST(Layer, fcLayer) {
testFcLayer
(
"csc"
,
4096
*
40
);
// testFcLayer("", 4096 * 4096 * 2);
testFcLayer
(
"csr"
,
4096
*
40
);
// testFcLayer("csc", 4096 * 40);
}
// testFcLayer("csr", 4096 * 40);
// }
TEST
(
Layer
,
SelectiveFullyConnectedLayer
)
{
//
TestConfig
config
;
// TEST(Layer, SelectiveFullyConnectedLayer) {
size_t
nin
=
16
;
// TestConfig config;
size_t
nout
=
256
;
// size_t nin = 16;
config
.
layerConfig
.
set_type
(
"selective_fc"
);
// size_t nout = 256;
config
.
layerConfig
.
set_size
(
nout
);
// config.layerConfig.set_type("selective_fc");
config
.
layerConfig
.
set_active_type
(
"sigmoid"
);
// config.layerConfig.set_size(nout);
config
.
layerConfig
.
set_has_selected_colums
(
true
);
// config.layerConfig.set_active_type("sigmoid");
config
.
layerConfig
.
set_selective_fc_pass_generation
(
false
);
// config.layerConfig.set_has_selected_colums(true);
config
.
biasSize
=
nout
;
// config.layerConfig.set_selective_fc_pass_generation(false);
// config.biasSize = nout;
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"input0"
,
nin
,
nin
*
nout
});
//
config
.
layerConfig
.
add_inputs
();
// config.inputDefs.push_back({INPUT_DATA, "input0", nin, nin * nout});
config
.
inputDefs
.
push_back
(
// config.layerConfig.add_inputs();
{
INPUT_SPARSE_NON_VALUE_DATA
,
"index"
,
nout
,
0
,
ParaSparse
(
"csr"
,
true
)});
// config.inputDefs.push_back(
config
.
layerConfig
.
add_inputs
();
// {INPUT_SPARSE_NON_VALUE_DATA, "index", nout, 0, ParaSparse("csr",
// true)});
testLayerGrad
(
config
,
// config.layerConfig.add_inputs();
"selective_fc"
,
//
100
,
// testLayerGrad(config,
/* trans= */
false
,
// "selective_fc",
/* useGup= */
false
,
// 100,
false
);
// /* trans= */ false,
#ifndef PADDLE_ONLY_CPU
// /* useGup= */ false,
testLayerGrad
(
config
,
// false);
"selective_fc"
,
// #ifndef PADDLE_ONLY_CPU
100
,
// testLayerGrad(config,
/* trans= */
false
,
// "selective_fc",
/* useGup= */
true
,
// 100,
false
);
// /* trans= */ false,
#endif
// /* useGup= */ true,
}
// false);
// #endif
TEST
(
Layer
,
DataNormLayer
)
{
// }
TestConfig
config
;
//
config
.
layerConfig
.
set_type
(
"data_norm"
);
// TEST(Layer, DataNormLayer) {
config
.
layerConfig
.
set_size
(
20
);
// TestConfig config;
config
.
biasSize
=
0
;
// config.layerConfig.set_type("data_norm");
// config.layerConfig.set_size(20);
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
20
,
100
});
// config.biasSize = 0;
config
.
inputDefs
.
back
().
isStatic
=
true
;
//
config
.
layerConfig
.
add_inputs
();
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 20, 100});
// config.inputDefs.back().isStatic = true;
for
(
auto
strategy
:
{
"z-score"
,
"min-max"
,
"decimal-scaling"
})
{
// config.layerConfig.add_inputs();
config
.
layerConfig
.
set_data_norm_strategy
(
strategy
);
//
// The parameters are static, so not support GPU now
// for (auto strategy : {"z-score", "min-max", "decimal-scaling"}) {
testLayerGrad
(
config
,
// config.layerConfig.set_data_norm_strategy(strategy);
"data_norm"
,
// // The parameters are static, so not support GPU now
200
,
// testLayerGrad(config,
/* trans */
false
,
// "data_norm",
/* useGpu */
false
);
// 200,
}
// /* trans */ false,
}
// /* useGpu */ false);
// }
TEST
(
Layer
,
hsigmoidLayer
)
{
// }
TestConfig
config
;
//
config
.
layerConfig
.
set_type
(
"hsigmoid"
);
// TEST(Layer, hsigmoidLayer) {
config
.
layerConfig
.
set_num_classes
(
5
);
// TestConfig config;
config
.
layerConfig
.
set_size
(
1
);
// config.layerConfig.set_type("hsigmoid");
config
.
biasSize
=
config
.
layerConfig
.
num_classes
()
-
1
;
// config.layerConfig.set_num_classes(5);
// config.layerConfig.set_size(1);
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
50
,
200
});
// config.biasSize = config.layerConfig.num_classes() - 1;
config
.
inputDefs
.
push_back
({
INPUT_LABEL
,
"layer_1"
,
5
,
0
});
//
config
.
layerConfig
.
add_inputs
();
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 50, 200});
config
.
layerConfig
.
add_inputs
();
// config.inputDefs.push_back({INPUT_LABEL, "layer_1", 5, 0});
// config.layerConfig.add_inputs();
// Not support GPU now
// config.layerConfig.add_inputs();
testLayerGrad
(
config
,
//
"hsigmoid"
,
// // Not support GPU now
100
,
// testLayerGrad(config,
/* trans */
false
,
/* useGpu */
// "hsigmoid",
false
);
// 100,
}
// /* trans */ false, /* useGpu */
// false);
TEST
(
Layer
,
multi_cross
)
{
// }
TestConfig
config
;
//
config
.
layerConfig
.
set_type
(
"multi-class-cross-entropy"
);
// TEST(Layer, multi_cross) {
config
.
biasSize
=
0
;
// TestConfig config;
// config.layerConfig.set_type("multi-class-cross-entropy");
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
50
,
0
});
// config.biasSize = 0;
config
.
inputDefs
.
push_back
({
INPUT_LABEL
,
"layer_1"
,
10
,
0
});
//
config
.
layerConfig
.
add_inputs
();
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 50, 0});
config
.
layerConfig
.
add_inputs
();
// config.inputDefs.push_back({INPUT_LABEL, "layer_1", 10, 0});
// config.layerConfig.add_inputs();
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.layerConfig.add_inputs();
testLayerGrad
(
//
config
,
"multi-class-cross-entropy"
,
100
,
/* trans */
false
,
useGpu
);
// for (auto useGpu : {false, true}) {
}
// testLayerGrad(
}
// config, "multi-class-cross-entropy", 100, /* trans */ false, useGpu);
// }
TEST
(
Layer
,
multi_binary_label_sparse_mat
)
{
// }
TestConfig
config
;
//
config
.
layerConfig
.
set_type
(
"multi_binary_label_cross_entropy"
);
// TEST(Layer, multi_binary_label_sparse_mat) {
config
.
biasSize
=
0
;
// TestConfig config;
// config.layerConfig.set_type("multi_binary_label_cross_entropy");
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
50
,
0
});
// config.biasSize = 0;
config
.
inputDefs
.
push_back
({
INPUT_SPARSE_NON_VALUE_DATA
,
"layer_1"
,
50
,
0
});
//
config
.
layerConfig
.
add_inputs
();
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 50, 0});
config
.
layerConfig
.
add_inputs
();
// config.inputDefs.push_back({INPUT_SPARSE_NON_VALUE_DATA, "layer_1", 50,
// 0});
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.layerConfig.add_inputs();
testLayerGrad
(
config
,
// config.layerConfig.add_inputs();
"multi_binary_label_cross_entropy"
,
//
100
,
// for (auto useGpu : {false, true}) {
/* trans */
false
,
// testLayerGrad(config,
useGpu
);
// "multi_binary_label_cross_entropy",
}
// 100,
}
// /* trans */ false,
// useGpu);
TEST
(
layer
,
multi_binary_label_id
)
{
// }
TestConfig
config
;
// }
config
.
layerConfig
.
set_type
(
"multi_binary_label_cross_entropy"
);
//
config
.
biasSize
=
0
;
// TEST(layer, multi_binary_label_id) {
// TestConfig config;
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
50
,
0
});
// config.layerConfig.set_type("multi_binary_label_cross_entropy");
config
.
inputDefs
.
push_back
({
INPUT_LABEL
,
"layer_1"
,
10
,
0
});
// config.biasSize = 0;
config
.
layerConfig
.
add_inputs
();
//
config
.
layerConfig
.
add_inputs
();
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 50, 0});
// config.inputDefs.push_back({INPUT_LABEL, "layer_1", 10, 0});
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.layerConfig.add_inputs();
testLayerGrad
(
config
,
// config.layerConfig.add_inputs();
"multi_binary_label_cross_entropy"
,
//
100
,
// for (auto useGpu : {false, true}) {
/* trans */
false
,
// testLayerGrad(config,
useGpu
);
// "multi_binary_label_cross_entropy",
}
// 100,
}
// /* trans */ false,
// useGpu);
TEST
(
Layer
,
multi_cross_with_selfnorm
)
{
// }
TestConfig
config
;
// }
config
.
layerConfig
.
set_type
(
"multi_class_cross_entropy_with_selfnorm"
);
//
config
.
layerConfig
.
set_softmax_selfnorm_alpha
(
0.1
);
// TEST(Layer, multi_cross_with_selfnorm) {
config
.
biasSize
=
0
;
// TestConfig config;
// config.layerConfig.set_type("multi_class_cross_entropy_with_selfnorm");
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
50
,
0
});
// config.layerConfig.set_softmax_selfnorm_alpha(0.1);
config
.
inputDefs
.
push_back
({
INPUT_LABEL
,
"layer_1"
,
10
,
0
});
// config.biasSize = 0;
config
.
layerConfig
.
add_inputs
();
//
config
.
layerConfig
.
add_inputs
();
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 50, 0});
// config.inputDefs.push_back({INPUT_LABEL, "layer_1", 10, 0});
// Not support GPU now
// config.layerConfig.add_inputs();
testLayerGrad
(
config
,
// config.layerConfig.add_inputs();
"multi_class_cross_entropy_with_selfnorm"
,
//
100
,
// // Not support GPU now
/* trans */
false
,
// testLayerGrad(config,
/* useGpu */
false
);
// "multi_class_cross_entropy_with_selfnorm",
}
// 100,
// /* trans */ false,
TEST
(
Layer
,
multi_cross_soft
)
{
// /* useGpu */ false);
TestConfig
config
;
// }
config
.
layerConfig
.
set_type
(
"soft_binary_class_cross_entropy"
);
//
config
.
biasSize
=
0
;
// TEST(Layer, multi_cross_soft) {
// TestConfig config;
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
10
,
0
});
// config.layerConfig.set_type("soft_binary_class_cross_entropy");
config
.
inputDefs
.
push_back
({
INPUT_DATA_TARGET
,
"layer_1"
,
10
,
0
});
// config.biasSize = 0;
config
.
layerConfig
.
add_inputs
();
//
config
.
layerConfig
.
add_inputs
();
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 10, 0});
// config.inputDefs.push_back({INPUT_DATA_TARGET, "layer_1", 10, 0});
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.layerConfig.add_inputs();
testLayerGrad
(
config
,
// config.layerConfig.add_inputs();
"soft_binary_class_cross_entropy"
,
//
100
,
// for (auto useGpu : {false, true}) {
/* trans */
false
,
// testLayerGrad(config,
useGpu
);
// "soft_binary_class_cross_entropy",
}
// 100,
}
// /* trans */ false,
// useGpu);
TEST
(
Layer
,
square_error
)
{
// }
TestConfig
config
;
// }
config
.
layerConfig
.
set_type
(
"square_error"
);
//
config
.
biasSize
=
0
;
// TEST(Layer, square_error) {
// TestConfig config;
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
10
,
0
});
// config.layerConfig.set_type("square_error");
config
.
inputDefs
.
push_back
({
INPUT_DATA_TARGET
,
"layer_1"
,
10
,
0
});
// config.biasSize = 0;
config
.
layerConfig
.
add_inputs
();
//
config
.
layerConfig
.
add_inputs
();
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 10, 0});
// config.inputDefs.push_back({INPUT_DATA_TARGET, "layer_1", 10, 0});
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.layerConfig.add_inputs();
testLayerGrad
(
config
,
"square_error"
,
100
,
/* trans */
false
,
useGpu
);
// config.layerConfig.add_inputs();
}
//
}
// for (auto useGpu : {false, true}) {
// testLayerGrad(config, "square_error", 100, /* trans */ false, useGpu);
TEST
(
Layer
,
sparse_square_error
)
{
// }
TestConfig
config
;
// }
config
.
layerConfig
.
set_type
(
"square_error"
);
//
config
.
biasSize
=
0
;
// TEST(Layer, sparse_square_error) {
// TestConfig config;
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
50
,
0
});
// config.layerConfig.set_type("square_error");
config
.
inputDefs
.
push_back
({
INPUT_SPARSE_NON_VALUE_DATA
,
"layer_1"
,
50
,
0
});
// config.biasSize = 0;
config
.
layerConfig
.
add_inputs
();
//
config
.
layerConfig
.
add_inputs
();
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 50, 0});
// config.inputDefs.push_back({INPUT_SPARSE_NON_VALUE_DATA, "layer_1", 50,
// "GpuSparseMatrix" as label is not supported
// 0});
testLayerGrad
(
config
,
// config.layerConfig.add_inputs();
"square_error"
,
// config.layerConfig.add_inputs();
100
,
//
/* trans */
false
,
// // "GpuSparseMatrix" as label is not supported
/* useGpu */
false
);
// testLayerGrad(config,
}
// "square_error",
// 100,
TEST
(
Layer
,
sparse_float_square_error
)
{
// /* trans */ false,
TestConfig
config
;
// /* useGpu */ false);
config
.
layerConfig
.
set_type
(
"square_error"
);
// }
config
.
biasSize
=
0
;
//
// TEST(Layer, sparse_float_square_error) {
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
50
,
0
});
// TestConfig config;
config
.
inputDefs
.
push_back
({
INPUT_SPARSE_FLOAT_VALUE_DATA
,
"layer_1"
,
50
,
0
});
// config.layerConfig.set_type("square_error");
config
.
layerConfig
.
add_inputs
();
// config.biasSize = 0;
config
.
layerConfig
.
add_inputs
();
//
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 50, 0});
// "GpuSparseMatrix" as label is not supported
// config.inputDefs.push_back({INPUT_SPARSE_FLOAT_VALUE_DATA, "layer_1", 50,
testLayerGrad
(
config
,
// 0});
"square_error"
,
// config.layerConfig.add_inputs();
100
,
// config.layerConfig.add_inputs();
/* trans */
false
,
//
/* useGpu */
false
);
// // "GpuSparseMatrix" as label is not supported
}
// testLayerGrad(config,
// "square_error",
TEST
(
Layer
,
square_error_weighted
)
{
// 100,
TestConfig
config
;
// /* trans */ false,
config
.
layerConfig
.
set_type
(
"square_error"
);
// /* useGpu */ false);
config
.
biasSize
=
0
;
// }
config
.
testAccumulate
=
false
;
//
// TEST(Layer, square_error_weighted) {
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
10
,
0
});
// TestConfig config;
config
.
inputDefs
.
push_back
({
INPUT_DATA_TARGET
,
"layer_1"
,
10
,
0
});
// config.layerConfig.set_type("square_error");
config
.
inputDefs
.
push_back
({
INPUT_DATA_TARGET
,
"layer_2"
,
1
,
0
});
// config.biasSize = 0;
config
.
layerConfig
.
add_inputs
();
// config.testAccumulate = false;
config
.
layerConfig
.
add_inputs
();
//
config
.
layerConfig
.
add_inputs
();
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 10, 0});
// config.inputDefs.push_back({INPUT_DATA_TARGET, "layer_1", 10, 0});
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.inputDefs.push_back({INPUT_DATA_TARGET, "layer_2", 1, 0});
testLayerGrad
(
config
,
"square_error"
,
100
,
/* trans */
false
,
useGpu
);
// config.layerConfig.add_inputs();
}
// config.layerConfig.add_inputs();
}
// config.layerConfig.add_inputs();
//
TEST
(
Layer
,
huber_two_class
)
{
// for (auto useGpu : {false, true}) {
TestConfig
config
;
// testLayerGrad(config, "square_error", 100, /* trans */ false, useGpu);
config
.
layerConfig
.
set_type
(
"huber"
);
// }
config
.
biasSize
=
0
;
// }
//
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
1
,
0
});
// TEST(Layer, huber_two_class) {
config
.
inputDefs
.
push_back
({
INPUT_LABEL
,
"layer_1"
,
2
,
0
});
// TestConfig config;
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.set_type("huber");
config
.
layerConfig
.
add_inputs
();
// config.biasSize = 0;
//
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 1, 0});
testLayerGrad
(
config
,
"huber"
,
100
,
/* trans */
false
,
useGpu
);
// config.inputDefs.push_back({INPUT_LABEL, "layer_1", 2, 0});
}
// config.layerConfig.add_inputs();
}
// config.layerConfig.add_inputs();
//
void
testExpandLayer
(
string
trans_type
,
bool
hasSubseq
)
{
// for (auto useGpu : {false, true}) {
TestConfig
config
;
// testLayerGrad(config, "huber", 100, /* trans */ false, useGpu);
config
.
layerConfig
.
set_type
(
"expand"
);
// }
// }
config
.
inputDefs
.
push_back
(
//
{
trans_type
==
"non-seq"
?
INPUT_DENSE_DIM_DATA
:
INPUT_SEQUENCE_DATA
,
// void testExpandLayer(string trans_type, bool hasSubseq) {
"layer_0"
,
// TestConfig config;
10
,
// config.layerConfig.set_type("expand");
0
});
//
config
.
inputDefs
.
push_back
(
// config.inputDefs.push_back(
{
hasSubseq
?
INPUT_HASSUB_SEQUENCE_DATA
:
INPUT_SEQUENCE_DATA
,
// {trans_type == "non-seq" ? INPUT_DENSE_DIM_DATA : INPUT_SEQUENCE_DATA,
"layer_1"
,
// "layer_0",
10
,
// 10,
0
});
// 0});
config
.
layerConfig
.
add_inputs
();
// config.inputDefs.push_back(
config
.
layerConfig
.
add_inputs
();
// {hasSubseq ? INPUT_HASSUB_SEQUENCE_DATA : INPUT_SEQUENCE_DATA,
config
.
layerConfig
.
set_trans_type
(
trans_type
);
// "layer_1",
LOG
(
INFO
)
<<
" trans_type="
<<
trans_type
<<
" hasSubseq="
<<
hasSubseq
;
// 10,
// 0});
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.layerConfig.add_inputs();
testLayerGrad
(
config
,
"expand"
,
30
,
false
,
useGpu
);
// config.layerConfig.add_inputs();
}
// config.layerConfig.set_trans_type(trans_type);
}
// LOG(INFO) << " trans_type=" << trans_type << " hasSubseq=" << hasSubseq;
//
TEST
(
Layer
,
ExpandLayer
)
{
// for (auto useGpu : {false, true}) {
testExpandLayer
(
"non-seq"
,
false
);
// non-seq expand to seq
// testLayerGrad(config, "expand", 30, false, useGpu);
testExpandLayer
(
"non-seq"
,
true
);
// non-seq expand to hasSubseq
// }
testExpandLayer
(
"seq"
,
true
);
// seq expand to hasSubseq
// }
}
//
// TEST(Layer, ExpandLayer) {
void
testDegradeLayer
(
bool
hasSubseq
,
// testExpandLayer("non-seq", false); // non-seq expand to seq
string
layer_type
,
// testExpandLayer("non-seq", true); // non-seq expand to hasSubseq
string
trans_type
,
// testExpandLayer("seq", true); // seq expand to hasSubseq
int
stride
)
{
// }
TestConfig
config
;
//
config
.
layerConfig
.
set_type
(
layer_type
);
// void testDegradeLayer(bool hasSubseq,
config
.
layerConfig
.
set_size
(
10
);
// string layer_type,
config
.
layerConfig
.
set_seq_pool_stride
(
stride
);
// string trans_type,
config
.
biasSize
=
0
;
// int stride) {
// TestConfig config;
config
.
inputDefs
.
push_back
(
// config.layerConfig.set_type(layer_type);
{
hasSubseq
?
INPUT_HASSUB_SEQUENCE_DATA
:
INPUT_SEQUENCE_DATA
,
// config.layerConfig.set_size(10);
"layer_0"
,
// config.layerConfig.set_seq_pool_stride(stride);
10
,
// config.biasSize = 0;
0
});
//
config
.
layerConfig
.
add_inputs
();
// config.inputDefs.push_back(
config
.
layerConfig
.
set_trans_type
(
trans_type
);
// {hasSubseq ? INPUT_HASSUB_SEQUENCE_DATA : INPUT_SEQUENCE_DATA,
// "layer_0",
auto
testDegradeLayerGrad
=
[](
TestConfig
&
config
,
string
layer_type
)
{
// 10,
for
(
auto
useGpu
:
{
false
,
true
})
{
// 0});
testLayerGrad
(
config
,
layer_type
,
100
,
false
,
useGpu
);
// config.layerConfig.add_inputs();
}
// config.layerConfig.set_trans_type(trans_type);
};
//
// auto testDegradeLayerGrad = [](TestConfig& config, string layer_type) {
if
(
layer_type
==
"average"
)
{
// for (auto useGpu : {false, true}) {
for
(
auto
strategy
:
{
"average"
,
"sum"
,
"squarerootn"
})
{
// testLayerGrad(config, layer_type, 100, false, useGpu);
LOG
(
INFO
)
<<
" hasSubseq="
<<
hasSubseq
<<
" trans_type="
<<
trans_type
// }
<<
" average_strategy="
<<
strategy
// };
<<
" seq_pool_stride="
<<
stride
;
//
config
.
layerConfig
.
set_average_strategy
(
strategy
);
// if (layer_type == "average") {
testDegradeLayerGrad
(
config
,
layer_type
);
// for (auto strategy : {"average", "sum", "squarerootn"}) {
}
// LOG(INFO) << " hasSubseq=" << hasSubseq << " trans_type=" << trans_type
}
else
{
// << " average_strategy=" << strategy
LOG
(
INFO
)
<<
" hasSubseq="
<<
hasSubseq
<<
" trans_type="
<<
trans_type
// << " seq_pool_stride=" << stride;
<<
" seq_pool_stride="
<<
stride
;
// config.layerConfig.set_average_strategy(strategy);
testDegradeLayerGrad
(
config
,
layer_type
);
// testDegradeLayerGrad(config, layer_type);
}
// }
}
// } else {
// LOG(INFO) << " hasSubseq=" << hasSubseq << " trans_type=" << trans_type
TEST
(
Layer
,
MaxLayer
)
{
// << " seq_pool_stride=" << stride;
testDegradeLayer
(
false
,
"max"
,
"non-seq"
,
-
1
);
// seq max to non-seq
// testDegradeLayerGrad(config, layer_type);
testDegradeLayer
(
false
,
// }
"max"
,
// }
"non-seq"
,
//
5
);
// seq max to a shorten seq, stride window = 5
// TEST(Layer, MaxLayer) {
testDegradeLayer
(
true
,
"max"
,
"non-seq"
,
-
1
);
// hasSubseq max to non-seq
// testDegradeLayer(false, "max", "non-seq", -1); // seq max to non-seq
testDegradeLayer
(
true
,
"max"
,
"seq"
,
-
1
);
// hasSubseq max to seq
// testDegradeLayer(false,
}
// "max",
// "non-seq",
TEST
(
Layer
,
SequenceLastInstanceLayer
)
{
// 5); // seq max to a shorten seq, stride window = 5
testDegradeLayer
(
false
,
// testDegradeLayer(true, "max", "non-seq", -1); // hasSubseq max to non-seq
"seqlastins"
,
// testDegradeLayer(true, "max", "seq", -1); // hasSubseq max to seq
"non-seq"
,
// }
-
1
);
// seq seqlastins to non-seq
//
testDegradeLayer
(
false
,
// TEST(Layer, SequenceLastInstanceLayer) {
"seqlastins"
,
// testDegradeLayer(false,
"non-seq"
,
// "seqlastins",
5
);
// seq seqlastins to a shorten seq, stride window = 5
// "non-seq",
testDegradeLayer
(
true
,
// -1); // seq seqlastins to non-seq
"seqlastins"
,
// testDegradeLayer(false,
"non-seq"
,
// "seqlastins",
-
1
);
// hasSubseq seqlastins to non-seq
// "non-seq",
testDegradeLayer
(
// 5); // seq seqlastins to a shorten seq, stride window = 5
true
,
"seqlastins"
,
"seq"
,
-
1
);
// hasSubseq seqlastins to seq
// testDegradeLayer(true,
}
// "seqlastins",
// "non-seq",
TEST
(
Layer
,
AverageLayer
)
{
// -1); // hasSubseq seqlastins to non-seq
testDegradeLayer
(
false
,
"average"
,
"non-seq"
,
-
1
);
// seq average to non-seq
// testDegradeLayer(
testDegradeLayer
(
false
,
// true, "seqlastins", "seq", -1); // hasSubseq seqlastins to seq
"average"
,
// }
"non-seq"
,
//
5
);
// seq average to a shorten seq, stride window = 5
// TEST(Layer, AverageLayer) {
testDegradeLayer
(
// testDegradeLayer(false, "average", "non-seq", -1); // seq average to
true
,
"average"
,
"non-seq"
,
-
1
);
// hasSubseq average to non-seq
// non-seq
testDegradeLayer
(
true
,
"average"
,
"seq"
,
-
1
);
// hasSubseq average to seq
// testDegradeLayer(false,
}
// "average",
// "non-seq",
TEST
(
Layer
,
SequenceConcatLayer
)
{
// 5); // seq average to a shorten seq, stride window = 5
TestConfig
config
;
// testDegradeLayer(
config
.
layerConfig
.
set_type
(
"seqconcat"
);
// true, "average", "non-seq", -1); // hasSubseq average to
config
.
layerConfig
.
set_size
(
10
);
// non-seq
config
.
biasSize
=
0
;
// testDegradeLayer(true, "average", "seq", -1); // hasSubseq average to seq
// }
config
.
inputDefs
.
push_back
({
INPUT_SEQUENCE_DATA
,
"layer_0"
,
10
,
0
});
//
config
.
layerConfig
.
add_inputs
();
// TEST(Layer, SequenceConcatLayer) {
config
.
inputDefs
.
push_back
({
INPUT_SEQUENCE_DATA
,
"layer_1"
,
10
,
0
});
// TestConfig config;
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.set_type("seqconcat");
// config.layerConfig.set_size(10);
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.biasSize = 0;
testLayerGrad
(
config
,
"seqconcat"
,
100
,
false
,
useGpu
);
//
}
// config.inputDefs.push_back({INPUT_SEQUENCE_DATA, "layer_0", 10, 0});
}
// config.layerConfig.add_inputs();
// config.inputDefs.push_back({INPUT_SEQUENCE_DATA, "layer_1", 10, 0});
TEST
(
Layer
,
SequenceReshapeLayer
)
{
// config.layerConfig.add_inputs();
TestConfig
config
;
//
config
.
layerConfig
.
set_type
(
"seqreshape"
);
// for (auto useGpu : {false, true}) {
config
.
layerConfig
.
set_size
(
10
);
// testLayerGrad(config, "seqconcat", 100, false, useGpu);
// }
config
.
inputDefs
.
push_back
({
INPUT_SEQUENCE_DATA
,
"layer_0"
,
100
,
0
});
// }
config
.
layerConfig
.
add_inputs
();
//
// TEST(Layer, SequenceReshapeLayer) {
for
(
auto
useGpu
:
{
false
,
true
})
{
// TestConfig config;
testLayerGrad
(
config
,
"seqreshape"
,
100
,
false
,
useGpu
);
// config.layerConfig.set_type("seqreshape");
}
// config.layerConfig.set_size(10);
}
//
// config.inputDefs.push_back({INPUT_SEQUENCE_DATA, "layer_0", 100, 0});
TEST
(
Layer
,
ConvShiftLayer
)
{
// config.layerConfig.add_inputs();
TestConfig
config
;
//
config
.
layerConfig
.
set_type
(
"conv_shift"
);
// for (auto useGpu : {false, true}) {
config
.
layerConfig
.
set_size
(
10
);
// testLayerGrad(config, "seqreshape", 100, false, useGpu);
// }
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
10
,
0
});
// }
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_1"
,
3
,
0
});
//
config
.
layerConfig
.
add_inputs
();
// TEST(Layer, ConvShiftLayer) {
config
.
layerConfig
.
add_inputs
();
// TestConfig config;
// config.layerConfig.set_type("conv_shift");
// Not support GPU now
// config.layerConfig.set_size(10);
testLayerGrad
(
config
,
"conv_shift"
,
100
,
false
,
false
);
//
}
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 10, 0});
// config.inputDefs.push_back({INPUT_DATA, "layer_1", 3, 0});
TEST
(
Layer
,
PowerLayer
)
{
// config.layerConfig.add_inputs();
TestConfig
config
;
// config.layerConfig.add_inputs();
config
.
layerConfig
.
set_type
(
"power"
);
//
config
.
layerConfig
.
set_size
(
10
);
// // Not support GPU now
// testLayerGrad(config, "conv_shift", 100, false, false);
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
1
,
0
});
// }
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_1"
,
10
,
0
});
//
config
.
layerConfig
.
add_inputs
();
// TEST(Layer, PowerLayer) {
config
.
layerConfig
.
add_inputs
();
// TestConfig config;
// config.layerConfig.set_type("power");
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.layerConfig.set_size(10);
testLayerGrad
(
config
,
"power"
,
100
,
false
,
useGpu
);
//
}
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 1, 0});
}
// config.inputDefs.push_back({INPUT_DATA, "layer_1", 10, 0});
// config.layerConfig.add_inputs();
TEST
(
Layer
,
ConvexCombinationLayer
)
{
// config.layerConfig.add_inputs();
TestConfig
config
;
//
config
.
layerConfig
.
set_type
(
"convex_comb"
);
// for (auto useGpu : {false, true}) {
config
.
layerConfig
.
set_size
(
20
);
// testLayerGrad(config, "power", 100, false, useGpu);
config
.
biasSize
=
0
;
// }
// }
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
5
,
0
});
//
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_1"
,
100
,
0
});
// TEST(Layer, ConvexCombinationLayer) {
config
.
layerConfig
.
add_inputs
();
// TestConfig config;
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.set_type("convex_comb");
// config.layerConfig.set_size(20);
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.biasSize = 0;
testLayerGrad
(
config
,
"convex_comb"
,
100
,
false
,
useGpu
);
//
}
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 5, 0});
}
// config.inputDefs.push_back({INPUT_DATA, "layer_1", 100, 0});
// config.layerConfig.add_inputs();
TEST
(
Layer
,
InterpolationLayer
)
{
// config.layerConfig.add_inputs();
TestConfig
config
;
//
config
.
layerConfig
.
set_type
(
"interpolation"
);
// for (auto useGpu : {false, true}) {
config
.
layerConfig
.
set_size
(
10
);
// testLayerGrad(config, "convex_comb", 100, false, useGpu);
config
.
biasSize
=
0
;
// }
// }
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
1
,
0
});
//
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_1"
,
10
,
0
});
// TEST(Layer, InterpolationLayer) {
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_2"
,
10
,
0
});
// TestConfig config;
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.set_type("interpolation");
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.set_size(10);
config
.
layerConfig
.
add_inputs
();
// config.biasSize = 0;
//
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 1, 0});
testLayerGrad
(
config
,
"interpolation"
,
100
,
false
,
useGpu
);
// config.inputDefs.push_back({INPUT_DATA, "layer_1", 10, 0});
}
// config.inputDefs.push_back({INPUT_DATA, "layer_2", 10, 0});
}
// config.layerConfig.add_inputs();
// config.layerConfig.add_inputs();
TEST
(
Layer
,
OuterProdLayer
)
{
// config.layerConfig.add_inputs();
TestConfig
config
;
//
config
.
layerConfig
.
set_type
(
"out_prod"
);
// for (auto useGpu : {false, true}) {
config
.
layerConfig
.
set_size
(
100
);
// testLayerGrad(config, "interpolation", 100, false, useGpu);
// }
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
10
,
0
});
// }
config
.
layerConfig
.
add_inputs
();
//
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_1"
,
10
,
0
});
// TEST(Layer, OuterProdLayer) {
config
.
layerConfig
.
add_inputs
();
// TestConfig config;
// config.layerConfig.set_type("out_prod");
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.layerConfig.set_size(100);
testLayerGrad
(
config
,
"out_prod"
,
100
,
false
,
useGpu
);
//
}
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 10, 0});
}
// config.layerConfig.add_inputs();
// config.inputDefs.push_back({INPUT_DATA, "layer_1", 10, 0});
TEST
(
Layer
,
SlopeInterceptLayer
)
{
// config.layerConfig.add_inputs();
TestConfig
config
;
//
config
.
layerConfig
.
set_type
(
"slope_intercept"
);
// for (auto useGpu : {false, true}) {
config
.
layerConfig
.
set_size
(
10
);
// testLayerGrad(config, "out_prod", 100, false, useGpu);
config
.
layerConfig
.
set_slope
(
1.0
);
// }
config
.
layerConfig
.
set_intercept
(
0.1
);
// }
//
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
10
,
0
});
// TEST(Layer, SlopeInterceptLayer) {
config
.
layerConfig
.
add_inputs
();
// TestConfig config;
// config.layerConfig.set_type("slope_intercept");
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.layerConfig.set_size(10);
testLayerGrad
(
config
,
"slope_intercept"
,
100
,
false
,
useGpu
);
// config.layerConfig.set_slope(1.0);
}
// config.layerConfig.set_intercept(0.1);
}
//
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 10, 0});
TEST
(
Layer
,
ScalingLayer
)
{
// config.layerConfig.add_inputs();
TestConfig
config
;
//
config
.
layerConfig
.
set_type
(
"scaling"
);
// for (auto useGpu : {false, true}) {
config
.
layerConfig
.
set_size
(
10
);
// testLayerGrad(config, "slope_intercept", 100, false, useGpu);
config
.
biasSize
=
0
;
// }
// }
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
1
,
0
});
//
config
.
layerConfig
.
add_inputs
();
// TEST(Layer, ScalingLayer) {
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_1"
,
10
,
0
});
// TestConfig config;
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.set_type("scaling");
// config.layerConfig.set_size(10);
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.biasSize = 0;
testLayerGrad
(
config
,
"scaling"
,
100
,
false
,
useGpu
);
//
}
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 1, 0});
}
// config.layerConfig.add_inputs();
// config.inputDefs.push_back({INPUT_DATA, "layer_1", 10, 0});
void
testNormLayer
(
const
string
&
normType
,
bool
trans
,
bool
useGpu
)
{
// config.layerConfig.add_inputs();
TestConfig
config
;
//
config
.
layerConfig
.
set_type
(
"norm"
);
// for (auto useGpu : {false, true}) {
config
.
layerConfig
.
set_active_type
(
"relu"
);
// testLayerGrad(config, "scaling", 100, false, useGpu);
// }
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
1568
,
0
});
// }
LayerInputConfig
*
input
=
config
.
layerConfig
.
add_inputs
();
//
NormConfig
*
norm
=
input
->
mutable_norm_conf
();
// void testNormLayer(const string& normType, bool trans, bool useGpu) {
norm
->
set_norm_type
(
normType
);
// TestConfig config;
norm
->
set_channels
(
16
);
// config.layerConfig.set_type("norm");
norm
->
set_size
(
5
);
// config.layerConfig.set_active_type("relu");
norm
->
set_scale
(
0.001
);
//
norm
->
set_pow
(
0.75
);
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 1568, 0});
norm
->
set_blocked
(
0
);
// LayerInputConfig* input = config.layerConfig.add_inputs();
norm
->
set_img_size
(
14
);
// NormConfig* norm = input->mutable_norm_conf();
norm
->
set_img_size_y
(
7
);
// norm->set_norm_type(normType);
norm
->
set_output_x
(
norm
->
img_size
());
// norm->set_channels(16);
norm
->
set_output_y
(
norm
->
img_size_y
());
// norm->set_size(5);
if
(
norm
->
norm_type
()
==
"cmrnorm"
||
// norm->set_scale(0.001);
norm
->
norm_type
()
==
"cmrnorm-projection"
)
{
// norm->set_pow(0.75);
norm
->
set_scale
(
norm
->
scale
()
/
norm
->
size
());
// norm->set_blocked(0);
}
else
{
// norm->set_img_size(14);
norm
->
set_scale
(
norm
->
scale
()
/
(
norm
->
size
()
*
norm
->
size
()));
// norm->set_img_size_y(7);
}
// norm->set_output_x(norm->img_size());
// norm->set_output_y(norm->img_size_y());
config
.
layerConfig
.
set_size
(
norm
->
output_x
()
*
norm
->
output_y
()
*
// if (norm->norm_type() == "cmrnorm" ||
norm
->
channels
());
// norm->norm_type() == "cmrnorm-projection") {
config
.
biasSize
=
0
;
// norm->set_scale(norm->scale() / norm->size());
// } else {
testLayerGrad
(
config
,
"norm"
,
100
,
trans
,
useGpu
);
// norm->set_scale(norm->scale() / (norm->size() * norm->size()));
}
// }
//
TEST
(
Layer
,
NormLayer
)
{
// config.layerConfig.set_size(norm->output_x() * norm->output_y() *
testNormLayer
(
"cmrnorm-projection"
,
// norm->channels());
/* trans= */
false
,
/* useGpu= */
// config.biasSize = 0;
true
);
//
testNormLayer
(
"cmrnorm-projection"
,
// testLayerGrad(config, "norm", 100, trans, useGpu);
/* trans= */
false
,
/* useGpu= */
// }
false
);
//
}
// TEST(Layer, NormLayer) {
// testNormLayer("cmrnorm-projection",
void
setPoolConfig
(
TestConfig
*
config
,
// /* trans= */ false, /* useGpu= */
PoolConfig
*
pool
,
// true);
const
string
&
poolType
)
{
// testNormLayer("cmrnorm-projection",
(
*
config
).
biasSize
=
0
;
// /* trans= */ false, /* useGpu= */
(
*
config
).
layerConfig
.
set_type
(
"pool"
);
// false);
(
*
config
).
layerConfig
.
set_num_filters
(
16
);
// }
//
int
kw
=
3
,
kh
=
3
;
// void setPoolConfig(TestConfig* config,
int
pw
=
0
,
ph
=
0
;
// PoolConfig* pool,
int
sw
=
2
,
sh
=
2
;
// const string& poolType) {
pool
->
set_pool_type
(
poolType
);
// (*config).biasSize = 0;
pool
->
set_channels
(
16
);
// (*config).layerConfig.set_type("pool");
pool
->
set_size_x
(
kw
);
// (*config).layerConfig.set_num_filters(16);
pool
->
set_size_y
(
kh
);
//
pool
->
set_start
(
0
);
// int kw = 3, kh = 3;
pool
->
set_padding
(
pw
);
// int pw = 0, ph = 0;
pool
->
set_padding_y
(
ph
);
// int sw = 2, sh = 2;
pool
->
set_stride
(
sw
);
// pool->set_pool_type(poolType);
pool
->
set_stride_y
(
sh
);
// pool->set_channels(16);
// pool->set_size_x(kw);
int
ow
=
outputSize
(
pool
->
img_size
(),
kw
,
pw
,
sw
,
/* caffeMode */
false
);
// pool->set_size_y(kh);
int
oh
=
outputSize
(
pool
->
img_size_y
(),
kh
,
ph
,
sh
,
/* caffeMode */
false
);
// pool->set_start(0);
pool
->
set_output_x
(
ow
);
// pool->set_padding(pw);
pool
->
set_output_y
(
oh
);
// pool->set_padding_y(ph);
}
// pool->set_stride(sw);
// pool->set_stride_y(sh);
void
testPoolLayer
(
const
string
&
poolType
,
bool
trans
,
bool
useGpu
)
{
//
TestConfig
config
;
// int ow = outputSize(pool->img_size(), kw, pw, sw, /* caffeMode */ false);
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
3136
,
0
});
// int oh = outputSize(pool->img_size_y(), kh, ph, sh, /* caffeMode */ false);
LayerInputConfig
*
input
=
config
.
layerConfig
.
add_inputs
();
// pool->set_output_x(ow);
PoolConfig
*
pool
=
input
->
mutable_pool_conf
();
// pool->set_output_y(oh);
// }
pool
->
set_img_size
(
14
);
//
pool
->
set_img_size_y
(
14
);
// void testPoolLayer(const string& poolType, bool trans, bool useGpu) {
setPoolConfig
(
&
config
,
pool
,
poolType
);
// TestConfig config;
config
.
layerConfig
.
set_size
(
pool
->
output_x
()
*
pool
->
output_y
()
*
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 3136, 0});
pool
->
channels
());
// LayerInputConfig* input = config.layerConfig.add_inputs();
// PoolConfig* pool = input->mutable_pool_conf();
testLayerGrad
(
config
,
"pool"
,
100
,
trans
,
useGpu
);
//
}
// pool->set_img_size(14);
// pool->set_img_size_y(14);
#ifndef PADDLE_ONLY_CPU
// setPoolConfig(&config, pool, poolType);
void
testPoolLayer2
(
const
string
&
poolType
,
bool
trans
,
bool
useGpu
)
{
// config.layerConfig.set_size(pool->output_x() * pool->output_y() *
TestConfig
config
;
// pool->channels());
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
3200
,
0
});
//
LayerInputConfig
*
input
=
config
.
layerConfig
.
add_inputs
();
// testLayerGrad(config, "pool", 100, trans, useGpu);
PoolConfig
*
pool
=
input
->
mutable_pool_conf
();
// }
//
pool
->
set_size_y
(
4
);
// #ifndef PADDLE_ONLY_CPU
pool
->
set_stride_y
(
3
);
// void testPoolLayer2(const string& poolType, bool trans, bool useGpu) {
pool
->
set_img_size
(
10
);
// TestConfig config;
pool
->
set_img_size_y
(
20
);
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 3200, 0});
setPoolConfig
(
&
config
,
pool
,
poolType
);
// LayerInputConfig* input = config.layerConfig.add_inputs();
pool
->
set_output_y
((
pool
->
img_size_y
()
-
pool
->
start
()
-
pool
->
size_y
())
/
// PoolConfig* pool = input->mutable_pool_conf();
((
float
)
pool
->
stride_y
())
+
//
1.5
);
// pool->set_size_y(4);
config
.
layerConfig
.
set_size
(
pool
->
output_x
()
*
pool
->
output_y
()
*
// pool->set_stride_y(3);
pool
->
channels
());
// pool->set_img_size(10);
// pool->set_img_size_y(20);
testLayerGrad
(
config
,
"pool"
,
100
,
trans
,
useGpu
);
// setPoolConfig(&config, pool, poolType);
}
// pool->set_output_y((pool->img_size_y() - pool->start() - pool->size_y()) /
#endif
// ((float)pool->stride_y()) +
// 1.5);
TEST
(
Layer
,
PoolLayer
)
{
// config.layerConfig.set_size(pool->output_x() * pool->output_y() *
testPoolLayer
(
"avg-projection"
,
/* trans= */
false
,
/* useGpu= */
false
);
// pool->channels());
testPoolLayer
(
"max-projection"
,
/* trans= */
false
,
/* useGpu= */
false
);
//
// testLayerGrad(config, "pool", 100, trans, useGpu);
#ifndef PADDLE_ONLY_CPU
// }
testPoolLayer
(
"avg-projection"
,
/* trans= */
false
,
/* useGpu= */
true
);
// #endif
testPoolLayer
(
"max-projection"
,
/* trans= */
false
,
/* useGpu= */
true
);
//
testPoolLayer
(
"cudnn-max-pool"
,
/* trans= */
false
,
/* useGpu= */
true
);
// TEST(Layer, PoolLayer) {
testPoolLayer
(
"cudnn-avg-pool"
,
/* trans= */
false
,
/* useGpu= */
true
);
// testPoolLayer("avg-projection", /* trans= */ false, /* useGpu= */ false);
testPoolLayer2
(
"cudnn-max-pool"
,
/* trans= */
false
,
/* useGpu= */
true
);
// testPoolLayer("max-projection", /* trans= */ false, /* useGpu= */ false);
testPoolLayer2
(
"cudnn-avg-pool"
,
/* trans= */
false
,
/* useGpu= */
true
);
//
#endif
// #ifndef PADDLE_ONLY_CPU
}
// testPoolLayer("avg-projection", /* trans= */ false, /* useGpu= */ true);
// testPoolLayer("max-projection", /* trans= */ false, /* useGpu= */ true);
void
testSppLayer
(
const
string
&
poolType
,
// testPoolLayer("cudnn-max-pool", /* trans= */ false, /* useGpu= */ true);
const
int
pyramidHeight
,
// testPoolLayer("cudnn-avg-pool", /* trans= */ false, /* useGpu= */ true);
bool
trans
,
// testPoolLayer2("cudnn-max-pool", /* trans= */ false, /* useGpu= */ true);
bool
useGpu
)
{
// testPoolLayer2("cudnn-avg-pool", /* trans= */ false, /* useGpu= */ true);
TestConfig
config
;
// #endif
config
.
layerConfig
.
set_type
(
"spp"
);
// }
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
3200
,
0
});
//
LayerInputConfig
*
input
=
config
.
layerConfig
.
add_inputs
();
// void testSppLayer(const string& poolType,
SppConfig
*
sppConfig
=
input
->
mutable_spp_conf
();
// const int pyramidHeight,
sppConfig
->
set_pool_type
(
poolType
);
// bool trans,
sppConfig
->
set_pyramid_height
(
pyramidHeight
);
// bool useGpu) {
ImageConfig
*
imageConfig
=
sppConfig
->
mutable_image_conf
();
// TestConfig config;
imageConfig
->
set_channels
(
16
);
// config.layerConfig.set_type("spp");
imageConfig
->
set_img_size
(
10
);
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 3200, 0});
imageConfig
->
set_img_size_y
(
20
);
// LayerInputConfig* input = config.layerConfig.add_inputs();
int
outputSize
=
(
std
::
pow
(
4
,
sppConfig
->
pyramid_height
())
-
1
)
/
(
4
-
1
);
// SppConfig* sppConfig = input->mutable_spp_conf();
config
.
layerConfig
.
set_size
(
outputSize
*
imageConfig
->
channels
());
// sppConfig->set_pool_type(poolType);
testLayerGrad
(
config
,
"spp"
,
100
,
trans
,
useGpu
);
// sppConfig->set_pyramid_height(pyramidHeight);
}
// ImageConfig* imageConfig = sppConfig->mutable_image_conf();
// imageConfig->set_channels(16);
TEST
(
Layer
,
SpatialPyramidPoolLayer
)
{
// imageConfig->set_img_size(10);
for
(
auto
useGpu
:
{
false
,
true
})
{
// imageConfig->set_img_size_y(20);
for
(
auto
pyramidHeight
:
{
1
,
2
,
3
})
{
// int outputSize = (std::pow(4, sppConfig->pyramid_height()) - 1) / (4 - 1);
testSppLayer
(
"avg-projection"
,
pyramidHeight
,
false
,
useGpu
);
// config.layerConfig.set_size(outputSize * imageConfig->channels());
testSppLayer
(
"max-projection"
,
pyramidHeight
,
false
,
useGpu
);
// testLayerGrad(config, "spp", 100, trans, useGpu);
}
// }
}
//
}
// TEST(Layer, SpatialPyramidPoolLayer) {
// for (auto useGpu : {false, true}) {
TEST
(
Layer
,
rankCostLayer
)
{
// for (auto pyramidHeight : {1, 2, 3}) {
TestConfig
config
;
// testSppLayer("avg-projection", pyramidHeight, false, useGpu);
config
.
layerConfig
.
set_type
(
"rank-cost"
);
// testSppLayer("max-projection", pyramidHeight, false, useGpu);
config
.
biasSize
=
0
;
// }
// }
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
1
,
0
});
// }
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_1"
,
1
,
0
});
//
config
.
inputDefs
.
push_back
({
INPUT_DATA_TARGET
,
"layer_2"
,
1
,
0
});
// TEST(Layer, rankCostLayer) {
config
.
layerConfig
.
add_inputs
();
// TestConfig config;
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.set_type("rank-cost");
config
.
layerConfig
.
add_inputs
();
// config.biasSize = 0;
//
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 1, 0});
testLayerGrad
(
config
,
"rank-cost"
,
100
,
false
,
useGpu
);
// config.inputDefs.push_back({INPUT_DATA, "layer_1", 1, 0});
}
// config.inputDefs.push_back({INPUT_DATA_TARGET, "layer_2", 1, 0});
}
// config.layerConfig.add_inputs();
// config.layerConfig.add_inputs();
TEST
(
Layer
,
sumCostLayer
)
{
// config.layerConfig.add_inputs();
TestConfig
config
;
//
config
.
layerConfig
.
set_type
(
"sum_cost"
);
// for (auto useGpu : {false, true}) {
config
.
biasSize
=
0
;
// testLayerGrad(config, "rank-cost", 100, false, useGpu);
// }
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
1
,
0
});
// }
config
.
layerConfig
.
add_inputs
();
//
// TEST(Layer, sumCostLayer) {
for
(
auto
useGpu
:
{
false
,
true
})
{
// TestConfig config;
testLayerGrad
(
config
,
"sum_cost"
,
100
,
false
,
useGpu
);
// config.layerConfig.set_type("sum_cost");
}
// config.biasSize = 0;
}
//
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 1, 0});
TEST
(
Layer
,
weightedRankCostLayer
)
{
// config.layerConfig.add_inputs();
TestConfig
config
;
//
config
.
layerConfig
.
set_type
(
"rank-cost"
);
// for (auto useGpu : {false, true}) {
config
.
biasSize
=
0
;
// testLayerGrad(config, "sum_cost", 100, false, useGpu);
// }
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
1
,
0
});
// }
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_1"
,
1
,
0
});
//
config
.
inputDefs
.
push_back
({
INPUT_DATA_TARGET
,
"layer_2"
,
1
,
0
});
// TEST(Layer, weightedRankCostLayer) {
config
.
inputDefs
.
push_back
({
INPUT_DATA_TARGET
,
"layer_3"
,
1
,
0
});
// TestConfig config;
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.set_type("rank-cost");
config
.
layerConfig
.
add_inputs
();
// config.biasSize = 0;
config
.
layerConfig
.
add_inputs
();
//
config
.
layerConfig
.
add_inputs
();
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 1, 0});
// config.inputDefs.push_back({INPUT_DATA, "layer_1", 1, 0});
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.inputDefs.push_back({INPUT_DATA_TARGET, "layer_2", 1, 0});
testLayerGrad
(
config
,
"weighted-rank-cost"
,
100
,
false
,
useGpu
);
// config.inputDefs.push_back({INPUT_DATA_TARGET, "layer_3", 1, 0});
}
// config.layerConfig.add_inputs();
}
// config.layerConfig.add_inputs();
// config.layerConfig.add_inputs();
TEST
(
Layer
,
TensorLayer
)
{
// config.layerConfig.add_inputs();
TestConfig
config
;
//
config
.
layerConfig
.
set_type
(
"tensor"
);
// for (auto useGpu : {false, true}) {
config
.
layerConfig
.
set_size
(
10
);
// testLayerGrad(config, "weighted-rank-cost", 100, false, useGpu);
config
.
layerConfig
.
set_active_type
(
"sigmoid"
);
// }
config
.
biasSize
=
config
.
layerConfig
.
size
();
// }
//
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
5
,
250
});
// TEST(Layer, TensorLayer) {
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_1"
,
5
,
0
});
// TestConfig config;
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.set_type("tensor");
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.set_size(10);
// config.layerConfig.set_active_type("sigmoid");
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.biasSize = config.layerConfig.size();
testLayerGrad
(
config
,
"tensor"
,
100
,
false
,
useGpu
);
//
}
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 5, 250});
}
// config.inputDefs.push_back({INPUT_DATA, "layer_1", 5, 0});
// config.layerConfig.add_inputs();
TEST
(
Layer
,
RecurrentLayer
)
{
// config.layerConfig.add_inputs();
TestConfig
config
;
//
config
.
layerConfig
.
set_type
(
"recurrent"
);
// for (auto useGpu : {false, true}) {
config
.
layerConfig
.
set_size
(
4
);
// testLayerGrad(config, "tensor", 100, false, useGpu);
config
.
layerConfig
.
set_active_type
(
"tanh"
);
// }
config
.
biasSize
=
4
;
// }
//
config
.
inputDefs
.
push_back
(
// TEST(Layer, RecurrentLayer) {
{
INPUT_SEQUENCE_DATA
,
"layer_0"
,
/* dim= */
4
,
/* paraSize= */
16
});
// TestConfig config;
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.set_type("recurrent");
// config.layerConfig.set_size(4);
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.layerConfig.set_active_type("tanh");
for
(
auto
reversed
:
{
false
,
true
})
{
// config.biasSize = 4;
config
.
layerConfig
.
set_reversed
(
reversed
);
//
config
.
testState
=
!
reversed
;
// config.inputDefs.push_back(
testLayerGrad
(
config
,
"recurrent"
,
50
,
/* trans= */
false
,
useGpu
);
// {INPUT_SEQUENCE_DATA, "layer_0", /* dim= */ 4, /* paraSize= */ 16});
}
// config.layerConfig.add_inputs();
}
//
}
// for (auto useGpu : {false, true}) {
// for (auto reversed : {false, true}) {
TEST
(
Layer
,
LstmLayer
)
{
// config.layerConfig.set_reversed(reversed);
TestConfig
config
;
// config.testState = !reversed;
config
.
layerConfig
.
set_type
(
"lstmemory"
);
// testLayerGrad(config, "recurrent", 50, /* trans= */ false, useGpu);
config
.
layerConfig
.
set_size
(
4
);
// }
config
.
layerConfig
.
set_active_type
(
"tanh"
);
// }
config
.
layerConfig
.
set_active_state_type
(
"sigmoid"
);
// }
config
.
layerConfig
.
set_active_gate_type
(
"sigmoid"
);
//
config
.
biasSize
=
28
;
// TEST(Layer, LstmLayer) {
// TestConfig config;
config
.
inputDefs
.
push_back
(
// config.layerConfig.set_type("lstmemory");
{
INPUT_SEQUENCE_DATA
,
"layer_0"
,
/* dim= */
16
,
/* paraSize= */
64
});
// config.layerConfig.set_size(4);
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.set_active_type("tanh");
// config.layerConfig.set_active_state_type("sigmoid");
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.layerConfig.set_active_gate_type("sigmoid");
for
(
auto
reversed
:
{
false
,
true
})
{
// config.biasSize = 28;
config
.
layerConfig
.
set_reversed
(
reversed
);
//
config
.
testState
=
!
reversed
;
// config.inputDefs.push_back(
testLayerGrad
(
config
,
"lstmemory"
,
100
,
/* trans= */
false
,
useGpu
);
// {INPUT_SEQUENCE_DATA, "layer_0", /* dim= */ 16, /* paraSize= */ 64});
}
// config.layerConfig.add_inputs();
}
//
for
(
auto
useGpu
:
{
true
})
{
// for (auto useGpu : {false, true}) {
config
.
testBatchState
=
true
;
// for (auto reversed : {false, true}) {
config
.
layerConfig
.
set_reversed
(
false
);
// config.layerConfig.set_reversed(reversed);
testLayerGrad
(
config
,
"lstmemory"
,
10
,
/* trans= */
false
,
useGpu
);
// config.testState = !reversed;
}
// testLayerGrad(config, "lstmemory", 100, /* trans= */ false, useGpu);
}
// }
// }
TEST
(
Layer
,
MDLstmLayer
)
{
// for (auto useGpu : {true}) {
TestConfig
config
;
// config.testBatchState = true;
config
.
layerConfig
.
set_type
(
"mdlstmemory"
);
// config.layerConfig.set_reversed(false);
config
.
layerConfig
.
set_size
(
4
);
// testLayerGrad(config, "lstmemory", 10, /* trans= */ false, useGpu);
config
.
layerConfig
.
set_active_type
(
"sigmoid"
);
// }
config
.
layerConfig
.
set_active_state_type
(
"sigmoid"
);
// }
config
.
layerConfig
.
set_active_gate_type
(
"sigmoid"
);
//
config
.
biasSize
=
4
*
9
;
// TEST(Layer, MDLstmLayer) {
// TestConfig config;
config
.
inputDefs
.
push_back
(
// config.layerConfig.set_type("mdlstmemory");
{
INPUT_SEQUENCE_MDIM_DATA
,
"layer_0"
,
4
*
5
,
4
*
4
*
5
});
// config.layerConfig.set_size(4);
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.set_active_type("sigmoid");
config
.
layerConfig
.
add_directions
(
true
);
// config.layerConfig.set_active_state_type("sigmoid");
config
.
layerConfig
.
add_directions
(
true
);
// config.layerConfig.set_active_gate_type("sigmoid");
// config.biasSize = 4 * 9;
for
(
auto
useGpu
:
{
false
,
true
})
{
//
for
(
int
i
=
0
;
i
<
2
;
i
++
)
{
// config.inputDefs.push_back(
for
(
int
j
=
0
;
j
<
2
;
j
++
)
{
// {INPUT_SEQUENCE_MDIM_DATA, "layer_0", 4 * 5, 4 * 4 * 5});
config
.
layerConfig
.
set_directions
(
0
,
bool
(
i
));
// config.layerConfig.add_inputs();
config
.
layerConfig
.
set_directions
(
1
,
bool
(
j
));
// config.layerConfig.add_directions(true);
testLayerGrad
(
config
,
"mdlstmemory"
,
100
,
false
,
useGpu
);
// config.layerConfig.add_directions(true);
}
//
}
// for (auto useGpu : {false, true}) {
}
// for (int i = 0; i < 2; i++) {
}
// for (int j = 0; j < 2; j++) {
// config.layerConfig.set_directions(0, bool(i));
TEST
(
Layer
,
ParameterReluLayer
)
{
// config.layerConfig.set_directions(1, bool(j));
auto
testParameterReluLayer
=
[
&
](
size_t
inputSize
,
size_t
channels
)
{
// testLayerGrad(config, "mdlstmemory", 100, false, useGpu);
TestConfig
config
;
// }
config
.
layerConfig
.
set_type
(
"prelu"
);
// }
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
inputSize
,
channels
});
// }
config
.
layerConfig
.
add_inputs
();
// }
config
.
layerConfig
.
set_size
(
inputSize
);
//
config
.
layerConfig
.
set_partial_sum
(
inputSize
/
// TEST(Layer, ParameterReluLayer) {
channels
);
// size of feature map
// auto testParameterReluLayer = [&](size_t inputSize, size_t channels) {
for
(
auto
useGpu
:
{
false
,
true
})
{
// TestConfig config;
testLayerGrad
(
config
,
"prelu"
,
100
,
false
,
useGpu
);
// config.layerConfig.set_type("prelu");
}
// config.inputDefs.push_back({INPUT_DATA, "layer_0", inputSize, channels});
};
// config.layerConfig.add_inputs();
// config.layerConfig.set_size(inputSize);
testParameterReluLayer
(
192
,
1
);
// config.layerConfig.set_partial_sum(inputSize /
testParameterReluLayer
(
192
,
3
);
// channels); // size of feature map
testParameterReluLayer
(
192
,
192
);
// for (auto useGpu : {false, true}) {
}
// testLayerGrad(config, "prelu", 100, false, useGpu);
// }
TEST
(
Layer
,
ResizeLayer
)
{
// };
TestConfig
config
;
//
config
.
biasSize
=
0
;
// testParameterReluLayer(192, 1);
config
.
layerConfig
.
set_type
(
"resize"
);
// testParameterReluLayer(192, 3);
config
.
layerConfig
.
set_size
(
64
);
// testParameterReluLayer(192, 192);
// }
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
16
,
0
});
//
config
.
layerConfig
.
add_inputs
();
// TEST(Layer, ResizeLayer) {
// TestConfig config;
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.biasSize = 0;
testLayerGrad
(
config
,
"resize"
,
100
,
false
,
useGpu
);
// config.layerConfig.set_type("resize");
}
// config.layerConfig.set_size(64);
}
//
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 16, 0});
TEST
(
Layer
,
RotateLayer
)
{
// config.layerConfig.add_inputs();
TestConfig
config
;
//
config
.
biasSize
=
0
;
// for (auto useGpu : {false, true}) {
config
.
layerConfig
.
set_type
(
"rotate"
);
// testLayerGrad(config, "resize", 100, false, useGpu);
const
int
CHANNEL
=
2
;
// }
const
int
HEIGHT
=
8
;
// }
const
int
WIDTH
=
4
;
//
const
int
INPUT_SIZE
=
HEIGHT
*
WIDTH
*
CHANNEL
;
// TEST(Layer, RotateLayer) {
config
.
layerConfig
.
set_size
(
INPUT_SIZE
);
// TestConfig config;
config
.
layerConfig
.
set_height
(
HEIGHT
);
// config.biasSize = 0;
config
.
layerConfig
.
set_width
(
WIDTH
);
// config.layerConfig.set_type("rotate");
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
INPUT_SIZE
,
0
});
// const int CHANNEL = 2;
config
.
layerConfig
.
add_inputs
();
// const int HEIGHT = 8;
// const int WIDTH = 4;
for
(
auto
useGpu
:
{
false
,
true
})
{
// const int INPUT_SIZE = HEIGHT * WIDTH * CHANNEL;
testLayerGrad
(
config
,
"rotate"
,
100
,
false
,
useGpu
);
// config.layerConfig.set_size(INPUT_SIZE);
}
// config.layerConfig.set_height(HEIGHT);
}
// config.layerConfig.set_width(WIDTH);
// config.inputDefs.push_back({INPUT_DATA, "layer_0", INPUT_SIZE, 0});
TEST
(
Layer
,
NCELayer
)
{
// config.layerConfig.add_inputs();
TestConfig
config
;
//
size_t
numClasses
=
4
;
// for (auto useGpu : {false, true}) {
config
.
layerConfig
.
set_type
(
"nce"
);
// testLayerGrad(config, "rotate", 100, false, useGpu);
config
.
layerConfig
.
set_size
(
1
);
// }
config
.
layerConfig
.
set_active_type
(
"sigmoid"
);
// }
config
.
layerConfig
.
set_num_classes
(
numClasses
);
//
config
.
biasSize
=
numClasses
;
// TEST(Layer, NCELayer) {
// TestConfig config;
config
.
inputDefs
.
push_back
(
// size_t numClasses = 4;
{
INPUT_DATA
,
"layer_0"
,
/* dim= */
16
,
/* paraSize= */
16
*
numClasses
});
// config.layerConfig.set_type("nce");
config
.
inputDefs
.
push_back
(
// config.layerConfig.set_size(1);
{
INPUT_LABEL
,
"label"
,
/* dim= */
numClasses
,
/* paraSize= */
0
});
// config.layerConfig.set_active_type("sigmoid");
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.set_num_classes(numClasses);
config
.
layerConfig
.
add_inputs
();
// config.biasSize = numClasses;
//
for
(
auto
withWeight
:
{
false
,
true
})
{
// config.inputDefs.push_back(
if
(
withWeight
)
{
// {INPUT_DATA, "layer_0", /* dim= */ 16, /* paraSize= */ 16 *
config
.
inputDefs
.
push_back
(
// numClasses});
{
INPUT_DATA_TARGET
,
"weight"
,
/* dim= */
1
,
/* paraSize= */
0
});
// config.inputDefs.push_back(
config
.
layerConfig
.
add_inputs
();
// {INPUT_LABEL, "label", /* dim= */ numClasses, /* paraSize= */ 0});
}
// config.layerConfig.add_inputs();
// config.layerConfig.add_inputs();
for
(
auto
isIdLabel
:
{
false
,
true
})
{
//
config
.
inputDefs
[
1
]
=
{
// for (auto withWeight : {false, true}) {
isIdLabel
?
INPUT_LABEL
:
INPUT_SPARSE_NON_VALUE_DATA
,
// if (withWeight) {
"label"
,
// config.inputDefs.push_back(
/* dim= */
numClasses
,
// {INPUT_DATA_TARGET, "weight", /* dim= */ 1, /* paraSize= */ 0});
/* paraSize= */
0
};
// config.layerConfig.add_inputs();
// }
for
(
auto
withDist
:
{
false
,
true
})
{
//
config
.
layerConfig
.
clear_neg_sampling_dist
();
// for (auto isIdLabel : {false, true}) {
if
(
withDist
)
{
// config.inputDefs[1] = {
double
sum
=
0
;
// isIdLabel ? INPUT_LABEL : INPUT_SPARSE_NON_VALUE_DATA,
for
(
size_t
i
=
0
;
i
<
numClasses
;
++
i
)
{
// "label",
real
p
=
rand
();
// NOLINT use rand_r
// /* dim= */ numClasses,
config
.
layerConfig
.
add_neg_sampling_dist
(
p
);
// /* paraSize= */ 0};
sum
+=
p
;
//
}
// for (auto withDist : {false, true}) {
for
(
size_t
i
=
0
;
i
<
numClasses
;
++
i
)
{
// config.layerConfig.clear_neg_sampling_dist();
real
p
=
config
.
layerConfig
.
neg_sampling_dist
(
i
)
/
sum
;
// if (withDist) {
config
.
layerConfig
.
set_neg_sampling_dist
(
i
,
p
);
// double sum = 0;
}
// for (size_t i = 0; i < numClasses; ++i) {
}
// real p = rand(); // NOLINT use rand_r
LOG
(
INFO
)
<<
"NCELayer "
// config.layerConfig.add_neg_sampling_dist(p);
<<
" isIdLabel="
<<
isIdLabel
<<
" withWeight="
<<
withWeight
// sum += p;
<<
" withDist="
<<
withDist
;
// }
// Not support GPU now
// for (size_t i = 0; i < numClasses; ++i) {
testLayerGrad
(
config
,
// real p = config.layerConfig.neg_sampling_dist(i) / sum;
"nce"
,
// config.layerConfig.set_neg_sampling_dist(i, p);
100
,
// }
/* trans= */
false
,
// }
/* useGpu */
false
);
// LOG(INFO) << "NCELayer "
}
// << " isIdLabel=" << isIdLabel << " withWeight=" <<
}
// withWeight
}
// << " withDist=" << withDist;
}
// // Not support GPU now
// testLayerGrad(config,
TEST
(
Layer
,
GatedRecurrentLayer
)
{
// "nce",
TestConfig
config
;
// 100,
config
.
layerConfig
.
set_type
(
"gated_recurrent"
);
// /* trans= */ false,
config
.
layerConfig
.
set_size
(
4
);
// /* useGpu */ false);
config
.
layerConfig
.
set_active_type
(
"sigmoid"
);
// }
config
.
layerConfig
.
set_active_gate_type
(
"sigmoid"
);
// }
config
.
biasSize
=
12
;
// }
// }
config
.
inputDefs
.
push_back
(
//
{
INPUT_SEQUENCE_DATA
,
"layer_0"
,
/* dim= */
12
,
/* paraSize= */
48
});
// TEST(Layer, GatedRecurrentLayer) {
config
.
layerConfig
.
add_inputs
();
// TestConfig config;
// config.layerConfig.set_type("gated_recurrent");
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.layerConfig.set_size(4);
for
(
auto
reversed
:
{
false
,
true
})
{
// config.layerConfig.set_active_type("sigmoid");
config
.
layerConfig
.
set_reversed
(
reversed
);
// config.layerConfig.set_active_gate_type("sigmoid");
config
.
testState
=
!
reversed
;
// config.biasSize = 12;
testLayerGrad
(
config
,
"gated_recurrent"
,
100
,
/* trans= */
false
,
useGpu
);
//
}
// config.inputDefs.push_back(
}
// {INPUT_SEQUENCE_DATA, "layer_0", /* dim= */ 12, /* paraSize= */ 48});
}
// config.layerConfig.add_inputs();
//
TEST
(
Layer
,
GruStepLayer
)
{
// for (auto useGpu : {false, true}) {
TestConfig
config
;
// for (auto reversed : {false, true}) {
config
.
layerConfig
.
set_type
(
"gru_step"
);
// config.layerConfig.set_reversed(reversed);
config
.
layerConfig
.
set_size
(
4
);
// config.testState = !reversed;
config
.
layerConfig
.
set_active_type
(
"sigmoid"
);
// testLayerGrad(config, "gated_recurrent", 100, /* trans= */ false,
config
.
layerConfig
.
set_active_gate_type
(
"sigmoid"
);
// useGpu);
config
.
biasSize
=
12
;
// }
// }
config
.
inputDefs
.
push_back
(
// }
{
INPUT_DATA
,
"layer_0"
,
/* dim= */
12
,
/* paraSize= */
48
});
//
config
.
inputDefs
.
push_back
(
// TEST(Layer, GruStepLayer) {
{
INPUT_DATA
,
"layer_1"
,
/* dim= */
4
,
/* paraSize= */
0
});
// TestConfig config;
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.set_type("gru_step");
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.set_size(4);
// config.layerConfig.set_active_type("sigmoid");
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.layerConfig.set_active_gate_type("sigmoid");
testLayerGrad
(
config
,
"gruStep"
,
100
,
/* trans= */
false
,
useGpu
);
// config.biasSize = 12;
}
//
}
// config.inputDefs.push_back(
// {INPUT_DATA, "layer_0", /* dim= */ 12, /* paraSize= */ 48});
TEST
(
Layer
,
LstmStepLayer
)
{
// config.inputDefs.push_back(
TestConfig
config
;
// {INPUT_DATA, "layer_1", /* dim= */ 4, /* paraSize= */ 0});
config
.
layerConfig
.
set_type
(
"lstm_step"
);
// config.layerConfig.add_inputs();
config
.
layerConfig
.
set_size
(
4
);
// config.layerConfig.add_inputs();
config
.
layerConfig
.
set_active_type
(
"sigmoid"
);
//
config
.
layerConfig
.
set_active_state_type
(
"sigmoid"
);
// for (auto useGpu : {false, true}) {
config
.
layerConfig
.
set_active_gate_type
(
"sigmoid"
);
// testLayerGrad(config, "gruStep", 100, /* trans= */ false, useGpu);
config
.
biasSize
=
12
;
// }
config
.
testAccumulate
=
false
;
// }
//
config
.
inputDefs
.
push_back
(
// TEST(Layer, LstmStepLayer) {
{
INPUT_DATA
,
"layer_0"
,
/* dim= */
16
,
/* paraSize= */
0
});
// TestConfig config;
config
.
inputDefs
.
push_back
(
// config.layerConfig.set_type("lstm_step");
{
INPUT_DATA
,
"layer_1"
,
/* dim= */
4
,
/* paraSize= */
0
});
// config.layerConfig.set_size(4);
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.set_active_type("sigmoid");
config
.
layerConfig
.
add_inputs
();
// config.layerConfig.set_active_state_type("sigmoid");
// config.layerConfig.set_active_gate_type("sigmoid");
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.biasSize = 12;
testLayerGrad
(
config
,
"lstmStep"
,
100
,
/* trans= */
false
,
useGpu
);
// config.testAccumulate = false;
}
//
}
// config.inputDefs.push_back(
// {INPUT_DATA, "layer_0", /* dim= */ 16, /* paraSize= */ 0});
void
testBatchNormLayer
(
const
string
&
type
,
bool
trans
,
bool
useGpu
)
{
// config.inputDefs.push_back(
TestConfig
config
;
// {INPUT_DATA, "layer_1", /* dim= */ 4, /* paraSize= */ 0});
const
int
CHANNELS
=
10
;
// config.layerConfig.add_inputs();
const
int
IMG_SIZE
=
16
;
// config.layerConfig.add_inputs();
const
int
IMG_SIZE_Y
=
8
;
//
size_t
size
=
CHANNELS
*
IMG_SIZE
*
IMG_SIZE_Y
;
// for (auto useGpu : {false, true}) {
config
.
layerConfig
.
set_type
(
type
);
// testLayerGrad(config, "lstmStep", 100, /* trans= */ false, useGpu);
config
.
layerConfig
.
set_size
(
size
);
// }
config
.
layerConfig
.
set_active_type
(
"sigmoid"
);
// }
config
.
biasSize
=
CHANNELS
;
//
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
// void testBatchNormLayer(const string& type, bool trans, bool useGpu) {
"layer_0"
,
// TestConfig config;
/* dim= */
size
,
// const int CHANNELS = 10;
/* paraSize= */
CHANNELS
});
// const int IMG_SIZE = 16;
// const int IMG_SIZE_Y = 8;
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_1_running_mean"
,
1
,
CHANNELS
});
// size_t size = CHANNELS * IMG_SIZE * IMG_SIZE_Y;
config
.
inputDefs
.
back
().
isStatic
=
true
;
// config.layerConfig.set_type(type);
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_2_running_var"
,
1
,
CHANNELS
});
// config.layerConfig.set_size(size);
config
.
inputDefs
.
back
().
isStatic
=
true
;
// config.layerConfig.set_active_type("sigmoid");
// config.biasSize = CHANNELS;
LayerInputConfig
*
input
=
config
.
layerConfig
.
add_inputs
();
// config.inputDefs.push_back({INPUT_DATA,
config
.
layerConfig
.
add_inputs
();
// "layer_0",
config
.
layerConfig
.
add_inputs
();
// /* dim= */ size,
// /* paraSize= */ CHANNELS});
ImageConfig
*
img_conf
=
input
->
mutable_image_conf
();
//
img_conf
->
set_channels
(
CHANNELS
);
// config.inputDefs.push_back({INPUT_DATA, "layer_1_running_mean", 1,
img_conf
->
set_img_size
(
IMG_SIZE
);
// CHANNELS});
img_conf
->
set_img_size_y
(
IMG_SIZE_Y
);
// config.inputDefs.back().isStatic = true;
// config.inputDefs.push_back({INPUT_DATA, "layer_2_running_var", 1,
testLayerGrad
(
config
,
// CHANNELS});
"batch_norm"
,
// config.inputDefs.back().isStatic = true;
64
,
//
/* trans= */
trans
,
// LayerInputConfig* input = config.layerConfig.add_inputs();
useGpu
,
// config.layerConfig.add_inputs();
/* useWeight */
true
);
// config.layerConfig.add_inputs();
}
//
// ImageConfig* img_conf = input->mutable_image_conf();
TEST
(
Layer
,
BatchNormalizationLayer
)
{
// img_conf->set_channels(CHANNELS);
testBatchNormLayer
(
"batch_norm"
,
false
,
false
);
// img_conf->set_img_size(IMG_SIZE);
#ifndef PADDLE_ONLY_CPU
// img_conf->set_img_size_y(IMG_SIZE_Y);
testBatchNormLayer
(
"batch_norm"
,
false
,
true
);
//
if
(
hl_get_cudnn_lib_version
()
>=
int
(
4000
))
{
// testLayerGrad(config,
testBatchNormLayer
(
"cudnn_batch_norm"
,
false
,
true
);
// "batch_norm",
}
// 64,
#endif
// /* trans= */ trans,
}
// useGpu,
// /* useWeight */ true);
void
testConvOperator
(
bool
isDeconv
)
{
// }
TestConfig
config
;
//
const
int
NUM_FILTERS
=
16
;
// TEST(Layer, BatchNormalizationLayer) {
const
int
FILTER_SIZE
=
2
;
// testBatchNormLayer("batch_norm", false, false);
const
int
FILTER_SIZE_Y
=
3
;
// #ifndef PADDLE_ONLY_CPU
const
int
CHANNELS
=
3
;
// testBatchNormLayer("batch_norm", false, true);
const
int
IMAGE_SIZE
=
16
;
// if (hl_get_cudnn_lib_version() >= int(4000)) {
const
int
IMAGE_SIZE_Y
=
9
;
// testBatchNormLayer("cudnn_batch_norm", false, true);
OperatorConfig
&
operatorConf
=
*
config
.
layerConfig
.
add_operator_confs
();
// }
if
(
isDeconv
)
{
// #endif
operatorConf
.
set_type
(
"convt"
);
// }
}
else
{
//
operatorConf
.
set_type
(
"conv"
);
// void testConvOperator(bool isDeconv) {
}
// TestConfig config;
ConvConfig
*
conv
=
operatorConf
.
mutable_conv_conf
();
// const int NUM_FILTERS = 16;
operatorConf
.
set_num_filters
(
NUM_FILTERS
);
// const int FILTER_SIZE = 2;
conv
->
set_filter_size
(
FILTER_SIZE
);
// const int FILTER_SIZE_Y = 3;
conv
->
set_filter_size_y
(
FILTER_SIZE_Y
);
// const int CHANNELS = 3;
conv
->
set_channels
(
CHANNELS
);
// const int IMAGE_SIZE = 16;
conv
->
set_padding
(
0
);
// const int IMAGE_SIZE_Y = 9;
conv
->
set_padding_y
(
1
);
// OperatorConfig& operatorConf = *config.layerConfig.add_operator_confs();
conv
->
set_stride
(
2
);
// if (isDeconv) {
conv
->
set_stride_y
(
2
);
// operatorConf.set_type("convt");
conv
->
set_groups
(
1
);
// } else {
conv
->
set_img_size
(
IMAGE_SIZE
);
// operatorConf.set_type("conv");
conv
->
set_img_size_y
(
IMAGE_SIZE_Y
);
// }
conv
->
set_output_x
(
outputSize
(
conv
->
img_size
(),
// ConvConfig* conv = operatorConf.mutable_conv_conf();
conv
->
filter_size
(),
// operatorConf.set_num_filters(NUM_FILTERS);
conv
->
padding
(),
// conv->set_filter_size(FILTER_SIZE);
conv
->
stride
(),
// conv->set_filter_size_y(FILTER_SIZE_Y);
/* caffeMode */
true
));
// conv->set_channels(CHANNELS);
conv
->
set_output_y
(
outputSize
(
conv
->
img_size_y
(),
// conv->set_padding(0);
conv
->
filter_size_y
(),
// conv->set_padding_y(1);
conv
->
padding_y
(),
// conv->set_stride(2);
conv
->
stride_y
(),
// conv->set_stride_y(2);
/* caffeMode */
true
));
// conv->set_groups(1);
// conv->set_img_size(IMAGE_SIZE);
if
(
isDeconv
)
{
// conv->set_img_size_y(IMAGE_SIZE_Y);
conv
->
set_filter_channels
(
NUM_FILTERS
/
conv
->
groups
());
// conv->set_output_x(outputSize(conv->img_size(),
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
// conv->filter_size(),
"layer_0"
,
// conv->padding(),
conv
->
output_x
()
*
conv
->
output_y
()
*
CHANNELS
,
// conv->stride(),
0
});
// /* caffeMode */ true));
config
.
layerConfig
.
set_size
(
IMAGE_SIZE
*
IMAGE_SIZE_Y
*
NUM_FILTERS
);
// conv->set_output_y(outputSize(conv->img_size_y(),
}
else
{
// conv->filter_size_y(),
conv
->
set_filter_channels
(
conv
->
channels
()
/
conv
->
groups
());
// conv->padding_y(),
config
.
inputDefs
.
push_back
(
// conv->stride_y(),
{
INPUT_DATA
,
"layer_0"
,
IMAGE_SIZE
*
IMAGE_SIZE_Y
*
CHANNELS
,
0
});
// /* caffeMode */ true));
config
.
layerConfig
.
set_size
(
conv
->
output_x
()
*
conv
->
output_y
()
*
//
NUM_FILTERS
);
// if (isDeconv) {
}
// conv->set_filter_channels(NUM_FILTERS / conv->groups());
// config.inputDefs.push_back({INPUT_DATA,
config
.
inputDefs
.
push_back
(
// "layer_0",
{
INPUT_DATA
,
// conv->output_x() * conv->output_y() *
"layer_1"
,
// CHANNELS,
FILTER_SIZE
*
FILTER_SIZE_Y
*
CHANNELS
*
NUM_FILTERS
,
// 0});
0
});
// config.layerConfig.set_size(IMAGE_SIZE * IMAGE_SIZE_Y * NUM_FILTERS);
config
.
layerConfig
.
add_inputs
();
// } else {
config
.
layerConfig
.
add_inputs
();
// conv->set_filter_channels(conv->channels() / conv->groups());
// config.inputDefs.push_back(
testOperatorGrad
(
config
,
operatorConf
,
100
,
/*useGpu*/
true
,
false
);
// {INPUT_DATA, "layer_0", IMAGE_SIZE * IMAGE_SIZE_Y * CHANNELS, 0});
}
// config.layerConfig.set_size(conv->output_x() * conv->output_y() *
// NUM_FILTERS);
TEST
(
Operator
,
conv
)
{
// }
testConvOperator
(
/*isDeconv*/
true
);
//
testConvOperator
(
/*isDeconv*/
false
);
// config.inputDefs.push_back(
}
// {INPUT_DATA,
// "layer_1",
TEST
(
Layer
,
FeatureMapExpandLayer
)
{
// FILTER_SIZE * FILTER_SIZE_Y * CHANNELS * NUM_FILTERS,
TestConfig
config
;
// 0});
config
.
layerConfig
.
set_type
(
"featmap_expand"
);
// config.layerConfig.add_inputs();
const
int
CHANNELS
=
10
;
// config.layerConfig.add_inputs();
const
int
INPUT_SIZE
=
100
;
//
config
.
layerConfig
.
set_size
(
INPUT_SIZE
*
CHANNELS
);
// testOperatorGrad(config, operatorConf, 100, /*useGpu*/ true, false);
config
.
layerConfig
.
set_num_filters
(
CHANNELS
);
// }
config
.
inputDefs
.
push_back
({
INPUT_SEQUENCE_DATA
,
//
"layer_0"
,
// TEST(Operator, conv) {
/* dim= */
INPUT_SIZE
,
// testConvOperator(/*isDeconv*/ true);
/* paraSize= */
0
});
// testConvOperator(/*isDeconv*/ false);
config
.
layerConfig
.
add_inputs
();
// }
for
(
auto
useGpu
:
{
false
,
true
})
{
//
for
(
auto
asRowVec
:
{
false
,
true
})
{
// TEST(Layer, FeatureMapExpandLayer) {
config
.
layerConfig
.
set_user_arg
(
asRowVec
?
"as_row_vec"
:
"as_col_vec"
);
// TestConfig config;
testLayerGrad
(
config
,
// config.layerConfig.set_type("featmap_expand");
"featmap_expand"
,
// const int CHANNELS = 10;
/*batch_size*/
100
,
// const int INPUT_SIZE = 100;
/* trans= */
false
,
// config.layerConfig.set_size(INPUT_SIZE * CHANNELS);
useGpu
,
// config.layerConfig.set_num_filters(CHANNELS);
/* useWeight */
true
);
// config.inputDefs.push_back({INPUT_SEQUENCE_DATA,
}
// "layer_0",
}
// /* dim= */ INPUT_SIZE,
}
// /* paraSize= */ 0});
// config.layerConfig.add_inputs();
TEST
(
Layer
,
MultiplexLayer
)
{
// for (auto useGpu : {false, true}) {
TestConfig
config
;
// for (auto asRowVec : {false, true}) {
const
int
LAYER_SIZE
=
100
;
// config.layerConfig.set_user_arg(asRowVec ? "as_row_vec" :
config
.
layerConfig
.
set_type
(
"multiplex"
);
// "as_col_vec");
config
.
layerConfig
.
set_size
(
LAYER_SIZE
);
// testLayerGrad(config,
// "featmap_expand",
config
.
inputDefs
.
push_back
({
INPUT_LABEL
,
"layer_0"
,
2
,
0
});
// /*batch_size*/ 100,
config
.
inputDefs
.
push_back
(
// /* trans= */ false,
{
INPUT_DATA
,
"layer_1"
,
/* dim= */
LAYER_SIZE
,
/* paraSize= */
0
});
// useGpu,
config
.
inputDefs
.
push_back
(
// /* useWeight */ true);
{
INPUT_DATA
,
"layer_2"
,
/* dim= */
LAYER_SIZE
,
/* paraSize= */
0
});
// }
config
.
layerConfig
.
add_inputs
();
// }
config
.
layerConfig
.
add_inputs
();
// }
config
.
layerConfig
.
add_inputs
();
//
// TEST(Layer, MultiplexLayer) {
for
(
auto
useGpu
:
{
false
,
true
})
{
// TestConfig config;
testLayerGrad
(
config
,
"multiplex"
,
512
,
/* trans= */
false
,
useGpu
);
// const int LAYER_SIZE = 100;
}
// config.layerConfig.set_type("multiplex");
}
// config.layerConfig.set_size(LAYER_SIZE);
//
TEST
(
Layer
,
PadLayer
)
{
// config.inputDefs.push_back({INPUT_LABEL, "layer_0", 2, 0});
TestConfig
config
;
// config.inputDefs.push_back(
config
.
biasSize
=
0
;
// {INPUT_DATA, "layer_1", /* dim= */ LAYER_SIZE, /* paraSize= */ 0});
config
.
layerConfig
.
set_type
(
"pad"
);
// config.inputDefs.push_back(
// {INPUT_DATA, "layer_2", /* dim= */ LAYER_SIZE, /* paraSize= */ 0});
int
c
=
4
;
// config.layerConfig.add_inputs();
int
h
=
31
;
// config.layerConfig.add_inputs();
int
w
=
36
;
// config.layerConfig.add_inputs();
size_t
size
=
c
*
h
*
w
;
//
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
size
,
0
});
// for (auto useGpu : {false, true}) {
LayerInputConfig
*
input
=
config
.
layerConfig
.
add_inputs
();
// testLayerGrad(config, "multiplex", 512, /* trans= */ false, useGpu);
PadConfig
*
pad
=
input
->
mutable_pad_conf
();
// }
ImageConfig
*
image
=
pad
->
mutable_image_conf
();
// }
//
image
->
set_channels
(
c
);
// TEST(Layer, PadLayer) {
image
->
set_img_size
(
h
);
// TestConfig config;
image
->
set_img_size_y
(
w
);
// config.biasSize = 0;
pad
->
add_pad_c
(
1
);
// config.layerConfig.set_type("pad");
pad
->
add_pad_c
(
2
);
//
pad
->
add_pad_h
(
2
);
// int c = 4;
pad
->
add_pad_h
(
3
);
// int h = 31;
pad
->
add_pad_w
(
3
);
// int w = 36;
pad
->
add_pad_w
(
5
);
// size_t size = c * h * w;
// config.inputDefs.push_back({INPUT_DATA, "layer_0", size, 0});
for
(
auto
useGpu
:
{
false
,
true
})
{
// LayerInputConfig* input = config.layerConfig.add_inputs();
testLayerGrad
(
config
,
"pad"
,
10
,
false
,
useGpu
);
// PadConfig* pad = input->mutable_pad_conf();
}
// ImageConfig* image = pad->mutable_image_conf();
}
//
// image->set_channels(c);
TEST
(
Layer
,
CrossChannelNormLayer
)
{
// image->set_img_size(h);
TestConfig
config
;
// image->set_img_size_y(w);
config
.
paramInitialMean
=
1.
;
// pad->add_pad_c(1);
config
.
paramInitialStd
=
0.
;
// pad->add_pad_c(2);
config
.
layerConfig
.
set_type
(
"norm"
);
// pad->add_pad_h(2);
config
.
layerConfig
.
set_size
(
100
);
// pad->add_pad_h(3);
LayerInputConfig
*
input
=
config
.
layerConfig
.
add_inputs
();
// pad->add_pad_w(3);
NormConfig
*
norm
=
input
->
mutable_norm_conf
();
// pad->add_pad_w(5);
norm
->
set_norm_type
(
"cross-channel-norm"
);
//
norm
->
set_channels
(
10
);
// for (auto useGpu : {false, true}) {
norm
->
set_size
(
100
);
// testLayerGrad(config, "pad", 10, false, useGpu);
norm
->
set_scale
(
0
);
// }
norm
->
set_pow
(
0
);
// }
norm
->
set_blocked
(
0
);
//
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
100
,
10
});
// TEST(Layer, CrossChannelNormLayer) {
// TestConfig config;
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.paramInitialMean = 1.;
testLayerGrad
(
config
,
"cross-channel-norm"
,
10
,
false
,
useGpu
,
false
);
// config.paramInitialStd = 0.;
}
// config.layerConfig.set_type("norm");
}
// config.layerConfig.set_size(100);
// LayerInputConfig* input = config.layerConfig.add_inputs();
TEST
(
Layer
,
smooth_l1
)
{
// NormConfig* norm = input->mutable_norm_conf();
TestConfig
config
;
// norm->set_norm_type("cross-channel-norm");
config
.
layerConfig
.
set_type
(
"smooth_l1"
);
// norm->set_channels(10);
// norm->set_size(100);
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
200
,
0
});
// norm->set_scale(0);
config
.
inputDefs
.
push_back
({
INPUT_DATA_TARGET
,
"layer_1"
,
200
,
0
});
// norm->set_pow(0);
config
.
layerConfig
.
add_inputs
();
// norm->set_blocked(0);
config
.
layerConfig
.
add_inputs
();
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 100, 10});
//
for
(
auto
useGpu
:
{
false
,
true
})
{
// for (auto useGpu : {false, true}) {
testLayerGrad
(
config
,
"smooth_l1"
,
100
,
false
,
useGpu
,
false
);
// testLayerGrad(config, "cross-channel-norm", 10, false, useGpu, false);
}
// }
}
// }
//
TEST
(
Layer
,
multibox_loss
)
{
// TEST(Layer, smooth_l1) {
TestConfig
config
;
// TestConfig config;
config
.
layerConfig
.
set_type
(
"multibox_loss"
);
// config.layerConfig.set_type("smooth_l1");
config
.
biasSize
=
0
;
//
LayerInputConfig
*
input
=
config
.
layerConfig
.
add_inputs
();
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 200, 0});
MultiBoxLossConfig
*
multiboxLoss
=
input
->
mutable_multibox_loss_conf
();
// config.inputDefs.push_back({INPUT_DATA_TARGET, "layer_1", 200, 0});
multiboxLoss
->
set_num_classes
(
21
);
// config.layerConfig.add_inputs();
multiboxLoss
->
set_input_num
(
1
);
// config.layerConfig.add_inputs();
multiboxLoss
->
set_overlap_threshold
(
0.5
);
//
multiboxLoss
->
set_neg_pos_ratio
(
3
);
// for (auto useGpu : {false, true}) {
multiboxLoss
->
set_neg_overlap
(
0.5
);
// testLayerGrad(config, "smooth_l1", 100, false, useGpu, false);
multiboxLoss
->
set_background_id
(
0
);
// }
multiboxLoss
->
set_height
(
3
);
// }
multiboxLoss
->
set_width
(
3
);
//
// TEST(Layer, multibox_loss) {
size_t
gtNum
=
1
;
// TestConfig config;
MatrixPtr
labelValue
=
Matrix
::
create
(
gtNum
,
6
,
false
,
false
);
// config.layerConfig.set_type("multibox_loss");
labelValue
->
randomizeUniform
();
// config.biasSize = 0;
labelValue
->
add
(
-
0.5
);
// LayerInputConfig* input = config.layerConfig.add_inputs();
labelValue
->
sigmoid
(
*
labelValue
);
// MultiBoxLossConfig* multiboxLoss = input->mutable_multibox_loss_conf();
real
*
labelData
=
labelValue
->
getData
();
// multiboxLoss->set_num_classes(21);
size_t
labelWidth
=
labelValue
->
getWidth
();
// multiboxLoss->set_input_num(1);
for
(
size_t
i
=
0
;
i
<
gtNum
;
++
i
)
{
// multiboxLoss->set_overlap_threshold(0.5);
*
(
labelData
+
i
*
labelWidth
)
=
std
::
rand
()
%
20
+
1
;
// multiboxLoss->set_neg_pos_ratio(3);
*
(
labelData
+
i
*
labelWidth
+
1
)
=
0.400259
;
// multiboxLoss->set_neg_overlap(0.5);
*
(
labelData
+
i
*
labelWidth
+
2
)
=
0.377857
;
// multiboxLoss->set_background_id(0);
*
(
labelData
+
i
*
labelWidth
+
3
)
=
0.525712
;
// multiboxLoss->set_height(3);
*
(
labelData
+
i
*
labelWidth
+
4
)
=
0.519368
;
// multiboxLoss->set_width(3);
}
//
vector
<
int
>
seqStartPositions
(
gtNum
+
1
,
0
);
// size_t gtNum = 1;
for
(
size_t
i
=
1
;
i
<=
gtNum
;
++
i
)
{
// MatrixPtr labelValue = Matrix::create(gtNum, 6, false, false);
seqStartPositions
[
i
]
=
i
;
// labelValue->randomizeUniform();
}
// labelValue->add(-0.5);
// labelValue->sigmoid(*labelValue);
// Ensure at lease one matched bbox
// real* labelData = labelValue->getData();
MatrixPtr
priorValue
=
Matrix
::
create
(
1
,
72
,
false
,
false
);
// size_t labelWidth = labelValue->getWidth();
priorValue
->
randomizeUniform
();
// for (size_t i = 0; i < gtNum; ++i) {
priorValue
->
add
(
-
0.5
);
// *(labelData + i * labelWidth) = std::rand() % 20 + 1;
priorValue
->
sigmoid
(
*
priorValue
);
// *(labelData + i * labelWidth + 1) = 0.400259;
real
*
priorData
=
priorValue
->
getData
();
// *(labelData + i * labelWidth + 2) = 0.377857;
*
(
priorData
)
=
0.424811
;
// *(labelData + i * labelWidth + 3) = 0.525712;
*
(
priorData
+
1
)
=
0.397059
;
// *(labelData + i * labelWidth + 4) = 0.519368;
*
(
priorData
+
2
)
=
0.538905
;
// }
*
(
priorData
+
3
)
=
0.447091
;
// vector<int> seqStartPositions(gtNum + 1, 0);
*
(
priorData
+
4
)
=
0.425720
;
// for (size_t i = 1; i <= gtNum; ++i) {
*
(
priorData
+
5
)
=
0.515228
;
// seqStartPositions[i] = i;
*
(
priorData
+
6
)
=
0.519452
;
// }
*
(
priorData
+
7
)
=
0.591065
;
//
// // Ensure at lease one matched bbox
config
.
inputDefs
.
push_back
(
// MatrixPtr priorValue = Matrix::create(1, 72, false, false);
{
INPUT_SELF_DEFINE_DATA
,
"priorbox"
,
priorValue
,
{}});
// priorValue->randomizeUniform();
config
.
inputDefs
.
push_back
(
// priorValue->add(-0.5);
{
INPUT_SELF_DEFINE_DATA
,
"label"
,
labelValue
,
seqStartPositions
});
// priorValue->sigmoid(*priorValue);
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"locPred"
,
36
,
0
});
// real* priorData = priorValue->getData();
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"confPred"
,
189
,
0
});
// *(priorData) = 0.424811;
config
.
layerConfig
.
add_inputs
();
// *(priorData + 1) = 0.397059;
config
.
layerConfig
.
add_inputs
();
// *(priorData + 2) = 0.538905;
config
.
layerConfig
.
add_inputs
();
// *(priorData + 3) = 0.447091;
// *(priorData + 4) = 0.425720;
for
(
auto
useGpu
:
{
false
,
true
})
{
// *(priorData + 5) = 0.515228;
testLayerGrad
(
config
,
"multibox_loss"
,
1
,
false
,
useGpu
,
false
);
// *(priorData + 6) = 0.519452;
}
// *(priorData + 7) = 0.591065;
}
//
// config.inputDefs.push_back(
TEST
(
Layer
,
TransLayer
)
{
// {INPUT_SELF_DEFINE_DATA, "priorbox", priorValue, {}});
TestConfig
config
;
// config.inputDefs.push_back(
const
int
height
=
128
;
// {INPUT_SELF_DEFINE_DATA, "label", labelValue, seqStartPositions});
const
int
width
=
1028
;
// config.inputDefs.push_back({INPUT_DATA, "locPred", 36, 0});
config
.
layerConfig
.
set_type
(
"trans"
);
// config.inputDefs.push_back({INPUT_DATA, "confPred", 189, 0});
config
.
layerConfig
.
set_size
(
width
);
// config.layerConfig.add_inputs();
// config.layerConfig.add_inputs();
config
.
inputDefs
.
push_back
(
// config.layerConfig.add_inputs();
{
INPUT_DATA
,
"layer_0"
,
/* dim= */
height
*
width
,
/* paraSize= */
0
});
//
config
.
layerConfig
.
add_inputs
();
// for (auto useGpu : {false, true}) {
// testLayerGrad(config, "multibox_loss", 1, false, useGpu, false);
for
(
auto
useGpu
:
{
false
,
true
})
{
// }
testLayerGrad
(
config
,
"trans"
,
height
,
/* trans= */
false
,
useGpu
);
// }
}
//
}
// TEST(Layer, TransLayer) {
// TestConfig config;
TEST
(
Layer
,
RowConvLayer
)
{
// const int height = 128;
const
int
context
=
3
;
// const int width = 1028;
const
int
size
=
512
;
// config.layerConfig.set_type("trans");
// config.layerConfig.set_size(width);
TestConfig
config
;
//
config
.
layerConfig
.
set_type
(
"row_conv"
);
// config.inputDefs.push_back(
config
.
layerConfig
.
set_size
(
size
);
// {INPUT_DATA, "layer_0", /* dim= */ height * width, /* paraSize= */ 0});
config
.
layerConfig
.
set_active_type
(
"sigmoid"
);
// config.layerConfig.add_inputs();
//
config
.
inputDefs
.
push_back
(
// for (auto useGpu : {false, true}) {
{
INPUT_SEQUENCE_DATA
,
"layer_0"
,
size
,
context
*
size
});
// testLayerGrad(config, "trans", height, /* trans= */ false, useGpu);
LayerInputConfig
*
input
=
config
.
layerConfig
.
add_inputs
();
// }
RowConvConfig
*
conv
=
input
->
mutable_row_conv_conf
();
// }
conv
->
set_context_length
(
context
);
//
// TEST(Layer, RowConvLayer) {
for
(
auto
useGpu
:
{
false
,
true
})
{
// const int context = 3;
testLayerGrad
(
config
,
"row_conv"
,
100
,
false
,
useGpu
,
false
);
// const int size = 512;
}
//
}
// TestConfig config;
// config.layerConfig.set_type("row_conv");
TEST
(
Layer
,
CropLayer
)
{
// config.layerConfig.set_size(size);
TestConfig
config
;
// config.layerConfig.set_active_type("sigmoid");
// config input_0
//
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
1024
,
0
});
// config.inputDefs.push_back(
LayerInputConfig
*
input
=
config
.
layerConfig
.
add_inputs
();
// {INPUT_SEQUENCE_DATA, "layer_0", size, context * size});
ImageConfig
*
img
=
input
->
mutable_image_conf
();
// LayerInputConfig* input = config.layerConfig.add_inputs();
img
->
set_channels
(
4
);
// RowConvConfig* conv = input->mutable_row_conv_conf();
img
->
set_img_size
(
16
);
// conv->set_context_length(context);
config
.
layerConfig
.
set_axis
(
2
);
//
config
.
layerConfig
.
add_offset
(
0
);
// for (auto useGpu : {false, true}) {
config
.
layerConfig
.
add_offset
(
0
);
// testLayerGrad(config, "row_conv", 100, false, useGpu, false);
// }
// config input_1
// }
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_1"
,
128
,
0
});
//
input
=
config
.
layerConfig
.
add_inputs
();
// TEST(Layer, CropLayer) {
img
=
input
->
mutable_image_conf
();
// TestConfig config;
img
->
set_channels
(
2
);
// // config input_0
img
->
set_img_size
(
8
);
// config.inputDefs.push_back({INPUT_DATA, "layer_0", 1024, 0});
// LayerInputConfig* input = config.layerConfig.add_inputs();
// config crop layer
// ImageConfig* img = input->mutable_image_conf();
config
.
layerConfig
.
set_type
(
"crop"
);
// img->set_channels(4);
config
.
layerConfig
.
set_name
(
"cropLayer"
);
// img->set_img_size(16);
// config.layerConfig.set_axis(2);
for
(
auto
useGpu
:
{
false
,
true
})
{
// config.layerConfig.add_offset(0);
testLayerGrad
(
config
,
"crop"
,
100
,
false
,
useGpu
,
false
);
// config.layerConfig.add_offset(0);
}
//
}
// // config input_1
// config.inputDefs.push_back({INPUT_DATA, "layer_1", 128, 0});
// input = config.layerConfig.add_inputs();
// img = input->mutable_image_conf();
// img->set_channels(2);
// img->set_img_size(8);
//
// // config crop layer
// config.layerConfig.set_type("crop");
// config.layerConfig.set_name("cropLayer");
//
// for (auto useGpu : {false, true}) {
// testLayerGrad(config, "crop", 100, false, useGpu, false);
// }
// }
vector
<
real
>
randSampling
(
real
range
,
int
n
)
{
vector
<
real
>
randSampling
(
real
range
,
int
n
)
{
CHECK_GE
(
range
,
n
);
CHECK_GE
(
range
,
n
);
...
@@ -1929,18 +1914,20 @@ vector<real> randSampling(real range, int n) {
...
@@ -1929,18 +1914,20 @@ vector<real> randSampling(real range, int n) {
TEST
(
Layer
,
SubNestedSequenceLayer
)
{
TEST
(
Layer
,
SubNestedSequenceLayer
)
{
// layer size is not crutial for this layer,
// layer size is not crutial for this layer,
// so use a small layer size in unittest
// so use a small layer size in unittest
const
int
layerSize
=
8
;
const
int
layerSize
=
4
;
const
int
maxSeqNum
=
5
;
const
int
maxSeqLen
=
5
;
const
int
maxSeqNum
=
50
;
const
int
beamSize
=
3
;
const
int
maxSeqLen
=
50
;
const
int
maxBeamSize
=
32
;
srand
((
size_t
)(
time
(
NULL
)));
int
beamSize
=
1
+
(
rand
()
%
maxBeamSize
);
TestConfig
config
;
TestConfig
config
;
config
.
layerConfig
.
set_type
(
"sub_nested_seq"
);
config
.
layerConfig
.
set_type
(
"sub_nested_seq"
);
config
.
layerConfig
.
set_name
(
"sub_nested_seq_layer"
);
config
.
layerConfig
.
set_name
(
"sub_nested_seq_layer"
);
config
.
layerConfig
.
set_size
(
layerSize
);
config
.
layerConfig
.
set_size
(
layerSize
);
// srand((size_t)(time(NULL)));
srand
(
1
);
int
seqNum
=
1
+
(
rand
()
%
maxSeqNum
);
int
seqNum
=
1
+
(
rand
()
%
maxSeqNum
);
// sequence information for the first input, it is a nested sequence
// sequence information for the first input, it is a nested sequence
...
@@ -1969,6 +1956,7 @@ TEST(Layer, SubNestedSequenceLayer) {
...
@@ -1969,6 +1956,7 @@ TEST(Layer, SubNestedSequenceLayer) {
MatrixPtr
seqInputPtr
=
MatrixPtr
seqInputPtr
=
Matrix
::
create
(
seqStartPos
.
back
(),
layerSize
,
false
,
false
);
Matrix
::
create
(
seqStartPos
.
back
(),
layerSize
,
false
,
false
);
seqInputPtr
->
randomizeUniform
();
config
.
inputDefs
.
push_back
({
INPUT_SELF_DEFINE_DATA
,
config
.
inputDefs
.
push_back
({
INPUT_SELF_DEFINE_DATA
,
"nested_seq_input"
,
"nested_seq_input"
,
seqInputPtr
,
seqInputPtr
,
...
@@ -1989,35 +1977,35 @@ TEST(Layer, SubNestedSequenceLayer) {
...
@@ -1989,35 +1977,35 @@ TEST(Layer, SubNestedSequenceLayer) {
}
}
}
}
//
TEST(Layer, ClipLayer) {
TEST
(
Layer
,
ClipLayer
)
{
//
const size_t batchSize = 128;
const
size_t
batchSize
=
128
;
//
const size_t size = 512;
const
size_t
size
=
512
;
//
TestConfig config;
TestConfig
config
;
//
config.layerConfig.set_type("clip");
config
.
layerConfig
.
set_type
(
"clip"
);
//
config.inputDefs.push_back({INPUT_DATA, "input", size, 0});
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"input"
,
size
,
0
});
//
LayerInputConfig* input = config.layerConfig.add_inputs();
LayerInputConfig
*
input
=
config
.
layerConfig
.
add_inputs
();
//
ClipConfig* layerConf = input->mutable_clip_conf();
ClipConfig
*
layerConf
=
input
->
mutable_clip_conf
();
//
double p1 = std::rand() / (double)RAND_MAX;
double
p1
=
std
::
rand
()
/
(
double
)
RAND_MAX
;
//
double p2 = std::rand() / (double)RAND_MAX;
double
p2
=
std
::
rand
()
/
(
double
)
RAND_MAX
;
//
layerConf->set_min(std::min(p1, p2));
layerConf
->
set_min
(
std
::
min
(
p1
,
p2
));
//
layerConf->set_max(std::max(p1, p2));
layerConf
->
set_max
(
std
::
max
(
p1
,
p2
));
//
for (auto useGpu : {false, true}) {
for
(
auto
useGpu
:
{
false
,
true
})
{
//
testLayerGrad(config, "clip", batchSize, false, useGpu, false);
testLayerGrad
(
config
,
"clip"
,
batchSize
,
false
,
useGpu
,
false
);
//
}
}
//
}
}
//
//
TEST(Layer, RowL2NormLayer) {
TEST
(
Layer
,
RowL2NormLayer
)
{
//
const size_t batchSize = 128;
const
size_t
batchSize
=
128
;
//
const size_t size = 512;
const
size_t
size
=
512
;
//
TestConfig config;
TestConfig
config
;
//
config.layerConfig.set_type("row_l2_norm");
config
.
layerConfig
.
set_type
(
"row_l2_norm"
);
//
config.layerConfig.set_size(size);
config
.
layerConfig
.
set_size
(
size
);
//
config.inputDefs.push_back({INPUT_DATA, "input", size, 0});
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"input"
,
size
,
0
});
//
config.layerConfig.add_inputs();
config
.
layerConfig
.
add_inputs
();
//
for (auto useGpu : {false, true}) {
for
(
auto
useGpu
:
{
false
,
true
})
{
//
testLayerGrad(config, "row_l2_norm", batchSize, false, useGpu, false);
testLayerGrad
(
config
,
"row_l2_norm"
,
batchSize
,
false
,
useGpu
,
false
);
//
}
}
//
}
}
int
main
(
int
argc
,
char
**
argv
)
{
int
main
(
int
argc
,
char
**
argv
)
{
testing
::
InitGoogleTest
(
&
argc
,
argv
);
testing
::
InitGoogleTest
(
&
argc
,
argv
);
...
...
python/paddle/trainer_config_helpers/layers.py
浏览文件 @
ffafc5c9
...
@@ -6097,9 +6097,11 @@ def sub_nested_seq_layer(input, selected_indices, name=None):
...
@@ -6097,9 +6097,11 @@ def sub_nested_seq_layer(input, selected_indices, name=None):
The sub_nested_seq_layer accepts two inputs: the first one is a nested
The sub_nested_seq_layer accepts two inputs: the first one is a nested
sequence; the second one is a set of selceted indices in the nested sequence.
sequence; the second one is a set of selceted indices in the nested sequence.
Then sub_nest_seq_layer trims the first nested sequence input according to
the selected indices to form a new output.
This layer is useful in beam training.
Then sub_nest_seq_layer selects trims the first input according to the
selected indices to give a new output. This layer is used in beam training.
The example usage is:
The example usage is:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录