Novice Lesson -PaddlePaddle Quick Start

Novice Lesson -PaddlePaddle Quick Start

PaddlePaddle base command

PaddlePaddle depth learning framework Baidu open source, as well as Google's Tensorflow, Facebook and other similar depth of Pytorch learning framework, at the time of entry depth study, learn and use a common framework that allows learning efficiency is greatly enhanced.

In PaddlePaddle, the object is calculated tensor , we can first use PaddlePaddle to calculate a [[1, 1], [1, 1]] * [[1, 1], [1, 1]].

Calculating constants addition: 1 + 1

First import PaddlePaddle library

import paddle.fluid as fluid

Tensor define two constants x1 and x2, and specify their shape [2, 2], and covered the entire value of 1 tensor of type int64.

# 定义两个张量
x1 = fluid.layers.fill_constant(shape=[2, 2], value=1, dtype='int64')
x2 = fluid.layers.fill_constant(shape=[2, 2], value=1, dtype='int64')

Then a defined operation, the calculation is an addition amount of the two sheets of the above calculation, and returns a summing operator. PaddlePaddle provides a large number of operations, such as addition and subtraction, multiplication and division, trigonometric functions, readers can find in fluid.layers.

# 将两个张量求和
y1 = fluid.layers.sum(x=[x1, x2])

Then create an interpreter may be used herein specified calculation CPU or GPU. Used when using CPUPlace () the CPU, if CUDAPlace () using the GPU. After the parser is to use it to calculate through, ie before performing the calculation, we must first perform initialization parameter is to be used to program the parser because the parser can only execute program.

# 创建一个使用CPU的解释器
place = fluid.CPUPlace()
exe = fluid.executor.Executor(place)
# 进行参数初始化
exe.run(fluid.default_startup_program())

Finally, perform calculations, parameter values ​​program is the main program, not the last step using the program initialization parameters, the default program, a total of two, namely default_startup_program () and default_main_program (). Fetch_list parameter value is a value to be run after the resolver output, we need to calculate the output result value after the addition. It is a tensor last calculated.

# 进行运算,并把y的结果输出
result = exe.run(program=fluid.default_main_program(),
                 fetch_list=[y1])
print(result)

Calculation results: [array ([[2, 2], [2, 2]])]
paddle1

The addition calculated variables: 1 + 1

The above calculation is the constant tensor 1 + 1, and the constant value can not be arbitrarily modified, so here we have to use to write a variable tensor program as a multiplier, is similar to a placeholder, until to be calculated, then to calculate the value added to calculated placeholder.

PaddlePaddle import numpy libraries and libraries.

import paddle.fluid as fluid
import numpy as np

Define two tensors, is not specified and the shape of the tensor values ​​after they are dynamically assigned. Here are just specify their type and name, the name is the key to our assignment after.

# 定义两个张量
a = fluid.layers.create_tensor(dtype='int64', name='a')
b = fluid.layers.create_tensor(dtype='int64', name='b')

The same way, the definition of the two tensor adding operation.

# 将两个张量求和
y = fluid.layers.sum(x=[a, b])

Here we also use the CPU to create a parser, and initialization parameters.

# 创建一个使用CPU的解释器
place = fluid.CPUPlace()
exe = fluid.executor.Executor(place)
# 进行参数初始化
exe.run(fluid.default_startup_program())

Then use numpy create two tensor-valued, then we have to calculate is that these two values.

# 定义两个要计算的变量
a1 = np.array([3, 2]).astype('int64')
b1 = np.array([1, 1]).astype('int64')

The exe.run () parameter bit different, one more feed parameters, this is to assign values ​​to the variables of tensor. The assignment is to use the format key-value pairs, key definition tensor variables are specified name, value is the value to be transmitted. In fetch_list parameter, the desired value I a, b, y are output to, so to use three variables to accept the return value.

# 进行运算,并把y的结果输出
out_a, out_b, result = exe.run(program=fluid.default_main_program(),
                               feed={'a': a1, 'b': b1},
                               fetch_list=[a, b, y])
print(out_a, " + ", out_b," = ", result)

Calculation results: [32] + [11] = [43]
paddle2

Use PaddlePaddle do linear regression

In the above teaching, teach you learn to do basic arithmetic operators with PaddlePaddle, the following will teach you how to use PaddlePaddle do a simple linear regression, from the definition of a network to use custom data for training, the final verification of our network Ability to predict.

First import PaddlePaddle libraries and some tools library.

import paddle.fluid as fluid
import paddle
import numpy as np

Define a simple linear network, this network is very simple, the structure is: output layer - Hidden Layer >> - >> __ output layer, the network layer is a total of 2, because the input number of layers is not the network layer. More specifically, a size is 100, ReLU activation function is fully connected layers, and an output connected to the full size of layer 1, thus constructed a very simple network. As used herein, input fluid.layers.data () defined in the input layer similar fluid.layers.create_tensor (), it is also a name attribute, then the data is filled in accordance with this attribute. Shape defined herein as the input layer 13, since each of the data rates Boston dataset attributes 13, custom data set then we also to comply with this dimension.

