|
| 1 | +Quick Start |
| 2 | +============= |
| 3 | + |
| 4 | +Quick Installation |
| 5 | +-------------------- |
| 6 | + |
| 7 | +PaddlePaddle supports quick installation by pip. Execute the following commands to finish quick installation of the CPU version: |
| 8 | + |
| 9 | +.. code-block:: bash |
| 10 | +
|
| 11 | + pip install paddlepaddle |
| 12 | +
|
| 13 | +If you need to install the GPU version, or look up more specific installation methods, please refer to `Installation Instructions <../beginners_guide/install/index_en.html>`_ |
| 14 | + |
| 15 | + |
| 16 | +Quick Usage |
| 17 | +------------- |
| 18 | + |
| 19 | +First, you need to import the fluid library |
| 20 | + |
| 21 | +.. code-block:: python |
| 22 | +
|
| 23 | + import paddle.fluid as fluid |
| 24 | +
|
| 25 | +* Tensor Operations |
| 26 | + |
| 27 | + |
| 28 | +The following simple examples may help you quickly know about Fluid: |
| 29 | + |
| 30 | +1.use Fluid to create a one-dimensional array with five elements, and each element is 1 |
| 31 | + |
| 32 | +.. code-block:: python |
| 33 | + |
| 34 | + # define the dimension of an array and the data type, and the parameter 'shape' can be modified to define an array of any size |
| 35 | + data = fluid.layers.ones(shape=[5], dtype='int64') |
| 36 | + # compute on the CPU |
| 37 | + place = fluid.CPUPlace() |
| 38 | + # create executors |
| 39 | + exe = fluid.Executor(place) |
| 40 | + # execute computation |
| 41 | + ones_result = exe.run(fluid.default_main_program(), |
| 42 | + # get data |
| 43 | + fetch_list=[data], |
| 44 | + return_numpy=True) |
| 45 | + # output the results |
| 46 | + print(ones_result[0]) |
| 47 | +
|
| 48 | +you can get the results: |
| 49 | + |
| 50 | +.. code-block:: text |
| 51 | +
|
| 52 | + [1 1 1 1 1] |
| 53 | +
|
| 54 | +2.use Fluid to add two arrays by bits |
| 55 | + |
| 56 | +.. code-block:: python |
| 57 | +
|
| 58 | + # call elementwise_op to add the generative arrays by bits |
| 59 | + add = fluid.layers.elementwise_add(data,data) |
| 60 | + # define computation place |
| 61 | + place = fluid.CPUPlace() |
| 62 | + exe = fluid.Executor(place) |
| 63 | + # execute computation |
| 64 | + add_result = exe.run(fluid.default_main_program(), |
| 65 | + fetch_list=[add], |
| 66 | + return_numpy=True) |
| 67 | + # output the results |
| 68 | + print (add_result[0]) |
| 69 | +
|
| 70 | +you can get the results: |
| 71 | + |
| 72 | +.. code-block:: text |
| 73 | +
|
| 74 | + [2 2 2 2 2] |
| 75 | +
|
| 76 | +3.use Fluid to transform the data type |
| 77 | + |
| 78 | +.. code-block:: python |
| 79 | +
|
| 80 | + # transform a one-dimentional array of int to float64 |
| 81 | + cast = fluid.layers.cast(x=data, dtype='float64') |
| 82 | + # define computation place to execute computation |
| 83 | + place = fluid.CPUPlace() |
| 84 | + exe = fluid.Executor(place) |
| 85 | + cast_result = exe.run(fluid.default_main_program(), |
| 86 | + fetch_list=[cast], |
| 87 | + return_numpy=True) |
| 88 | + # output the results |
| 89 | + print(cast_result[0]) |
| 90 | +
|
| 91 | +you can get the results: |
| 92 | + |
| 93 | +.. code-block:: text |
| 94 | +
|
| 95 | + [1. 1. 1. 1. 1.] |
| 96 | +
|
| 97 | +
|
| 98 | +Operate the Linear Regression Model |
| 99 | +------------------------------------- |
| 100 | + |
| 101 | +By the simple example above, you may have known how to operate data with Fluid to some extent, so please try to create a test.py, and copy the following codes. |
| 102 | + |
| 103 | +This a a simple linear regression model to help us quickly solve the quaternary linear equation. |
| 104 | + |
| 105 | +.. code-block:: python |
| 106 | +
|
| 107 | + #load the library |
| 108 | + import paddle.fluid as fluid |
| 109 | + import numpy as np |
| 110 | + #generate data |
| 111 | + np.random.seed(0) |
| 112 | + outputs = np.random.randint(5, size=(10, 4)) |
| 113 | + res = [] |
| 114 | + for i in range(10): |
| 115 | + # assume the equation is y=4a+6b+7c+2d |
| 116 | + y = 4*outputs[i][0]+6*outputs[i][1]+7*outputs[i][2]+2*outputs[i][3] |
| 117 | + res.append([y]) |
| 118 | + # define data |
| 119 | + train_data=np.array(outputs).astype('float32') |
| 120 | + y_true = np.array(res).astype('float32') |
| 121 | +
|
| 122 | + #define the network |
| 123 | + x = fluid.layers.data(name="x",shape=[4],dtype='float32') |
| 124 | + y = fluid.layers.data(name="y",shape=[1],dtype='float32') |
| 125 | + y_predict = fluid.layers.fc(input=x,size=1,act=None) |
| 126 | + #define loss function |
| 127 | + cost = fluid.layers.square_error_cost(input=y_predict,label=y) |
| 128 | + avg_cost = fluid.layers.mean(cost) |
| 129 | + #define optimization methods |
| 130 | + sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.05) |
| 131 | + sgd_optimizer.minimize(avg_cost) |
| 132 | + #initialize parameters |
| 133 | + cpu = fluid.CPUPlace() |
| 134 | + exe = fluid.Executor(cpu) |
| 135 | + exe.run(fluid.default_startup_program()) |
| 136 | + ##start training and iterate for 500 times |
| 137 | + for i in range(500): |
| 138 | + outs = exe.run( |
| 139 | + feed={'x':train_data,'y':y_true}, |
| 140 | + fetch_list=[y_predict.name,avg_cost.name]) |
| 141 | + if i%50==0: |
| 142 | + print ('iter={:.0f},cost={}'.format(i,outs[1][0])) |
| 143 | + #save the training result |
| 144 | + params_dirname = "result" |
| 145 | + fluid.io.save_inference_model(params_dirname, ['x'], [y_predict], exe) |
| 146 | +
|
| 147 | + # start inference |
| 148 | + infer_exe = fluid.Executor(cpu) |
| 149 | + inference_scope = fluid.Scope() |
| 150 | + # load the trained model |
| 151 | + with fluid.scope_guard(inference_scope): |
| 152 | + [inference_program, feed_target_names, |
| 153 | + fetch_targets] = fluid.io.load_inference_model(params_dirname, infer_exe) |
| 154 | +
|
| 155 | + # generate test data |
| 156 | + test = np.array([[[9],[5],[2],[10]]]).astype('float32') |
| 157 | + # inference |
| 158 | + results = infer_exe.run(inference_program, |
| 159 | + feed={"x": test}, |
| 160 | + fetch_list=fetch_targets) |
| 161 | + # give the problem 【9,5,2,10】 and output the value of y=4*9+6*5+7*2+10*2 |
| 162 | + print ("9a+5b+2c+10d={}".format(results[0][0])) |
| 163 | +
|
| 164 | +.. code-block:: text |
| 165 | +
|
| 166 | + get the result: |
| 167 | + |
| 168 | + 9a+5b+2c+10d=[99.946] |
| 169 | + |
| 170 | +The output result should be a value close to 100, which may have a few errors every time. |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | + |
0 commit comments