目录
1 创建张量
1.1 指定数据创建张量
1.2 指定形状创建
1.3 指定区间创建
2 张量的属性
2.1 形状的改变
3 张量的访问
3.1 访问张量
3.2 修改张量
声明:本博文仅为个人学习笔记,不做他用。
安装教程与学习参考:飞桨PaddlePaddle-源于产业实践的开源深度学习平台
学习课程参考:AI算法大赛 - 飞桨AI Studio - 人工智能学习实训社区 (baidu.com)
神经网络与深度学习PDF:神经网络与深度学习 (nndl.github.io)
神经网络与深度学习实践代码:GitHub - nndl/practice-in-paddle: 《神经网络与深度学习》案例与实践
输入和输出都是多维数组,叫作张量(Tensor)。张量是矩阵的扩展与延伸,可以认为是高阶的矩阵。1阶张量为向量,2阶张量为矩阵。
这里我们以二维为例:
# 导入PaddlePaddle
import paddle
# 创建二维Tensor
ndim_2_Tensor = paddle.to_tensor([[2,3,4],[5,6,7]])
print(ndim_2_Tensor)
运行结果如下:
Tensor(shape=[2, 3], dtype=int64, place=Place(gpu:0), stop_gradient=True,
[[2, 3, 4],
[5, 6, 7]])
其它维是一样的,需要注意的是每一维元素的数量必须相同。
m, n = 2, 3
# 使用paddle.zeros创建数据全为0,形状为[m, n]的Tensor
zeros_Tensor = paddle.zeros([m, n])
# 使用paddle.ones创建数据全为1,形状为[m, n]的Tensor
ones_Tensor = paddle.ones([m, n])
# 使用paddle.full创建数据全为指定值,形状为[m, n]的Tensor,这里我们指定数据为10
full_Tensor = paddle.full([m, n], 10)
print('zeros Tensor: ', zeros_Tensor)
print('ones Tensor: ', ones_Tensor)
print('full Tensor: ', full_Tensor)
运行结果如下:
zeros Tensor: Tensor(shape=[2, 3], dtype=float32, place=CPUPlace, stop_gradient=True,
[[0., 0., 0.],
[0., 0., 0.]])
ones Tensor: Tensor(shape=[2, 3], dtype=float32, place=CPUPlace, stop_gradient=True,
[[1., 1., 1.],
[1., 1., 1.]])
full Tensor: Tensor(shape=[2, 3], dtype=float32, place=CPUPlace, stop_gradient=True,
[[10., 10., 10.],
[10., 10., 10.]])
# 使用paddle.arange创建以步长step均匀分隔数值区间(start, end)的一维Tensor
arange_Tensor = paddle.arange(start=1, end=5, step=1)
# 使用paddle.linspace创建以元素个数num均匀分隔数值区间(start, stop)的Tensor
linspace_Tensor = paddle.linspace(start=1, stop=5, num=5)
print('arange Tensor: ', arange_Tensor)
print('linspace Tensor: ', linspace_Tensor)
运行结果如下:
arange Tensor: Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True,
[1, 2, 3, 4])
linspace Tensor: Tensor(shape=[5], dtype=float32, place=Place(cpu), stop_gradient=True,
[1., 2., 3., 4., 5.])
ndim_4_Tensor = paddle.ones([2, 3, 4, 5])#四维张量
print("Number of dimensions:", ndim_4_Tensor.ndim)#张量的维度,例如向量的维度为1,矩阵的维度为2
print("Shape of Tensor:", ndim_4_Tensor.shape)#张量每个维度上元素的数量
print("Elements number along axis 0 of Tensor:", ndim_4_Tensor.shape[0])#张量第 维的大小,第 维也称为轴(axis)
print("Elements number along the last axis of Tensor:", ndim_4_Tensor.shape[-1])
print('Number of elements in Tensor: ', ndim_4_Tensor.size)#张量中全部元素的个数
运行结果如下:
Number of dimensions: 4
Shape of Tensor: [2, 3, 4, 5]
Elements number along axis 0 of Tensor: 2
Elements number along the last axis of Tensor: 5
Number of elements in Tensor: 120
# 定义一个shape为[3,2,5]的三维Tensor
ndim_3_Tensor = paddle.ones([3,2,5])
print(ndim_3_Tensor)
# paddle.reshape 可以保持在输入数据不变的情况下,改变数据形状。这里我们设置reshape为[2,5,3]
reshape_Tensor = paddle.reshape(ndim_3_Tensor, [2, 5, 3])
print("After reshape:", reshape_Tensor)
运行结果如下:
Tensor(shape=[3, 2, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
[[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]]])
After reshape: Tensor(shape=[2, 5, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]],
[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]])
注意:
使用reshape时存在一些技巧,比如:
new_Tensor1 = ndim_3_Tensor.reshape([-1])
print('new Tensor 1 shape: ', new_Tensor1.shape)
new_Tensor2 = ndim_3_Tensor.reshape([0, 5, 2])
print('new Tensor 2 shape: ', new_Tensor2.shape)
运行结果如下:
new Tensor 1 shape: [30]
new Tensor 2 shape: [3, 5, 2]
可以通过paddle.unsqueeze
将张量中的一个或多个维度中插入尺寸为1的维度。
ones_Tensor = paddle.ones([5, 10])
#axis指定插入的位置
new_Tensor1 = paddle.unsqueeze(ones_Tensor, axis=0)
print('new Tensor 1 shape: ', new_Tensor1.shape)
new_Tensor2 = paddle.unsqueeze(ones_Tensor, axis=[1, 2])
print('new Tensor 2 shape: ', new_Tensor2.shape)
运行结果如下:
new Tensor 1 shape: [1, 5, 10]
new Tensor 2 shape: [5, 1, 1, 10]
与NumPy同:
# 定义1个二维Tensor
ndim_2_Tensor = paddle.to_tensor([[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11]])
print("Origin Tensor:", ndim_2_Tensor)
print("First row:", ndim_2_Tensor[0])
print("First row:", ndim_2_Tensor[0, :])
print("First column:", ndim_2_Tensor[:, 0])
print("Last column:", ndim_2_Tensor[:, -1])
print("All element:", ndim_2_Tensor[:])
print("First row and second column:", ndim_2_Tensor[0, 1])
运行结果如下:
Origin Tensor: Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
[[0 , 1 , 2 , 3 ],
[4 , 5 , 6 , 7 ],
[8 , 9 , 10, 11]])
First row: Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True,
[0, 1, 2, 3])
First row: Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True,
[0, 1, 2, 3])
First column: Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True,
[0, 4, 8])
Last column: Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True,
[3 , 7 , 11])
All element: Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
[[0 , 1 , 2 , 3 ],
[4 , 5 , 6 , 7 ],
[8 , 9 , 10, 11]])
First row and second column: Tensor(shape=[1], dtype=int64, place=Place(cpu), stop_gradient=True,
[1])
# 定义1个二维Tensor
ndim_2_Tensor = paddle.ones([2, 3], dtype='float32')
print('Origin Tensor: ', ndim_2_Tensor)
# 修改第1维为0
ndim_2_Tensor[0] = 0
print('change Tensor: ', ndim_2_Tensor)
# 修改第1维为2.1
ndim_2_Tensor[0:1] = 2.1
print('change Tensor: ', ndim_2_Tensor)
# 修改全部Tensor
ndim_2_Tensor[...] = 3
print('change Tensor: ', ndim_2_Tensor)
运行结果如下:
Origin Tensor: Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1., 1., 1.],
[1., 1., 1.]])
change Tensor: Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0., 0., 0.],
[1., 1., 1.]])
change Tensor: Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[2.09999990, 2.09999990, 2.09999990],
[1. , 1. , 1. ]])
change Tensor: Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[3., 3., 3.],
[3., 3., 3.]])