前面的步骤跟乌班图安装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 paddle
if __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_TENSOR
Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[[1., 1.],
[1., 1.]])
VarType.LOD_TENSOR
Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[[0., 0.],
[0., 0.]])
VarType.LOD_TENSOR
Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[[1., 0.],
[0., 1.]])
VarType.LOD_TENSOR
Tensor(shape=[2, 2], dtype=int64, place=Place(gpu:0), stop_gradient=True,
[[0, 0],
[0, 0]])
VarType.LOD_TENSOR
Tensor(shape=[2, 2], dtype=int64, place=Place(gpu:0), stop_gradient=True,
[[1, 1],
[1, 1]])
VarType.LOD_TENSOR
Tensor(shape=[11], dtype=int64, place=Place(gpu:0), stop_gradient=True,
[0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10])
VarType.LOD_TENSOR
Tensor(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 paddle
if __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.]])
{{o.name}}
{{m.name}}