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
删除有利于张量切片符号#7924torch.arange
现在做dtype
推论:任何浮点参数都被推断为默认值dtype
; 所有整数参数都被推断为int64
。#7016torch.nn.functional.embedding_bag
不推荐使用旧签名embedding_bag(weight, input, ...)
,而应使用embedding_bag(input, weight, ...)
(与torch.nn.functional.embedding
一致)-
torch.nn.functional.sigmoid
和torch.nn.functional.tanh
不支持取代torch.sigmoid
和torch.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.ModuleDict
和nn.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
- torch.randperm对于CUDA张量#7606
- nn.HardShrink对于CUDA张量#8117
- torch.flip (沿指定的dims翻转张量)#7873
- torch.flatten (使连续的角度变平)#8578
- torch.pinverse (计算基于svd的伪逆)#9052
- torch.meshgrid #8581
- torch.unique 对于CUDA张量#8899
- torch.erfc (补充错误功能)https://github.com/pytorch/pytorch/pull/9366/files
- torch.isinf 和torch.isfinite #9169 #9487
- torch.reshape_as #9452
- 在
torch.nn.functional.kl_div
中支持目标张量后向 #7839 - torch.logsumexp #7254
- 将批量线性求解器添加到torch.gesv #6100
- torch.sum 现在支持多个维度的求和: https://github.com/pytorch/pytorch/pull/6152/files
torch.diagonal
,torch.diagflat
采取任意对角线使用numpy
语义#6718tensor.any
和tensor.all
在ByteTensor
现在可以接受dim
和keepdim
参数#4627
3、Distributions
4、Misc
Autograd
自动异常检测NaN
和向后发生的错误。为此提供了两个函数detect_anomaly
和set_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、打印张量
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.norm
和torch.renorm
中正确支持无限范围 #6969 - 允许经由传递一个输出张量
out=
关键字arugment
在torch.dot
和torch.matmul
#6961
d、分布
- 在计算时始终启用
gradlazy_property
#7708
e、稀疏张量
f、数据并行
- 允许在中返回标量的模块nn.DataParallel [#7973]()
- 允许
nn.parallel.parallel_apply
接受list/tuple
张量 #8047
g、Misc
torch.Size
现在可以接受PyTorch
标量 #5676- 移动
torch.utils.data.dataset.random_split
到torch.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