|
| 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