Tensor基础1——张量的创建与转换

1. 张量的创建

1)张量的基本创建方式
  • torch.tensor 根据指定数据创建张量
  • torch.Tensor 根据形状创建张量, 其也可用来创建指定数据的张量
  • torch.IntTensor、torch.FloatTensor、torch.DoubleTensor 创建指定类型的张量

1、torch.tensor() 根据指定数据创建张量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import torch
import numpy as np

# 1. 创建张量标量
data = torch.tensor(10)
print(data)
>>> tensor(10)

# 2. numpy 数组, 由于 data 为 float64, 下面代码也使用该类型
data = np.random.randn(2, 3)
data = torch.tensor(data)
print(data)
>>> tensor([[ 0.1345, 0.1149, 0.2435],
[ 0.8026, -0.6744, -1.0918]], dtype=torch.float64)

# 3. 列表, 下面代码使用默认元素类型 float32
data = [[10., 20., 30.], [40., 50., 60.]]
data = torch.tensor(data)
print(data)
>>> tensor([[10., 20., 30.],
[40., 50., 60.]])

2、torch.Tensor() 根据指定形状创建张量,也可以用来创建指定数据的张量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 1. 创建2行3列的张量, 默认 dtype 为 float32
data = torch.Tensor(2, 3)
print(data)
>>> tensor([[0.0000e+00, 3.6893e+19, 2.2018e+05],
[4.6577e-10, 2.4158e-12, 1.1625e+33]])

# 2. 注意: 如果传递列表, 则创建包含指定元素的张量
data = torch.Tensor([10])
print(data)
>>> tensor([10.])

data = torch.Tensor([10, 20])
print(data)
>>> tensor([10., 20.])

3、torch.IntTensor()、torch.FloatTensor()、torch.DoubleTensor() 创建指定类型的张量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 1. 创建2行3列, dtype 为 int32 的张量
data = torch.IntTensor(2, 3)
print(data)
>>> tensor([[ 0, 1610612736, 1213662609],
[ 805308409, 156041223, 1]], dtype=torch.int32)

# 2. 注意: 如果传递的元素类型不正确, 则会进行类型转换
data = torch.IntTensor([2.5, 3.3])
print(data)
>>> tensor([2, 3], dtype=torch.int32)

# 3. 其他的类型
data = torch.ShortTensor() # int16
data = torch.LongTensor() # int64
data = torch.FloatTensor() # float32
data = torch.DoubleTensor() # float64
2)创建线性张量和随机张量
  • torch.arange 和 torch.linspace 创建线性张量
  • torch.random.init_seed 和 torch.random.manual_seed 随机种子设置
  • torch.randn 创建随机张量

1、torch.arange()、torch.linspace() 创建线性张量

1
2
3
4
5
6
7
8
9
# 1. 在指定区间按照步长生成元素 [start, end, step)
data = torch.arange(0, 10, 2)
print(data)
>>> tensor([0, 2, 4, 6, 8])

# 2. 在指定区间按照元素个数生成 [start, end, steps]
data = torch.linspace(0, 11, 10)
print(data)
>>> tensor([0.0000, 1.2222, 2.4444, 3.6667, 4.8889, 6.1111, 7.3333, 8.5556, 9.7778, 11.0000])

2、随机种子操作

  • torch.random.initial_seed()查看随机种子
  • torch.random.manual_seed() 设置随机数种子
  • torch.randn() 创建随机张量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 1. 创建随机张量
data = torch.randn(2, 3) # 创建23列张量
print(data)
>>> tensor([[-0.5209, -0.2439, -1.1780],
[ 0.8133, 1.1442, 0.6790]])

# 2.查看随机数种子
print('随机数种子:', torch.random.initial_seed())
>>> 随机数种子: 4508475192273306739

# 3.设置随机数种子
torch.random.manual_seed(100)
data = torch.randn(2, 3)
print(data)
print('随机数种子:', torch.random.initial_seed())
>>> tensor([[ 0.3607, -0.2859, -0.3938],
[ 0.2429, -1.3833, -2.3134]])
随机数种子: 100
3)创建0-1张量
  • torch.ones 和 torch.ones_like 创建全1张量
  • torch.zeros 和 torch.zeros_like 创建全0张量
  • torch.full 和 torch.full_like 创建全为指定值张量

1、torch.ones()、torch.ones_like() 创建全1张量

1
2
3
4
5
6
7
8
9
10
11
# 1. 创建指定形状全0张量
data = torch.zeros(2, 3)
print(data)
>>> tensor([[0., 0., 0.],
[0., 0., 0.]])

# 2. 根据张量形状创建全0张量
data = torch.zeros_like(data)
print(data)
>>> tensor([[0., 0., 0.],
[0., 0., 0.]])

2、torch.zeros()、torch.zeros_like() 创建全0张量

1
2
3
4
5
6
7
8
9
10
11
# 1. 创建指定形状全1张量
data = torch.ones(2, 3)
print(data)
>>> tensor([[1., 1., 1.],
[1., 1., 1.]])

# 2. 根据张量形状创建全1张量
data = torch.ones_like(data)
print(data)
>>> tensor([[1., 1., 1.],
[1., 1., 1.]])

3、torch.full()、torch.full_like() 创建全为指定值张量

1
2
3
4
5
6
7
8
9
10
11
# 1. 创建指定形状指定值的张量
data = torch.full([2, 3], 10)
print(data)
>>> tensor([[10, 10, 10],
[10, 10, 10]])

