1. Manipulating Tensor

  • 数据:image,text,video,audio,protein(蛋白质) structure等;

  • 在DL中,数据被表示为张量,模型研究张量,对张量操作(可能百万次以上),
    以创建输入数据中模式的表示(pattern representation);

  • 张量操作是神经网络的基本组成部分,如,Addition,Substraction,
    Multiplication(Element Wise),Division,Matrix Multiplication;

  • 以正确的方式堆叠Stacking这些构建块,可创建最复杂的神经网络;

1.1. Basic Operation

  • Addition,Substraction,Multiplication,Division;

tensor = torch.tensor([1, 2, 3])
tensor + 10,tensor - 10,tensor * 10,tensor / 2,tensor
(tensor([11, 12, 13]),
 tensor([-9, -8, -7]),
 tensor([10, 20, 30]),
 tensor([0.5000,1.0000,1.5000]),
 tensor([1, 2, 3]))]

注:张量操作后,张量值并未改变,因张量内部的值不会改变,除非被重新分配;

  • 另PyTorch提供内置函数:如torch.multiply(tensor,10)

  • 但通常使用像*这样的运算符号,而非torch.mul();

1.2. Element Wise

  • Element Wise Multiplication,each element multiply its equivalent,
    每个元素与相对应的相乘:index 0→0, 1→1, 2→2;

print(f"tensor * tensor = tensor([1,2,3]) * tensor([1,2,3]) = {tensor * tensor}")

* 输出:tensor * tensor = tensor([1,2,3]) * tensor([1,2,3]) = tensor([1,4,9])

1.3. Matrix Multiplication

ta = torch.tensor([3,2])

tb = torch.tensor([2,3])

tc = torch.tensor([1,2,3])

torch.matmul(ta,ta),ta @ ta,ta @ tb,tc @ tc, tc * tc

# 输出:(tensor(13),tensor(13),tensor(12),tensor(14),tensor([1,4,9]))

1.4. Element Wise VS Matrix

Operation Calculation Code

Element Wise

[1*1, 2*2, 3*3] = [1, 4, 9]

tensor * tensor

Matrix

[1*1 + 2*2 + 3*3] = [14]

tensor.matmul(tensor)

  • PyTorch实现矩阵乘法:torch.matmul(),矩阵乘法规则;

  • 注:Python中的@是矩阵乘法的符号,不推荐使用,
    而用torch.matmul(t1,t2),因torch.matmul()更快;

  • torch.mm() 等价于 torch.matmul()

import torch
tensor = torch.tensor([1,2,3])
%%time
value = 0
for i in range(len(tensor)):
  value += tensor[i] * tensor[i]
value
CPU times: user 1.46 ms, sys: 3.29 ms, total: 4.76 ms
Wall time: 47.3 ms

tensor(14)
%%time
torch.matmul(tensor, tensor)
CPU times: user 1.15 ms, sys: 1.03 ms, total: 2.18 ms
Wall time: 17.3 ms

tensor(14)

2. Matrix Multiplication

  • http://matrixmultiplication.xyz

  • 注:像这样的矩阵乘法matrix multiplication也被称为两个矩阵matrice的点积dot product;

  • 神经网络充满矩阵乘法和点积,torch.nn.Linear()模块也称feed-forward layer
    或fully connected layer,实现输入x和权重矩阵A之间的矩阵乘法;

  • y = x * AT + b

  • x:是层的输入(DL是一堆层(stack of layer),
    如torch.nn.Linear(),和其它层(on top of each other);

  • A:该层创建的权重矩阵weight matrix,最初是随机数,随神经网络学习更好的表示
    数据中的模式pattern,这些随机数会被调整(注意T,因权重矩阵会被转置transpose);

  • b:用于稍微偏移权重和输入的偏置项,
    bias term used to slightly offset weight and input;

  • y:输出(对输入进行操作,希望发现其中的模式),
    manipulation input in hope to discover pattern in it;

  • 这是线性函数linear function,如y = mx + b;

# since linear layer start with random weight matrix,make it reproducible
tlf = torch.tensor([[1,2],[3,4],[5,6]],dtype=torch.float32)

torch.manual_seed(42)

# use matrix multiplication
# in_feature = match inner dimension of input
# out_feature = describe outer value
linear = torch.nn.Linear(in_features=2,out_features=6)

x = tlf
output = linear(x)
print(f"Input Shape: {x.shape}\n")
print(f"Output:\n{output}\n\nOutput Shape: {output.shape}")
Input Shape: torch.Size([3, 2])

Output:
tensor([[2.2368, 1.2292, 0.4714, 0.3864, 0.1309, 0.9838],
        [4.4919, 2.1970, 0.4469, 0.5285, 0.3401, 2.4777],
        [6.7469, 3.1648, 0.4224, 0.6705, 0.5493, 3.9716]],
       grad_fn=<AddmmBackward0>)

Output Shape: torch.Size([3, 6])