Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
d8cc21da
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
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看板
提交
d8cc21da
编写于
2月 01, 2018
作者:
F
fengjiayi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine inheritance relationship
上级
f32ca636
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
37 addition
and
31 deletion
+37
-31
paddle/framework/reader.cc
paddle/framework/reader.cc
+1
-1
paddle/framework/reader.h
paddle/framework/reader.h
+36
-30
未找到文件。
paddle/framework/reader.cc
浏览文件 @
d8cc21da
...
...
@@ -17,7 +17,7 @@
namespace
paddle
{
namespace
framework
{
DDim
Reader
::
shape
(
size_t
idx
)
const
{
DDim
File
Reader
::
shape
(
size_t
idx
)
const
{
PADDLE_ENFORCE_LT
(
idx
,
shapes_
.
size
(),
"Cannot get the %d'th shape, 'shapes_' only has %d elements."
,
idx
,
...
...
paddle/framework/reader.h
浏览文件 @
d8cc21da
...
...
@@ -20,32 +20,48 @@
namespace
paddle
{
namespace
framework
{
class
Reader
{
class
Reader
Base
{
public:
Reader
()
{}
explicit
Reader
(
const
std
::
vector
<
DDim
>&
shapes
)
:
shapes_
(
shapes
)
{}
virtual
std
::
vector
<
LoDTensor
>
ReadNext
()
=
0
;
virtual
bool
HasNext
()
const
=
0
;
virtual
DDim
shape
(
size_t
idx
)
const
;
virtual
std
::
vector
<
DDim
>
shapes
()
const
{
return
shapes_
;
}
virtual
DDim
shape
(
size_t
idx
)
const
=
0
;
virtual
std
::
vector
<
DDim
>
shapes
()
const
=
0
;
virtual
~
Reader
()
{}
virtual
~
ReaderBase
()
{}
};
private:
// set private to prevent directly access in decorators
// a decorator should access its underlying reader_'s shape, not its own.
class
FileReader
:
public
ReaderBase
{
public:
explicit
FileReader
(
const
std
::
vector
<
DDim
>&
shapes
)
:
shapes_
(
shapes
)
{}
DDim
shape
(
size_t
idx
)
const
override
;
std
::
vector
<
DDim
>
shapes
()
const
override
{
return
shapes_
;
}
protected:
std
::
vector
<
DDim
>
shapes_
;
};
class
ReaderDecorator
:
public
ReaderBase
{
public:
explicit
ReaderDecorator
(
ReaderBase
*
reader
)
:
reader_
(
reader
)
{}
bool
HasNext
()
const
override
{
return
reader_
->
HasNext
();
}
DDim
shape
(
size_t
idx
)
const
override
{
return
reader_
->
shape
(
idx
);
}
std
::
vector
<
DDim
>
shapes
()
const
override
{
return
reader_
->
shapes
();
}
protected:
ReaderBase
*
reader_
;
};
// file readers
template
<
typename
T
>
class
RandomReader
:
public
Reader
{
class
RandomReader
:
public
File
Reader
{
public:
RandomReader
(
const
std
::
vector
<
DDim
>&
shapes
,
float
min
,
float
max
)
:
Reader
(
shapes
),
min_
(
min
),
max_
(
max
)
{
:
File
Reader
(
shapes
),
min_
(
min
),
max_
(
max
)
{
PADDLE_ENFORCE_LE
(
min
,
max
,
"'min' should be less than or equal to 'max'.(%f vs %f)"
,
min
,
max
);
...
...
@@ -58,8 +74,8 @@ class RandomReader : public Reader {
std
::
uniform_real_distribution
<
float
>
dist
(
min_
,
max_
);
std
::
vector
<
LoDTensor
>
res
;
res
.
reserve
(
shapes
()
.
size
());
for
(
const
DDim
&
shape
:
shapes
()
)
{
res
.
reserve
(
shapes
_
.
size
());
for
(
const
DDim
&
shape
:
shapes
_
)
{
PADDLE_ENFORCE_GE
(
shape
.
size
(),
2
,
"The rank of input data should be 2 at least.(Now it's %d)"
,
...
...
@@ -85,37 +101,27 @@ class RandomReader : public Reader {
// decorators
class
ShuffleReader
:
public
Reader
{
class
ShuffleReader
:
public
Reader
Decorator
{
public:
ShuffleReader
(
Reader
*
reader
,
int
buffer_size
)
:
reader_
(
reader
),
buffer_size_
(
buffer_size
),
iteration_pos_
(
0
)
{
ShuffleReader
(
Reader
Base
*
reader
,
int
buffer_size
)
:
ReaderDecorator
(
reader
),
buffer_size_
(
buffer_size
),
iteration_pos_
(
0
)
{
buffer_
.
reserve
(
buffer_size
);
}
std
::
vector
<
LoDTensor
>
ReadNext
()
override
;
bool
HasNext
()
const
override
{
return
reader_
->
HasNext
();
}
DDim
shape
(
size_t
idx
)
const
override
{
return
reader_
->
shape
(
idx
);
}
std
::
vector
<
DDim
>
shapes
()
const
override
{
return
reader_
->
shapes
();
}
private:
Reader
*
reader_
;
int
buffer_size_
;
std
::
vector
<
std
::
vector
<
LoDTensor
>>
buffer_
;
size_t
iteration_pos_
;
};
class
BatchReader
:
public
Reader
{
class
BatchReader
:
public
Reader
Decorator
{
public:
BatchReader
(
Reader
*
reader
,
int
batch_size
)
:
reader_
(
reader
),
batch_size_
(
batch_size
)
{}
BatchReader
(
Reader
Base
*
reader
,
int
batch_size
)
:
ReaderDecorator
(
reader
),
batch_size_
(
batch_size
)
{}
std
::
vector
<
LoDTensor
>
ReadNext
()
override
;
bool
HasNext
()
const
override
{
return
reader_
->
HasNext
();
};
DDim
shape
(
size_t
idx
)
const
override
{
return
reader_
->
shape
(
idx
);
}
std
::
vector
<
DDim
>
shapes
()
const
override
{
return
reader_
->
shapes
();
}
private:
Reader
*
reader_
;
int
batch_size_
;
std
::
vector
<
std
::
vector
<
LoDTensor
>>
buffer_
;
};
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录