torch 创建
pytorch
本文字数:1k 字 | 阅读时长 ≈ 5 min

torch 创建

pytorch
本文字数:1k 字 | 阅读时长 ≈ 5 min

1. 创建 tensor

1.1 指定 tensor

tensor = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(tensor, '\n', tensor.dtype)
'''
tensor([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]]) 
 torch.int64
'''


tensor = torch.tensor([[1.1, 2.2, 3.3], [4.4, 5.5, 6.6], [7.7, 8.8, 9.9]])
print(tensor, '\n', tensor.dtype)
'''
tensor([[1.1000, 2.2000, 3.3000],
        [4.4000, 5.5000, 6.6000],
        [7.7000, 8.8000, 9.9000]]) 
 torch.float32
'''


tensor = torch.tensor([[1.1, 2.2, 3.3], [4.4, 5.5, 6.6], [7.7, 8.8, 9.9]])
tensor = tensor.type(torch.float64)
print(tensor, '\n', tensor.dtype)
'''
tensor([[1.1000, 2.2000, 3.3000],
        [4.4000, 5.5000, 6.6000],
        [7.7000, 8.8000, 9.9000]], dtype=torch.float64) 
 torch.float64
'''

1.2 创建特殊类型的 tensor

(1)空 tensor(empty)

torch.empty(size)

返回一个未初始化的 tensor(初始化比较随意)

Returns a tensor filled with uninitialized data. The shape of the tensor is defined by the variable argument size.

tensor = torch.empty((2, 3))
print(tensor)
'''
tensor([[1.2332e+34, 4.5827e-41, 0.0000e+00],
        [0.0000e+00, 0.0000e+00, 0.0000e+00]])
'''


tensor = torch.tensor([[0, 0, 0],[0, 0, 0.]])
print(tensor)
'''
tensor([[0., 0., 0.],
        [0., 0., 0.]])
'''

(2)全为一(ones)

torch.ones(size)

返回一个全为 1 的 tensor

Returns a tensor filled with the scalar value 1, with the shape defined by the variable argument size.

tensor = torch.ones((2, 3))
print(tensor)
'''
tensor([[1., 1., 1.],
        [1., 1., 1.]])
'''

(3)全为零(zeros)

torch.ones(size)

返回一个全为 0 的 tensor

Returns a tensor filled with the scalar value 0, with the shape defined by the variable argument size.

tensor = torch.zeros((2, 3))
print(tensor)
'''
tensor([[0., 0., 0.],
        [0., 0., 0.]])
'''

(4)均匀分布(rand)

torch.rand(size)

返回符合$[0, 1)$的均匀分布:torch.float32

Returns a tensor filled with random numbers from a uniform distribution on the interval [0, 1)

tensor = torch.rand(4)
print(tensor)
'''
tensor([0.1069, 0.4704, 0.1034, 0.8400])
'''


tensor = torch.rand((2, 3))
print(tensor)
print(tensor.type(), tensor.dtype)
'''
tensor([[0.8729, 0.0053, 0.2537],
        [0.5580, 0.3945, 0.5647]])
torch.FloatTensor torch.float32
'''

(5)正态分布(randn)

torch.randn(size)

返回$out~N(0,1)$的正态分布:torch.float32

Returns a tensor filled with random numbers from a normal distribution with mean 0 and variance 1 (also called the standard normal distribution).

tensor = torch.randn(4)
print(tensor)
'''
tensor([ 2.4390, -1.1983,  0.1438,  1.4247])
'''


tensor = torch.randn((2, 3))
print(tensor)
print(tensor.type(), tensor.dtype)
'''
tensor([[-0.2153,  1.9469, -1.0524],
        [-1.2857,  0.3539, -0.2951]])
torch.FloatTensor torch.float32
'''

(6)整数范围(randint)

torch.randint(low=0, high, size)

返回随机 tensor,每一项的取值范围为$[low, high)$:torch.int64

Returns a tensor filled with random integers generated uniformly between low (inclusive) and high (exclusive).

tensor = torch.randint(3, 5, (3,))
print(tensor)
'''
tensor([3, 4, 4])
'''


tensor = torch.randint(10, (2, 2))
print(tensor)
'''
tensor([[0, 9],
        [6, 2]])
'''


tensor = torch.randint(3, 10, (2, 2))
print(tensor)
print(tensor.type(), tensor.dtype)
'''
tensor([[3, 3],
        [7, 3]])
torch.LongTensor torch.int64
'''

(7)N 范围内的随机序列(randperm)

torch.randperm(n)

返回$[0,n)$之间的随机 tensor 序列:torch.int64

Returns a random permutation of integers from 0 to n - 1.

可以用 torch.randperm() 来将 tensor 打乱顺序,如下所示:x 输出按照 [2, 3, 0, 1] 排列

tensor = torch.randperm(4)
print(tensor)
print(tensor.type(), tensor.dtype)
'''
tensor([2, 3, 0, 1])
torch.LongTensor torch.int64
'''


x = torch.randn(4, 2)
print(x)
print(x[tensor])
'''
tensor([[ 1.0033, -0.3932],
        [-0.1972, -0.0260],
        [-1.7497, -1.8479],
        [ 1.3488, -1.1916]])
tensor([[-1.7497, -1.8479],
        [ 1.3488, -1.1916],
        [ 1.0033, -0.3932],
        [-0.1972, -0.0260]])
'''

(8)全是 value 的 tensor(full)

torch.full(size, full_value)

创建一个 tensor,他的值全是 value

Creates a tensor of size size filled with fill_value. The tensor’s dtype is inferred from fill_value.

tensor = torch.full((2, 3), 2.649)
print(tensor)
'''
tensor([[2.6490, 2.6490, 2.6490],
        [2.6490, 2.6490, 2.6490]])
'''

3. 创建于当前 tensor 相同大小的 tensor

假如有一个 tensor1,我们想创建一个和他一样大小的 tensor2,PyTorch 也给我们提供了 *_like 函数,其中的星号可以为上述讲过的 ones,zeros 等

例如我们的 tensor1 大小为(2, 3),我们想创建一个大小为(2 ,3)并且是全 1 的 tensor,就可以使用 ones_like() 函数

tensor1 = torch.tensor([[1, 2, 3],[4, 5, 6]])
tensor2 = torch.ones_like(tensor1)
print(tensor2)
'''
tensor([[1, 1, 1],
        [1, 1, 1]])
'''

与上述相同的还有 torch.empty_like()torch.ones_like()torch.zeros_like()torch.rand_like()torch.randn_like()torch.randint_like(),使用方法原函数相同,不同的地方在于 size 换成要与之相匹配的 tensor 即可

4月 06, 2025
3月 10, 2025
12月 31, 2024