Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
book
提交
2e55fd46
B
book
项目概览
PaddlePaddle
/
book
通知
17
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看板
提交
2e55fd46
编写于
2月 20, 2019
作者:
C
ceci3
提交者:
Cheerego
2月 20, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update fit a line (#684)
上级
c064457d
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
76 addition
and
98 deletion
+76
-98
01.fit_a_line/README.cn.md
01.fit_a_line/README.cn.md
+38
-49
01.fit_a_line/index.cn.html
01.fit_a_line/index.cn.html
+38
-49
未找到文件。
01.fit_a_line/README.cn.md
浏览文件 @
2e55fd46
...
...
@@ -236,24 +236,6 @@ def train_test(executor, program, reader, feeder, fetch_list):
count
+=
1
# 累加测试集中的样本数量
return
[
x_d
/
count
for
x_d
in
accumulated
]
# 计算平均损失
```
可以直接输出损失值来观察
`训练进程`
:
```
python
train_prompt
=
"train cost"
test_prompt
=
"test cost"
print
(
"%s', out %f"
%
(
train_prompt
,
out
))
print
(
"%s', out %f"
%
(
test_prompt
,
out
))
```
除此之外,还可以通过画图,来展现
`训练进程`
:
```
python
from
paddle.utils.plot
import
ploter
plot_prompt
=
ploter
(
train_prompt
,
test_prompt
)
```
### 训练主循环
...
...
@@ -264,8 +246,11 @@ plot_prompt = ploter(train_prompt, test_prompt)
%
matplotlib
inline
params_dirname
=
"fit_a_line.inference.model"
feeder
=
fluid
.
DataFeeder
(
place
=
place
,
feed_list
=
[
x
,
y
])
naive_exe
=
fluid
.
Executor
(
place
)
naive_exe
.
run
(
startup_program
)
exe
.
run
(
startup_program
)
train_prompt
=
"train cost"
test_prompt
=
"test cost"
from
paddle.utils.plot
import
Ploter
plot_prompt
=
Ploter
(
train_prompt
,
test_prompt
)
step
=
0
exe_test
=
fluid
.
Executor
(
place
)
...
...
@@ -280,10 +265,12 @@ for pass_id in range(num_epochs):
avg_loss_value
,
=
exe
.
run
(
main_program
,
feed
=
feeder
.
feed
(
data_train
),
fetch_list
=
[
avg_loss
])
if
step
%
10
==
0
:
# 每10个批次记录一下训练损失
if
step
%
10
==
0
:
# 每10个批次记录
并输出
一下训练损失
plot_prompt
.
append
(
train_prompt
,
step
,
avg_loss_value
[
0
])
plot_prompt
.
plot
()
if
step
%
100
==
0
:
# 每100批次记录一下测试损失
print
(
"%s, Step %d, Cost %f"
%
(
train_prompt
,
step
,
avg_loss_value
[
0
]))
if
step
%
100
==
0
:
# 每100批次记录并输出一下测试损失
test_metics
=
train_test
(
executor
=
exe_test
,
program
=
test_program
,
reader
=
test_reader
,
...
...
@@ -291,6 +278,8 @@ for pass_id in range(num_epochs):
feeder
=
feeder
)
plot_prompt
.
append
(
test_prompt
,
step
,
test_metics
[
0
])
plot_prompt
.
plot
()
print
(
"%s, Step %d, Cost %f"
%
(
test_prompt
,
step
,
test_metics
[
0
]))
if
test_metics
[
0
]
<
10.0
:
# 如果准确率达到要求,则停止训练
break
...
...
@@ -316,6 +305,24 @@ inference_scope = fluid.core.Scope()
```
### 预测
保存图片
```
python
def
save_result
(
points1
,
points2
):
import
matplotlib
matplotlib
.
use
(
'Agg'
)
import
matplotlib.pyplot
as
plt
x1
=
[
idx
for
idx
in
range
(
len
(
points1
))]
y1
=
points1
y2
=
points2
l1
=
plt
.
plot
(
x1
,
y1
,
'r--'
,
label
=
'predictions'
)
l2
=
plt
.
plot
(
x1
,
y2
,
'g--'
,
label
=
'GT'
)
plt
.
plot
(
x1
,
y1
,
'ro-'
,
x1
,
y2
,
'g+-'
)
plt
.
title
(
'predictions VS GT'
)
plt
.
legend
()
plt
.
savefig
(
'./image/prediction_gt.png'
)
```
通过fluid.io.load_inference_model,预测器会从
`params_dirname`
中读取已经训练好的模型,来对从未遇见过的数据进行预测。
```
python
...
...
@@ -337,37 +344,19 @@ with fluid.scope_guard(inference_scope):
results
=
infer_exe
.
run
(
inference_program
,
feed
=
{
feed_target_names
[
0
]:
numpy
.
array
(
infer_feat
)},
fetch_list
=
fetch_targets
)
# 进行预测
```
#打印预测结果和标签并可视化结果
print
(
"infer results: (House Price)"
)
for
idx
,
val
in
enumerate
(
results
[
0
]):
print
(
"%d: %.2f"
%
(
idx
,
val
))
# 打印预测结果
保存图片
```
python
def
save_result
(
points1
,
points2
):
import
matplotlib
matplotlib
.
use
(
'Agg'
)
import
matplotlib.pyplot
as
plt
x1
=
[
idx
for
idx
in
range
(
len
(
points1
))]
y1
=
points1
y2
=
points2
l1
=
plt
.
plot
(
x1
,
y1
,
'r--'
,
label
=
'predictions'
)
l2
=
plt
.
plot
(
x1
,
y2
,
'g--'
,
label
=
'GT'
)
plt
.
plot
(
x1
,
y1
,
'ro-'
,
x1
,
y2
,
'g+-'
)
plt
.
title
(
'predictions VS GT'
)
plt
.
legend
()
plt
.
savefig
(
'./image/prediction_gt.png'
)
```
print
(
"
\n
ground truth:"
)
for
idx
,
val
in
enumerate
(
infer_label
):
print
(
"%d: %.2f"
%
(
idx
,
val
))
# 打印标签值
打印预测结果和标签并可视化结果
```
python
print
(
"infer results: (House Price)"
)
for
idx
,
val
in
enumerate
(
results
[
0
]):
print
(
"%d: %.2f"
%
(
idx
,
val
))
# 打印预测结果
save_result
(
results
[
0
],
infer_label
)
# 保存图片
```
print
(
"
\n
ground truth:"
)
for
idx
,
val
in
enumerate
(
infer_label
):
print
(
"%d: %.2f"
%
(
idx
,
val
))
# 打印标签值
save_result
(
results
[
0
],
infer_label
)
# 保存图片
```
## 总结
在这章里,我们借助波士顿房价这一数据集,介绍了线性回归模型的基本概念,以及如何使用PaddlePaddle实现训练和测试的过程。很多的模型和技巧都是从简单的线性回归模型演化而来,因此弄清楚线性模型的原理和局限非常重要。
...
...
01.fit_a_line/index.cn.html
浏览文件 @
2e55fd46
...
...
@@ -278,24 +278,6 @@ def train_test(executor, program, reader, feeder, fetch_list):
count += 1 # 累加测试集中的样本数量
return [x_d / count for x_d in accumulated] # 计算平均损失
```
可以直接输出损失值来观察`训练进程`:
```python
train_prompt = "train cost"
test_prompt = "test cost"
print("%s', out %f" % (train_prompt, out))
print("%s', out %f" % (test_prompt, out))
```
除此之外,还可以通过画图,来展现`训练进程`:
```python
from paddle.utils.plot import ploter
plot_prompt = ploter(train_prompt, test_prompt)
```
### 训练主循环
...
...
@@ -306,8 +288,11 @@ plot_prompt = ploter(train_prompt, test_prompt)
%matplotlib inline
params_dirname = "fit_a_line.inference.model"
feeder = fluid.DataFeeder(place=place, feed_list=[x, y])
naive_exe = fluid.Executor(place)
naive_exe.run(startup_program)
exe.run(startup_program)
train_prompt = "train cost"
test_prompt = "test cost"
from paddle.utils.plot import Ploter
plot_prompt = Ploter(train_prompt, test_prompt)
step = 0
exe_test = fluid.Executor(place)
...
...
@@ -322,10 +307,12 @@ for pass_id in range(num_epochs):
avg_loss_value, = exe.run(main_program,
feed=feeder.feed(data_train),
fetch_list=[avg_loss])
if step % 10 == 0: # 每10个批次记录一下训练损失
if step % 10 == 0: # 每10个批次记录
并输出
一下训练损失
plot_prompt.append(train_prompt, step, avg_loss_value[0])
plot_prompt.plot()
if step % 100 == 0: # 每100批次记录一下测试损失
print("%s, Step %d, Cost %f" %
(train_prompt, step, avg_loss_value[0]))
if step % 100 == 0: # 每100批次记录并输出一下测试损失
test_metics = train_test(executor=exe_test,
program=test_program,
reader=test_reader,
...
...
@@ -333,6 +320,8 @@ for pass_id in range(num_epochs):
feeder=feeder)
plot_prompt.append(test_prompt, step, test_metics[0])
plot_prompt.plot()
print("%s, Step %d, Cost %f" %
(test_prompt, step, test_metics[0]))
if test_metics[0]
<
10.0
:
#
如果准确率达到要求
,
则停止训练
break
...
...
@@ -358,6 +347,24 @@ inference_scope = fluid.core.Scope()
```
###
预测
保存图片
```
python
def
save_result
(
points1
,
points2
)
:
import
matplotlib
matplotlib.use
('
Agg
')
import
matplotlib.pyplot
as
plt
x1 =
[idx
for
idx
in
range
(
len
(
points1
))]
y1 =
points1
y2 =
points2
l1 =
plt.plot(x1,
y1
,
'
r--
',
label=
'predictions'
)
l2 =
plt.plot(x1,
y2
,
'
g--
',
label=
'GT'
)
plt.plot
(
x1
,
y1
,
'
ro-
',
x1
,
y2
,
'
g
+
-
')
plt.title
('
predictions
VS
GT
')
plt.legend
()
plt.savefig
('./
image
/
prediction_gt.png
')
```
通过fluid.io.load_inference_model
,
预测器会从
`
params_dirname
`
中读取已经训练好的模型
,
来对从未遇见过的数据进行预测
。
```
python
...
...
@@ -379,37 +386,19 @@ with fluid.scope_guard(inference_scope):
results =
infer_exe.run(inference_program,
feed=
{feed_target_names[0]:
numpy.array
(
infer_feat
)},
fetch_list=
fetch_targets)
#
进行预测
```
#打印预测结果和标签并可视化结果
print
("
infer
results:
(
House
Price
)")
for
idx
,
val
in
enumerate
(
results
[0])
:
print
("%
d:
%.2
f
"
%
(
idx
,
val
))
#
打印预测结果
保存图片
```
python
def
save_result
(
points1
,
points2
)
:
import
matplotlib
matplotlib.use
('
Agg
')
import
matplotlib.pyplot
as
plt
x1 =
[idx
for
idx
in
range
(
len
(
points1
))]
y1 =
points1
y2 =
points2
l1 =
plt.plot(x1,
y1
,
'
r--
',
label=
'predictions'
)
l2 =
plt.plot(x1,
y2
,
'
g--
',
label=
'GT'
)
plt.plot
(
x1
,
y1
,
'
ro-
',
x1
,
y2
,
'
g
+
-
')
plt.title
('
predictions
VS
GT
')
plt.legend
()
plt.savefig
('./
image
/
prediction_gt.png
')
```
print
("\
nground
truth:
")
for
idx
,
val
in
enumerate
(
infer_label
)
:
print
("%
d:
%.2
f
"
%
(
idx
,
val
))
#
打印标签值
打印预测结果和标签并可视化结果
```
python
print
("
infer
results:
(
House
Price
)")
for
idx
,
val
in
enumerate
(
results
[0])
:
print
("%
d:
%.2
f
"
%
(
idx
,
val
))
#
打印预测结果
save_result
(
results
[0],
infer_label
)
#
保存图片
```
print
("\
nground
truth:
")
for
idx
,
val
in
enumerate
(
infer_label
)
:
print
("%
d:
%.2
f
"
%
(
idx
,
val
))
#
打印标签值
save_result
(
results
[0],
infer_label
)
#
保存图片
```
##
总结
在这章里
,
我们借助波士顿房价这一数据集
,
介绍了线性回归模型的基本概念
,
以及如何使用PaddlePaddle实现训练和测试的过程
。
很多的模型和技巧都是从简单的线性回归模型演化而来
,
因此弄清楚线性模型的原理和局限非常重要
。
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录