# 2. 根据张量形状创建指定值的张量
data = torch.full_like(data, 20)
print(data)
>>> tensor([[20, 20, 20],
[20, 20, 20]])
4)张量元素类型转换
  • data.type(torch.DoubleTensor)
  • data.double()

1、data.type(torch.DoubleTensor)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
data = torch.full([2, 3], 10)
print(data.dtype)
>>> torch.int64

# 将 data 元素类型转换为 float64 类型
data = data.type(torch.DoubleTensor)
print(data.dtype)
>>> torch.float64

# 转换为其他类型
# data = data.type(torch.ShortTensor) # int16
# data = data.type(torch.IntTensor) # int32
# data = data.type(torch.LongTensor) # int64
# data = data.type(torch.FloatTensor) # float32

2、data.double()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
data = torch.full([2, 3], 10)
print(data.dtype)
>>> torch.int64

# 将 data 元素类型转换为 float64 类型
data = data.double()
print(data.dtype)
>>> torch.float64

# 转换为其他类型
# data = data.short()
# data = data.int()
# data = data.long()
# data = data.float()
5)总结

<1> 创建张量的方式

  • torch.tensor() 根据指定数据创建张量
  • torch.Tensor() 根据形状创建张量, 其也可用来创建指定数据的张量
  • torch.IntTensor()、torch.FloatTensor()、torch.DoubleTensor() 创建指定类型的张量

<2> 创建线性和随机张量

  • torch.arrange() 和 torch.linspace() 创建线性张量
  • torch.random.initial_seed() 和 torch.random.manual_seed() 随机种子设置
  • torch.randn() 创建随机张量

<3> 创建01张量

  • torch.ones() 和 torch.ones_like() 创建全1张量
  • torch.zeros() 和 torch.zeros_like() 创建全0张量
  • torch.full() 和 torch.full_like() 创建全为指定值张量

<4> 张量元素类型转换

  • data.type(torch.DoubleTensor)
  • data.double()

2. 张量的类型转换

1)张量转换为NUMPY数组
  • 使用 Tensor.numpy 函数可以将张量转换为 ndarray 数组,但是共享内存,可以使用 copy 函数避免共享。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 1. 将张量转换为 numpy 数组
data_tensor = torch.tensor([2, 3, 4])
# 使用张量对象中的 numpy 函数进行转换
data_numpy = data_tensor.numpy()
print(type(data_tensor))
>>> <class 'torch.Tensor'>

print(type(data_numpy))
>>> <class 'numpy.ndarray'>

# 注意: data_tensor 和 data_numpy 共享内存
# 修改其中的一个,另外一个也会发生改变
# data_tensor[0] = 100
data_numpy[0] = 100
print(data_tensor)
>>> tensor([100, 3, 4])

print(data_numpy)
>>> [100 3 4]

# 2. 对象拷贝避免共享内存
data_tensor = torch.tensor([2, 3, 4])
# 使用张量对象中的 numpy 函数进行转换,通过copy方法拷贝对象
data_numpy = data_tensor.numpy().copy()
print(type(data_tensor))
>>> <class 'torch.Tensor'>

print(type(data_numpy))
>>> <class 'numpy.ndarray'>

# 注意: data_tensor 和 data_numpy 此时不共享内存
# 修改其中的一个,另外一个不会发生改变
# data_tensor[0] = 100
data_numpy[0] = 100
print(data_tensor)
>>> tensor([2, 3, 4])

print(data_numpy)
>>> [100 3 4]
2)NUMPY数组转换为张量
  • 使用 from_numpy 可以将 ndarray 数组转换为 Tensor,默认共享内存,使用 copy 函数避免共享。
1
2
3
4
5
6
7
8
9
10
11
12
13
data_numpy = np.array([2, 3, 4])
# 将 numpy 数组转换为张量类型
# 1. from_numpy
# 2. torch.tensor(ndarray)
data_tensor = torch.from_numpy(data_numpy)
# nunpy 和 tensor 共享内存
# data_numpy[0] = 100
data_tensor[0] = 100
print(data_tensor)
>>> tensor([100, 3, 4], dtype=torch.int32)

print(data_numpy)
>>> [100 3 4]
  • 使用 torch.tensor 可以将 ndarray 数组转换为 Tensor,默认不共享内存
1
2
3
4
5
6
7
8
9
10
data_numpy = np.array([2, 3, 4])
data_tensor = torch.tensor(data_numpy)
# nunpy 和 tensor 不共享内存
# data_numpy[0] = 100
data_tensor[0] = 100
print(data_tensor)
>>> tensor([100, 3, 4], dtype=torch.int32)

print(data_numpy)
>>> [2 3 4]
3)标量张量和数字转换
  • 对于只有一个元素的张量,使用item()函数将该值从张量中提取出来
1
2
3
4
5
6
7
8
# 当张量只包含一个元素时, 可以通过 item() 函数提取出该值
data = torch.tensor([30,])
print(data.item())
>>> 30

data = torch.tensor(30)
print(data.item())
>>> 30
4)总结

1. 张量转换为 numpy 数组

  • data_tensor.numpy()
  • data_tensor.numpy().copy()

2. numpy 转换为张量

  • torch.from_numpy(data_numpy)
  • torch.tensor(data_numpy)

3. 标量张量和数字转换

  • data.item()

Tensor基础1——张量的创建与转换
https://linxkon.github.io/Tensor基础1-张量的创建与转换.html
作者
linxkon
发布于
2021年2月11日
许可协议