“Pytorch”的版本间差异

来自Shiyin's note
跳到导航 跳到搜索
无编辑摘要
无编辑摘要
第28行: 第28行:
a = torch.Tensor([[1, 2, np.nan], [2, np.nan, 4], [3, 4, 5]])
a = torch.Tensor([[1, 2, np.nan], [2, np.nan, 4], [3, 4, 5]])
a = torch.where(torch.isnan(a), torch.full_like(a, 0), a)
a = torch.where(torch.isnan(a), torch.full_like(a, 0), a)

===数据初值归一化===
*from sklearn.preprocessing import scale,StandardScaler,其中scale可以对一维数据标准化(自动或略nan),scaler 可以对二维数据进行归一化,有fit,transform等功能



==Tensor==
==Tensor==

2021年10月23日 (六) 03:44的版本

网络初始化

  • Xavier and Kaiming initialization [4]

数据准备

import torch
import torch.utils.data as Data
torch.manual_seed(1)    # reproducible
BATCH_SIZE = 5      # 批训练的数据个数
x = torch.linspace(1, 10, 10)       # x data (torch tensor)
y = np.arange(1, 10)
y = torch.from_numpy(y) #numpy ndarray to torch tensor       
torch_dataset = Data.TensorDataset(x, y)  # 换成 torch 能识别的 Dataset
# 把 dataset 放入 DataLoader
loader = Data.DataLoader(
   dataset=torch_dataset,      # torch TensorDataset format
   batch_size=BATCH_SIZE,      # mini batch size
   shuffle=True,               # 要不要打乱数据 (打乱比较好)
   num_workers=2,              # 多线程来读数据
   sampler=tr_sampler,         #
)


nan问题

  • 第一个是判断条件,第二个是符合条件的设置值,第三个是不符合条件的设置值。
a = torch.Tensor([[1, 2, np.nan], [2, np.nan, 4], [3, 4, 5]])
a = torch.where(torch.isnan(a), torch.full_like(a, 0), a)

=数据初值归一化=

  • from sklearn.preprocessing import scale,StandardScaler,其中scale可以对一维数据标准化(自动或略nan),scaler 可以对二维数据进行归一化,有fit,transform等功能


Tensor

  • 获取 Tensor 的元素个数 ,a.numel() 等价 a.nelement()
  • 查看 Tensor 的形状,Tensor.size() 返回 torch.Size() 对象, Tensor.shape 等价于 Tensor.size()
  • 通过 tensor.view 方法可以调整 tensor 的形状,但必须保证调整前后的元素总数保持一致,view 不会修改自身的数据,返回的新 tensor 与源 tensor 共享内存,即更改其中一个,另外一个也跟着改变。
  • 添加或减少某一维度,可以使用 squeeze 和 unsqueeze 函数。
  • squeeze()删除一个张量中所有维数为1的维度
  • resize 是另一种可用来调整 size 的方法,但与 view 不同,它可以修改 tensor 的尺寸,如果新尺寸超过了源尺寸,会自动分配新的内存空间,而如果新尺寸小于源尺寸,则之前的数据依旧会被保存。
  • cpu() numpy()
res=scores.cpu().numpy()
注意cuda上面的变量类型只能是tensor,不能是其他,把一个GPU上出来的数据转换成numpy ndarry
  • detach() item() [5]


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"]


函数

  • 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.cat & torch.stack
torch.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.

第三方库

  • 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")