Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
2ddb6305
M
models
项目概览
PaddlePaddle
/
models
1 年多 前同步成功
通知
222
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
2ddb6305
编写于
12月 22, 2020
作者:
S
smallv0221
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
upgrade MapDatasetWrapper
上级
bec2933a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
74 addition
and
16 deletion
+74
-16
PaddleNLP/paddlenlp/datasets/dataset.py
PaddleNLP/paddlenlp/datasets/dataset.py
+74
-16
未找到文件。
PaddleNLP/paddlenlp/datasets/dataset.py
浏览文件 @
2ddb6305
...
...
@@ -89,14 +89,26 @@ class MapDatasetWrapper(Dataset):
def
__init__
(
self
,
data
):
self
.
data
=
data
self
.
_transform_func
=
None
self
.
_transform_pipline
=
[]
self
.
new_data
=
[]
def
transform
(
self
,
data
,
pipline
):
for
fn
in
reversed
(
pipline
):
data
=
fn
(
data
)
return
data
def
__getitem__
(
self
,
idx
):
return
self
.
_transform_func
(
self
.
data
[
idx
])
if
self
.
_transform_func
else
self
.
data
[
idx
]
if
not
self
.
new_data
:
return
self
.
transform
(
self
.
data
[
idx
],
self
.
_transform_pipline
)
if
self
.
_transform_pipline
else
self
.
data
[
idx
]
else
:
return
self
.
transform
(
self
.
new_data
[
idx
],
self
.
_transform_pipline
)
if
self
.
_transform_pipline
else
self
.
new_data
[
idx
]
def
__len__
(
self
):
return
len
(
self
.
data
)
return
len
(
self
.
data
)
if
not
self
.
new_data
else
len
(
self
.
new_data
)
def
filter
(
self
,
fn
):
"""
...
...
@@ -108,11 +120,17 @@ class MapDatasetWrapper(Dataset):
Returns:
MapDatasetWrapper: The filtered dataset
"""
filted_data
=
[
self
.
data
[
idx
]
for
idx
in
range
(
len
(
self
.
data
))
if
fn
(
self
.
data
[
idx
])
]
return
type
(
self
)(
filted_data
)
if
not
self
.
new_data
:
self
.
new_data
=
[
self
.
data
[
idx
]
for
idx
in
range
(
len
(
self
.
data
))
if
fn
(
self
.
data
[
idx
])
]
else
:
self
.
new_data
=
[
self
.
data
[
idx
]
for
idx
in
range
(
len
(
self
.
new_data
))
if
fn
(
self
.
new_data
[
idx
])
]
return
self
def
shard
(
self
,
num_shards
=
None
,
index
=
None
):
"""
...
...
@@ -135,13 +153,53 @@ class MapDatasetWrapper(Dataset):
num_samples
=
int
(
math
.
ceil
(
len
(
self
.
data
)
*
1.0
/
num_shards
))
total_size
=
num_samples
*
num_shards
# add extra samples to make it evenly divisible
sharded_data
=
[
self
.
data
[
idx
]
for
idx
in
range
(
len
(
self
.
data
))
if
idx
%
num_shards
==
index
]
if
len
(
sharded_data
)
<
num_samples
:
sharded_data
.
append
(
self
.
data
[
index
+
1
-
num_shards
])
return
type
(
self
)(
sharded_data
)
if
not
self
.
new_data
:
self
.
new_data
=
[
self
.
data
[
idx
]
for
idx
in
range
(
len
(
self
.
data
))
if
idx
%
num_shards
==
index
]
if
len
(
self
.
new_data
)
<
num_samples
:
self
.
new_data
.
append
(
self
.
data
[
index
+
1
-
num_shards
])
else
:
self
.
new_data
=
[
self
.
data
[
idx
]
for
idx
in
range
(
len
(
self
.
new_data
))
if
idx
%
num_shards
==
index
]
if
len
(
self
.
new_data
)
<
num_samples
:
self
.
new_data
.
append
(
self
.
new_data
[
index
+
1
-
num_shards
])
return
self
def
apply
(
self
,
fn
,
lazy
=
False
):
"""
Performs specific function on the dataset to transform every sample.
Args:
fn (callable): Transformations to be performed. It receives single
sample as argument rather than dataset.
lazy (bool, optional): If True, transformations would be delayed and
performed on demand. Otherwise, transforms all samples at once
and return a new MapDatasetWrapper instance. Note that if `fn` is
stochastic, `lazy` should be True or you will get the same
result on all epochs. Defalt: False.
Returns:
MapDatasetWrapper: A new MapDatasetWrapper instance if `lazy` is True,
\
otherwise bind `fn` as a property to transform on demand.
"""
if
lazy
:
self
.
_transform_pipline
.
append
(
fn
)
else
:
if
not
self
.
new_data
:
self
.
new_data
=
[
fn
(
self
.
data
[
idx
])
for
idx
in
range
(
len
(
self
.
data
))
]
else
:
self
.
new_data
=
[
fn
(
self
.
new_data
[
idx
])
for
idx
in
range
(
len
(
self
.
new_data
))
]
return
self
def
__getattr__
(
self
,
name
):
return
getattr
(
self
.
data
,
name
)
def
apply
(
self
,
fn
,
lazy
=
False
):
"""
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录