Skip to content

Commit 3a3ff62

Browse files
committed
fix quick start for fluid #9660
1 parent e0babe7 commit 3a3ff62

File tree

2 files changed

+238
-2
lines changed

2 files changed

+238
-2
lines changed

doc/fluid/getstarted/quickstart_cn.rst

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
快速开始
2+
========
3+
4+
快速安装
5+
--------
6+
7+
PaddlePaddle支持使用pip快速安装,目前支持CentOS 6以上, Ubuntu 14.04以及MacOS 10.12,并安装有Python2.7。
8+
执行下面的命令完成快速安装,版本为cpu_avx_openblas:
9+
10+
.. code-block:: bash
11+
12+
pip install paddlepaddle
13+
14+
如果需要安装支持GPU的版本(cuda7.5_cudnn5_avx_openblas),需要执行:
15+
16+
.. code-block:: bash
17+
18+
pip install paddlepaddle-gpu
19+
20+
更详细的安装和编译方法参考::ref:`install_steps` 。
21+
22+
快速使用
23+
--------
24+
25+
创建一个 housing.py 并粘贴此Python代码:
26+
27+
.. code-block:: python
28+
29+
import sys
30+
31+
import math
32+
import numpy
33+
34+
import paddle.fluid as fluid
35+
import paddle.fluid.core as core
36+
import paddle
37+
38+
def train(save_dirname):
39+
x = fluid.layers.data(name='x', shape=[13], dtype='float32')
40+
y_predict = fluid.layers.fc(input=x, size=1, act=None)
41+
y = fluid.layers.data(name='y', shape=[1], dtype='float32')
42+
43+
cost = fluid.layers.square_error_cost(input=y_predict, label=y)
44+
avg_cost = fluid.layers.mean(cost)
45+
46+
sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.001)
47+
optimize_ops, params_grads = sgd_optimizer.minimize(avg_cost)
48+
49+
BATCH_SIZE = 20
50+
51+
train_reader = paddle.batch(
52+
paddle.reader.shuffle(paddle.dataset.uci_housing.train(), buf_size=500), batch_size=BATCH_SIZE)
53+
54+
place = fluid.CPUPlace()
55+
exe = fluid.Executor(place)
56+
57+
feeder = fluid.DataFeeder(place=place, feed_list=[x, y])
58+
exe.run(fluid.default_startup_program())
59+
60+
main_program = fluid.default_main_program()
61+
62+
PASS_NUM = 100
63+
for pass_id in range(PASS_NUM):
64+
for data in train_reader():
65+
avg_loss_value, = exe.run(main_program,
66+
feed=feeder.feed(data),
67+
fetch_list=[avg_cost])
68+
if avg_loss_value[0] < 10.0:
69+
if save_dirname is not None:
70+
fluid.io.save_inference_model(save_dirname, ['x'],
71+
[y_predict], exe)
72+
return
73+
if math.isnan(float(avg_loss_value)):
74+
sys.exit("got NaN loss, training failed.")
75+
raise AssertionError("Fit a line cost is too large, {0:2.2}".format(
76+
avg_loss_value[0]))
77+
78+
def infer(save_dirname):
79+
place = fluid.CPUPlace()
80+
exe = fluid.Executor(place)
81+
82+
probs = []
83+
84+
inference_scope = fluid.core.Scope()
85+
with fluid.scope_guard(inference_scope):
86+
# Use fluid.io.load_inference_model to obtain the inference program desc,
87+
# the feed_target_names (the names of variables that will be feeded
88+
# data using feed operators), and the fetch_targets (variables that
89+
# we want to obtain data from using fetch operators).
90+
[inference_program, feed_target_names,
91+
fetch_targets] = fluid.io.load_inference_model(save_dirname, exe)
92+
93+
# The input's dimension should be 2-D and the second dim is 13
94+
# The input data should be >= 0
95+
batch_size = 10
96+
tensor_x = numpy.random.uniform(0, 10,
97+
[batch_size, 13]).astype("float32")
98+
assert feed_target_names[0] == 'x'
99+
results = exe.run(inference_program,
100+
feed={feed_target_names[0]: tensor_x},
101+
fetch_list=fetch_targets)
102+
probs.append(results)
103+
104+
for i in xrange(len(probs)):
105+
print(probs[i][0] * 1000)
106+
print('Predicted price: ${0}'.format(probs[i][0] * 1000))
107+
108+
def main():
109+
# Directory for saving the trained model
110+
save_dirname = "fit_a_line.inference.model"
111+
112+
train(save_dirname)
113+
infer(save_dirname)
114+
115+
if __name__=="__main__":
116+
main()
117+
118+
执行 :code:`python housing.py` 瞧! 它应该打印出预测住房数据的清单。

