一些基本函数使用,在阅读pytorch代码时记录
基础函数
torch.equal(input, other) → bool
torch.Tensor.dim()
Returns the number of dimensions of self tensor.
torch.addmm(input, mat1, mat2, *, beta=1, alpha=1, out=None) → Tensor
@为矩阵乘法
torch.Tensor.t()
等价于torch.t()
含义是输入的维度小于等于2,当维度为0或1时,返回输入tensor,当维度等于2时,返回输入的转置
torch.Tensor.chunk()
等价于torch.chunk(input, chunks, dim=0)
Tensor.chunk()的输入第二个参数是切分的个数,第三个参数在哪个维度上切分,返回的是tensor list,如果不能被完全切分,最后一个tensor维度可能和前面的不一样。torch.chunk()的区别在于第一个参数是输入tensor。
torch.Tensor.to()
类型和设备转换
>>> tensor = torch.randn(2, 2) # Initially dtype=float32, device=cpu
>>> tensor.to(torch.float64)
tensor([[-0.5044, 0.0005],
[ 0.3310, -0.0584]], dtype=torch.float64)
>>> cuda0 = torch.device('cuda:0')
>>> tensor.to(cuda0)
tensor([[-0.5044, 0.0005],
[ 0.3310, -0.0584]], device='cuda:0')
torch.Tensor.repeat(*size)
复制数据,首先将给定的tensor按照size维度扩维,然后再在对应的维度上按size大小复制。
torch.nn.functional.pad(input, pad, mode='constant', value=0)
pad参数为一个tuple,其中的元素个数为偶数,且除2小于input的维度,依次从input最后一维向前扩展,(左边填充,右边填充),见:
>>> t4d = torch.empty(3, 3, 4, 2)
>>> p1d = (1, 1) # pad last dim by 1 on each side
>>> out = F.pad(t4d, p1d, "constant", 0) # effectively zero padding
>>> print(out.size())
torch.Size([3, 3, 4, 4])
>>> p2d = (1, 1, 2, 2) # pad last dim by (1, 1) and 2nd to last by (2, 2)
>>> out = F.pad(t4d, p2d, "constant", 0)
>>> print(out.size())
torch.Size([3, 3, 8, 4])
>>> t4d = torch.empty(3, 3, 4, 2)
>>> p3d = (0, 1, 2, 1, 3, 3) # pad by (0, 1), (2, 1), and (3, 3)
>>> out = F.pad(t4d, p3d, "constant", 0)
>>> print(out.size())
torch.Size([3, 9, 7, 3])
一些疑问
关于linear中输入tensor的向量等于2维和不是2维的计算方式为什么不同,会有什么差异:
def linear(input, weight, bias=None):
if input.dim() == 2 and bias is not None:
# fused op is marginally faster
ret = torch.addmm(bias, input, weight.t())
else:
output = input.matmul(weight.t())
if bias is not None:
output += bias
ret = output
return ret
暂无评论