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]))]
注:张量操作后,张量值并未改变,因张量内部的值不会改变,除非被重新分配;
|
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
-
矩阵乘法:ML和DL算法(如神经网络)中最常见的操作之一是矩阵乘法;
-
Matrix Multiplication,即Dot Product;
https://www.mathsisfun.com/algebra/vectors-dot-product.html -
A:内部维度必须匹配:inner dimension must match;
-
(3, 2) @ (3, 2) 不工作;
-
(2, 3) @ (3, 2) 可工作;
-
(3, 2) @ (2, 3) 可工作;
-
-
B:结果矩阵具有外部维度的形状:resulting matrix has outer dimension shape;
-
(2, 3) @ (3, 2) → (2, 2)
-
(3, 2) @ (2, 3) → (3, 3)
-
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) |
|
2. Matrix Multiplication
-
注:像这样的矩阵乘法matrix multiplication也被称为两个矩阵matrice的点积dot product;
-
神经网络充满矩阵乘法和点积,torch.nn.Linear()模块也称feed-forward layer
或fully connected layer,实现输入x和权重矩阵A之间的矩阵乘法; -
y = x * AT + b;
|
-
这是线性函数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])