Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
book
提交
da00779c
B
book
项目概览
PaddlePaddle
/
book
通知
16
Star
4
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
40
列表
看板
标记
里程碑
合并请求
37
Wiki
5
Wiki
分析
仓库
DevOps
项目成员
Pages
B
book
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
40
Issue
40
列表
看板
标记
里程碑
合并请求
37
合并请求
37
Pages
分析
分析
仓库分析
DevOps
Wiki
5
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
da00779c
编写于
9月 15, 2017
作者:
Q
qiaolongfei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
typo
上级
3ab94d5b
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
44 addition
and
36 deletion
+44
-36
04.word2vec/README.cn.md
04.word2vec/README.cn.md
+11
-9
04.word2vec/README.md
04.word2vec/README.md
+11
-9
04.word2vec/index.cn.html
04.word2vec/index.cn.html
+11
-9
04.word2vec/index.html
04.word2vec/index.html
+11
-9
未找到文件。
04.word2vec/README.cn.md
浏览文件 @
da00779c
...
@@ -209,15 +209,6 @@ N = 5 # 训练5-Gram
...
@@ -209,15 +209,6 @@ N = 5 # 训练5-Gram
用于保存和加载word_dict和embedding table的函数
用于保存和加载word_dict和embedding table的函数
```
python
```
python
def
wordemb
(
inlayer
):
wordemb
=
paddle
.
layer
.
table_projection
(
input
=
inlayer
,
size
=
embsize
,
param_attr
=
paddle
.
attr
.
Param
(
name
=
"_proj"
,
initial_std
=
0.001
,
learning_rate
=
1
,
l2_rate
=
0
))
return
wordemb
# save and load word dict and embedding table
# save and load word dict and embedding table
def
save_dict_and_embedding
(
word_dict
,
embeddings
):
def
save_dict_and_embedding
(
word_dict
,
embeddings
):
with
open
(
"word_dict"
,
"w"
)
as
f
:
with
open
(
"word_dict"
,
"w"
)
as
f
:
...
@@ -225,6 +216,17 @@ def save_dict_and_embedding(word_dict, embeddings):
...
@@ -225,6 +216,17 @@ def save_dict_and_embedding(word_dict, embeddings):
f
.
write
(
key
+
" "
+
str
(
word_dict
[
key
])
+
"
\n
"
)
f
.
write
(
key
+
" "
+
str
(
word_dict
[
key
])
+
"
\n
"
)
with
open
(
"embedding_table"
,
"w"
)
as
f
:
with
open
(
"embedding_table"
,
"w"
)
as
f
:
numpy
.
savetxt
(
f
,
embeddings
,
delimiter
=
','
,
newline
=
'
\n
'
)
numpy
.
savetxt
(
f
,
embeddings
,
delimiter
=
','
,
newline
=
'
\n
'
)
def
load_dict_and_embedding
():
word_dict
=
dict
()
with
open
(
"word_dict"
,
"r"
)
as
f
:
for
line
in
f
:
key
,
value
=
line
.
strip
().
split
(
" "
)
word_dict
[
key
]
=
value
embeddings
=
numpy
.
loadtxt
(
"embedding_table"
,
delimiter
=
","
)
return
word_dict
,
embeddings
```
```
接着,定义网络结构:
接着,定义网络结构:
...
...
04.word2vec/README.md
浏览文件 @
da00779c
...
@@ -227,15 +227,6 @@ N = 5 # train 5-gram
...
@@ -227,15 +227,6 @@ N = 5 # train 5-gram
-
functions used to save and load word dict and embedding table
-
functions used to save and load word dict and embedding table
```
python
```
python
def
wordemb
(
inlayer
):
wordemb
=
paddle
.
layer
.
table_projection
(
input
=
inlayer
,
size
=
embsize
,
param_attr
=
paddle
.
attr
.
Param
(
name
=
"_proj"
,
initial_std
=
0.001
,
learning_rate
=
1
,
l2_rate
=
0
))
return
wordemb
# save and load word dict and embedding table
# save and load word dict and embedding table
def
save_dict_and_embedding
(
word_dict
,
embeddings
):
def
save_dict_and_embedding
(
word_dict
,
embeddings
):
with
open
(
"word_dict"
,
"w"
)
as
f
:
with
open
(
"word_dict"
,
"w"
)
as
f
:
...
@@ -243,6 +234,17 @@ def save_dict_and_embedding(word_dict, embeddings):
...
@@ -243,6 +234,17 @@ def save_dict_and_embedding(word_dict, embeddings):
f
.
write
(
key
+
" "
+
str
(
word_dict
[
key
])
+
"
\n
"
)
f
.
write
(
key
+
" "
+
str
(
word_dict
[
key
])
+
"
\n
"
)
with
open
(
"embedding_table"
,
"w"
)
as
f
:
with
open
(
"embedding_table"
,
"w"
)
as
f
:
numpy
.
savetxt
(
f
,
embeddings
,
delimiter
=
','
,
newline
=
'
\n
'
)
numpy
.
savetxt
(
f
,
embeddings
,
delimiter
=
','
,
newline
=
'
\n
'
)
def
load_dict_and_embedding
():
word_dict
=
dict
()
with
open
(
"word_dict"
,
"r"
)
as
f
:
for
line
in
f
:
key
,
value
=
line
.
strip
().
split
(
" "
)
word_dict
[
key
]
=
value
embeddings
=
numpy
.
loadtxt
(
"embedding_table"
,
delimiter
=
","
)
return
word_dict
,
embeddings
```
```
-
Map the $n-1$ words $w_{t-n+1},...w_{t-1}$ before $w_t$ to a D-dimensional vector though matrix of dimention $|V|
\t
imes D$ (D=32 in this example).
-
Map the $n-1$ words $w_{t-n+1},...w_{t-1}$ before $w_t$ to a D-dimensional vector though matrix of dimention $|V|
\t
imes D$ (D=32 in this example).
...
...
04.word2vec/index.cn.html
浏览文件 @
da00779c
...
@@ -251,15 +251,6 @@ N = 5 # 训练5-Gram
...
@@ -251,15 +251,6 @@ N = 5 # 训练5-Gram
用于保存和加载word_dict和embedding table的函数
用于保存和加载word_dict和embedding table的函数
```python
```python
def wordemb(inlayer):
wordemb = paddle.layer.table_projection(
input=inlayer,
size=embsize,
param_attr=paddle.attr.Param(
name="_proj", initial_std=0.001, learning_rate=1, l2_rate=0))
return wordemb
# save and load word dict and embedding table
# save and load word dict and embedding table
def save_dict_and_embedding(word_dict, embeddings):
def save_dict_and_embedding(word_dict, embeddings):
with open("word_dict", "w") as f:
with open("word_dict", "w") as f:
...
@@ -267,6 +258,17 @@ def save_dict_and_embedding(word_dict, embeddings):
...
@@ -267,6 +258,17 @@ def save_dict_and_embedding(word_dict, embeddings):
f.write(key + " " + str(word_dict[key]) + "\n")
f.write(key + " " + str(word_dict[key]) + "\n")
with open("embedding_table", "w") as f:
with open("embedding_table", "w") as f:
numpy.savetxt(f, embeddings, delimiter=',', newline='\n')
numpy.savetxt(f, embeddings, delimiter=',', newline='\n')
def load_dict_and_embedding():
word_dict = dict()
with open("word_dict", "r") as f:
for line in f:
key, value = line.strip().split(" ")
word_dict[key] = value
embeddings = numpy.loadtxt("embedding_table", delimiter=",")
return word_dict, embeddings
```
```
接着,定义网络结构:
接着,定义网络结构:
...
...
04.word2vec/index.html
浏览文件 @
da00779c
...
@@ -269,15 +269,6 @@ N = 5 # train 5-gram
...
@@ -269,15 +269,6 @@ N = 5 # train 5-gram
- functions used to save and load word dict and embedding table
- functions used to save and load word dict and embedding table
```python
```python
def wordemb(inlayer):
wordemb = paddle.layer.table_projection(
input=inlayer,
size=embsize,
param_attr=paddle.attr.Param(
name="_proj", initial_std=0.001, learning_rate=1, l2_rate=0))
return wordemb
# save and load word dict and embedding table
# save and load word dict and embedding table
def save_dict_and_embedding(word_dict, embeddings):
def save_dict_and_embedding(word_dict, embeddings):
with open("word_dict", "w") as f:
with open("word_dict", "w") as f:
...
@@ -285,6 +276,17 @@ def save_dict_and_embedding(word_dict, embeddings):
...
@@ -285,6 +276,17 @@ def save_dict_and_embedding(word_dict, embeddings):
f.write(key + " " + str(word_dict[key]) + "\n")
f.write(key + " " + str(word_dict[key]) + "\n")
with open("embedding_table", "w") as f:
with open("embedding_table", "w") as f:
numpy.savetxt(f, embeddings, delimiter=',', newline='\n')
numpy.savetxt(f, embeddings, delimiter=',', newline='\n')
def load_dict_and_embedding():
word_dict = dict()
with open("word_dict", "r") as f:
for line in f:
key, value = line.strip().split(" ")
word_dict[key] = value
embeddings = numpy.loadtxt("embedding_table", delimiter=",")
return word_dict, embeddings
```
```
- Map the $n-1$ words $w_{t-n+1},...w_{t-1}$ before $w_t$ to a D-dimensional vector though matrix of dimention $|V|\times D$ (D=32 in this example).
- Map the $n-1$ words $w_{t-n+1},...w_{t-1}$ before $w_t$ to a D-dimensional vector though matrix of dimention $|V|\times D$ (D=32 in this example).
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录