提交 9ed43b01 编写于 作者: Y Yi Wang

Separate English/Chinese README.md and fix yapf config in pre-commit

上级 5da93f17
- repo: https://github.com/reyoung/mirrors-yapf.git - repo: https://github.com/pre-commit/mirrors-yapf.git
sha: v0.13.2 sha: v0.16.0
hooks: hooks:
- id: yapf - id: yapf
files: (.*\.(py|bzl)|BUILD|.*\.BUILD|WORKSPACE)$ # Bazel BUILD files follow Python syntax. files: \.py$
- repo: https://github.com/pre-commit/pre-commit-hooks - repo: https://github.com/pre-commit/pre-commit-hooks
sha: v0.7.1 sha: a11d9314b22d8f8c7556443875b731ef05965464
hooks: hooks:
- id: check-merge-conflict - id: check-merge-conflict
- id: check-symlinks - id: check-symlinks
...@@ -24,11 +24,11 @@ ...@@ -24,11 +24,11 @@
files: \.md$ files: \.md$
- id: remove-tabs - id: remove-tabs
files: \.md$ files: \.md$
- repo: local - repo: local
hooks: hooks:
- id: convert-markdown-into-html - id: convert-markdown-into-html
name: convert-markdown-into-html name: convert-markdown-into-html
description: "Convert README.md into index.html and README.en.md into index.en.html" description: Convert README.md into index.html and README.en.md into index.en.html
entry: python pre-commit-hooks/convert_markdown_into_html.py entry: python pre-commit-hooks/convert_markdown_into_html.py
language: system language: system
files: \.md$ files: \.md$
# Deep Learning with PaddlePaddle
1. [Fit a Line](http://book.paddlepaddle.org/fit_a_line/index.en.html)
1. [Recognize Digits](http://book.paddlepaddle.org/recognize_digits/index.en.html)
1. [Image Classification](http://book.paddlepaddle.org/image_classification/index.en.html)
1. [Word to Vector](http://book.paddlepaddle.org/word2vec/index.en.html)
1. [Understand Sentiment](http://book.paddlepaddle.org/understand_sentiment/index.en.html)
1. [Label Semantic Roles](http://book.paddlepaddle.org/label_semantic_roles/index.en.html)
1. [Machine Translation](http://book.paddlepaddle.org/machine_translation/index.en.html)
1. [Recommender System](http://book.paddlepaddle.org/recommender_system/index.en.html)
This tutorial is contributed by <a xmlns:cc="http://creativecommons.org/ns#" href="http://book.paddlepaddle.org" property="cc:attributionName" rel="cc:attributionURL">PaddlePaddle</a>, and licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License</a>.
# 深度学习入门 # 深度学习入门
1. [新手入门](fit_a_line/) [[html](http://book.paddlepaddle.org/fit_a_line)] 1. [新手入门](http://book.paddlepaddle.org/fit_a_line)
1. [识别数字](recognize_digits/) [[html](http://book.paddlepaddle.org/recognize_digits)] 1. [识别数字](http://book.paddlepaddle.org/recognize_digits)
1. [图像分类](image_classification/) [[html](http://book.paddlepaddle.org/image_classification)] 1. [图像分类](http://book.paddlepaddle.org/image_classification)
1. [词向量](word2vec/) [[html](http://book.paddlepaddle.org/word2vec)] 1. [词向量](http://book.paddlepaddle.org/word2vec)
1. [情感分析](understand_sentiment/) [[html](http://book.paddlepaddle.org/understand_sentiment)] 1. [情感分析](http://book.paddlepaddle.org/understand_sentiment)
1. [语义角色标注](label_semantic_roles/) [[html](http://book.paddlepaddle.org/label_semantic_roles)] 1. [语义角色标注](http://book.paddlepaddle.org/label_semantic_roles)
1. [机器翻译](machine_translation/) [[html](http://book.paddlepaddle.org/machine_translation)] 1. [机器翻译](http://book.paddlepaddle.org/machine_translation)
1. [个性化推荐](recommender_system/) [[html](http://book.paddlepaddle.org/recommender_system)] 1. [个性化推荐](http://book.paddlepaddle.org/recommender_system)
# Deep Learning Introduction
1. [Fit a Line](fit_a_line/) [[html](http://book.paddlepaddle.org/fit_a_line/index.en.html)]
1. [Recognize Digits](recognize_digits/) [[html](http://book.paddlepaddle.org/recognize_digits/index.en.html)]
1. [Image Classification](image_classification/) [[html](http://book.paddlepaddle.org/image_classification/index.en.html)]
1. [Word to Vector](word2vec/) [[html](http://book.paddlepaddle.org/word2vec/index.en.html)]
1. [Understand Sentiment](understand_sentiment/) [[html](http://book.paddlepaddle.org/understand_sentiment/index.en.html)]
1. [Label Semantic Roles](label_semantic_roles/) [[html](http://book.paddlepaddle.org/label_semantic_roles/index.en.html)]
1. [Machine Translation](machine_translation/) [[html](http://book.paddlepaddle.org/machine_translation/index.en.html)]
1. [Recommender System](recommender_system/) [[html](http://book.paddlepaddle.org/recommender_system/index.en.html)]
<br/>
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/"><img alt="知识共享许可协议" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/Text" property="dct:title" rel="dct:type">本教程</span><a xmlns:cc="http://creativecommons.org/ns#" href="http://book.paddlepaddle.org" property="cc:attributionName" rel="cc:attributionURL">PaddlePaddle</a> 创作,采用 <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">知识共享 署名-非商业性使用-相同方式共享 4.0 国际 许可协议</a>进行许可。 <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/"><img alt="知识共享许可协议" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/Text" property="dct:title" rel="dct:type">本教程</span><a xmlns:cc="http://creativecommons.org/ns#" href="http://book.paddlepaddle.org" property="cc:attributionName" rel="cc:attributionURL">PaddlePaddle</a> 创作,采用 <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">知识共享 署名-非商业性使用-相同方式共享 4.0 国际 许可协议</a>进行许可。
This tutorial is contributed by <a xmlns:cc="http://creativecommons.org/ns#" href="http://book.paddlepaddle.org" property="cc:attributionName" rel="cc:attributionURL">PaddlePaddle</a>, and licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License</a>.
...@@ -18,9 +18,8 @@ def main(): ...@@ -18,9 +18,8 @@ def main():
# create optimizer # create optimizer
optimizer = paddle.optimizer.Momentum(momentum=0) optimizer = paddle.optimizer.Momentum(momentum=0)
trainer = paddle.trainer.SGD(cost=cost, trainer = paddle.trainer.SGD(
parameters=parameters, cost=cost, parameters=parameters, update_equation=optimizer)
update_equation=optimizer)
feeding = {'x': 0, 'y': 1} feeding = {'x': 0, 'y': 1}
...@@ -33,16 +32,14 @@ def main(): ...@@ -33,16 +32,14 @@ def main():
if isinstance(event, paddle.event.EndPass): if isinstance(event, paddle.event.EndPass):
result = trainer.test( result = trainer.test(
reader=paddle.batch( reader=paddle.batch(uci_housing.test(), batch_size=2),
uci_housing.test(), batch_size=2),
feeding=feeding) feeding=feeding)
print "Test %d, Cost %f" % (event.pass_id, result.cost) print "Test %d, Cost %f" % (event.pass_id, result.cost)
# training # training
trainer.train( trainer.train(
reader=paddle.batch( reader=paddle.batch(
paddle.reader.shuffle( paddle.reader.shuffle(uci_housing.train(), buf_size=500),
uci_housing.train(), buf_size=500),
batch_size=2), batch_size=2),
feeding=feeding, feeding=feeding,
event_handler=event_handler, event_handler=event_handler,
......
...@@ -44,8 +44,9 @@ def vis_square(data, fname): ...@@ -44,8 +44,9 @@ def vis_square(data, fname):
(0, 1)) # add some space between filters (0, 1)) # add some space between filters
+ ((0, 0), ) * + ((0, 0), ) *
(data.ndim - 3)) # don't pad the last dimension (if there is one) (data.ndim - 3)) # don't pad the last dimension (if there is one)
data = np.pad(data, padding, mode='constant', data = np.pad(
constant_values=1) # pad with ones (white) data, padding, mode='constant',
constant_values=1) # pad with ones (white)
# tile the filters into an image # tile the filters into an image
data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple( data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(
range(4, data.ndim + 1))) range(4, data.ndim + 1)))
......
...@@ -36,9 +36,8 @@ def main(): ...@@ -36,9 +36,8 @@ def main():
# option 2. vgg # option 2. vgg
net = vgg_bn_drop(image) net = vgg_bn_drop(image)
out = paddle.layer.fc(input=net, out = paddle.layer.fc(
size=classdim, input=net, size=classdim, act=paddle.activation.Softmax())
act=paddle.activation.Softmax())
lbl = paddle.layer.data( lbl = paddle.layer.data(
name="label", type=paddle.data_type.integer_value(classdim)) name="label", type=paddle.data_type.integer_value(classdim))
...@@ -75,9 +74,8 @@ def main(): ...@@ -75,9 +74,8 @@ def main():
print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics) print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics)
# Create trainer # Create trainer
trainer = paddle.trainer.SGD(cost=cost, trainer = paddle.trainer.SGD(
parameters=parameters, cost=cost, parameters=parameters, update_equation=momentum_optimizer)
update_equation=momentum_optimizer)
trainer.train( trainer.train(
reader=paddle.batch( reader=paddle.batch(
paddle.reader.shuffle( paddle.reader.shuffle(
......
<html>
<head>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSsymbols.js", "TeX/AMSmath.js"],
jax: ["input/TeX", "output/HTML-CSS"],
tex2jax: {
inlineMath: [ ['$','$'] ],
displayMath: [ ['$$','$$'] ],
processEscapes: true
},
"HTML-CSS": { availableFonts: ["TeX"] }
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js" async></script>
<script type="text/javascript" src="../.tmpl/marked.js">
</script>
<link href="http://cdn.bootcss.com/highlight.js/9.9.0/styles/darcula.min.css" rel="stylesheet">
<script src="http://cdn.bootcss.com/highlight.js/9.9.0/highlight.min.js"></script>
<link href="http://cdn.bootcss.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/perfect-scrollbar/0.6.14/css/perfect-scrollbar.min.css" rel="stylesheet">
<link href="../.tmpl/github-markdown.css" rel='stylesheet'>
</head>
<style type="text/css" >
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 45px;
}
</style>
<body>
<div id="context" class="container markdown-body">
</div>
<!-- This block will be replaced by each markdown file content. Please do not change lines below.-->
<div id="markdown" style='display:none'>
# Deep Learning with PaddlePaddle
1. [Fit a Line](http://book.paddlepaddle.org/fit_a_line/index.en.html)
1. [Recognize Digits](http://book.paddlepaddle.org/recognize_digits/index.en.html)
1. [Image Classification](http://book.paddlepaddle.org/image_classification/index.en.html)
1. [Word to Vector](http://book.paddlepaddle.org/word2vec/index.en.html)
1. [Understand Sentiment](http://book.paddlepaddle.org/understand_sentiment/index.en.html)
1. [Label Semantic Roles](http://book.paddlepaddle.org/label_semantic_roles/index.en.html)
1. [Machine Translation](http://book.paddlepaddle.org/machine_translation/index.en.html)
1. [Recommender System](http://book.paddlepaddle.org/recommender_system/index.en.html)
This tutorial is contributed by <a xmlns:cc="http://creativecommons.org/ns#" href="http://book.paddlepaddle.org" property="cc:attributionName" rel="cc:attributionURL">PaddlePaddle</a>, and licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License</a>.
</div>
<!-- You can change the lines below now. -->
<script type="text/javascript">
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
breaks: false,
smartypants: true,
highlight: function(code, lang) {
code = code.replace(/&amp;/g, "&")
code = code.replace(/&gt;/g, ">")
code = code.replace(/&lt;/g, "<")
code = code.replace(/&nbsp;/g, " ")
return hljs.highlightAuto(code, [lang]).value;
}
});
document.getElementById("context").innerHTML = marked(
document.getElementById("markdown").innerHTML)
</script>
</body>
...@@ -42,31 +42,17 @@ ...@@ -42,31 +42,17 @@
<div id="markdown" style='display:none'> <div id="markdown" style='display:none'>
# 深度学习入门 # 深度学习入门
1. [新手入门](fit_a_line/) [[html](http://book.paddlepaddle.org/fit_a_line)] 1. [新手入门](http://book.paddlepaddle.org/fit_a_line)
1. [识别数字](recognize_digits/) [[html](http://book.paddlepaddle.org/recognize_digits)] 1. [识别数字](http://book.paddlepaddle.org/recognize_digits)
1. [图像分类](image_classification/) [[html](http://book.paddlepaddle.org/image_classification)] 1. [图像分类](http://book.paddlepaddle.org/image_classification)
1. [词向量](word2vec/) [[html](http://book.paddlepaddle.org/word2vec)] 1. [词向量](http://book.paddlepaddle.org/word2vec)
1. [情感分析](understand_sentiment/) [[html](http://book.paddlepaddle.org/understand_sentiment)] 1. [情感分析](http://book.paddlepaddle.org/understand_sentiment)
1. [语义角色标注](label_semantic_roles/) [[html](http://book.paddlepaddle.org/label_semantic_roles)] 1. [语义角色标注](http://book.paddlepaddle.org/label_semantic_roles)
1. [机器翻译](machine_translation/) [[html](http://book.paddlepaddle.org/machine_translation)] 1. [机器翻译](http://book.paddlepaddle.org/machine_translation)
1. [个性化推荐](recommender_system/) [[html](http://book.paddlepaddle.org/recommender_system)] 1. [个性化推荐](http://book.paddlepaddle.org/recommender_system)
# Deep Learning Introduction
1. [Fit a Line](fit_a_line/) [[html](http://book.paddlepaddle.org/fit_a_line/index.en.html)]
1. [Recognize Digits](recognize_digits/) [[html](http://book.paddlepaddle.org/recognize_digits/index.en.html)]
1. [Image Classification](image_classification/) [[html](http://book.paddlepaddle.org/image_classification/index.en.html)]
1. [Word to Vector](word2vec/) [[html](http://book.paddlepaddle.org/word2vec/index.en.html)]
1. [Understand Sentiment](understand_sentiment/) [[html](http://book.paddlepaddle.org/understand_sentiment/index.en.html)]
1. [Label Semantic Roles](label_semantic_roles/) [[html](http://book.paddlepaddle.org/label_semantic_roles/index.en.html)]
1. [Machine Translation](machine_translation/) [[html](http://book.paddlepaddle.org/machine_translation/index.en.html)]
1. [Recommender System](recommender_system/) [[html](http://book.paddlepaddle.org/recommender_system/index.en.html)]
<br/>
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/"><img alt="知识共享许可协议" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/Text" property="dct:title" rel="dct:type">本教程</span><a xmlns:cc="http://creativecommons.org/ns#" href="http://book.paddlepaddle.org" property="cc:attributionName" rel="cc:attributionURL">PaddlePaddle</a> 创作,采用 <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">知识共享 署名-非商业性使用-相同方式共享 4.0 国际 许可协议</a>进行许可。 <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/"><img alt="知识共享许可协议" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/Text" property="dct:title" rel="dct:type">本教程</span><a xmlns:cc="http://creativecommons.org/ns#" href="http://book.paddlepaddle.org" property="cc:attributionName" rel="cc:attributionURL">PaddlePaddle</a> 创作,采用 <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">知识共享 署名-非商业性使用-相同方式共享 4.0 国际 许可协议</a>进行许可。
This tutorial is contributed by <a xmlns:cc="http://creativecommons.org/ns#" href="http://book.paddlepaddle.org" property="cc:attributionName" rel="cc:attributionURL">PaddlePaddle</a>, and licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License</a>.
</div> </div>
<!-- You can change the lines below now. --> <!-- You can change the lines below now. -->
......
...@@ -75,8 +75,7 @@ settings( ...@@ -75,8 +75,7 @@ settings(
learning_method=MomentumOptimizer(momentum=0), learning_method=MomentumOptimizer(momentum=0),
learning_rate=2e-2, learning_rate=2e-2,
regularization=L2Regularization(8e-4), regularization=L2Regularization(8e-4),
model_average=ModelAverage( model_average=ModelAverage(average_window=0.5, max_average_window=10000), )
average_window=0.5, max_average_window=10000), )
####################################### network ############################## ####################################### network ##############################
#8 features and 1 target #8 features and 1 target
...@@ -102,13 +101,12 @@ std_default = ParameterAttribute(initial_std=default_std) ...@@ -102,13 +101,12 @@ std_default = ParameterAttribute(initial_std=default_std)
predicate_embedding = embedding_layer( predicate_embedding = embedding_layer(
size=word_dim, size=word_dim,
input=predicate, input=predicate,
param_attr=ParameterAttribute( param_attr=ParameterAttribute(name='vemb', initial_std=default_std))
name='vemb', initial_std=default_std))
word_input = [word, ctx_n2, ctx_n1, ctx_0, ctx_p1, ctx_p2] word_input = [word, ctx_n2, ctx_n1, ctx_0, ctx_p1, ctx_p2]
emb_layers = [ emb_layers = [
embedding_layer( embedding_layer(size=word_dim, input=x, param_attr=emb_para)
size=word_dim, input=x, param_attr=emb_para) for x in word_input for x in word_input
] ]
emb_layers.append(predicate_embedding) emb_layers.append(predicate_embedding)
mark_embedding = embedding_layer( mark_embedding = embedding_layer(
...@@ -120,8 +118,8 @@ hidden_0 = mixed_layer( ...@@ -120,8 +118,8 @@ hidden_0 = mixed_layer(
size=hidden_dim, size=hidden_dim,
bias_attr=std_default, bias_attr=std_default,
input=[ input=[
full_matrix_projection( full_matrix_projection(input=emb, param_attr=std_default)
input=emb, param_attr=std_default) for emb in emb_layers for emb in emb_layers
]) ])
mix_hidden_lr = 1e-3 mix_hidden_lr = 1e-3
...@@ -171,10 +169,8 @@ feature_out = mixed_layer( ...@@ -171,10 +169,8 @@ feature_out = mixed_layer(
size=label_dict_len, size=label_dict_len,
bias_attr=std_default, bias_attr=std_default,
input=[ input=[
full_matrix_projection( full_matrix_projection(input=input_tmp[0], param_attr=hidden_para_attr),
input=input_tmp[0], param_attr=hidden_para_attr), full_matrix_projection(input=input_tmp[1], param_attr=lstm_para_attr)
full_matrix_projection(
input=input_tmp[1], param_attr=lstm_para_attr)
], ) ], )
if not is_predict: if not is_predict:
......
...@@ -40,15 +40,14 @@ def db_lstm(): ...@@ -40,15 +40,14 @@ def db_lstm():
predicate_embedding = paddle.layer.embedding( predicate_embedding = paddle.layer.embedding(
size=word_dim, size=word_dim,
input=predicate, input=predicate,
param_attr=paddle.attr.Param( param_attr=paddle.attr.Param(name='vemb', initial_std=default_std))
name='vemb', initial_std=default_std))
mark_embedding = paddle.layer.embedding( mark_embedding = paddle.layer.embedding(
size=mark_dim, input=mark, param_attr=std_0) size=mark_dim, input=mark, param_attr=std_0)
word_input = [word, ctx_n2, ctx_n1, ctx_0, ctx_p1, ctx_p2] word_input = [word, ctx_n2, ctx_n1, ctx_0, ctx_p1, ctx_p2]
emb_layers = [ emb_layers = [
paddle.layer.embedding( paddle.layer.embedding(size=word_dim, input=x, param_attr=emb_para)
size=word_dim, input=x, param_attr=emb_para) for x in word_input for x in word_input
] ]
emb_layers.append(predicate_embedding) emb_layers.append(predicate_embedding)
emb_layers.append(mark_embedding) emb_layers.append(mark_embedding)
...@@ -109,13 +108,12 @@ def db_lstm(): ...@@ -109,13 +108,12 @@ def db_lstm():
input=input_tmp[1], param_attr=lstm_para_attr) input=input_tmp[1], param_attr=lstm_para_attr)
], ) ], )
crf_cost = paddle.layer.crf(size=label_dict_len, crf_cost = paddle.layer.crf(
input=feature_out, size=label_dict_len,
label=target, input=feature_out,
param_attr=paddle.attr.Param( label=target,
name='crfw', param_attr=paddle.attr.Param(
initial_std=default_std, name='crfw', initial_std=default_std, learning_rate=mix_hidden_lr))
learning_rate=mix_hidden_lr))
crf_dec = paddle.layer.crf_decoding( crf_dec = paddle.layer.crf_decoding(
name='crf_dec_l', name='crf_dec_l',
...@@ -151,13 +149,11 @@ def main(): ...@@ -151,13 +149,11 @@ def main():
model_average=paddle.optimizer.ModelAverage( model_average=paddle.optimizer.ModelAverage(
average_window=0.5, max_average_window=10000), ) average_window=0.5, max_average_window=10000), )
trainer = paddle.trainer.SGD(cost=crf_cost, trainer = paddle.trainer.SGD(
parameters=parameters, cost=crf_cost, parameters=parameters, update_equation=optimizer)
update_equation=optimizer)
reader = paddle.batch( reader = paddle.batch(
paddle.reader.shuffle( paddle.reader.shuffle(conll05.test(), buf_size=8192), batch_size=10)
conll05.test(), buf_size=8192), batch_size=10)
feeding = { feeding = {
'word_data': 0, 'word_data': 0,
......
...@@ -105,9 +105,8 @@ def main(): ...@@ -105,9 +105,8 @@ def main():
# define optimize method and trainer # define optimize method and trainer
optimizer = paddle.optimizer.Adam(learning_rate=1e-4) optimizer = paddle.optimizer.Adam(learning_rate=1e-4)
trainer = paddle.trainer.SGD(cost=cost, trainer = paddle.trainer.SGD(
parameters=parameters, cost=cost, parameters=parameters, update_equation=optimizer)
update_equation=optimizer)
# define data reader # define data reader
feeding = { feeding = {
......
...@@ -110,8 +110,7 @@ group_inputs = [group_input1, group_input2] ...@@ -110,8 +110,7 @@ group_inputs = [group_input1, group_input2]
if not is_generating: if not is_generating:
trg_embedding = embedding_layer( trg_embedding = embedding_layer(
input=data_layer( input=data_layer(name='target_language_word', size=target_dict_dim),
name='target_language_word', size=target_dict_dim),
size=word_vector_dim, size=word_vector_dim,
param_attr=ParamAttr(name='_target_language_embedding')) param_attr=ParamAttr(name='_target_language_embedding'))
group_inputs.append(trg_embedding) group_inputs.append(trg_embedding)
...@@ -156,8 +155,7 @@ else: ...@@ -156,8 +155,7 @@ else:
seqtext_printer_evaluator( seqtext_printer_evaluator(
input=beam_gen, input=beam_gen,
id_input=data_layer( id_input=data_layer(name="sent_id", size=1),
name="sent_id", size=1),
dict_file=trg_lang_dict, dict_file=trg_lang_dict,
result_file=gen_trans_file) result_file=gen_trans_file)
outputs(beam_gen) outputs(beam_gen)
...@@ -2,9 +2,8 @@ import paddle.v2 as paddle ...@@ -2,9 +2,8 @@ import paddle.v2 as paddle
def softmax_regression(img): def softmax_regression(img):
predict = paddle.layer.fc(input=img, predict = paddle.layer.fc(
size=10, input=img, size=10, act=paddle.activation.Softmax())
act=paddle.activation.Softmax())
return predict return predict
...@@ -12,14 +11,12 @@ def multilayer_perceptron(img): ...@@ -12,14 +11,12 @@ def multilayer_perceptron(img):
# The first fully-connected layer # The first fully-connected layer
hidden1 = paddle.layer.fc(input=img, size=128, act=paddle.activation.Relu()) hidden1 = paddle.layer.fc(input=img, size=128, act=paddle.activation.Relu())
# The second fully-connected layer and the according activation function # The second fully-connected layer and the according activation function
hidden2 = paddle.layer.fc(input=hidden1, hidden2 = paddle.layer.fc(
size=64, input=hidden1, size=64, act=paddle.activation.Relu())
act=paddle.activation.Relu())
# The thrid fully-connected layer, note that the hidden size should be 10, # The thrid fully-connected layer, note that the hidden size should be 10,
# which is the number of unique digits # which is the number of unique digits
predict = paddle.layer.fc(input=hidden2, predict = paddle.layer.fc(
size=10, input=hidden2, size=10, act=paddle.activation.Softmax())
act=paddle.activation.Softmax())
return predict return predict
...@@ -43,14 +40,12 @@ def convolutional_neural_network(img): ...@@ -43,14 +40,12 @@ def convolutional_neural_network(img):
pool_stride=2, pool_stride=2,
act=paddle.activation.Tanh()) act=paddle.activation.Tanh())
# The first fully-connected layer # The first fully-connected layer
fc1 = paddle.layer.fc(input=conv_pool_2, fc1 = paddle.layer.fc(
size=128, input=conv_pool_2, size=128, act=paddle.activation.Tanh())
act=paddle.activation.Tanh())
# The softmax layer, note that the hidden size should be 10, # The softmax layer, note that the hidden size should be 10,
# which is the number of unique digits # which is the number of unique digits
predict = paddle.layer.fc(input=fc1, predict = paddle.layer.fc(
size=10, input=fc1, size=10, act=paddle.activation.Softmax())
act=paddle.activation.Softmax())
return predict return predict
...@@ -76,9 +71,8 @@ optimizer = paddle.optimizer.Momentum( ...@@ -76,9 +71,8 @@ optimizer = paddle.optimizer.Momentum(
momentum=0.9, momentum=0.9,
regularization=paddle.optimizer.L2Regularization(rate=0.0005 * 128)) regularization=paddle.optimizer.L2Regularization(rate=0.0005 * 128))
trainer = paddle.trainer.SGD(cost=cost, trainer = paddle.trainer.SGD(
parameters=parameters, cost=cost, parameters=parameters, update_equation=optimizer)
update_equation=optimizer)
lists = [] lists = []
...@@ -99,8 +93,7 @@ def event_handler(event): ...@@ -99,8 +93,7 @@ def event_handler(event):
trainer.train( trainer.train(
reader=paddle.batch( reader=paddle.batch(
paddle.reader.shuffle( paddle.reader.shuffle(paddle.dataset.mnist.train(), buf_size=8192),
paddle.dataset.mnist.train(), buf_size=8192),
batch_size=128), batch_size=128),
event_handler=event_handler, event_handler=event_handler,
num_passes=100) num_passes=100)
......
...@@ -208,8 +208,8 @@ class EmbeddingFieldParser(object): ...@@ -208,8 +208,8 @@ class EmbeddingFieldParser(object):
elif config['dict']['type'] == 'split': elif config['dict']['type'] == 'split':
self.dict = SplitEmbeddingDict(config['dict'].get('delimiter', ',')) self.dict = SplitEmbeddingDict(config['dict'].get('delimiter', ','))
elif config['dict']['type'] == 'whole_content': elif config['dict']['type'] == 'whole_content':
self.dict = EmbeddingFieldParser.WholeContentDict(config['dict'][ self.dict = EmbeddingFieldParser.WholeContentDict(
'sort']) config['dict']['sort'])
else: else:
print config print config
assert False assert False
......
...@@ -24,9 +24,8 @@ def convolution_net(input_dim, class_dim=2, emb_dim=128, hid_dim=128): ...@@ -24,9 +24,8 @@ def convolution_net(input_dim, class_dim=2, emb_dim=128, hid_dim=128):
input=emb, context_len=3, hidden_size=hid_dim) input=emb, context_len=3, hidden_size=hid_dim)
conv_4 = paddle.networks.sequence_conv_pool( conv_4 = paddle.networks.sequence_conv_pool(
input=emb, context_len=4, hidden_size=hid_dim) input=emb, context_len=4, hidden_size=hid_dim)
output = paddle.layer.fc(input=[conv_3, conv_4], output = paddle.layer.fc(
size=class_dim, input=[conv_3, conv_4], size=class_dim, act=paddle.activation.Softmax())
act=paddle.activation.Softmax())
lbl = paddle.layer.data("label", paddle.data_type.integer_value(2)) lbl = paddle.layer.data("label", paddle.data_type.integer_value(2))
cost = paddle.layer.classification_cost(input=output, label=lbl) cost = paddle.layer.classification_cost(input=output, label=lbl)
return cost return cost
...@@ -64,20 +63,19 @@ def stacked_lstm_net(input_dim, ...@@ -64,20 +63,19 @@ def stacked_lstm_net(input_dim,
paddle.data_type.integer_value_sequence(input_dim)) paddle.data_type.integer_value_sequence(input_dim))
emb = paddle.layer.embedding(input=data, size=emb_dim) emb = paddle.layer.embedding(input=data, size=emb_dim)
fc1 = paddle.layer.fc(input=emb, fc1 = paddle.layer.fc(
size=hid_dim, input=emb, size=hid_dim, act=linear, bias_attr=bias_attr)
act=linear,
bias_attr=bias_attr)
lstm1 = paddle.layer.lstmemory( lstm1 = paddle.layer.lstmemory(
input=fc1, act=relu, bias_attr=bias_attr, layer_attr=layer_attr) input=fc1, act=relu, bias_attr=bias_attr, layer_attr=layer_attr)
inputs = [fc1, lstm1] inputs = [fc1, lstm1]
for i in range(2, stacked_num + 1): for i in range(2, stacked_num + 1):
fc = paddle.layer.fc(input=inputs, fc = paddle.layer.fc(
size=hid_dim, input=inputs,
act=linear, size=hid_dim,
param_attr=para_attr, act=linear,
bias_attr=bias_attr) param_attr=para_attr,
bias_attr=bias_attr)
lstm = paddle.layer.lstmemory( lstm = paddle.layer.lstmemory(
input=fc, input=fc,
reverse=(i % 2) == 0, reverse=(i % 2) == 0,
...@@ -90,11 +88,12 @@ def stacked_lstm_net(input_dim, ...@@ -90,11 +88,12 @@ def stacked_lstm_net(input_dim,
input=inputs[0], pooling_type=paddle.pooling.Max()) input=inputs[0], pooling_type=paddle.pooling.Max())
lstm_last = paddle.layer.pooling( lstm_last = paddle.layer.pooling(
input=inputs[1], pooling_type=paddle.pooling.Max()) input=inputs[1], pooling_type=paddle.pooling.Max())
output = paddle.layer.fc(input=[fc_last, lstm_last], output = paddle.layer.fc(
size=class_dim, input=[fc_last, lstm_last],
act=paddle.activation.Softmax(), size=class_dim,
bias_attr=bias_attr, act=paddle.activation.Softmax(),
param_attr=para_attr) bias_attr=bias_attr,
param_attr=para_attr)
lbl = paddle.layer.data("label", paddle.data_type.integer_value(2)) lbl = paddle.layer.data("label", paddle.data_type.integer_value(2))
cost = paddle.layer.classification_cost(input=output, label=lbl) cost = paddle.layer.classification_cost(input=output, label=lbl)
...@@ -148,9 +147,8 @@ if __name__ == '__main__': ...@@ -148,9 +147,8 @@ if __name__ == '__main__':
print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics) print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics)
# create trainer # create trainer
trainer = paddle.trainer.SGD(cost=cost, trainer = paddle.trainer.SGD(
parameters=parameters, cost=cost, parameters=parameters, update_equation=adam_optimizer)
update_equation=adam_optimizer)
trainer.train( trainer.train(
reader=train_reader, reader=train_reader,
......
...@@ -40,18 +40,19 @@ def main(): ...@@ -40,18 +40,19 @@ def main():
Efourth = wordemb(fourthword) Efourth = wordemb(fourthword)
contextemb = paddle.layer.concat(input=[Efirst, Esecond, Ethird, Efourth]) contextemb = paddle.layer.concat(input=[Efirst, Esecond, Ethird, Efourth])
hidden1 = paddle.layer.fc(input=contextemb, hidden1 = paddle.layer.fc(
size=hiddensize, input=contextemb,
act=paddle.activation.Sigmoid(), size=hiddensize,
layer_attr=paddle.attr.Extra(drop_rate=0.5), act=paddle.activation.Sigmoid(),
bias_attr=paddle.attr.Param(learning_rate=2), layer_attr=paddle.attr.Extra(drop_rate=0.5),
param_attr=paddle.attr.Param( bias_attr=paddle.attr.Param(learning_rate=2),
initial_std=1. / math.sqrt(embsize * 8), param_attr=paddle.attr.Param(
learning_rate=1)) initial_std=1. / math.sqrt(embsize * 8), learning_rate=1))
predictword = paddle.layer.fc(input=hidden1, predictword = paddle.layer.fc(
size=dict_size, input=hidden1,
bias_attr=paddle.attr.Param(learning_rate=2), size=dict_size,
act=paddle.activation.Softmax()) bias_attr=paddle.attr.Param(learning_rate=2),
act=paddle.activation.Softmax())
def event_handler(event): def event_handler(event):
if isinstance(event, paddle.event.EndIteration): if isinstance(event, paddle.event.EndIteration):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册