【动手学深度学习 | 学习笔记】第2章-预备知识-第1节-数据操作

本文介绍了如何在PyTorch中进行数据操作,包括张量的基本概念、创建、形状变换、运算符应用(如加减乘除、指数运算和线性代数),以及内存优化技巧,如广播机制、索引切片和类型转换。
摘要由CSDN通过智能技术生成

2 预备知识

2.1 数据操作

2.1.1 入门

In [ ]:

import torch

In [ ]:

'''创建行张量'''
x = torch.arange(12)    # 生成[0, 12)行张量
x

Out[ ]:

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

In [ ]:

x.shape     # 张量的形状

Out[ ]:

torch.Size([12])

In [ ]:

x.numel()   # 张量元素的个数

Out[ ]:

12

In [ ]:

X = x.reshape(3, 4)     # 3行4列,reshape(<row>, <col>)
X, X.shape, X.numel()

Out[ ]:

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

In [ ]:

'''用-1自动计算reshape张量维数'''
X1 = x.reshape(-1, 4)
X2 = x.reshape(3, -1)
print("X1: ")
print((X1, X1.shape, X1.numel()))
print("X2: ")
print((X2, X2.shape, X2.numel()))
X1:
(tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]]), torch.Size([3, 4]), 12)
X2:
(tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]]), torch.Size([3, 4]), 12)

In [ ]:

'''设置张量元素为全0'''
torch.zeros((2, 3, 4))
# ones()同理

Out[ ]:

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.]]])

In [ ]:

'''张量元素随机生成'''
torch.randn(3, 4)   # 3行4列张量
# 每个元素都从均值为0、标准差为1的标准高斯分布(正态分布)中随机采样

Out[ ]:

tensor([[-0.5533,  0.8260,  1.1194, -1.8316],
        [ 0.2473, -0.8359, -0.9787,  0.6522],
        [-0.1199,  0.7770, -0.7957,  0.2867]])

In [ ]:

'''每个元素赋定值'''
torch.tensor([[2, 1, 4, 3],
              [1, 2, 3, 4],
              [4, 3, 2, 1]])

Out[ ]:

tensor([[2, 1, 4, 3],
        [1, 2, 3, 4],
        [4, 3, 2, 1]])

2.1.2 运算符

In [ ]:

'''按元素运算'''
import torch
x = torch.tensor([1.0, 2, 4, 8])
y = torch.tensor([2, 2, 2, 2])
x+y, x-y, x*y, x/y, x**y

Out[ ]:

(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.]))

In [ ]:

'''按元素运算,求幂'''
torch.exp(x)

Out[ ]:

tensor([2.7183e+00, 7.3891e+00, 5.4598e+01, 2.9810e+03])

In [ ]:

'''线性代数运算'''

Out[ ]:

'线性代数运算'

In [ ]:

'''张量连结(concatenate)torch.cat()'''
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]])

('X=', X,
'Y=', Y,
'cat dim=0: 上下堆叠',
torch.cat((X, Y), dim=0),
'cat dim=1: 左右排列',
torch.cat((X, Y), dim=1))

Out[ ]:

('X=',
 tensor([[ 0.,  1.,  2.,  3.],
         [ 4.,  5.,  6.,  7.],
         [ 8.,  9., 10., 11.]]),
 'Y=',
 tensor([[2., 1., 4., 3.],
         [1., 2., 3., 4.],
         [4., 3., 2., 1.]]),
 'cat dim=0: 上下堆叠',
 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.]]),
 'cat dim=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.]]))

In [ ]:

'''逻辑运算符构建二元张量'''
X == Y

Out[ ]:

tensor([[False,  True, False,  True],
        [False, False, False, False],
        [False, False, False, False]])

In [ ]:

X > Y

Out[ ]:

tensor([[True, True, True, True],
        [True, True, True, True],
        [True, True, True, True]])

In [ ]:

'''张量所有元素求和'''
X.sum()

Out[ ]:

tensor(66.)

2.1.3 广播机制

In [ ]:

a = torch.arange(3).reshape((3, 1)) # 3x1张量
b = torch.arange(2).reshape((1, 2)) # 1x2张量
a, b

Out[ ]:

(tensor([[0],
         [1],
         [2]]),
 tensor([[0, 1]]))

In [ ]:

'''
两个矩阵广播为一个更大的3x2矩阵
矩阵a将复制列,矩阵b将复制行
然后再按元素相加
a' = [0,0;1,1;2,2]
b' = [0,1;0,1;0,1]
'''
a + b

Out[ ]:

tensor([[0, 1],
        [1, 2],
        [2, 3]])

2.1.4 索引和切片

In [ ]:

X

Out[ ]:

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

In [ ]:

X[-1], X [1:3]

Out[ ]:

(tensor([ 8.,  9., 10., 11.]),
 tensor([[ 4.,  5.,  6.,  7.],
         [ 8.,  9., 10., 11.]]))

In [ ]:

X[1, 2] = 9
X

Out[ ]:

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

In [ ]:

'''第0到1行,所有列'''
X[0:2, :]

Out[ ]:

tensor([[0., 1., 2., 3.],
        [4., 5., 9., 7.]])

2.1.5 节省内存

In [ ]:

before = id(Y)
Y = Y + X
id(Y) == before

Out[ ]:

False

In [ ]:

Z = torch.zeros_like(Y)
print('id(Z):', id(Z))
Z[:] = X + Y
print('id(Z):', id(Z))
id(Z): 1498367700224
id(Z): 1498367700224

In [ ]:

'''如果在后续计算中没有重复使用X
我们也可以使用X[:] = X + Y或X += Y来减少操作的内存开销'''
before = id(X)
X += Y
id(X) == before

Out[ ]:

True

In [ ]:

before = id(X)
X[:] = X + Y
id(X) == before

Out[ ]:

True

2.1.6 转换为其它Python对象

In [ ]:

X

Out[ ]:

tensor([[ 6., 10., 26., 30.],
        [31., 41., 72., 61.],
        [68., 72., 76., 80.]])

In [ ]:

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

Out[ ]:

(numpy.ndarray, torch.Tensor)

In [ ]:

a = torch.tensor([3.5])
a, a.item(), float(a), int(a)

Out[ ]:

(tensor([3.5000]), 3.5, 3.5, 3)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值