Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
book
提交
be45facf
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看板
提交
be45facf
编写于
4月 17, 2017
作者:
Q
qijun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine some codes
上级
58a6d233
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
68 addition
and
57 deletion
+68
-57
02.recognize_digits/train.py
02.recognize_digits/train.py
+58
-54
08.recommender_system/train.py
08.recommender_system/train.py
+10
-3
未找到文件。
02.recognize_digits/train.py
浏览文件 @
be45facf
...
@@ -44,36 +44,36 @@ def convolutional_neural_network(img):
...
@@ -44,36 +44,36 @@ def convolutional_neural_network(img):
input
=
conv_pool_2
,
size
=
10
,
act
=
paddle
.
activation
.
Softmax
())
input
=
conv_pool_2
,
size
=
10
,
act
=
paddle
.
activation
.
Softmax
())
return
predict
return
predict
def
main
():
paddle
.
init
(
use_gpu
=
False
,
trainer_count
=
1
)
paddle
.
init
(
use_gpu
=
False
,
trainer_count
=
1
)
# define network topology
images
=
paddle
.
layer
.
data
(
# define network topology
images
=
paddle
.
layer
.
data
(
name
=
'pixel'
,
type
=
paddle
.
data_type
.
dense_vector
(
784
))
name
=
'pixel'
,
type
=
paddle
.
data_type
.
dense_vector
(
784
))
label
=
paddle
.
layer
.
data
(
name
=
'label'
,
type
=
paddle
.
data_type
.
integer_value
(
10
))
label
=
paddle
.
layer
.
data
(
name
=
'label'
,
type
=
paddle
.
data_type
.
integer_value
(
10
))
# Here we can build the prediction network in different ways. Please
# Here we can build the prediction network in different ways. Please
# choose one by uncomment corresponding line.
# choose one by uncomment corresponding line.
# predict = softmax_regression(images)
# predict = softmax_regression(images)
# predict = multilayer_perceptron(images)
# predict = multilayer_perceptron(images)
predict
=
convolutional_neural_network
(
images
)
predict
=
convolutional_neural_network
(
images
)
cost
=
paddle
.
layer
.
classification_cost
(
input
=
predict
,
label
=
label
)
cost
=
paddle
.
layer
.
classification_cost
(
input
=
predict
,
label
=
label
)
parameters
=
paddle
.
parameters
.
create
(
cost
)
parameters
=
paddle
.
parameters
.
create
(
cost
)
optimizer
=
paddle
.
optimizer
.
Momentum
(
optimizer
=
paddle
.
optimizer
.
Momentum
(
learning_rate
=
0.1
/
128.0
,
learning_rate
=
0.1
/
128.0
,
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
(
trainer
=
paddle
.
trainer
.
SGD
(
cost
=
cost
,
parameters
=
parameters
,
update_equation
=
optimizer
)
cost
=
cost
,
parameters
=
parameters
,
update_equation
=
optimizer
)
lists
=
[]
lists
=
[]
def
event_handler
(
event
):
def
event_handler
(
event
):
if
isinstance
(
event
,
paddle
.
event
.
EndIteration
):
if
isinstance
(
event
,
paddle
.
event
.
EndIteration
):
if
event
.
batch_id
%
100
==
0
:
if
event
.
batch_id
%
100
==
0
:
print
"Pass %d, Batch %d, Cost %f, %s"
%
(
print
"Pass %d, Batch %d, Cost %f, %s"
%
(
...
@@ -81,20 +81,24 @@ def event_handler(event):
...
@@ -81,20 +81,24 @@ def event_handler(event):
if
isinstance
(
event
,
paddle
.
event
.
EndPass
):
if
isinstance
(
event
,
paddle
.
event
.
EndPass
):
result
=
trainer
.
test
(
reader
=
paddle
.
batch
(
result
=
trainer
.
test
(
reader
=
paddle
.
batch
(
paddle
.
dataset
.
mnist
.
test
(),
batch_size
=
128
))
paddle
.
dataset
.
mnist
.
test
(),
batch_size
=
128
))
print
"Test with Pass %d, Cost %f, %s
\n
"
%
(
event
.
pass_id
,
result
.
cost
,
print
"Test with Pass %d, Cost %f, %s
\n
"
%
(
event
.
pass_id
,
result
.
cost
,
result
.
metrics
)
result
.
metrics
)
lists
.
append
((
event
.
pass_id
,
result
.
cost
,
lists
.
append
((
event
.
pass_id
,
result
.
cost
,
result
.
metrics
[
'classification_error_evaluator'
]))
result
.
metrics
[
'classification_error_evaluator'
]))
trainer
.
train
(
trainer
.
train
(
reader
=
paddle
.
batch
(
reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
mnist
.
train
(),
buf_size
=
8192
),
paddle
.
reader
.
shuffle
(
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
)
# find the best pass
# find the best pass
best
=
sorted
(
lists
,
key
=
lambda
list
:
float
(
list
[
1
]))[
0
]
best
=
sorted
(
lists
,
key
=
lambda
list
:
float
(
list
[
1
]))[
0
]
print
'Best pass is %s, testing Avgcost is %s'
%
(
best
[
0
],
best
[
1
])
print
'Best pass is %s, testing Avgcost is %s'
%
(
best
[
0
],
best
[
1
])
print
'The classification accuracy is %.2f%%'
%
(
100
-
float
(
best
[
2
])
*
100
)
print
'The classification accuracy is %.2f%%'
%
(
100
-
float
(
best
[
2
])
*
100
)
if
__name__
==
'__main__'
:
main
()
08.recommender_system/train.py
浏览文件 @
be45facf
...
@@ -3,9 +3,7 @@ import cPickle
...
@@ -3,9 +3,7 @@ import cPickle
import
copy
import
copy
def
main
():
def
get_usr_combined_features
():
paddle
.
init
(
use_gpu
=
False
)
movie_title_dict
=
paddle
.
dataset
.
movielens
.
get_movie_title_dict
()
uid
=
paddle
.
layer
.
data
(
uid
=
paddle
.
layer
.
data
(
name
=
'user_id'
,
name
=
'user_id'
,
type
=
paddle
.
data_type
.
integer_value
(
type
=
paddle
.
data_type
.
integer_value
(
...
@@ -36,7 +34,10 @@ def main():
...
@@ -36,7 +34,10 @@ def main():
input
=
[
usr_fc
,
usr_gender_fc
,
usr_age_fc
,
usr_job_fc
],
input
=
[
usr_fc
,
usr_gender_fc
,
usr_age_fc
,
usr_job_fc
],
size
=
200
,
size
=
200
,
act
=
paddle
.
activation
.
Tanh
())
act
=
paddle
.
activation
.
Tanh
())
return
usr_combined_features
def
get_mov_combined_features
():
movie_title_dict
=
paddle
.
dataset
.
movielens
.
get_movie_title_dict
()
mov_id
=
paddle
.
layer
.
data
(
mov_id
=
paddle
.
layer
.
data
(
name
=
'movie_id'
,
name
=
'movie_id'
,
type
=
paddle
.
data_type
.
integer_value
(
type
=
paddle
.
data_type
.
integer_value
(
...
@@ -61,7 +62,13 @@ def main():
...
@@ -61,7 +62,13 @@ def main():
input
=
[
mov_fc
,
mov_categories_hidden
,
mov_title_conv
],
input
=
[
mov_fc
,
mov_categories_hidden
,
mov_title_conv
],
size
=
200
,
size
=
200
,
act
=
paddle
.
activation
.
Tanh
())
act
=
paddle
.
activation
.
Tanh
())
return
mov_combined_features
def
main
():
paddle
.
init
(
use_gpu
=
False
)
usr_combined_features
=
get_usr_combined_features
()
mov_combined_features
=
get_mov_combined_features
()
inference
=
paddle
.
layer
.
cos_sim
(
inference
=
paddle
.
layer
.
cos_sim
(
a
=
usr_combined_features
,
b
=
mov_combined_features
,
size
=
1
,
scale
=
5
)
a
=
usr_combined_features
,
b
=
mov_combined_features
,
size
=
1
,
scale
=
5
)
cost
=
paddle
.
layer
.
mse_cost
(
cost
=
paddle
.
layer
.
mse_cost
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录