Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
0b080a42
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
0b080a42
编写于
12月 22, 2017
作者:
T
tensor-tang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add recurrent layer header
上级
b95834dc
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
131 addition
and
105 deletion
+131
-105
paddle/gserver/layers/RecurrentLayer.cpp
paddle/gserver/layers/RecurrentLayer.cpp
+1
-105
paddle/gserver/layers/RecurrentLayer.h
paddle/gserver/layers/RecurrentLayer.h
+130
-0
未找到文件。
paddle/gserver/layers/RecurrentLayer.cpp
浏览文件 @
0b080a42
...
...
@@ -12,6 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "RecurrentLayer.h"
#include <gflags/gflags.h>
#include "Layer.h"
#include "SequenceToBatch.h"
...
...
@@ -21,110 +22,6 @@ DEFINE_bool(rnn_use_batch, false, "Using the batch method for calculation.");
namespace
paddle
{
/**
* @brief RecurrentLayer takes 1 input layer. The output size is the same with
* input layer.
* For each sequence [start, end] it performs the following computation:
* \f[
* out_{i} = act(in_{i}) \ \ \text{for} \ i = start \\
* out_{i} = act(in_{i} + out_{i-1} * W) \ \ \text{for} \ start < i <= end
*
* \f]
* If reversed is true, the order is reversed:
* \f[
* out_{i} = act(in_{i}) \ \ \text{for} \ i = end \\
* out_{i} = act(in_{i} + out_{i+1} * W) \ \ \text{for} \ start <= i < end
* \f]
* There are two methods to calculate rnn. One way is to compute rnn one
* sequence by one sequence. The other way is to reorganize the input
* into batches, then compute rnn one batch by one batch. Users can select
* them by rnn_use_batch flag.
*/
class
RecurrentLayer
:
public
Layer
{
public:
explicit
RecurrentLayer
(
const
LayerConfig
&
config
)
:
Layer
(
config
)
{}
bool
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
override
;
void
forward
(
PassType
passType
)
override
;
void
backward
(
const
UpdateCallback
&
callback
)
override
;
void
resetState
()
override
;
void
setState
(
LayerStatePtr
state
)
override
;
LayerStatePtr
getState
()
override
;
protected:
/**
* @brief If user do not set --rnn_use_batch=true, it will
* compute rnn forward one sequence by one sequence in default.
* @param batchSize Total words number of all samples in this batch.
* @param numSequences The sample number.
* @param starts Each start position of each samples.
*/
void
forwardSequence
(
int
batchSize
,
size_t
numSequences
,
const
int
*
starts
);
/**
* @brief Compute rnn forward by one sequence.
* @param start The start position of this sequence (or sample).
* @param length The length of this sequence (or sample), namely the words
* number of this sequence.
*/
void
forwardOneSequence
(
int
start
,
int
length
);
/**
* @brief Compute rnn backward one sequence by onesequence.
* @param batchSize Total words number of all samples in this batch.
* @param numSequences The sample number.
* @param starts Each start position of each samples.
*/
void
backwardSequence
(
int
batchSize
,
size_t
numSequences
,
const
int
*
starts
);
/**
* @brief Compute rnn backward by one sequence.
* @param start The start position of this sequence (or sample).
* @param length The length of this sequence (or sample), namely the words
* number of this sequence.
*/
void
backwardOneSequence
(
int
start
,
int
length
);
/**
* @brief Reorganize input into batches and compute rnn forward batch
* by batch. It will convert batch shape to sequence after finishing forward.
* The batch info can refer to SequenceToBatch class.
* @param batchSize Total words number of all samples in this batch.
* @param numSequences The sample number.
* @param starts Each start position of each samples.
*/
void
forwardBatch
(
int
batchSize
,
size_t
numSequences
,
const
int
*
starts
);
/**
* @brief Reorganize input into batches and compute rnn forward batch
* by batch.
* @param batchSize Total words number of all samples in this batch.
* @param numSequences The sample number.
* @param starts Each start position of each samples.
*/
void
backwardBatch
(
int
batchSize
,
size_t
numSequences
,
const
int
*
starts
);
protected:
std
::
unique_ptr
<
Weight
>
weight_
;
std
::
unique_ptr
<
Weight
>
bias_
;
/// frameOutput_[i] is used to hold the i-th sample of output_
std
::
vector
<
Argument
>
frameOutput_
;
MatrixPtr
prevOutput_
;
/// Whether compute rnn by reverse.
bool
reversed_
;
/// If compute batch by batch, batchValue_ will be used to save the
/// reorganized input value.
std
::
unique_ptr
<
SequenceToBatch
>
batchValue_
;
/// If compute batch by batch, batchGrad_ will be used to save the
/// gradient with respect to reorganized input value.
std
::
unique_ptr
<
SequenceToBatch
>
batchGrad_
;
};
REGISTER_LAYER
(
recurrent
,
RecurrentLayer
);
bool
RecurrentLayer
::
init
(
const
LayerMap
&
layerMap
,
...
...
@@ -260,7 +157,6 @@ void RecurrentLayer::backward(const UpdateCallback& callback) {
bias_
->
getWGrad
()
->
collectBias
(
*
output_
.
grad
,
1
);
bias_
->
getParameterPtr
()
->
incUpdate
(
callback
);
}
weight_
->
getParameterPtr
()
->
incUpdate
(
callback
);
}
...
...
paddle/gserver/layers/RecurrentLayer.h
0 → 100644
浏览文件 @
0b080a42
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include <gflags/gflags.h>
#include "Layer.h"
#include "SequenceToBatch.h"
#include "paddle/utils/Stat.h"
namespace
paddle
{
/**
* @brief RecurrentLayer takes 1 input layer. The output size is the same with
* input layer.
* For each sequence [start, end] it performs the following computation:
* \f[
* out_{i} = act(in_{i}) \ \ \text{for} \ i = start \\
* out_{i} = act(in_{i} + out_{i-1} * W) \ \ \text{for} \ start < i <= end
*
* \f]
* If reversed is true, the order is reversed:
* \f[
* out_{i} = act(in_{i}) \ \ \text{for} \ i = end \\
* out_{i} = act(in_{i} + out_{i+1} * W) \ \ \text{for} \ start <= i < end
* \f]
* There are two methods to calculate rnn. One way is to compute rnn one
* sequence by one sequence. The other way is to reorganize the input
* into batches, then compute rnn one batch by one batch. Users can select
* them by rnn_use_batch flag.
*/
class
RecurrentLayer
:
public
Layer
{
public:
explicit
RecurrentLayer
(
const
LayerConfig
&
config
)
:
Layer
(
config
)
{}
bool
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
override
;
void
forward
(
PassType
passType
)
override
;
void
backward
(
const
UpdateCallback
&
callback
)
override
;
void
resetState
()
override
;
void
setState
(
LayerStatePtr
state
)
override
;
LayerStatePtr
getState
()
override
;
protected:
/**
* @brief If user do not set --rnn_use_batch=true, it will
* compute rnn forward one sequence by one sequence in default.
* @param batchSize Total words number of all samples in this batch.
* @param numSequences The sample number.
* @param starts Each start position of each samples.
*/
void
forwardSequence
(
int
batchSize
,
size_t
numSequences
,
const
int
*
starts
);
/**
* @brief Compute rnn forward by one sequence.
* @param start The start position of this sequence (or sample).
* @param length The length of this sequence (or sample), namely the words
* number of this sequence.
*/
void
forwardOneSequence
(
int
start
,
int
length
);
/**
* @brief Compute rnn backward one sequence by onesequence.
* @param batchSize Total words number of all samples in this batch.
* @param numSequences The sample number.
* @param starts Each start position of each samples.
*/
void
backwardSequence
(
int
batchSize
,
size_t
numSequences
,
const
int
*
starts
);
/**
* @brief Compute rnn backward by one sequence.
* @param start The start position of this sequence (or sample).
* @param length The length of this sequence (or sample), namely the words
* number of this sequence.
*/
void
backwardOneSequence
(
int
start
,
int
length
);
/**
* @brief Reorganize input into batches and compute rnn forward batch
* by batch. It will convert batch shape to sequence after finishing forward.
* The batch info can refer to SequenceToBatch class.
* @param batchSize Total words number of all samples in this batch.
* @param numSequences The sample number.
* @param starts Each start position of each samples.
*/
virtual
void
forwardBatch
(
int
batchSize
,
size_t
numSequences
,
const
int
*
starts
);
/**
* @brief Reorganize input into batches and compute rnn forward batch
* by batch.
* @param batchSize Total words number of all samples in this batch.
* @param numSequences The sample number.
* @param starts Each start position of each samples.
*/
virtual
void
backwardBatch
(
int
batchSize
,
size_t
numSequences
,
const
int
*
starts
);
protected:
std
::
unique_ptr
<
Weight
>
weight_
;
std
::
unique_ptr
<
Weight
>
bias_
;
/// frameOutput_[i] is used to hold the i-th sample of output_
std
::
vector
<
Argument
>
frameOutput_
;
MatrixPtr
prevOutput_
;
/// Whether compute rnn by reverse.
bool
reversed_
;
/// If compute batch by batch, batchValue_ will be used to save the
/// reorganized input value.
std
::
unique_ptr
<
SequenceToBatch
>
batchValue_
;
/// If compute batch by batch, batchGrad_ will be used to save the
/// gradient with respect to reorganized input value.
std
::
unique_ptr
<
SequenceToBatch
>
batchGrad_
;
};
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录