Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
book
提交
785f9d44
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看板
提交
785f9d44
编写于
3月 06, 2017
作者:
G
gongweibao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
remove w,b input params in train.py and remove def main in README.md
上级
413ba962
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
42 addition
and
47 deletion
+42
-47
fit_a_line/README.md
fit_a_line/README.md
+41
-44
fit_a_line/train.py
fit_a_line/train.py
+1
-3
未找到文件。
fit_a_line/README.md
浏览文件 @
785f9d44
...
...
@@ -114,9 +114,8 @@ fit_a_line下trainer.py演示了训练的整体过程
### 首先初始化paddle
```
python
def
main
():
# init
paddle
.
init
(
use_gpu
=
False
,
trainer_count
=
1
)
# init
paddle
.
init
(
use_gpu
=
False
,
trainer_count
=
1
)
```
### 然后进行模型配置
...
...
@@ -124,32 +123,30 @@ def main():
使用
`fc_layer`
和
`LinearActivation`
来表示线性回归的模型本身。
```
python
#输入数据,13维的房屋信息
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'
),
#输入数据,13维的房屋信息
x
=
paddle
.
layer
.
data
(
name
=
'x'
,
type
=
paddle
.
data_type
.
dense_vector
(
13
))
y_predict
=
paddle
.
layer
.
fc
(
input
=
x
,
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
)
act
=
paddle
.
activation
.
Linear
())
y
=
paddle
.
layer
.
data
(
name
=
'y'
,
type
=
paddle
.
data_type
.
dense_vector
(
1
))
cost
=
paddle
.
layer
.
regression_cost
(
input
=
y_predict
,
label
=
y
)
```
### 接着创建参数和优化器
```
python
# create parameters
parameters
=
paddle
.
parameters
.
create
(
cost
)
# create parameters
parameters
=
paddle
.
parameters
.
create
(
cost
)
# create optimizer
optimizer
=
paddle
.
optimizer
.
Momentum
(
momentum
=
0
)
# create optimizer
optimizer
=
paddle
.
optimizer
.
Momentum
(
momentum
=
0
)
```
### 创建trainer
```
python
trainer
=
paddle
.
trainer
.
SGD
(
cost
=
cost
,
parameters
=
parameters
,
update_equation
=
optimizer
)
trainer
=
paddle
.
trainer
.
SGD
(
cost
=
cost
,
parameters
=
parameters
,
update_equation
=
optimizer
)
```
### 读取数据且打印训练的中间信息
...
...
@@ -157,38 +154,38 @@ def main():
reader_dict中设置了训练数据和测试数据的下标,reader通过下标区分训练和测试数据。
```
python
reader_dict
=
{
'x'
:
0
,
'y'
:
1
}
# 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"
%
(
event
.
pass_id
,
event
.
batch_id
,
event
.
cost
)
if
isinstance
(
event
,
paddle
.
event
.
EndPass
):
result
=
trainer
.
test
(
reader
=
paddle
.
reader
.
batched
(
uci_housing
.
test
(),
batch_size
=
2
),
reader_dict
=
reader_dict
)
print
"Test %d, Cost %f"
%
(
event
.
pass_id
,
result
.
cost
)
reader_dict
=
{
'x'
:
0
,
'y'
:
1
}
# 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"
%
(
event
.
pass_id
,
event
.
batch_id
,
event
.
cost
)
if
isinstance
(
event
,
paddle
.
event
.
EndPass
):
result
=
trainer
.
test
(
reader
=
paddle
.
reader
.
batched
(
uci_housing
.
test
(),
batch_size
=
2
),
reader_dict
=
reader_dict
)
print
"Test %d, Cost %f"
%
(
event
.
pass_id
,
result
.
cost
)
```
### 开始训练
```
python
# training
trainer
.
train
(
reader
=
paddle
.
reader
.
batched
(
paddle
.
reader
.
shuffle
(
uci_housing
.
train
(),
buf_size
=
500
),
batch_size
=
2
),
reader_dict
=
reader_dict
,
event_handler
=
event_handler
,
num_passes
=
30
)
# training
trainer
.
train
(
reader
=
paddle
.
reader
.
batched
(
paddle
.
reader
.
shuffle
(
uci_housing
.
train
(),
buf_size
=
500
),
batch_size
=
2
),
reader_dict
=
reader_dict
,
event_handler
=
event_handler
,
num_passes
=
30
)
```
## 执行训练程序
##
bash中
执行训练程序
**注意设置好paddle的安装包路径**
```
bash
...
...
fit_a_line/train.py
浏览文件 @
785f9d44
...
...
@@ -9,10 +9,8 @@ def main():
# 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'
))
act
=
paddle
.
activation
.
Linear
())
y
=
paddle
.
layer
.
data
(
name
=
'y'
,
type
=
paddle
.
data_type
.
dense_vector
(
1
))
cost
=
paddle
.
layer
.
regression_cost
(
input
=
y_predict
,
label
=
y
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录