1.0基础-paddlepaddle常量

一个最基本的paddlepaddle程序执行过程,对两个常量进行加法算子操作
参考API:


常量定义:fluid.layers.fill_constant:https://www.paddlepaddle.org.cn/documentation/docs/zh/api_cn/layers_cn/fill_constant_cn.html#fill-constant


求和:fluid.layers.sum:https://www.paddlepaddle.org.cn/documentation/docs/zh/api_cn/layers_cn/sum_cn.html#sum


执行器:fluid.executor:https://www.paddlepaddle.org.cn/documentation/docs/zh/api_cn/executor_cn/Executor_cn.html#executor


import paddle.fluid as fluid

#定义2个常量
x1 = fluid.layers.fill_constant(shape=[2, 2], value=3, dtype='int64')
x2 = fluid.layers.fill_constant(shape=[2, 2], value=1, dtype='int64')
# 执行sum算子
y1 = fluid.layers.sum(x=[x1, x2])
# 是否使用GPU
if_cuda = False
place = fluid.CUDAPlace(0) if if_cuda else fluid.CPUPlace()
# 定义执行器
exe = fluid.executor.Executor(place)
# 参数初始化
exe.run(fluid.default_startup_program())
# 正式执行参数,在主程序default_main_program()下执行,返回参数[y1]
result = exe.run(program=fluid.default_main_program(),
                 fetch_list=[y1])
# 显示返回结果
print(result)
'''
[array([[4, 4],
       [4, 4]], dtype=int64)]
'''
原文地址:https://www.cnblogs.com/onenoteone/p/12441684.html