当前位置:网站首页>飞桨paddle技术点整理
飞桨paddle技术点整理
2022-08-02 06:38:00 【算法之名】
前面的步骤跟乌班图安装Pytorch、Tensorflow Cuda环境 是一样。
安装GPU版本的paddle
python -m pip install paddlepaddle-gpu==2.3.1.post116 -f https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html
张量
import paddleif __name__ == '__main__': a = paddle.to_tensor([[1, 2], [3, 4]]) print(a) print(a.shape) print(a.type) b = paddle.ones([2, 2]) print(b) print(b.type) c = paddle.zeros([2, 2]) print(c) print(c.type) d = paddle.eye(2, 2) print(d) print(d.type) e = paddle.zeros_like(a) print(e) print(e.type) f = paddle.ones_like(a) print(f) print(f.type) g = paddle.arange(0, 11, 1) print(g) print(g.type) h = paddle.linspace(2, 10, 4) print(h) i = paddle.rand([2, 2]) print(i) j = paddle.normal(mean=0.0, std=paddle.rand([5])) print(j) k = paddle.uniform(shape=[2, 2]) print(k) l = paddle.randperm(10) print(l)
运行结果
Tensor(shape=[2, 2], dtype=int64, place=Place(gpu:0), stop_gradient=True, [[1, 2], [3, 4]])[2, 2]VarType.LOD_TENSORTensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[1., 1.], [1., 1.]])VarType.LOD_TENSORTensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[0., 0.], [0., 0.]])VarType.LOD_TENSORTensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[1., 0.], [0., 1.]])VarType.LOD_TENSORTensor(shape=[2, 2], dtype=int64, place=Place(gpu:0), stop_gradient=True, [[0, 0], [0, 0]])VarType.LOD_TENSORTensor(shape=[2, 2], dtype=int64, place=Place(gpu:0), stop_gradient=True, [[1, 1], [1, 1]])VarType.LOD_TENSORTensor(shape=[11], dtype=int64, place=Place(gpu:0), stop_gradient=True, [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10])VarType.LOD_TENSORTensor(shape=[4], dtype=float32, place=Place(gpu:0), stop_gradient=True, [2. , 4.66666651, 7.33333349, 10. ])Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[0.17855753, 0.15026711], [0.54343289, 0.04870688]])Tensor(shape=[5], dtype=float32, place=Place(gpu:0), stop_gradient=True, [-0.07493367, -0.10425358, -1.67506480, 0.02299307, 0.38065284])Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[ 0.01213348, -0.30467188], [-0.81535292, 0.09958601]])Tensor(shape=[10], dtype=int64, place=Place(gpu:0), stop_gradient=True, [2, 9, 4, 5, 8, 7, 0, 1, 6, 3])
- 算数运算、矩阵乘法
import paddleif __name__ == '__main__': a = paddle.to_tensor([[1., 2.], [3., 4.]]) print(a) b = paddle.ones([2, 2]) print(b) c = a + b print(c) c = paddle.add(a, b) print(c) d = paddle.subtract(a, b) print(d) e = paddle.to_tensor([2., 3.]) f = a * e print(f) f = paddle.multiply(a, e) print(f) g = a / e print(g) g = paddle.divide(a, e) print(g) h = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype='float32') i = paddle.to_tensor([[2, 4], [11, 13], [7, 9]], dtype='float32') j = paddle.mm(h, i) print(j) k = paddle.matmul(h, i) print(k)
运行结果
Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[1., 2.], [3., 4.]])Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[1., 1.], [1., 1.]])Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[2., 3.], [4., 5.]])Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[2., 3.], [4., 5.]])Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[0., 1.], [2., 3.]])Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[2. , 6. ], [6. , 12.]])Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[2. , 6. ], [6. , 12.]])Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[0.50000000, 0.66666669], [1.50000000, 1.33333337]])Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[0.50000000, 0.66666669], [1.50000000, 1.33333337]])Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[45. , 57. ], [105., 135.]])Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[45. , 57. ], [105., 135.]])
- 平方、开方、对数运算
import paddleif __name__ == '__main__': a = paddle.to_tensor([1, 2, 3]) c = paddle.pow(a, 2) print(c) c = a**2 print(c) a = paddle.to_tensor([2.]) c = paddle.exp(a) print(c) a = paddle.to_tensor([1, 2, 3], dtype='float32') c = paddle.sqrt(a) print(c) c = paddle.log2(a) print(c) c = paddle.log10(a) print(c) c = paddle.log(a) print(c)
运行结果
Tensor(shape=[3], dtype=int64, place=Place(gpu:0), stop_gradient=True, [1, 4, 9])Tensor(shape=[3], dtype=int64, place=Place(gpu:0), stop_gradient=True, [1, 4, 9])Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True, [7.38905621])Tensor(shape=[3], dtype=float32, place=Place(gpu:0), stop_gradient=True, [1. , 1.41421354, 1.73205078])Tensor(shape=[3], dtype=float32, place=Place(gpu:0), stop_gradient=True, [0. , 1. , 1.58496249])Tensor(shape=[3], dtype=float32, place=Place(gpu:0), stop_gradient=True, [0. , 0.30103001, 0.47712126])Tensor(shape=[3], dtype=float32, place=Place(gpu:0), stop_gradient=True, [0. , 0.69314718, 1.09861231])
- 取整/取余
import paddleif __name__ == '__main__': a = paddle.rand([2, 2]) b = paddle.multiply(a, paddle.to_tensor([10.])) print(b) print(paddle.floor(b)) print(paddle.ceil(b)) print(paddle.round(b)) print(paddle.trunc(b)) print(b % 2)
运行结果
Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[2.33501291, 3.41357899], [6.85909081, 5.18760014]])Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[2., 3.], [6., 5.]])Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[3., 4.], [7., 6.]])Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[2., 3.], [7., 5.]])Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[2., 3.], [6., 5.]])Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True, [[0.33501291, 1.41357899], [0.85909081, 1.18760014]])
边栏推荐
猜你喜欢
雷达人体存在感应器方案,智能物联网感知技术,实时感应人体存在
张驰课堂:六西格玛测量系统的误差分析与判定
宝塔+FastAdmin 404 Not Found
新产品立大功 伟世通第二季度营收双增
jvm 二之 栈帧内部结构
反射课后习题及做题记录
nacos源码启动找不到istio包
Reverse resolve dns server
System.Security.SecurityException: 未找到源,但未能搜索某些或全部事件日志。不可 访问的日志: Security
How does abaqus quickly import the assembly of other cae files?
随机推荐
解决Pytorch模型在Gunicorn部署无法运行或者超时问题
项目开发规范
SQL执行顺序
张驰课堂:六西格玛培训工具——箱线图
两篇不错的php debug教程
交换网络----三种生成树协议
数据库概论之MySQL表的增删改查1
The second day HCIP
docker 安装mysql
SphereEx苗立尧:云原生架构下的Database Mesh研发实践
Project development specification
数据库概论之MySQL表的增删改查2
Unity Shader学习(七)纹理图像的简单使用
punch day05
C# FileInfo class
August 2022 plan, focusing on ue4 video tutorials
实验8 VLAN综合实验
【论文精读】Geometric Structure Preserving Warp for Natural Image Stitching
rhce homework
abaqus如何快速导入其他cae文件的assembly?