Skip to content

Commit 2dfd884

Browse files
committed
make quick start code more simpler #9660
1 parent d13ca96 commit 2dfd884

File tree

3 files changed

+33
-62
lines changed

3 files changed

+33
-62
lines changed

doc/fluid/getstarted/quickstart_cn.rst

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,29 @@ PaddlePaddle支持使用pip快速安装,目前支持CentOS 6以上, Ubuntu 14.
1717
1818
pip install paddlepaddle-gpu
1919
20-
更详细的安装和编译方法参考: `安装与编译 <http://www.paddlepaddle.org/docs/develop/documentation/fluid/en/build_and_install/index_cn.html>`_
20+
更详细的安装和编译方法参考: :ref:`install_steps`
2121

2222
快速使用
2323
--------
2424

2525
创建一个 housing.py 并粘贴此Python代码:
2626

2727
.. code-block:: python
28-
import paddle
28+
29+
import paddle.dataset.uci_housing as uci_housing
2930
import paddle.fluid as fluid
30-
31-
32-
x = fluid.layers.data(name='x', shape=[13], dtype='float32')
33-
place = fluid.CPUPlace()
34-
exe = fluid.Executor(place=place)
35-
feeder = fluid.DataFeeder(place=place, feed_list=[x])
36-
31+
3732
with fluid.scope_guard(fluid.core.Scope()):
38-
parameter_model = paddle.dataset.uci_housing.fluid_model()
39-
33+
# initialize executor with cpu
34+
exe = fluid.Executor(place=fluid.CPUPlace())
35+
# load inference model
4036
[inference_program, feed_target_names,fetch_targets] = \
41-
fluid.io.load_inference_model(parameter_model, exe)
42-
43-
predict_reader = paddle.batch(paddle.dataset.uci_housing.predict_reader(), batch_size=20)
44-
45-
results = []
46-
for data in predict_reader():
47-
result = exe.run(inference_program,
48-
feed=feeder.feed(data),
49-
fetch_list=fetch_targets)
50-
results.append(result)
51-
52-
for res in results:
53-
for i in xrange(len(res[0])):
54-
print 'Predicted price: ${:,.2f}'.format(res[0][i][0] * 1000)
37+
fluid.io.load_inference_model(uci_housing.fluid_model(), exe)
38+
# run inference
39+
result = exe.run(inference_program,
40+
feed={feed_target_names[0]: uci_housing.predict_reader()},
41+
fetch_list=fetch_targets)
42+
# print predicted price is $12,273.97
43+
print 'Predicted price: ${:,.2f}'.format(result[0][0][0] * 1000)
44+
5545
执行 :code:`python housing.py` 瞧! 它应该打印出预测住房数据的清单。

doc/fluid/getstarted/quickstart_en.rst

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ If you need to install GPU version (cuda7.5_cudnn5_avx_openblas), run:
1818
1919
pip install paddlepaddle-gpu
2020
21-
For more details about installation and build: `install and Compile <http://www.paddlepaddle.org/docs/develop/documentation/fluid/en/build_and_install/index_en.html>`_ .
21+
For more details about installation and build: :ref:`install_steps` .
2222

2323
Quick Use
2424
---------
@@ -28,33 +28,22 @@ code:
2828

2929

3030
.. code-block:: python
31-
import paddle
31+
32+
import paddle.dataset.uci_housing as uci_housing
3233
import paddle.fluid as fluid
33-
34-
35-
x = fluid.layers.data(name='x', shape=[13], dtype='float32')
36-
place = fluid.CPUPlace()
37-
exe = fluid.Executor(place=place)
38-
feeder = fluid.DataFeeder(place=place, feed_list=[x])
39-
34+
4035
with fluid.scope_guard(fluid.core.Scope()):
41-
parameter_model = paddle.dataset.uci_housing.fluid_model()
42-
36+
# initialize executor with cpu
37+
exe = fluid.Executor(place=fluid.CPUPlace())
38+
# load inference model
4339
[inference_program, feed_target_names,fetch_targets] = \
44-
fluid.io.load_inference_model(parameter_model, exe)
45-
46-
predict_reader = paddle.batch(paddle.dataset.uci_housing.predict_reader(), batch_size=20)
47-
48-
results = []
49-
for data in predict_reader():
50-
result = exe.run(inference_program,
51-
feed=feeder.feed(data),
52-
fetch_list=fetch_targets)
53-
results.append(result)
54-
55-
for res in results:
56-
for i in xrange(len(res[0])):
57-
print 'Predicted price: ${:,.2f}'.format(res[0][i][0] * 1000)
40+
fluid.io.load_inference_model(uci_housing.fluid_model(), exe)
41+
# run inference
42+
result = exe.run(inference_program,
43+
feed={feed_target_names[0]: uci_housing.predict_reader()},
44+
fetch_list=fetch_targets)
45+
# print predicted price is $12,273.97
46+
print 'Predicted price: ${:,.2f}'.format(result[0][0][0] * 1000)
5847
5948
Run :code:`python housing.py` and voila! It should print out a list of predictions
6049
for the test housing data.

python/paddle/dataset/uci_housing.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,22 +128,14 @@ def fluid_model():
128128

129129
def predict_reader():
130130
"""
131-
UCI_HOUSING test set creator.
132-
133-
It returns a reader creator, each sample in the reader is features after
134-
normalization and price number.
131+
It returns just one tuple data to do inference.
135132
136-
:return: Test reader creator
137-
:rtype: callable
133+
:return: one tuple data
134+
:rtype: tuple
138135
"""
139136
global UCI_TEST_DATA
140137
load_data(paddle.dataset.common.download(URL, 'uci_housing', MD5))
141-
142-
def reader():
143-
for d in UCI_TEST_DATA:
144-
yield (d[:-1],)
145-
146-
return reader
138+
return (UCI_TEST_DATA[0][:-1],)
147139

148140
def fetch():
149141
paddle.dataset.common.download(URL, 'uci_housing', MD5)

0 commit comments

Comments
 (0)