pytorch 、numpy杂记

1、torch.matmul()就是简单的矩阵乘法

2、torch.permute(inputdims) ,按照指定的维度对tensor的重新排列

>>> x = torch.randn(2, 3, 5)
>>> x.size()
torch.Size([2, 3, 5])
>>> torch.permute(x, (2, 0, 1)).size()
torch.Size([5, 2, 3]

3、torch.unsqueeze(inputdim)  在指定的位置添加维度

      dim的范围[-input.dim() - 1, input.dim() + 1)

      dim为负数的话, dim = dim + input.dim() + 1.

>>> x = torch.tensor([1, 2, 3, 4])

>>> x.unsqueeze(2).shape
torch.Size([2, 2, 1])
>>> x.unsqueeze(0).shape
torch.Size([1, 2, 2])
>>> x.unsqueeze(1).shape
torch.Size([2, 1, 2])
>>> x.shape
torch.Size([2, 2])
>>> x.unsqueeze(-3).shape
torch.Size([1, 2, 2])
>>> x.unsqueeze(-2).shape
torch.Size([2, 1, 2])
>>> x.unsqueeze(-1).shape
torch.Size([2, 2, 1])

4、inplace作用

    inplace=True指的是进行原地操作,选择进行原地覆盖运算。 比如 x+=1则是对原值x进行操作,然后将得到的结果又直接覆盖该值。y=x+5,x=y则不是对x的原地操作。
    inplace=True操作的好处就是可以节省运算内存,不用多储存其他无关变量。
    注意:当使用 inplace=True后,对于上层网络传递下来的tensor会直接进行修改,改变输入数据


5、torch.cat是将两个张量(tensor)拼接在一起

>>> x = torch.randn(2, 3,3)
>>> torch.cat([x,x],dim=0).shape
torch.Size([4, 3, 3])
>>> torch.cat([x,x],dim=1).shape
torch.Size([2, 6, 3])
>>> torch.cat([x,x],dim=2).shape
torch.Size([2, 3, 6])
>>> torch.cat([x,x],dim=-1).shape
torch.Size([2, 3, 6])
>>> torch.cat([x,x],dim=-2).shape
torch.Size([2, 6, 3])
>>> torch.cat([x,x],dim=-3).shape
torch.Size([4, 3, 3])

6、

  • type() 不会认为子类是一种父类类型,不考虑继承关系。

  • isinstance() 会认为子类是一种父类类型,考虑继承关系。

  • type() 函数如果你只有第一个参数则返回对象的类型,三个参数返回新的类型对象

class A:

pass

class B(A):

pass

isinstance(A(), A) # returns True

type(A()) == A # returns True i

sinstance(B(), A) # returns True

type(B()) == A # returns False

7、shuffle=True 

     用于打乱数据集

8、np.random.choice(a,b)

      a、b都是int型数据

     返回的是一个b维数组,数组中的元素是[0,a]范围内的数据

9、shutil.rmtree(dir) 递归地删除文件夹dir中的文件

​​​​​​​10、torch.from_numpy()方法把数组转换成张量,且二者共享内存,对张量进行修改比如重新赋值,那么原始数组也会相应发生改变。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值