pytroch基础用法

torch.randn(size)

torch.randn:用来生成随机数字的tensor,这些随机数字满足标准正态分布(0~1
import torch
a=torch.randn(3)
b=torch.randn(3,4)
print("a:",a)
print("b:",b)
a: tensor([ 0.9405, -0.1068,  0.1712])
b: tensor([[-1.0962, -0.1893,  1.2323,  0.5703],
        [-1.5256, -1.4923,  0.4275,  0.5143],
        [ 1.1200,  0.5317,  1.1961, -2.2533]])

torch.full(size, fill_value)

import torch
a = torch.full([2,3],2.0)
print("a:", a)
a: tensor([[2., 2., 2.],
        [2., 2., 2.]])

troch.arange() / torch.range()
在这里插入图片描述
torch.linspace()/torch.logspace()
在这里插入图片描述

torch.ones()/torch.zeros()/torch.eye()

torch.eye(): 输出3 x 3的矩阵 对角线全为1

在这里插入图片描述
torch.randperm()

0~n-1(包括0和n-1)随机打乱后获得的数字序列

在这里插入图片描述

维度变换

View reshape
在这里插入图片描述
torch.unsqueeze()

torch.unsqueeze(input, dim, out=None)
作用:扩展维度
返回一个新的张量,对输入的既定位置插入维度 1

在这里插入图片描述
torch.squeeze()

对数据的维度进行压缩
torch.squeeze(input, dim=None, out=None)

在这里插入图片描述
torch.expand()

  expand()函数的功能是用来扩展张量中某维数据的尺寸,它返回输入张量在某维扩展为更大尺寸后的张量。
  扩展张量不会分配新的内存,只是在存在的张量上创建一个新的视图(关于张量的视图可以参考博文:由浅入深地分析张量),而且原始tensor和处理后的tensor是不共享内存的。
  expand()函数括号中的输入参数为指定经过维度尺寸扩展后的张量的size。
(1)# 在行上更改
import torch
a = torch.tensor([1, 2, 3])   # C:一行三列
c = a.expand(2, 3) # 将C进行扩为:两行三列
print(a)
print(c)
# 输出信息:
tensor([1, 2, 3])
tensor([[1, 2, 3],
        [1, 2, 3]]
(2) # 在列上更改
import torch
a = torch.tensor([[1], [2], [3]])  # C:三行一列
print(a.size())
c = a.expand(3, 3)  # 将C进行扩为:三行三列
print(a)
print(c)
# 输出信息:
torch.Size([3, 1])
tensor([[1],
        [2],
        [3]])
tensor([[1, 1, 1],
        [2, 2, 2],
        [3, 3, 3]])

torch.repeat()

PyTorch中的repeat()函数可以对张量进行复制。
当参数只有两个时,第一个参数表示的是复制后的列数,第二个参数表示复制后的行数。
当参数有三个时,第一个参数表示的是复制后的通道数,第二个参数表示的是复制后的列数,第三个参数表示复制后的行数。
>>> x = torch.tensor([6,7,8])
>>> x.repeat(4,2)
tensor([[6, 7, 8, 6, 7, 8],
        [6, 7, 8, 6, 7, 8],
        [6, 7, 8, 6, 7, 8],
        [6, 7, 8, 6, 7, 8]])
>>> x.repeat(4,2,1)
tensor([[[6, 7, 8],
         [6, 7, 8]],

        [[6, 7, 8],
         [6, 7, 8]],

        [[6, 7, 8],
         [6, 7, 8]],

        [[6, 7, 8],
         [6, 7, 8]]])
>>> x.repeat(4,2,1).size()
torch.Size([4, 2, 3])

torch.permute()

维度变化

在这里插入图片描述
torch.transpose()

transpose可以理解为矩阵转置概念在高维数组上的扩展
from torch import Tensor
import numpy as np
x = np.random.rand(3, 2, 5, 6)
z = Tensor(x)
# z.shape is (3, 2, 5, 6)
print(z.transpose(2, 3).shape)
torch.Size([3, 2, 6, 5])

torch.t()

torch.t()是一个类似于求矩阵的转置的函数,但是它要求输入的tensor结构维度<=2D
>>> x = torch.randn(()) ##0D
>>> x
tensor(0.1995)
>>> torch.t(x)
tensor(0.1995)
>>> x = torch.randn(3) ## 1D
>>> x
tensor([ 2.4320, -0.4608,  0.7702])
>>> torch.t(x)  ### torch version =1.0.1 报错
tensor([.2.4320,.-0.4608,..0.7702])
>>> x = torch.randn(2, 3)  ##2D size(2,3)
>>> x
tensor([[ 0.4875,  0.9158, -0.5872],
        [ 0.3938, -0.6929,  0.6932]])
>>> torch.t(x)  ## size(3,2)
tensor([[ 0.4875,  0.3938],
        [ 0.9158, -0.6929],
        [-0.5872,  0.6932]])

拼接与拆分

torch.cat()
在这里插入图片描述
在这里插入图片描述
torch.stack()
在这里插入图片描述
torch.cat()

dim控制拼接的维度,dim=0为上下拼接,dim=1为左右拼接[1
>>> import torch
>>> a = torch.rand((2,3))
>>> a
tensor([[0.7515, 0.1021, 0.0726],
        [0.0575, 0.1666, 0.2763]])
>>> b = torch.rand((2,3))
>>> b
tensor([[0.7485, 0.8340, 0.2617],
        [0.7847, 0.2847, 0.3445]])
>>> c =torch.cat((a,b),dim=0)
>>> c
tensor([[0.7515, 0.1021, 0.0726],
        [0.0575, 0.1666, 0.2763],
        [0.7485, 0.8340, 0.2617],
        [0.7847, 0.2847, 0.3445]])
>>> d = torch.cat((a,b),dim=1)
>>> d
tensor([[0.7515, 0.1021, 0.0726, 0.7485, 0.8340, 0.2617],
        [0.0575, 0.1666, 0.2763, 0.7847, 0.2847, 0.3445]])

torch.split()

import torch
x = torch.randn(1, 2, 4, 4)
y = torch.split(x, 1, dim=2)  # 每块大小为1
# print(x[0])
for i in y:
    print(i.size())

a = torch.rand(1, 4, 8, 6)
b = torch.split(a, 2, dim=1)  # 每块大小为2
for i in b:
    print(i.size())
torch.Size([1, 2, 1, 4])
torch.Size([1, 2, 1, 4])
torch.Size([1, 2, 1, 4])
torch.Size([1, 2, 1, 4])
torch.Size([1, 2, 8, 6])
torch.Size([1, 2, 8, 6])

torch.chunk()

import torch
a=torch.tensor([[[1,2],[3,4]],
               [[5,6],[7,8]]])
print(a.size())
b=torch.chunk(a,2,1)
print("a:", a)
print("b:", b)
torch.Size([2, 2, 2])
a: tensor([[[1, 2],
         [3, 4]],
        [[5, 6],
         [7, 8]]])
b: (tensor([[[1, 2]],
        [[5, 6]]]), tensor([[[3, 4]],
        [[7, 8]]]))

torch.add()/ +
在这里插入图片描述
torch.matmul()/troch.mm()/@
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值