当前位置:网站首页>飞桨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]])
边栏推荐
猜你喜欢
随机推荐
实例028:递归求等差数列
Servlet
笔记本开机黑屏提示:ERROR 0199:System Security-Security password retry count exceeded
[npm install error report collection] - npm ERR! code ENOTEMPTY npm ERR! syscall rmdir
GCC编译器技术解析
jvm 二之 栈帧内部结构
The nacos source code can not find the istio package
C# FileInfo类
Submit code process
MySQL Advanced Statements (1)
abaqus如何快速导入其他cae文件的assembly?
Go inside the basic knowledge
实验7 MPLS实验
能与观众实时互动的Claper
Py's mlxtend: a detailed guide to the introduction, installation, and usage of the mlxtend library
HCIP 第四天
“蔚来杯“2022牛客暑期多校训练营5,签到题KBGHFCD
love
JS初识高阶函数和函数柯里化
振兴农村循环经济 和数链串起农业“生态链”