Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
554cea68
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
554cea68
编写于
4月 11, 2018
作者:
T
tangwei12
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
move most codes to uci_housing #9660
上级
3a3ff62e
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
80 addition
and
171 deletion
+80
-171
doc/fluid/getstarted/quickstart_cn.rst
doc/fluid/getstarted/quickstart_cn.rst
+18
-81
doc/fluid/getstarted/quickstart_en.rst
doc/fluid/getstarted/quickstart_en.rst
+28
-88
python/paddle/dataset/uci_housing.py
python/paddle/dataset/uci_housing.py
+34
-2
未找到文件。
doc/fluid/getstarted/quickstart_cn.rst
浏览文件 @
554cea68
...
@@ -25,94 +25,31 @@ PaddlePaddle支持使用pip快速安装,目前支持CentOS 6以上, Ubuntu 14.
...
@@ -25,94 +25,31 @@ PaddlePaddle支持使用pip快速安装,目前支持CentOS 6以上, Ubuntu 14.
创建一个 housing.py 并粘贴此Python代码:
创建一个 housing.py 并粘贴此Python代码:
.. code-block:: python
.. code-block:: python
import sys
import math
import numpy
import paddle.fluid as fluid
import paddle.fluid.core as core
import paddle
import paddle
import paddle.fluid as fluid
def train(save_dirname):
x = fluid.layers.data(name='x', shape=[13], dtype='float32')
y_predict = fluid.layers.fc(input=x, size=1, act=None)
y = fluid.layers.data(name='y', shape=[1], dtype='float32')
cost = fluid.layers.square_error_cost(input=y_predict, label=y)
avg_cost = fluid.layers.mean(cost)
sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.001)
optimize_ops, params_grads = sgd_optimizer.minimize(avg_cost)
BATCH_SIZE = 20
train_reader = paddle.batch(
paddle.reader.shuffle(paddle.dataset.uci_housing.train(), buf_size=500), batch_size=BATCH_SIZE)
place = fluid.CPUPlace()
exe = fluid.Executor(place)
feeder = fluid.DataFeeder(place=place, feed_list=[x, y])
exe.run(fluid.default_startup_program())
main_program = fluid.default_main_program()
PASS_NUM = 100
x = fluid.layers.data(name='x', shape=[13], dtype='float32')
for pass_id in range(PASS_NUM):
place = fluid.CPUPlace()
for data in train_reader():
exe = fluid.Executor(place=place)
avg_loss_value, = exe.run(main_program,
feeder = fluid.DataFeeder(place=place, feed_list=[x])
feed=feeder.feed(data),
fetch_list=[avg_cost])
if avg_loss_value[0] < 10.0:
if save_dirname is not None:
fluid.io.save_inference_model(save_dirname, ['x'],
[y_predict], exe)
return
if math.isnan(float(avg_loss_value)):
sys.exit("got NaN loss, training failed.")
raise AssertionError("Fit a line cost is too large, {0:2.2}".format(
avg_loss_value[0]))
def infer(save_dirname):
with fluid.scope_guard(fluid.core.Scope()):
place = fluid.CPUPlace()
parameter_model = paddle.dataset.uci_housing.fluid_model()
exe = fluid.Executor(place)
probs = []
[inference_program, feed_target_names,fetch_targets] = \
fluid.io.load_inference_model(parameter_model, exe)
inference_scope = fluid.core.Scope()
predict_reader = paddle.batch(paddle.dataset.uci_housing.predict_reader(), batch_size=20)
with fluid.scope_guard(inference_scope):
# Use fluid.io.load_inference_model to obtain the inference program desc,
# the feed_target_names (the names of variables that will be feeded
# data using feed operators), and the fetch_targets (variables that
# we want to obtain data from using fetch operators).
[inference_program, feed_target_names,
fetch_targets] = fluid.io.load_inference_model(save_dirname, exe)
# The input's dimension should be 2-D and the second dim is 13
results = []
# The input data should be >= 0
for data in predict_reader():
batch_size = 10
result = exe.run(inference_program,
tensor_x = numpy.random.uniform(0, 10,
feed=feeder.feed(data),
[batch_size, 13]).astype("float32")
assert feed_target_names[0] == 'x'
results = exe.run(inference_program,
feed={feed_target_names[0]: tensor_x},
fetch_list=fetch_targets)
fetch_list=fetch_targets)
probs.append(results
)
results.append(result
)
for i in xrange(len(probs)):
for res in results:
print(probs[i][0] * 1000)
for i in xrange(len(res[0])):
print('Predicted price: ${0}'.format(probs[i][0] * 1000))
print 'Predicted price: ${:,.2f}'.format(res[0][i][0] * 1000)
def main():
# Directory for saving the trained model
save_dirname = "fit_a_line.inference.model"
train(save_dirname)
infer(save_dirname)
if __name__=="__main__":
main()
执行 :code:`python housing.py` 瞧! 它应该打印出预测住房数据的清单。
执行 :code:`python housing.py` 瞧! 它应该打印出预测住房数据的清单。
doc/fluid/getstarted/quickstart_en.rst
浏览文件 @
554cea68
...
@@ -28,93 +28,33 @@ code:
...
@@ -28,93 +28,33 @@ code:
.. code-block:: python
.. code-block:: python
import sys
import paddle
import paddle.fluid as fluid
import math
import numpy
x = fluid.layers.data(name='x', shape=[13], dtype='float32')
import paddle.fluid as fluid
place = fluid.CPUPlace()
import paddle.fluid.core as core
exe = fluid.Executor(place=place)
import paddle
feeder = fluid.DataFeeder(place=place, feed_list=[x])
def train(save_dirname):
with fluid.scope_guard(fluid.core.Scope()):
x = fluid.layers.data(name='x', shape=[13], dtype='float32')
parameter_model = paddle.dataset.uci_housing.fluid_model()
y_predict = fluid.layers.fc(input=x, size=1, act=None)
y = fluid.layers.data(name='y', shape=[1], dtype='float32')
[inference_program, feed_target_names,fetch_targets] = \
fluid.io.load_inference_model(parameter_model, exe)
cost = fluid.layers.square_error_cost(input=y_predict, label=y)
avg_cost = fluid.layers.mean(cost)
predict_reader = paddle.batch(paddle.dataset.uci_housing.predict_reader(), batch_size=20)
sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.001)
results = []
optimize_ops, params_grads = sgd_optimizer.minimize(avg_cost)
for data in predict_reader():
result = exe.run(inference_program,
BATCH_SIZE = 20
feed=feeder.feed(data),
fetch_list=fetch_targets)
train_reader = paddle.batch(
results.append(result)
paddle.reader.shuffle(paddle.dataset.uci_housing.train(), buf_size=500), batch_size=BATCH_SIZE)
for res in results:
place = fluid.CPUPlace()
for i in xrange(len(res[0])):
exe = fluid.Executor(place)
print 'Predicted price: ${:,.2f}'.format(res[0][i][0] * 1000)
feeder = fluid.DataFeeder(place=place, feed_list=[x, y])
exe.run(fluid.default_startup_program())
main_program = fluid.default_main_program()
PASS_NUM = 100
for pass_id in range(PASS_NUM):
for data in train_reader():
avg_loss_value, = exe.run(main_program,
feed=feeder.feed(data),
fetch_list=[avg_cost])
if avg_loss_value[0] < 10.0:
if save_dirname is not None:
fluid.io.save_inference_model(save_dirname, ['x'],
[y_predict], exe)
return
if math.isnan(float(avg_loss_value)):
sys.exit("got NaN loss, training failed.")
raise AssertionError("Fit a line cost is too large, {0:2.2}".format(
avg_loss_value[0]))
def infer(save_dirname):
place = fluid.CPUPlace()
exe = fluid.Executor(place)
probs = []
inference_scope = fluid.core.Scope()
with fluid.scope_guard(inference_scope):
# Use fluid.io.load_inference_model to obtain the inference program desc,
# the feed_target_names (the names of variables that will be feeded
# data using feed operators), and the fetch_targets (variables that
# we want to obtain data from using fetch operators).
[inference_program, feed_target_names,
fetch_targets] = fluid.io.load_inference_model(save_dirname, exe)
# The input's dimension should be 2-D and the second dim is 13
# The input data should be >= 0
batch_size = 10
tensor_x = numpy.random.uniform(0, 10,
[batch_size, 13]).astype("float32")
assert feed_target_names[0] == 'x'
results = exe.run(inference_program,
feed={feed_target_names[0]: tensor_x},
fetch_list=fetch_targets)
probs.append(results)
for i in xrange(len(probs)):
print(probs[i][0] * 1000)
print('Predicted price: ${0}'.format(probs[i][0] * 1000))
def main():
# Directory for saving the trained model
save_dirname = "fit_a_line.inference.model"
train(save_dirname)
infer(save_dirname)
if __name__=="__main__":
main()
Run :code:`python housing.py` and voila! It should print out a list of predictions
Run :code:`python housing.py` and voila! It should print out a list of predictions
for the test housing data.
for the test housing data.
python/paddle/dataset/uci_housing.py
浏览文件 @
554cea68
...
@@ -19,7 +19,11 @@ https://archive.ics.uci.edu/ml/machine-learning-databases/housing/ and
...
@@ -19,7 +19,11 @@ https://archive.ics.uci.edu/ml/machine-learning-databases/housing/ and
parse training set and test set into paddle reader creators.
parse training set and test set into paddle reader creators.
"""
"""
import
os
import
numpy
as
np
import
numpy
as
np
import
tempfile
import
tarfile
import
os
import
os
import
paddle.dataset.common
import
paddle.dataset.common
...
@@ -34,8 +38,9 @@ feature_names = [
...
@@ -34,8 +38,9 @@ feature_names = [
UCI_TRAIN_DATA
=
None
UCI_TRAIN_DATA
=
None
UCI_TEST_DATA
=
None
UCI_TEST_DATA
=
None
URL_MODEL
=
'https://github.com/PaddlePaddle/book/raw/develop/01.fit_a_line/fit_a_line.tar'
MD5_MODEL
=
'52fc3da8ef3937822fcdd87ee05c0c9b'
FLUID_URL_MODEL
=
'https://github.com/PaddlePaddle/book/raw/develop/01.fit_a_line/fluid/fit_a_line.fluid.tar'
FLUID_MD5_MODEL
=
'6e6dd637ccd5993961f68bfbde46090b'
def
feature_range
(
maximums
,
minimums
):
def
feature_range
(
maximums
,
minimums
):
...
@@ -112,6 +117,33 @@ def test():
...
@@ -112,6 +117,33 @@ def test():
return
reader
return
reader
def
fluid_model
():
parameter_tar
=
paddle
.
dataset
.
common
.
download
(
FLUID_URL_MODEL
,
'uci_housing'
,
FLUID_MD5_MODEL
,
'fit_a_line.fluid.tar'
)
tar
=
tarfile
.
TarFile
(
parameter_tar
,
mode
=
'r'
)
dirpath
=
tempfile
.
mkdtemp
()
tar
.
extractall
(
path
=
dirpath
)
return
dirpath
def
predict_reader
():
"""
UCI_HOUSING test set creator.
It returns a reader creator, each sample in the reader is features after
normalization and price number.
:return: Test reader creator
:rtype: callable
"""
global
UCI_TEST_DATA
load_data
(
paddle
.
dataset
.
common
.
download
(
URL
,
'uci_housing'
,
MD5
))
def
reader
():
for
d
in
UCI_TEST_DATA
:
yield
(
d
[:
-
1
],)
return
reader
def
fetch
():
def
fetch
():
paddle
.
dataset
.
common
.
download
(
URL
,
'uci_housing'
,
MD5
)
paddle
.
dataset
.
common
.
download
(
URL
,
'uci_housing'
,
MD5
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录