pytorch 深度学习之数据操作

基础

张量表示由一个数值组成的数组,这个数组可能有多个维度。 具有一个轴的张量对应数学上的向量(vector); 具有两个轴的张量对应数学上的矩阵(matrix); 具有两个轴以上的张量没有特殊的数学名称。
张量中的每个值都称为张量的元素(element)。

arange 创建一个行向量,可以指定数据类型,tensor 可以使用 python 列表的形式指定张量:

x1 = torch.arange(12) 
x2 = torch.arange(10,dtype=torch.float32)

print(x1)
print(x2)

x3 = torch.tensor([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
print(x3)
tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
tensor([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
tensor([[2, 1, 4, 3],
        [1, 2, 3, 4],
        [4, 3, 2, 1]])

通过张量的 shape 属性来访问张量(沿每个轴的长度)的形状,numel 函数可以获取所有元素的个数:

x1 = torch.arange(5)
x2 = torch.tensor([[1,2,3],[4,5,6]])

print(x1.shape)
print(x2.shape)

print(x1.numel())
print(x2.numel())
torch.Size([5])
torch.Size([2, 3])
5
6

reshape 函数可以改变一个张量的形状而不改变元素数量和元素值,可以通过 -1 来自动计算出维度:

x1 = torch.arange(12).reshape(4,3)
x2 = torch.arange(12).reshape(-1,6)
x3 = torch.arange(12).reshape(2,3,-1)

print(x1)
print(x2)
print(x3)
tensor([[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8],
        [ 9, 10, 11]])
tensor([[ 0,  1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10, 11]])
tensor([[[ 0,  1],
         [ 2,  3],
         [ 4,  5]],

        [[ 6,  7],
         [ 8,  9],
         [10, 11]]])

zeros 函数和 ones 函数可以分别用来生成全 0 和 全 1 张量,randn 函数可以用来生成每个元素都从均值为0、标准差为1的标准高斯分布(正态分布)中随机采样:

x1 = torch.zeros((2, 3, 4))
print(x1)

x2 = torch.ones((1,2,3))
print(x2)

x3 = torch.randn((2,2,3))
print(x3)
tensor([[[0., 0., 0., 0.],
         [0., 0., 0., 0.],
         [0., 0., 0., 0.]],

        [[0., 0., 0., 0.],
         [0., 0., 0., 0.],
         [0., 0., 0., 0.]]])
tensor([[[1., 1., 1.],
         [1., 1., 1.]]])
tensor([[[ 0.3054, -0.2960,  0.1536],
         [-0.7202,  1.5987, -0.3821]],

        [[ 0.3733,  0.2818, -1.2353],
         [-0.7004,  0.6075,  0.5294]]])

运算符

常见的标准算术运算符(+-*/**)都可以被升级为按元素运算,“按元素”方式可以应用更多的计算,包括像求幂这样的一元运算符,比如 exp,逻辑运算:

x = torch.tensor([1.0, 2, 4, 8])
y = torch.tensor([2, 2, 2, 2])

print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x ** y)

print(torch.exp(x))

print(x == y)
tensor([ 3.,  4.,  6., 10.])
tensor([-1.,  0.,  2.,  6.])
tensor([ 2.,  4.,  8., 16.])
tensor([0.5000, 1.0000, 2.0000, 4.0000])
tensor([ 1.,  4., 16., 64.])
tensor([2.7183e+00, 7.3891e+00, 5.4598e+01, 2.9810e+03])
tensor([False,  True, False, False])

张量连结,也就是把张量端对端地叠起来形成一个更大的张量,需要指定按照哪个轴进行连结:

X = torch.arange(12, dtype=torch.float32).reshape((3,4))
Y = torch.tensor([[2.0, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
P = torch.cat((X, Y), dim=0)
Q = torch.cat((X, Y), dim=1)

print(P)
print(Q)
tensor([[ 0.,  1.,  2.,  3.],
        [ 4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11.],
        [ 2.,  1.,  4.,  3.],
        [ 1.,  2.,  3.,  4.],
        [ 4.,  3.,  2.,  1.]])
tensor([[ 0.,  1.,  2.,  3.,  2.,  1.,  4.,  3.],
        [ 4.,  5.,  6.,  7.,  1.,  2.,  3.,  4.],
        [ 8.,  9., 10., 11.,  4.,  3.,  2.,  1.]])

对所有的元素求和:

print(torch.sum(X))
print(torch.sum(Y))
tensor(66.)
tensor(30.)

广播机制

在某些情况下,即使形状不同,我们仍然可以通过调用 广播机制(broadcasting mechanism)来执行按元素操作。 这种机制的工作方式如下:首先,通过适当复制元素来扩展一个或两个数组, 以便在转换之后,两个张量具有相同的形状。 其次,对生成的数组执行按元素操作:

a = torch.arange(3).reshape((3, 1))
b = torch.arange(2).reshape((1, 2))

print(a)
print(b)
print(a + b)
tensor([[0],
        [1],
        [2]])
tensor([[0, 1]])
tensor([[0, 1],
        [1, 2],
        [2, 3]])

其过程为:

索引和切片

张量中的元素可以通过索引访问。 与任何 Python 数组一样:第一个元素的索引是 0,最后一个元素索引是 -1; 可以指定范围以包含第一个元素和最后一个之前的元素:

X = torch.arange(16).reshape(4,4)

print(X)
print(X[-1])
print(X[0:2])
print(X[3:])
print(X[-1][-1])
print(X[-1][1:3])
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11],
        [12, 13, 14, 15]])
tensor([12, 13, 14, 15])
tensor([[0, 1, 2, 3],
        [4, 5, 6, 7]])
tensor([[12, 13, 14, 15]])
tensor(15)
tensor([13, 14])

利用切片和索引可以对元素赋值:

X[-1][-1] = 99
print(X)

X[-1][:] = 1099
print(X)

X[:2][:] = 10
print(X)
tensor([[   0,    1,    2,    3],
        [   4,    5,    6,    7],
        [   8,    9,   10,   11],
        [1099, 1099, 1099,   99]])
tensor([[   0,    1,    2,    3],
        [   4,    5,    6,    7],
        [   8,    9,   10,   11],
        [1099, 1099, 1099, 1099]])
tensor([[  10,   10,   10,   10],
        [  10,   10,   10,   10],
        [   8,    9,   10,   11],
        [1099, 1099, 1099, 1099]])

节省内存

运行一些操作可能会导致为新结果分配内存。 例如,如果我们用 Y = X + Y,我们将取消引用Y指向的张量,而是指向新分配的内存处的张量:

Y = torch.arange(12)
before = id(Y)
print(before)

Y = Y + torch.ones(12)
after = id(Y)
print(after)

print(after == before)
1923522705264
1923522706464
False

可以使用切片表示法将操作的结果分配给先前分配的数组,也可以使用 X[:] = X + YX += Y 来减少操作的内存开销:

Z = torch.zeros_like(Y)  # 创建一个新的矩阵Z,其形状与另一个Y相同, 使用zeros_like来分配一个全的块
print('id(Z):', id(Z))
Z[:] = torch.ones(12) + Y
print('id(Z):', id(Z))


before = id(Y)
Y += torch.ones(12)
print(id(Y) == before)
id(Z): 1923522863488
id(Z): 1923522863488
True

转换为其他Python对象

torch 张量和 numpy 数组将共享它们的底层内存,就地操作更改一个张量也会同时更改另一个张量:

A = X.numpy()
B = torch.tensor(A)
type(A), type(B)
(numpy.ndarray, torch.Tensor)

要将大小为1的张量转换为 Python 标量,我们可以调用 item 函数或 Python 的内置函数:

a = torch.tensor([3.5])

print(a)
print(a.item())
print(float(a))
print(int(a))
tensor([3.5000])
3.5
3.5
3
posted @ 2022-04-15 16:19  刘-皇叔  阅读(101)  评论(0编辑  收藏  举报