文章目录
- 1. 张量的创建
- 1.1 张量的基本类型
- 1.2 张量的基本创建方式
- 1.3 创建线性和随机张量
- 1.4 创建 0-1张量
- 1.5 张量的类型转换
1. 张量的创建
1.1 张量的基本类型
0维张量:标量(scalar)
scalar = torch.tensor(7)
print(scalar.ndim)
>>> 0
1维张量:向量(vector)
vector = torch.tensor([7,7])
print(vector.ndim)
>>> 1
2维张量:矩阵(matrix)
matrix = torch.tensor([[1,2],[2,3]
])
print(matrix.ndim)
>>> 2
多维张量
t = torch.tensor([[[1,2,3],[4,5,6],[7,8,9]]
])
print(t.ndim)
>>> 3
1.2 张量的基本创建方式
-
torch.tensor() 根据指定数据创建张量
# 1. 创建张量标量 data = torch.tensor(10) print(data) # 2. numpy 数组,由于 data 为 float64, 下面代码也使用该类型 data = np.random.rand(2,3) data = torch.tensor(data) print(data) # 3. 列表, 下面代码使用默认元素类型 float32 data = [[10,20,30],[40,50,60],[70,80,90] ] data = torch.tensor(data) print(data)
-
torch.Tensor()根据指定形状创建张量,也可以用来创建指定数据的张量
# 1. 创建2行3列的张量, 默认 dtype 为 float32 data = torch.Tensor(2,3) print(data) # 注意,如果传递列表,则创建包含指定元素的张量 data = torch.Tensor([10]) print(data) data = torch.Tensor([10,20,30]) print(data)
-
torch.IntTensor()、torch.FloatTensor()、torch.DoubleTensor() 创建指定类型的张量
data = torch.IntTensor([10,20,30]) print(data) # 如果传递的类型不对,会自动转换 data = torch.IntTensor([10.2,20.8,30.9]) print(data) # 其他类型的有 data = torch.ShortTensor([10,20,30]) print(data) data = torch.LongTensor([10,20,30]) print(data) data = torch.FloatTensor([10,20,30]) print(data) data = torch.DoubleTensor([10,20,30]) print(data)
1.3 创建线性和随机张量
-
torch.arange 和 torch.linspace 创建线性张量
# 1. 在指定区间按照步长生成元素 [start, end, step) data = torch.arange(0,10,2) print(data) # 2. 在指定区间按照元素个数生成 [start, end, steps] data = torch.linspace(0,11,10) print(data)
输出结果:
tensor([0, 2, 4, 6, 8]) tensor([ 0.0000, 1.2222, 2.4444, 3.6667, 4.8889, 6.1111, 7.3333, 8.5556,9.7778, 11.0000])
-
torch.random.init_seed 和 torch.random.manual_seed 随机种子设置,torch.randn 创建随机张量
# 1. 创建随机张量 data = torch.randn(2,3) # 创建2行3列张量 print(data) # 查看随机种子 print('随机种子:',torch.random.initial_seed()) # 2. 随机种子设置 torch.random.manual_seed(100) data = torch.randn(2,3) print(data) print('随机种子:',torch.random.initial_seed())
结果输出:
tensor([[-0.2407, 0.0889, -0.1580],[-0.3603, -0.2630, 0.8047]]) 随机种子: 973960656430200 tensor([[ 0.3607, -0.2859, -0.3938],[ 0.2429, -1.3833, -2.3134]]) 随机种子: 100
1.4 创建 0-1张量
-
torch.ones 和 torch.ones_like 创建全1张量
# 1. 创建指定形状全1张量 data = torch.ones(2,3) print(data) # 2. 根据张量形状创建全1张量 data = torch.ones_like(data) print(data)
输出结果:
tensor([[1., 1., 1.],[1., 1., 1.]]) tensor([[1., 1., 1.],[1., 1., 1.]])
-
torch.zeros 和 torch.zeros_like 创建全0张量
# 1. 创建指定形状全0张量 data = torch.zeros(2,3) print(data) # 2. 根据张量形状创建全0张量 data = torch.zeros_like(data) print(data)
输出结果:
# 1. 创建指定形状全0张量 data = torch.zeros(2,3) print(data) # 2. 根据张量形状创建全0张量 data = torch.zeros_like(data) print(data)
-
torch.full 和 torch.full_like 创建全为指定值张量
# 1. 创建指定形状指定值的张量 data = torch.full([2,3],10) print(data) # 2. 根据张量形状创建指定值的张量 data = torch.full_like(data,20) print(data)
输出结果:
tensor([[10, 10, 10],[10, 10, 10]]) tensor([[20, 20, 20],[20, 20, 20]])
1.5 张量的类型转换
-
data.type(torch.DoubleTensor)
data = torch.full([2,3],10) print("张量的类型:",data.dtype) # 1.将类型转为 float64 data = data.type(torch.DoubleTensor) print("张量的类型:",data.dtype)
输出结果:
张量的类型: torch.int64 张量的类型: torch.float64
-
data.double()
data = torch.full([2,3],10) print("张量的类型:",data.dtype) data = data.double() print("张量的类型:",data.dtype)
输出结果:
张量的类型: torch.int64 张量的类型: torch.float64