“Pytorch”的版本间差异

来自Shiyin's note
跳到导航 跳到搜索
无编辑摘要
第15行: 第15行:
:orch.stack()沿着一个新维度对输入张量序列进行连接。 序列中所有的张量都应该为相同形状。
:orch.stack()沿着一个新维度对输入张量序列进行连接。 序列中所有的张量都应该为相同形状。
:torch.cat()是为了把函数torch.stack()得到tensor进行拼接而存在的 (不增加新的维度)
:torch.cat()是为了把函数torch.stack()得到tensor进行拼接而存在的 (不增加新的维度)
*gather
:In PyTorch you can perform the same operation using the gather() method. If s is a PyTorch Tensor of shape (N, C) and y is a PyTorch Tensor of shape (N,) containing longs in the range 0 <= y[i] < C, then s.gather(1, y.view(-1, 1)).squeeze() will be a PyTorch Tensor of shape (N,) containing one entry from each row of s, selected according to the indices in y.


==Tensor==
==Tensor==

2021年10月17日 (日) 12:45的版本

网络初始化

  • Xavier and Kaiming initialization [4]

函数

  • torch.clamp(input, min=None, max=None, *, out=None) → Tensor
Clamps all elements in input into the range [ min, max ]. Letting min_value and max_value be min and max, respectively
  • torch.eye(n, m=None, out=None)
返回一个2维张量,对角线位置全1,其它位置全0
  • torch.view()
相当于numpy的reshape,某个维度上等于-1,就是让计算机自己算一下这一维度上应该有多少
  • torch.cat & torch.stack
orch.stack()沿着一个新维度对输入张量序列进行连接。 序列中所有的张量都应该为相同形状。
torch.cat()是为了把函数torch.stack()得到tensor进行拼接而存在的 (不增加新的维度)
  • gather
In PyTorch you can perform the same operation using the gather() method. If s is a PyTorch Tensor of shape (N, C) and y is a PyTorch Tensor of shape (N,) containing longs in the range 0 <= y[i] < C, then s.gather(1, y.view(-1, 1)).squeeze() will be a PyTorch Tensor of shape (N,) containing one entry from each row of s, selected according to the indices in y.

Tensor

  • cpu() numpy() detach() item() [5]
注意cuda上面的变量类型只能是tensor,不能是其他

torchvision

  • PyTorch框架中有一个非常重要且好用的包:torchvision,该包主要由3个子包组成,分别是:torchvision.datasets、torchvision.models、torchvision.transforms [6]
  • __all__ = ["Compose", "ToTensor", "ToPILImage", "Normalize", "Resize",

"Scale", "CenterCrop", "Pad", "Lambda", "RandomCrop", "RandomHorizontalFlip", "RandomVerticalFlip", "RandomResizedCrop", "RandomSizedCrop", "FiveCrop", "TenCrop","LinearTransformation", "ColorJitter", "RandomRotation", "Grayscale", "RandomGrayscale"]

第三方库

  • thop
THOP 是 PyTorch 非常实用的一个第三方库,可以统计模型的 FLOPs 和参数量。
from thop import clever_format
from thop import profile
class YourModule(nn.Module):
   # your definition
input = torch.randn(10, 128, 128)
flops, params = profile(model, inputs=(input, ))
flops, params = clever_format([flops, params], "%.3f")