doc/fluid/getstarted/quickstart_en.rst

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
Quick Start
2+
============
3+
4+
Quick Install
5+
-------------
6+
7+
You can use pip to install PaddlePaddle with a single command, supports
8+
CentOS 6 above, Ubuntu 14.04 above or MacOS 10.12, with Python 2.7 installed.
9+
Simply run the following command to install, the version is cpu_avx_openblas:
10+
11+
.. code-block:: bash
12+
13+
pip install paddlepaddle
14+
15+
If you need to install GPU version (cuda7.5_cudnn5_avx_openblas), run:
16+
17+
.. code-block:: bash
18+
19+
pip install paddlepaddle-gpu
20+
21+
For more details about installation and build: :ref:`install_steps` .
22+
23+
Quick Use
24+
---------
25+
26+
Create a new file called housing.py, and paste this Python
27+
code:
28+
29+
30+
.. code-block:: python
31+
import sys
32+
33+
import math
34+
import numpy
35+
36+
import paddle.fluid as fluid
37+
import paddle.fluid.core as core
38+
import paddle
39+
40+
def train(save_dirname):
41+
x = fluid.layers.data(name='x', shape=[13], dtype='float32')
42+
y_predict = fluid.layers.fc(input=x, size=1, act=None)
43+
y = fluid.layers.data(name='y', shape=[1], dtype='float32')
44+
45+
cost = fluid.layers.square_error_cost(input=y_predict, label=y)
46+
avg_cost = fluid.layers.mean(cost)
47+
48+
sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.001)
49+
optimize_ops, params_grads = sgd_optimizer.minimize(avg_cost)
50+
51+
BATCH_SIZE = 20
52+
53+
train_reader = paddle.batch(
54+
paddle.reader.shuffle(paddle.dataset.uci_housing.train(), buf_size=500), batch_size=BATCH_SIZE)
55+
56+
place = fluid.CPUPlace()
57+
exe = fluid.Executor(place)
58+
59+
feeder = fluid.DataFeeder(place=place, feed_list=[x, y])
60+
exe.run(fluid.default_startup_program())
61+
62+
main_program = fluid.default_main_program()
63+
64+
PASS_NUM = 100
65+
for pass_id in range(PASS_NUM):
66+
for data in train_reader():
67+
avg_loss_value, = exe.run(main_program,
68+
feed=feeder.feed(data),
69+
fetch_list=[avg_cost])
70+
if avg_loss_value[0] < 10.0:
71+
if save_dirname is not None:
72+
fluid.io.save_inference_model(save_dirname, ['x'],
73+
[y_predict], exe)
74+
return
75+
if math.isnan(float(avg_loss_value)):
76+
sys.exit("got NaN loss, training failed.")
77+
raise AssertionError("Fit a line cost is too large, {0:2.2}".format(
78+
avg_loss_value[0]))
79+
80+
def infer(save_dirname):
81+
place = fluid.CPUPlace()
82+
exe = fluid.Executor(place)
83+
84+
probs = []
85+
86+
inference_scope = fluid.core.Scope()
87+
with fluid.scope_guard(inference_scope):
88+
# Use fluid.io.load_inference_model to obtain the inference program desc,
89+
# the feed_target_names (the names of variables that will be feeded
90+
# data using feed operators), and the fetch_targets (variables that
91+
# we want to obtain data from using fetch operators).
92+
[inference_program, feed_target_names,
93+
fetch_targets] = fluid.io.load_inference_model(save_dirname, exe)
94+
95+
# The input's dimension should be 2-D and the second dim is 13
96+
# The input data should be >= 0
97+
batch_size = 10
98+
tensor_x = numpy.random.uniform(0, 10,
99+
[batch_size, 13]).astype("float32")
100+
assert feed_target_names[0] == 'x'
101+
results = exe.run(inference_program,
102+
feed={feed_target_names[0]: tensor_x},
103+
fetch_list=fetch_targets)
104+
probs.append(results)
105+
106+
for i in xrange(len(probs)):
107+
print(probs[i][0] * 1000)
108+
print('Predicted price: ${0}'.format(probs[i][0] * 1000))
109+
110+
def main():
111+
# Directory for saving the trained model
112+
save_dirname = "fit_a_line.inference.model"
113+
114+
train(save_dirname)
115+
infer(save_dirname)
116+
117+
if __name__=="__main__":
118+
main()
119+
Run :code:`python housing.py` and voila! It should print out a list of predictions
120+
for the test housing data.

0 commit comments

Comments
 (0)