# 定义一个简单的线性网络
x = fluid.layers.data(name='x', shape=[13], dtype='float32')
hidden = fluid.layers.fc(input=x, size=100, act='relu')
net = fluid.layers.fc(input=hidden, size=1, act=None)

Loss function is then defined neural network, here also uses fluid.layers.data () of this interface, this data can be understood as corresponding to a result of the above is x name fluid.layers.data () as attribute data. As used herein, the squared difference between the loss function (square_error_cost), PaddlePaddle interface provides much loss function, such as cross-entropy loss function (cross_entropy). Because this project is a linear regression task, so we use the squared difference loss function. Because fluid.layers.square_error_cost () is seeking a Batch of loss of value, so we have to find him a mean value.

# 定义损失函数
y = fluid.layers.data(name='y', shape=[1], dtype='float32')
cost = fluid.layers.square_error_cost(input=net, label=y)
avg_cost = fluid.layers.mean(cost)

After definition of loss function, a program can be cloned in the main program (fluid.default_main_program) as a prediction program for predicting the use of the prediction data after the training program is completed. This definition of the order can not be wrong, because we define the network structure, loss of function, and so are more PaddlePaddle order to record the main program. The main program defines a neural network model, the forward reverse calculation, as well as network optimization algorithm update can learn parameters, is the core of our entire program, this is PaddlePaddle has helped us to achieve, and we just need to focus on building a network of and training can be.

# 复制一个主程序,方便之后使用
test_program = fluid.default_main_program().clone(for_test=True)

Followed by optimization method defined training used herein is stochastic gradient descent optimization method. PaddlePaddle provides a number of optimization function interface, in addition to stochastic gradient descent (SGD), as well as Momentum, Adagrad, Adagrad so on. The project used, readers can demand more of their own projects using different optimization methods.

# 定义优化方法
optimizer = fluid.optimizer.SGDOptimizer(learning_rate=0.01)
opts = optimizer.minimize(avg_cost)

Then create a parser, we also use the CPU for training. After you create a parser using parser to perform fluid.default_startup_program () initialization parameters.

# 创建一个使用CPU的解释器
place = fluid.CPUPlace()
exe = fluid.Executor(place)
# 进行参数初始化
exe.run(fluid.default_startup_program())

We define a set of data using numpy, each data of the set 13 of data, this is because when we define network input layer, a Shape 13, but the end of each data of 12 data is meaningless, because I use are all 0 to fill, simply to conform to the format of data only. This set of data is in line y = 2 * x + 1, but the program is not known, we use this set of data after the training, to see if a powerful neural network can be trained to fit the model of a function. Finally, the definition of a prediction data, is in training to complete, using this data as input x to see if the correct value can predict similar results.

# 定义训练和测试数据
x_data = np.array([[1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 
                   [2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 
                   [3.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 
                   [4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 
                   [5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]).astype('float32')
y_data = np.array([[3.0], [5.0], [7.0], [9.0], [11.0]]).astype('float32')
test_data = np.array([[6.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]).astype('float32')

After defining the data, we can use the data for training. This time we trained 10 pass, the reader can be set up more training rounds according to the situation, the more the number of training Generally speaking, the better the convergence model. Profram same time we are using the fluid.default_main_program (), feed is in the training data into fluid.layers.data () defined variable, and the key for the key to use is fluid.layers. name value () of the data. We let the value of the output avg_cost of the training process.

During training, we can see that the loss of value of the output is constantly reduced, prove our model continues to converge.

# 开始训练10个passfor pass_id in range(10):
    train_cost = exe.run(program=fluid.default_main_program(),
                         feed={'x': x_data, 'y': y_data},
                         fetch_list=[avg_cost])
    print("Pass:%d, Cost:%0.5f" % (pass_id, train_cost[0]))

paddle3

After training is completed, we use the above cloning prediction program main program to get a prediction of data we have just defined. The same forecast data as input x in the feed, in predicting, in theory, do not enter y, but to match the input format, we simulate a data value of y, this value does not affect our predictions. Fetch_list value, that is, after the implementation of the results we forecast to be output, this is the last layer of the network, rather than the average loss function (avg_cost), because we want to predict the program output forecast. According to our definition data above, the law satisfies y = 2 * x + 1, when the x is 6, y should be 13 when the result is the final output should be close to 13.

# 开始预测
result = exe.run(program=test_program,
                 feed={'x': test_data, 'y': np.array([[0.0]]).astype('float32')},
                 fetch_list=[net])
print("当x为6.0时,y为:%0.5f" % result[0][0][0])

Prediction: when x is 6.0, y is: 13.23625
paddle4

Published 61 original articles · won praise 25 · views 7175

Guess you like

Origin blog.csdn.net/qq_42582489/article/details/105244819