PyTorch入门
张量
定义
一种特殊的数据结构,与数组和矩阵非常相似。在PyTorch中,使用张量来编码模型的输入和输出,以及模型的参数。张量类似于NumPy 的 ndarray,只是张量可以在 GPU 或其他专用硬件上运行以加速计算。
1
2import torch
import numpy as np
创建张量
直接从数据创建
1
2data = [[1, 2],[3, 4]]
x_data = torch.tensor(data)从 NumPy 数组创建
1
2np_array = np.array(data)
x_np = torch.from_numpy(np_array)从另一个张量创建
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18x_ones = torch.ones_like(x_data) # retains the properties of x_data
print(f"Ones Tensor: \n {x_ones} \n")
x_rand = torch.rand_like(x_data, dtype=torch.float) # overrides the datatype of x_data
print(f"Random Tensor: \n {x_rand} \n")
'''
Ones Tensor:
tensor([[1, 1],
[1, 1]])
Random Tensor:
tensor([[0.0217, 0.9983],
[0.3847, 0.8636]])
tensor([[0, 0],
[0, 0]])
'''直接从随机值或常数值创建
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22shape = (2,3,)
rand_tensor = torch.rand(shape)
ones_tensor = torch.ones(shape)
zeros_tensor = torch.zeros(shape)
print(f"Random Tensor: \n {rand_tensor} \n")
print(f"Ones Tensor: \n {ones_tensor} \n")
print(f"Zeros Tensor: \n {zeros_tensor}")
'''
Random Tensor:
tensor([[0.7145, 0.4068, 0.0370],
[0.0858, 0.3721, 0.2952]])
Ones Tensor:
tensor([[1., 1., 1.],
[1., 1., 1.]])
Zeros Tensor:
tensor([[0., 0., 0.],
[0., 0., 0.]])
'''
张量的属性
1
2
3
4
5
6
7
8
9
10
11tensor = torch.rand(3,4)
print(f"Shape of tensor: {tensor.shape}")
print(f"Datatype of tensor: {tensor.dtype}")
print(f"Device tensor is stored on: {tensor.device}")
'''
Shape of tensor: torch.Size([3, 4])
Datatype of tensor: torch.float32
Device tensor is stored on: cpu
'''张量运算
转置、索引、切片、数学运算、线性代数、随机抽样等
- 索引和切片
1
2
3
4
5
6
7
8
9
10# 创建一个四行四列的张量,然后把这个张量的第一列设置为 0
tensor = torch.ones(4, 4)
tensor[:,1] = 0
print(tensor)
'''
tensor([[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.]])
'''合并张量
1
2
3
4
5
6
7
8
9t1 = torch.cat([tensor, tensor, tensor], dim=1)
print(t1)
'''
tensor([[1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
[1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
[1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
[1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.]])
'''张量乘法
- 逐元素相乘
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18# 张量乘法
print(f"tensor.mul(tensor) \n {tensor.mul(tensor)} \n")
# 替代语法:
print(f"tensor * tensor \n {tensor * tensor}")
'''
tensor.mul(tensor)
tensor([[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.]])
tensor * tensor
tensor([[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.]])
'''矩阵相乘
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18# 张量乘法(矩阵乘法)
print(f"tensor.matmul(tensor.T) \n {tensor.matmul(tensor.T)} \n")
# 替代语法:
print(f"tensor @ tensor.T \n {tensor @ tensor.T}")
'''
tensor.matmul(tensor.T)
tensor([[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.]])
tensor @ tensor.T
tensor([[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.]])
'''
加法
就地操作: 带有后缀的操作会立刻改变前者的值。例如:
x.copy (y)
,x.t()
, 将改变x
可以节省一些内存,但在计算导数时可能会出现问题,因为会立即丢失历史记录。因此不建议使用它们。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16# 逐元素加
print(tensor, "\n")
tensor.add_(5)
print(tensor)
'''
tensor([[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.]])
tensor([[6., 5., 6., 6.],
[6., 5., 6., 6.],
[6., 5., 6., 6.],
[6., 5., 6., 6.]])
'''
与
NumPy
桥接
[未完待续]