PyTorch:tensor常用 *** 作

PyTorch:tensor常用 *** 作,第1张

PyTorch:tensor常用 *** 作

本文目录
    • 一、创建tensor
      • 1. 指定tensor
      • 2. 创建特殊类型的tensor
        • (1)均匀分布
        • (2)正态分布
        • (3)整数范围
        • (4)N范围内的随机序列
    • 二、Tensor.expand()
    • 三、torch.cat()
    • 四、torch.split()
    • 五、torch.chunk()

一、创建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)均匀分布

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
'''
(2)正态分布

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
'''
(3)整数范围

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) 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
'''
(4)N范围内的随机序列

torch.randperm(n)

返回 [ 0 , n ) [0,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]])
'''
二、Tensor.expand()

Tensor.expand(sizes)

将tensor进行扩展,size为扩展后的维度,-1表示对这一维度不进行拓展

Returns a new view of the self tensor with singleton dimensions expanded to a larger size.

Passing -1 as the size for a dimension means not changing the size of that dimension.

Tensor can be also expanded to a larger number of dimensions, and the new ones will be appended at the front. For the new dimensions, the size cannot be set to -1.

Expanding a tensor does not allocate new memory, but only creates a new view on the existing tensor where a dimension of size one is expanded to a larger size by setting the stride to 0. Any dimension of size 1 can be expanded to an arbitrary value without allocating new memory.

x = torch.tensor([[1], [2], [3]])
print(x)
print(x.size())
'''
tensor([[1],
        [2],
        [3]])
torch.Size([3, 1])
'''


x_ex = x.expand(3, 4)
print(x_ex)
x_ex2 = x.expand(-1, 4)   # -1 means not changing the size of that dimension
print(x_ex2)
'''
tensor([[1, 1, 1, 1],
        [2, 2, 2, 2],
        [3, 3, 3, 3]])
tensor([[1, 1, 1, 1],
        [2, 2, 2, 2],
        [3, 3, 3, 3]])
'''
三、torch.cat()

torch.cat(tensors, dim=0)

将tensor进行拼接,拼接的维度根据dim设置,默认为0(行拼接)

Concatenates the given sequence of seq tensors in the given dimension. All tensors must either have the same shape (except in the concatenating dimension) or be empty.

torch.cat() can be seen as an inverse operation for torch.split() and torch.chunk()

x = torch.randn(2, 3)
print(x)
'''
tensor([[ 0.7004, -0.0935, -0.2668],
        [ 0.7922,  0.9567,  1.4191]])
'''

x_row = torch.cat((x, x, x), 0)
print(x_row)
x_col = torch.cat((x, x), 1)
print(x_col)
'''
tensor([[ 0.7004, -0.0935, -0.2668],
        [ 0.7922,  0.9567,  1.4191],
        [ 0.7004, -0.0935, -0.2668],
        [ 0.7922,  0.9567,  1.4191],
        [ 0.7004, -0.0935, -0.2668],
        [ 0.7922,  0.9567,  1.4191]])
tensor([[ 0.7004, -0.0935, -0.2668,  0.7004, -0.0935, -0.2668],
        [ 0.7922,  0.9567,  1.4191,  0.7922,  0.9567,  1.4191]])
'''
四、torch.split()

torch.split(tensor, split_size_or_sections, dim=0)

将tensor进行切片,split_size_or_sections表示切片的大小,可以为整型或者列表,dim为切片维度,默认为0对行进行切片

Splits the tensor into chunks. Each chunk is a view of the original tensor.

If split_size_or_sections is an integer type, then tensorwill be split into equally sized chunks (if possible). Last chunk will be smaller if the tensor size along the given dimension dim is not divisible by split_size.

If split_size_or_sections is a list, then tensor will be split into len(split_size_or_sections) chunks with sizes in dim according to split_size_or_sections.

a = torch.arange(10).reshape(5,2)
print(a)
'''
tensor([[0, 1],
        [2, 3],
        [4, 5],
        [6, 7],
        [8, 9]])
'''


a1 = torch.split(a, 2)
print(a1)
print(a1[1]) # 因为分为了三组,所以可以选择任一项输出
'''
(tensor([[0, 1],
         [2, 3]]), 
 tensor([[4, 5],
         [6, 7]]), 
 tensor([[8, 9]]))
tensor([[4, 5],
        [6, 7]])
'''


a2 = torch.split(a, [1,4])
print(a2)
'''
(tensor([[0, 1]]), 
 tensor([[2, 3],
         [4, 5],
         [6, 7],
         [8, 9]]))
'''
五、torch.chunk()

torch.chunk(tensor, chunks, dim=0)

  • input - [Tensor] – the tensor to split
  • chunks - [int] – number of chunks to return
  • dim - [int] – dimension along which to split the tensor

torch.chunk()与torch.split()功能完全一致,唯一的区别是参数chunks输入只能是整型

Splits a tensor into a specific number of chunks. Each chunk is a view of the input tensor.

Last chunk will be smaller if the tensor size along the given dimension dim is not divisible by chunks.

b = torch.arange(10).reshape(2,5)
print(b)
'''
tensor([[0, 1, 2, 3, 4],
        [5, 6, 7, 8, 9]])
'''


b1 = torch.chunk(b, 2, dim=1)
print(b1)
print(b1[1])
'''
(tensor([[0, 1, 2],
         [5, 6, 7]]), 
 tensor([[3, 4],
         [8, 9]]))
tensor([[3, 4],
        [8, 9]])
'''


b2 = torch.chunk(b, 2, dim=1)
print(b2)
'''
(tensor([[0, 1, 2],
         [5, 6, 7]]), 
 tensor([[3, 4],
         [8, 9]]))
'''

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/3972953.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-10-21
下一篇2022-10-21

发表评论

登录后才能评论

评论列表(0条)

    保存