Pytorch v0.4.1发布:添加频谱范数,自适应Softmax,优化CPU处理速度,添加异常检测NaN等

Pytorch v0.4.1发布:添加频谱范数,自适应Softmax,优化CPU处理速度,添加异常检测(NaN等)以及支持Python 3.7和CUDA 9.2支持

一、目录

  • 突破性的变化
  • 新功能
    • 神经网络
      • 自适应Softmax,频谱范数等
    • Operators
      • torch.bincount,torch.as_tensor,...
    • torch.distributions
      • 半柯西分布,伽马采样,......
    • 其他
      • 自动异常检测(检测NaN等)
  • 性能
    • 在各种情况下CPU操作速度比以前更快
  • 其他改进
  • Bug修复
  • 文档改进

二、突破性变化

  • torch.stft已将其签名更改为与librosa 一致#9497
    • 以前: stft(signal, frame_length, hop, fft_size=None, normalized=False, onesided=True, window=None, pad_end=0)
    • 以后: stft(input, n_fft, hop_length=None, win_length=None, window=None, center=True, pad_mode='reflect', normalized=False, onesided=True)
    • torch.stft现在也在内部使用FFT并且速度更快。
  • torch.slice删除有利于张量切片符号#7924
  • torch.arange现在做dtype推论:任何浮点参数都被推断为默认值dtype; 所有整数参数都被推断为int64#7016
  • torch.nn.functional.embedding_bag不推荐使用旧签名embedding_bag(weight, input, ...),而应使用embedding_bag(input, weight, ...)(与torch.nn.functional.embedding一致)
  • torch.nn.functional.sigmoidtorch.nn.functional.tanh不支持取代torch.sigmoidtorch.tanh #8748

  • 广播行为在(非常罕见的)边缘情况下发生变化:[1] x [0]现在广播到[0](以前是[1]#9209

三、新功能

1、神经网络

  • 自适应nn.AdaptiveLogSoftmaxWithLoss #5287
>>> in_features = 1000
>>> n_classes = 200
>>> adaptive_softmax = nn.AdaptiveLogSoftmaxWithLoss(in_features, n_classes, cutoffs=[20, 100, 150])
>>> adaptive_softmax
AdaptiveLogSoftmaxWithLoss(
  (head): Linear(in_features=1000, out_features=23, bias=False)
  (tail): ModuleList(
    (0): Sequential(
      (0): Linear(in_features=1000, out_features=250, bias=False)
      (1): Linear(in_features=250, out_features=80, bias=False)
    )
    (1): Sequential(
      (0): Linear(in_features=1000, out_features=62, bias=False)
      (1): Linear(in_features=62, out_features=50, bias=False)
    )
    (2): Sequential(
      (0): Linear(in_features=1000, out_features=15, bias=False)
      (1): Linear(in_features=15, out_features=50, bias=False)
    )
  )
)
>>> batch = 15
>>> input = torch.randn(batch, in_features)
>>> target = torch.randint(n_classes, (batch,), dtype=torch.long)
>>> # get the log probabilities of target given input, and mean negative log probability loss
>>> adaptive_softmax(input, target) 
ASMoutput(output=tensor([-6.8270, -7.9465, -7.3479, -6.8511, -7.5613, -7.1154, -2.9478, -6.9885,
        -7.7484, -7.9102, -7.1660, -8.2843, -7.7903, -8.4459, -7.2371],
       grad_fn=<ThAddBackward>), loss=tensor(7.2112, grad_fn=<MeanBackward1>))
>>> # get the log probabilities of all targets given input as a (batch x n_classes) tensor
>>> adaptive_softmax.log_prob(input)  
tensor([[-2.6533, -3.3957, -2.7069,  ..., -6.4749, -5.8867, -6.0611],
        [-3.4209, -3.2695, -2.9728,  ..., -7.6664, -7.5946, -7.9606],
        [-3.6789, -3.6317, -3.2098,  ..., -7.3722, -6.9006, -7.4314],
        ...,
        [-3.3150, -4.0957, -3.4335,  ..., -7.9572, -8.4603, -8.2080],
        [-3.8726, -3.7905, -4.3262,  ..., -8.0031, -7.8754, -8.7971],
        [-3.6082, -3.1969, -3.2719,  ..., -6.9769, -6.3158, -7.0805]],
       grad_fn=<CopySlices>)
>>> # predit: get the class that maximize log probaility for each input
>>> adaptive_softmax.predict(input)  
tensor([ 8,  6,  6, 16, 14, 16, 16,  9,  4,  7,  5,  7,  8, 14,  3])
  • 添加光谱标准化nn.utils.spectral_norm #6929
>>> # Usage is similar to weight_norm
>>> convT = nn.ConvTranspose2d(3, 64, kernel_size=3, pad=1)
>>> # Can specify number of power iterations applied each time, or use default (1)
>>> convT = nn.utils.spectral_norm(convT, n_power_iterations=2)
>>>
>>> # apply to every conv and conv transpose module in a model
>>> def add_sn(m):
        for name, c in m.named_children():
             m.add_module(name, add_sn(c))    
         if isinstance(m, (nn.Conv2d, nn.ConvTranspose2d)):
             return nn.utils.spectral_norm(m)
         else:
             return m

>>> my_model = add_sn(my_model)
  • nn.ModuleDictnn.ParameterDict容器#8463
  • 添加nn.init.zeros_nn.init.ones_ #7488
  • 将稀疏渐变选项添加到预训练嵌入#7492
  • 将最大池支持添加到nn.EmbeddingBag #5725
  • MKLDNN的深度卷积支持 #8782
  • 添加nn.FeatureAlphaDropout(特征Alpha Dropout图层)#9073

2、Operators

  • torch.bincount (计算积分张量中每个值的频率)#6688
>>> input = torch.randint(0, 8, (5,), dtype=torch.int64)
>>> weights = torch.linspace(0, 1, steps=5)
>>> input, weights
(tensor([4, 3, 6, 3, 4]),
 tensor([ 0.0000,  0.2500,  0.5000,  0.7500,  1.0000])

>>> torch.bincount(input)
tensor([0, 0, 0, 2, 2, 0, 1])

>>> input.bincount(weights)
tensor([0.0000, 0.0000, 0.0000, 1.0000, 1.0000, 0.0000, 0.5000])
  • torch.as_tensor (torch.tensor除非必要,否则类似但永远不会复制)#7109
>>> tensor = torch.randn(3, device='cpu', dtype=torch.float32)
>>> torch.as_tensor(tensor)                       # doesn't copy
>>> torch.as_tensor(tensor, dtype=torch.float64)  # copies due to incompatible dtype
>>> torch.as_tensor(tensor, device='cuda')        # copies due to incompatible device
>>> array = np.array([3, 4.5])
>>> torch.as_tensor(array)                        # doesn't copy, sharing memory with the numpy array
>>> torch.as_tensor(array, device='cuda')         # copies due to incompatible device

3、Distributions

  • 半柯西分布和半常态#8411
  • CUDA张量的伽马采样#6855
  • 允许二项分布中的矢量化计数#6720

4、Misc

  • Autograd自动异常检测NaN和向后发生的错误。为此提供了两个函数detect_anomalyset_detect_anomaly#7677
  • 支持reversed(torch.Tensor) #9216
  • 支持hash(torch.device) #9246
  • 在torch.load支持gzip #6490

5、性能

  • 加速CPU上的bernoulli数生成#7171
  • 启用cuFFT计划缓存(在某些情况下加速80%)#8344
  • 修复中不必要的复制bernoulli_ #8682
  • 修复中不必要的复制broadcast #8222
  • 加速multidim sum(在某些情况下加速2x~6x)#8992
  • 矢量化CPU sigmoid(在大多数情况下加速> 3倍)#8612
  • 优化CPU nn.LeakyReLU和nn.PReLU(2倍加速)#9206
  • Vectorize softmax和logsoftmax(单核上4.5倍速,10线上1.8倍)#7375
  • 加速nn.init.sparse(10-20倍加速)#6899

6、改进

a、打印张量

  • 打印张量现在包括requires_gradgrad_fn信息#8211
  • 改善张量打印中的数字格式#7632
  • 在打印一些张量时修正比例#7189
  • 加快大张量的打印#6876

b、神经网络

  • NaN现在通过许多激活函数传播#8033
  • 添加non_blocking选项到nn.Module.to #7312
  • 损耗模块现在允许目标需要梯度#8460
  • 添加pos_weight参数到nn.BCEWithLogitsLoss #6856
  • 支持grad_clip不同设备上的参数#9302
  • 删除了pad_sequence必须对输入序列进行排序的要求#7928
  • stride论据max_unpool1d,max_unpool2d,max_unpool3d现在默认为kernel_size #7388
  • 允许调用grad模式上下文管理器(例如torch.no_grad,torch.enable_grad)作为装饰器#7737
  • torch.optim.lr_scheduler._LRSchedulers getstate包括优化器信息 #7757
  • 添加对函数中接受Tensor输入的支持clipgrad#7769
  • 返回NaN在max_pool/ adaptive_max_pool为NaN输入#7670
  • nn.EmbeddingBag现在可以在所有模式下处理空袋#7389
  • torch.optim.lr_scheduler.ReduceLROnPlateau现在可序列化#7201
  • 仅允许浮点dtype的张量需要渐变#7034#7185
  • 允许重置BatchNorm运行统计数据和累积移动平均值#5766
  • LP-Pool如果所有输入元素与p的幂之和为零,则将梯度设置为零#6766

c、Operators

  • 将省略号('...')和对角线(例如'ii→i')添加到torch.einsum #7173
  • 添加to方法到PackedSequence #7319
  • 对整体的张量添加__floordiv____rdiv__ #7245
  • torch.clamp现在在最小和最大处有次级梯度1 #7049
  • torch.arange 现在使用NumPy风格的类型:#7016
  • torch.normtorch.renorm中正确支持无限范围 #6969
  • 允许经由传递一个输出张量out=关键字arugmenttorch.dottorch.matmul #6961

d、分布

  • 在计算时始终启用gradlazy_property #7708

e、稀疏张量

  • 为稀疏张量添加log1p #8969
  • 更好地支持添加零填充稀疏张量 #7479

f、数据并行

  • 允许在中返回标量的模块nn.DataParallel [#7973]()
  • 允许nn.parallel.parallel_apply接受list/tuple张量 #8047

g、Misc

  • torch.Size现在可以接受PyTorch标量 #5676
  • 移动torch.utils.data.dataset.random_splittorch.utils.data.random_split,并torch.utils.data.dataset.Subset转至torch.utils.data.Subset #7816
  • torch.device添加序列化#7713
  • 允许torch.(int/float/...)*copy.deepcopy对象#7699
  • torch.load现在可以采取torch.device map位置#7339


原创文章,转载请注明 :Pytorch v0.4.1发布:添加频谱范数,自适应Softmax,优化CPU处理速度,添加异常检测(NaN等)以及支持Python 3.7和CUDA 9.2支持 - pytorch中文网
原文出处: https://ptorch.com/news/197.html

猜你喜欢

转载自blog.csdn.net/jacke121/article/details/82932378