paddlepaddle编程入门
##简单示例
from paddle import fluid
# create a simple layer that reurns a varible
x = fluid.layers.fill_constant(shape=[1],dtype='int64',value=5)
y = fluid.layers.fill_constant(shape=[1],dtype='int64',value=1)
z = x+y
# create an executor that runs on cpu
exe = fluid.Executor(fluid.CPUPlace())
# run the program an fetch the result
exe.run(fluid.default_main_program(),fetch_list=[z])
## OUT:[array([6], dtype=int64)]
## if-else 使用
# create two tensor variables
a = fluid.layers.fill_constant(shape=[2,1],dtype='int64',value=5)
b = fluid.layers.fill_constant(shape=[2,1],dtype='int64',value=6)
# create a if-else block
if_cond = fluid.layers.less_than(x=a,y=b)
ie = fluid.layers.IfElse(if_cond)
with ie.true_block():
c = ie.input(a)
c += 1
ie.output(c)
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_main_program(),fetch_list=[c])
## OUT:[array([[6],[6]], dtype=int64)]
## while 循环使用
from paddle import fluid
a = fluid.layers.fill_constant(shape=[2,2],dtype='float32',value=1)
i = fluid.layers.zeros(shape=[1],dtype='int64')
until = fluid.layers.fill_constant(shape=[1],dtype='int64',value=10)
data_arr = fluid.layers.array_write(a,i)
while_cond = fluid.layers.less_than(x=i,y=until)
while_op = fluid.layers.While(while_cond)
with while_op.block():
a = fluid.layers.array_read(data_arr,i)
a = a+1
i = fluid.layers.increment(x=i,value=1,in_place=True)
fluid.layers.less_than(x=i,y=until,cond=while_cond)
fluid.layers.array_write(a,i,data_arr)
ret = fluid.layers.array_read(data_arr,i-1)
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
exe.run(fetch_list=[ret])
## OUT:[array([[10., 10.],[10., 10.]], dtype=float32)]
## 多卡优化
compiler.CompiledProgram(fluid.default_main_program()).with_data_parallel(loss_name=cost.name)
## mnist数字识别样例
import paddle
from paddle import fluid
import numpy as np
# define input for model
image = fluid.layers.data(name='pixel',shape=[1,28,28],dtype='float32')
label = fluid.layers.data(name='label',shape=[1],dtype='int64')
#define the model
conv1 = fluid.layers.conv2d(input=image,filter_size=5,num_filters=20)
relu1 = fluid.layers.relu(conv1)
pool1 = fluid.layers.pool2d(input=relu1,pool_size=2,pool_stride=2)
conv2 = fluid.layers.conv2d(input=pool1,filter_size=5,num_filters=50)
relu2 = fluid.layers.relu(conv2)
pool2 = fluid.layers.pool2d(input=relu2,pool_size=2,pool_stride=2)
predict = fluid.layers.fc(input=pool2,size=10,act='softmax')
#get the loss
loss = fluid.layers.cross_entropy(input=predict,label=label)
avg_cost = fluid.layers.mean(loss)
batch_acc = fluid.layers.accuracy(input=predict,label=label)
'''
fluid提供了多种优化算法:SGD,Momentum,Adagrad,Adam,DecayedAdagrad,
Ftrl,Adadelta,RMSProp,LarsMomentum等
'''
opt = fluid.optimizer.AdamOptimizer()
opt.minimize(avg_cost)
place = fluid.CPUPlace()#在指定的设备上进行参数初始化
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
#数据准备reader
train_reader = paddle.batch(paddle.dataset.mnist.train(),batch_size=128)
#多卡训练
# from paddle.fluid import compiler
# compiled_program = compiler.CompiledProgram(fluid.default_main_program())
# compiled_program.with_data_parallel(loss_name=avg_cost.name)
# 单卡训练
for epoch_id in range(5):
for batch_id,data in enumerate(train_reader()):
img_data = np.array([x[0].reshape([1,28,28]) for x in data]).astype('float32')
y_data = np.array([x[1] for x in data]).reshape([len(img_data),1]).astype('int64')
loss,acc = exe.run(fluid.default_main_program(),#compiled_program
feed={'pixel':img_data,'label':y_data},
fetch_list=[avg_cost,batch_acc])
print("epoch:%d,batch=%d,loss=%.3f,accuracy=%.3f" % (epoch_id,batch_id,loss,acc))
模型保存与重载
VisualDL
新特性
优势:多卡,多线程,分布式
psserver:distributed lookup table
forward backward update
deep&wide model
paddle mobile
移动端技术栈
移动端深度学习优势:低延迟、节省流量、节省吞吐量、隐私安全
VisualDL
python && C++ sdk 全开源