Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
b63d38d1
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
b63d38d1
编写于
2月 28, 2017
作者:
W
wangkuiyi
提交者:
GitHub
2月 28, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1480 from wangkuiyi/mikolov
Add imikolov.py and unittest for book chapter word2vec
上级
00d7aeb7
7275e0a8
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
99 addition
and
0 deletion
+99
-0
python/paddle/v2/dataset/imikolov.py
python/paddle/v2/dataset/imikolov.py
+79
-0
python/paddle/v2/dataset/tests/imikolov_test.py
python/paddle/v2/dataset/tests/imikolov_test.py
+20
-0
未找到文件。
python/paddle/v2/dataset/imikolov.py
0 → 100644
浏览文件 @
b63d38d1
"""
imikolov's simple dataset: http://www.fit.vutbr.cz/~imikolov/rnnlm/
"""
import
paddle.v2.dataset.common
import
tarfile
__all__
=
[
'train'
,
'test'
]
URL
=
'http://www.fit.vutbr.cz/~imikolov/rnnlm/simple-examples.tgz'
MD5
=
'30177ea32e27c525793142b6bf2c8e2d'
def
word_count
(
f
,
word_freq
=
None
):
add
=
paddle
.
v2
.
dataset
.
common
.
dict_add
if
word_freq
==
None
:
word_freq
=
{}
for
l
in
f
:
for
w
in
l
.
strip
().
split
():
add
(
word_freq
,
w
)
add
(
word_freq
,
'<s>'
)
add
(
word_freq
,
'<e>'
)
return
word_freq
def
build_dict
(
train_filename
,
test_filename
):
with
tarfile
.
open
(
paddle
.
v2
.
dataset
.
common
.
download
(
paddle
.
v2
.
dataset
.
imikolov
.
URL
,
'imikolov'
,
paddle
.
v2
.
dataset
.
imikolov
.
MD5
))
as
tf
:
trainf
=
tf
.
extractfile
(
train_filename
)
testf
=
tf
.
extractfile
(
test_filename
)
word_freq
=
word_count
(
testf
,
word_count
(
trainf
))
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_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
():
with
tarfile
.
open
(
paddle
.
v2
.
dataset
.
common
.
download
(
paddle
.
v2
.
dataset
.
imikolov
.
URL
,
'imikolov'
,
paddle
.
v2
.
dataset
.
imikolov
.
MD5
))
as
tf
:
f
=
tf
.
extractfile
(
filename
)
UNK
=
word_idx
[
'<unk>'
]
for
l
in
f
:
l
=
[
'<s>'
]
+
l
.
strip
().
split
()
+
[
'<e>'
]
if
len
(
l
)
>=
n
:
l
=
[
word_idx
.
get
(
w
,
UNK
)
for
w
in
l
]
for
i
in
range
(
n
,
len
(
l
)
+
1
):
yield
tuple
(
l
[
i
-
n
:
i
])
return
reader
def
train
(
n
):
return
reader_creator
(
'./simple-examples/data/ptb.train.txt'
,
n
)
def
test
(
n
):
return
reader_creator
(
'./simple-examples/data/ptb.valid.txt'
,
n
)
python/paddle/v2/dataset/tests/imikolov_test.py
0 → 100644
浏览文件 @
b63d38d1
import
paddle.v2.dataset.imikolov
import
unittest
class
TestMikolov
(
unittest
.
TestCase
):
def
check_reader
(
self
,
reader
,
n
):
for
l
in
reader
():
self
.
assertEqual
(
len
(
l
),
n
)
def
test_train
(
self
):
n
=
5
self
.
check_reader
(
paddle
.
v2
.
dataset
.
imikolov
.
train
(
n
),
n
)
def
test_test
(
self
):
n
=
5
self
.
check_reader
(
paddle
.
v2
.
dataset
.
imikolov
.
test
(
n
),
n
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录