Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
8530d3c7
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
8530d3c7
编写于
3月 03, 2017
作者:
H
helinwang
提交者:
GitHub
3月 03, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1511 from helinwang/dataset
expose build_dict in imikolov dataset, fix bug that len(word_dict) is…
上级
247a2a46
4cbbb23f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
22 addition
and
19 deletion
+22
-19
python/paddle/v2/dataset/imikolov.py
python/paddle/v2/dataset/imikolov.py
+14
-17
python/paddle/v2/dataset/tests/imikolov_test.py
python/paddle/v2/dataset/tests/imikolov_test.py
+8
-2
未找到文件。
python/paddle/v2/dataset/imikolov.py
浏览文件 @
8530d3c7
...
...
@@ -17,7 +17,7 @@ imikolov's simple dataset: http://www.fit.vutbr.cz/~imikolov/rnnlm/
import
paddle.v2.dataset.common
import
tarfile
__all__
=
[
'train'
,
'test'
]
__all__
=
[
'train'
,
'test'
,
'build_dict'
]
URL
=
'http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz'
MD5
=
'30177ea32e27c525793142b6bf2c8e2d'
...
...
@@ -37,7 +37,9 @@ def word_count(f, word_freq=None):
return
word_freq
def
build_dict
(
train_filename
,
test_filename
):
def
build_dict
():
train_filename
=
'./simple-examples/data/ptb.train.txt'
test_filename
=
'./simple-examples/data/ptb.valid.txt'
with
tarfile
.
open
(
paddle
.
v2
.
dataset
.
common
.
download
(
paddle
.
v2
.
dataset
.
imikolov
.
URL
,
'imikolov'
,
...
...
@@ -45,27 +47,22 @@ def build_dict(train_filename, test_filename):
trainf
=
tf
.
extractfile
(
train_filename
)
testf
=
tf
.
extractfile
(
test_filename
)
word_freq
=
word_count
(
testf
,
word_count
(
trainf
))
if
'<unk>'
in
word_freq
:
# remove <unk> for now, since we will set it as last index
del
word_freq
[
'<unk>'
]
TYPO_FREQ
=
50
word_freq
=
filter
(
lambda
x
:
x
[
1
]
>
TYPO_FREQ
,
word_freq
.
items
())
dictionary
=
sorted
(
word_freq
,
key
=
lambda
x
:
(
-
x
[
1
],
x
[
0
]))
words
,
_
=
list
(
zip
(
*
dictionary
))
word_freq_sorted
=
sorted
(
word_freq
,
key
=
lambda
x
:
(
-
x
[
1
],
x
[
0
]))
words
,
_
=
list
(
zip
(
*
word_freq_sorted
))
word_idx
=
dict
(
zip
(
words
,
xrange
(
len
(
words
))))
word_idx
[
'<unk>'
]
=
len
(
words
)
return
word_idx
word_idx
=
{}
def
reader_creator
(
filename
,
n
):
global
word_idx
if
len
(
word_idx
)
==
0
:
word_idx
=
build_dict
(
'./simple-examples/data/ptb.train.txt'
,
'./simple-examples/data/ptb.valid.txt'
)
def
reader_creator
(
filename
,
word_idx
,
n
):
def
reader
():
with
tarfile
.
open
(
paddle
.
v2
.
dataset
.
common
.
download
(
...
...
@@ -84,9 +81,9 @@ def reader_creator(filename, n):
return
reader
def
train
(
n
):
return
reader_creator
(
'./simple-examples/data/ptb.train.txt'
,
n
)
def
train
(
word_idx
,
n
):
return
reader_creator
(
'./simple-examples/data/ptb.train.txt'
,
word_idx
,
n
)
def
test
(
n
):
return
reader_creator
(
'./simple-examples/data/ptb.valid.txt'
,
n
)
def
test
(
word_idx
,
n
):
return
reader_creator
(
'./simple-examples/data/ptb.valid.txt'
,
word_idx
,
n
)
python/paddle/v2/dataset/tests/imikolov_test.py
浏览文件 @
8530d3c7
import
paddle.v2.dataset.imikolov
import
unittest
WORD_DICT
=
paddle
.
v2
.
dataset
.
imikolov
.
build_dict
()
class
TestMikolov
(
unittest
.
TestCase
):
def
check_reader
(
self
,
reader
,
n
):
...
...
@@ -9,11 +11,15 @@ class TestMikolov(unittest.TestCase):
def
test_train
(
self
):
n
=
5
self
.
check_reader
(
paddle
.
v2
.
dataset
.
imikolov
.
train
(
n
),
n
)
self
.
check_reader
(
paddle
.
v2
.
dataset
.
imikolov
.
train
(
WORD_DICT
,
n
),
n
)
def
test_test
(
self
):
n
=
5
self
.
check_reader
(
paddle
.
v2
.
dataset
.
imikolov
.
test
(
n
),
n
)
self
.
check_reader
(
paddle
.
v2
.
dataset
.
imikolov
.
test
(
WORD_DICT
,
n
),
n
)
def
test_total
(
self
):
_
,
idx
=
zip
(
*
WORD_DICT
.
items
())
self
.
assertEqual
(
sorted
(
idx
)[
-
1
],
len
(
WORD_DICT
)
-
1
)
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录