在机器翻译中,我们总是需要切掉注释和预测中的第一个时间步(SOS标记)
使用batch_first=False
时,切掉第一个时间步仍然保持张量连续
import torch
batch_size = 128
seq_len = 12
embedding = 50
# Making a dummy output that is `batch_first=False`
batch_not_first = torch.randn((seq_len,batch_size,embedding))
batch_not_first = batch_first[1:].view(-1, embedding) # slicing out the first time step
然而,如果我们使用batch_first=True
,在切片之后,张量不再是连续的。我们需要先使其连续,然后才能执行不同的操作,例如view
batch_first = torch.randn((batch_size,seq_len,embedding))
batch_first[:,1:].view(-1, embedding) # slicing out the first time step
output>>>
"""
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-8-a9bd590a1679> in <module>
----> 1 batch_first[:,1:].view(-1, embedding) # slicing out the first time step
RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.
"""
这是否意味着batch_first=False
至少在机器翻译环境中更好?因为它避免了我们执行contiguous()
步骤。是否有batch_first=True
效果更好的案例
演出
在
batch_first=True
和batch_first=False
之间似乎没有太大的区别。请参阅下面的脚本:在我的设备(GTX 1050 Ti)、PyTorch
1.6.0
和CUDA 11.0上,以下是结果:(这两种情况各不相同,因此没有任何结论)
代码可读性
当您想要使用需要} )的情况)
batch
作为第0
维度的其他PyTorch层时batch_first=True
更简单(这是几乎所有torch.nn
层(如^{在这种情况下,如果指定了
batch_first=False
,您将不得不permute
返回张量机器翻译
它应该更好,因为
tensor
一直是连续的,不需要进行数据拷贝。使用[1:]
而不是[:,1:]
进行切片看起来也更干净编程相关推荐