Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
97d01620
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
97d01620
编写于
3月 03, 2017
作者:
W
wenboyang
提交者:
GitHub
3月 03, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'develop' into develop
上级
c9f379ed
325b2caf
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
252 addition
and
25 deletion
+252
-25
demo/introduction/api_train_v2.py
demo/introduction/api_train_v2.py
+58
-0
demo/mnist/api_train_v2.py
demo/mnist/api_train_v2.py
+87
-22
demo/semantic_role_labeling/api_train_v2.py
demo/semantic_role_labeling/api_train_v2.py
+16
-1
python/paddle/v2/dataset/__init__.py
python/paddle/v2/dataset/__init__.py
+3
-1
python/paddle/v2/dataset/uci_housing.py
python/paddle/v2/dataset/uci_housing.py
+86
-0
python/paddle/v2/event.py
python/paddle/v2/event.py
+2
-1
未找到文件。
demo/introduction/api_train_v2.py
0 → 100644
浏览文件 @
97d01620
import
paddle.v2
as
paddle
import
paddle.v2.dataset.uci_housing
as
uci_housing
def
main
():
# init
paddle
.
init
(
use_gpu
=
False
,
trainer_count
=
1
)
# network config
x
=
paddle
.
layer
.
data
(
name
=
'x'
,
type
=
paddle
.
data_type
.
dense_vector
(
13
))
y_predict
=
paddle
.
layer
.
fc
(
input
=
x
,
param_attr
=
paddle
.
attr
.
Param
(
name
=
'w'
),
size
=
1
,
act
=
paddle
.
activation
.
Linear
(),
bias_attr
=
paddle
.
attr
.
Param
(
name
=
'b'
))
y
=
paddle
.
layer
.
data
(
name
=
'y'
,
type
=
paddle
.
data_type
.
dense_vector
(
1
))
cost
=
paddle
.
layer
.
regression_cost
(
input
=
y_predict
,
label
=
y
)
# create parameters
parameters
=
paddle
.
parameters
.
create
(
cost
)
# create optimizer
optimizer
=
paddle
.
optimizer
.
Momentum
(
momentum
=
0
)
trainer
=
paddle
.
trainer
.
SGD
(
cost
=
cost
,
parameters
=
parameters
,
update_equation
=
optimizer
)
# event_handler to print training and testing info
def
event_handler
(
event
):
if
isinstance
(
event
,
paddle
.
event
.
EndIteration
):
if
event
.
batch_id
%
100
==
0
:
print
"Pass %d, Batch %d, Cost %f, %s"
%
(
event
.
pass_id
,
event
.
batch_id
,
event
.
cost
,
event
.
metrics
)
if
isinstance
(
event
,
paddle
.
event
.
EndPass
):
result
=
trainer
.
test
(
reader
=
paddle
.
reader
.
batched
(
uci_housing
.
test
(),
batch_size
=
2
),
reader_dict
=
{
'x'
:
0
,
'y'
:
1
})
if
event
.
pass_id
%
10
==
0
:
print
"Test %d, %s"
%
(
event
.
pass_id
,
result
.
metrics
)
# training
trainer
.
train
(
reader
=
paddle
.
reader
.
batched
(
paddle
.
reader
.
shuffle
(
uci_housing
.
train
(),
buf_size
=
500
),
batch_size
=
2
),
reader_dict
=
{
'x'
:
0
,
'y'
:
1
},
event_handler
=
event_handler
,
num_passes
=
30
)
if
__name__
==
'__main__'
:
main
()
demo/mnist/api_train_v2.py
浏览文件 @
97d01620
import
paddle.v2
as
paddle
def
softmax_regression
(
img
):
predict
=
paddle
.
layer
.
fc
(
input
=
img
,
size
=
10
,
act
=
paddle
.
activation
.
Softmax
())
return
predict
def
multilayer_perceptron
(
img
):
# The first fully-connected layer
hidden1
=
paddle
.
layer
.
fc
(
input
=
img
,
size
=
128
,
act
=
paddle
.
activation
.
Relu
())
# The second fully-connected layer and the according activation function
hidden2
=
paddle
.
layer
.
fc
(
input
=
hidden1
,
size
=
64
,
act
=
paddle
.
activation
.
Relu
())
# The thrid fully-connected layer, note that the hidden size should be 10,
# which is the number of unique digits
predict
=
paddle
.
layer
.
fc
(
input
=
hidden2
,
size
=
10
,
act
=
paddle
.
activation
.
Softmax
())
return
predict
def
convolutional_neural_network
(
img
):
# first conv layer
conv_pool_1
=
paddle
.
networks
.
simple_img_conv_pool
(
input
=
img
,
filter_size
=
5
,
num_filters
=
20
,
num_channel
=
1
,
pool_size
=
2
,
pool_stride
=
2
,
act
=
paddle
.
activation
.
Tanh
())
# second conv layer
conv_pool_2
=
paddle
.
networks
.
simple_img_conv_pool
(
input
=
conv_pool_1
,
filter_size
=
5
,
num_filters
=
50
,
num_channel
=
20
,
pool_size
=
2
,
pool_stride
=
2
,
act
=
paddle
.
activation
.
Tanh
())
# The first fully-connected layer
fc1
=
paddle
.
layer
.
fc
(
input
=
conv_pool_2
,
size
=
128
,
act
=
paddle
.
activation
.
Tanh
())
# The softmax layer, note that the hidden size should be 10,
# which is the number of unique digits
predict
=
paddle
.
layer
.
fc
(
input
=
fc1
,
size
=
10
,
act
=
paddle
.
activation
.
Softmax
())
return
predict
def
main
():
paddle
.
init
(
use_gpu
=
False
,
trainer_count
=
1
)
...
...
@@ -9,46 +62,58 @@ def main():
name
=
'pixel'
,
type
=
paddle
.
data_type
.
dense_vector
(
784
))
label
=
paddle
.
layer
.
data
(
name
=
'label'
,
type
=
paddle
.
data_type
.
integer_value
(
10
))
hidden1
=
paddle
.
layer
.
fc
(
input
=
images
,
size
=
200
)
hidden2
=
paddle
.
layer
.
fc
(
input
=
hidden1
,
size
=
200
)
inference
=
paddle
.
layer
.
fc
(
input
=
hidden2
,
size
=
10
,
act
=
paddle
.
activation
.
Softmax
())
cost
=
paddle
.
layer
.
classification_cost
(
input
=
inference
,
label
=
label
)
# Here we can build the prediction network in different ways. Please
# choose one by uncomment corresponding line.
predict
=
softmax_regression
(
images
)
#predict = multilayer_perceptron(images)
#predict = convolutional_neural_network(images)
cost
=
paddle
.
layer
.
classification_cost
(
input
=
predict
,
label
=
label
)
parameters
=
paddle
.
parameters
.
create
(
cost
)
adam_optimizer
=
paddle
.
optimizer
.
Adam
(
learning_rate
=
0.01
)
optimizer
=
paddle
.
optimizer
.
Momentum
(
learning_rate
=
0.1
/
128.0
,
momentum
=
0.9
,
regularization
=
paddle
.
optimizer
.
L2Regularization
(
rate
=
0.0005
*
128
))
trainer
=
paddle
.
trainer
.
SGD
(
cost
=
cost
,
parameters
=
parameters
,
update_equation
=
adam_optimizer
)
update_equation
=
optimizer
)
lists
=
[]
def
event_handler
(
event
):
if
isinstance
(
event
,
paddle
.
event
.
EndIteration
):
if
event
.
batch_id
%
1000
==
0
:
result
=
trainer
.
test
(
reader
=
paddle
.
reader
.
batched
(
paddle
.
dataset
.
mnist
.
test
(),
batch_size
=
256
))
print
"Pass %d, Batch %d, Cost %.2f, %s
\n
"
\
"Testing cost %.2f metrics %s"
%
(
event
.
pass_id
,
event
.
batch_id
,
event
.
cost
,
event
.
metrics
,
result
.
cost
,
result
.
metrics
)
else
:
pass
if
event
.
batch_id
%
100
==
0
:
print
"Pass %d, Batch %d, Cost %f, %s"
%
(
event
.
pass_id
,
event
.
batch_id
,
event
.
cost
,
event
.
metrics
)
if
isinstance
(
event
,
paddle
.
event
.
EndPass
):
result
=
trainer
.
test
(
reader
=
paddle
.
reader
.
batched
(
paddle
.
dataset
.
mnist
.
test
(),
batch_size
=
128
))
print
"Test with Pass %d, Cost %f, %s
\n
"
%
(
event
.
pass_id
,
result
.
cost
,
result
.
metrics
)
lists
.
append
((
event
.
pass_id
,
result
.
cost
,
result
.
metrics
[
'classification_error_evaluator'
]))
trainer
.
train
(
reader
=
paddle
.
reader
.
batched
(
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
mnist
.
train
(),
buf_size
=
8192
),
batch_size
=
32
),
event_handler
=
event_handler
)
batch_size
=
128
),
event_handler
=
event_handler
,
num_passes
=
100
)
# find the best pass
best
=
sorted
(
lists
,
key
=
lambda
list
:
float
(
list
[
1
]))[
0
]
print
'Best pass is %s, testing Avgcost is %s'
%
(
best
[
0
],
best
[
1
])
print
'The classification accuracy is %.2f%%'
%
(
100
-
float
(
best
[
2
])
*
100
)
# output is a softmax layer. It returns probabilities.
# Shape should be (100, 10)
probs
=
paddle
.
infer
(
output
=
inference
,
output
=
predict
,
parameters
=
parameters
,
reader
=
paddle
.
reader
.
batched
(
paddle
.
reader
.
firstn
(
...
...
demo/semantic_role_labeling/api_train_v2.py
浏览文件 @
97d01620
...
...
@@ -167,8 +167,23 @@ def main():
paddle
.
reader
.
shuffle
(
conll05
.
test
(),
buf_size
=
8192
),
batch_size
=
10
)
reader_dict
=
{
'word_data'
:
0
,
'ctx_n2_data'
:
1
,
'ctx_n1_data'
:
2
,
'ctx_0_data'
:
3
,
'ctx_p1_data'
:
4
,
'ctx_p2_data'
:
5
,
'verb_data'
:
6
,
'mark_data'
:
7
,
'target'
:
8
}
trainer
.
train
(
reader
=
trn_reader
,
event_handler
=
event_handler
,
num_passes
=
10000
)
reader
=
trn_reader
,
event_handler
=
event_handler
,
num_passes
=
10000
,
reader_dict
=
reader_dict
)
if
__name__
==
'__main__'
:
...
...
python/paddle/v2/dataset/__init__.py
浏览文件 @
97d01620
...
...
@@ -18,8 +18,10 @@ import imdb
import
cifar
import
movielens
import
conll05
import
uci_housing
import
sentiment
__all__
=
[
'mnist'
,
'imikolov'
,
'imdb'
,
'cifar'
,
'movielens'
,
'conll05'
,
'sentiment'
]
'uci_housing'
]
\ No newline at end of file
python/paddle/v2/dataset/uci_housing.py
0 → 100644
浏览文件 @
97d01620
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
numpy
as
np
import
os
from
common
import
download
__all__
=
[
'train'
,
'test'
]
URL
=
'https://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data'
MD5
=
'd4accdce7a25600298819f8e28e8d593'
feature_names
=
[
'CRIM'
,
'ZN'
,
'INDUS'
,
'CHAS'
,
'NOX'
,
'RM'
,
'AGE'
,
'DIS'
,
'RAD'
,
'TAX'
,
'PTRATIO'
,
'B'
,
'LSTAT'
]
UCI_TRAIN_DATA
=
None
UCI_TEST_DATA
=
None
def
feature_range
(
maximums
,
minimums
):
import
matplotlib
matplotlib
.
use
(
'Agg'
)
import
matplotlib.pyplot
as
plt
fig
,
ax
=
plt
.
subplots
()
feature_num
=
len
(
maximums
)
ax
.
bar
(
range
(
feature_num
),
maximums
-
minimums
,
color
=
'r'
,
align
=
'center'
)
ax
.
set_title
(
'feature scale'
)
plt
.
xticks
(
range
(
feature_num
),
feature_names
)
plt
.
xlim
([
-
1
,
feature_num
])
fig
.
set_figheight
(
6
)
fig
.
set_figwidth
(
10
)
if
not
os
.
path
.
exists
(
'./image'
):
os
.
makedirs
(
'./image'
)
fig
.
savefig
(
'image/ranges.png'
,
dpi
=
48
)
plt
.
close
(
fig
)
def
load_data
(
filename
,
feature_num
=
14
,
ratio
=
0.8
):
global
UCI_TRAIN_DATA
,
UCI_TEST_DATA
if
UCI_TRAIN_DATA
is
not
None
and
UCI_TEST_DATA
is
not
None
:
return
data
=
np
.
fromfile
(
filename
,
sep
=
' '
)
data
=
data
.
reshape
(
data
.
shape
[
0
]
/
feature_num
,
feature_num
)
maximums
,
minimums
,
avgs
=
data
.
max
(
axis
=
0
),
data
.
min
(
axis
=
0
),
data
.
sum
(
axis
=
0
)
/
data
.
shape
[
0
]
feature_range
(
maximums
[:
-
1
],
minimums
[:
-
1
])
for
i
in
xrange
(
feature_num
-
1
):
data
[:,
i
]
=
(
data
[:,
i
]
-
avgs
[
i
])
/
(
maximums
[
i
]
-
minimums
[
i
])
offset
=
int
(
data
.
shape
[
0
]
*
ratio
)
UCI_TRAIN_DATA
=
data
[:
offset
]
UCI_TEST_DATA
=
data
[
offset
:]
def
train
():
global
UCI_TRAIN_DATA
load_data
(
download
(
URL
,
'uci_housing'
,
MD5
))
def
reader
():
for
d
in
UCI_TRAIN_DATA
:
yield
d
[:
-
1
],
d
[
-
1
:]
return
reader
def
test
():
global
UCI_TEST_DATA
load_data
(
download
(
URL
,
'uci_housing'
,
MD5
))
def
reader
():
for
d
in
UCI_TEST_DATA
:
yield
d
[:
-
1
],
d
[
-
1
:]
return
reader
python/paddle/v2/event.py
浏览文件 @
97d01620
...
...
@@ -53,8 +53,9 @@ class EndPass(WithMetric):
Event On One Pass Training Complete.
"""
def
__init__
(
self
,
pass_id
,
evaluator
):
def
__init__
(
self
,
pass_id
,
cost
,
evaluator
):
self
.
pass_id
=
pass_id
self
.
cost
=
cost
WithMetric
.
__init__
(
self
,
evaluator
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录