本文目录
创建tensor
1. 指定tensor
- 如果tensor都是整形,默认创建的都是
torch.int64
- 如果tensor有一个浮点型,创建的就是
torch.float32
- 也可以用
tensor.type()
来改变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
'''
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 ) [0, 1) [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)
返回 o u t ~ N ( 0 , 1 ) out~N(0,1) 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,每一项的取值范围为 [ l o w , h i g h ) [low, high) [low,high):torch.int64
Returns a tensor filled with random integers generated uniformly between
low
(inclusive) andhigh
(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 ) [0,n) [0,n)之间的随机tensor序列:torch.int64
Returns a random permutation of integers from
0
ton - 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即可