2.3线性代数
作者 github链接: github链接
练习
- 证明一个矩阵 A \mathbf{A} A的转置的转置是 A \mathbf{A} A,即 ( A ⊤ ) ⊤ = A (\mathbf{A}^\top)^\top = \mathbf{A} (A⊤)⊤=A。
- 给出两个矩阵 A \mathbf{A} A和 B \mathbf{B} B,证明“它们转置的和”等于“它们和的转置”,即 A ⊤ + B ⊤ = ( A + B ) ⊤ \mathbf{A}^\top + \mathbf{B}^\top = (\mathbf{A} + \mathbf{B})^\top A⊤+B⊤=(A+B)⊤。
- 给定任意方阵 A \mathbf{A} A, A + A ⊤ \mathbf{A} + \mathbf{A}^\top A+A⊤总是对称的吗?为什么?
- 我们在本节中定义了形状
(
2
,
3
,
4
)
(2,3,4)
(2,3,4)的张量
X
。len(X)
的输出结果是什么? - 对于任意形状的张量
X
,len(X)
是否总是对应于X
特定轴的长度?这个轴是什么? - 运行
A/A.sum(axis=1)
,看看会发生什么。你能分析原因吗? - 考虑一个具有形状 ( 2 , 3 , 4 ) (2,3,4) (2,3,4)的张量,在轴0、1、2上的求和输出是什么形状?
- 为
linalg.norm
函数提供3个或更多轴的张量,并观察其输出。对于任意形状的张量这个函数计算得到什么?
练习参考答案(如有错误,还请大家指正)
1.证明一个矩阵 A \mathbf{A} A的转置的转置是 A \mathbf{A} A,即 ( A ⊤ ) ⊤ = A (\mathbf{A}^\top)^\top = \mathbf{A} (A⊤)⊤=A。
A·=·torch.arange(12).reshape(3,4)A,(A.T).T
output:
(tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]), tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]))
2.给出两个矩阵 A \mathbf{A} A和 B \mathbf{B} B,证明“它们转置的和”等于“它们和的转置”,即 A ⊤ + B ⊤ = ( A + B ) ⊤ \mathbf{A}^\top + \mathbf{B}^\top = (\mathbf{A} + \mathbf{B})^\top A⊤+B⊤=(A+B)⊤。
B = torch.tensor([[9, 8, 7,6], [5, 4, 3,2], [3, 4, 5,1]])
A.T+B.T ,(A+B).T
output:
(tensor([[ 9, 9, 11],
[ 9, 9, 13],
[ 9, 9, 15],
[ 9, 9, 12]]), tensor([[ 9, 9, 11],
[ 9, 9, 13],
[ 9, 9, 15],
[ 9, 9, 12]]))
3.给定任意方阵
A
\mathbf{A}
A,
A
+
A
⊤
\mathbf{A} + \mathbf{A}^\top
A+A⊤总是对称的吗?为什么?
答:因为
(
A
+
A
⊤
)
⊤
=
A
⊤
+
A
=
A
+
A
⊤
(\mathbf{A} + \mathbf{A}^\top)^\top=\mathbf{A}^\top+\mathbf{A}=\mathbf{A} + \mathbf{A}^\top
(A+A⊤)⊤=A⊤+A=A+A⊤
A = torch.arange(9).reshape(3,3)
A
output:
tensor([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
A+A.T
output:
tensor([[ 0, 4, 8],
[ 4, 8, 12],
[ 8, 12, 16]])
4.我们在本节中定义了形状
(
2
,
3
,
4
)
(2,3,4)
(2,3,4)的张量X
。len(X)
的输出结果是什么?
X = torch.arange(24).reshape(2, 3, 4)
len(X)
output:
2
5.对于任意形状的张量X
,len(X)
是否总是对应于X
特定轴的长度?这个轴是什么?
答:len(X) 总对应第 0 轴的长度。
6. 运行A/A.sum(axis=1)
,看看会发生什么。你能分析原因吗?
答:无法运行,原因是 A 是一个 5 * 4 的矩阵,而 A.sum(axis=1) 是一个被拍扁的1维的向量,两者维数不匹配不能相除。(注:广播只能发生在两者维数相同的情况下,比如都是二维)
A = torch.arange(20, dtype=torch.float32).reshape(5, 4)
A/A.sum(axis=1,keepdims=True)
output:
tensor([[0.0000, 0.1667, 0.3333, 0.5000],
[0.1818, 0.2273, 0.2727, 0.3182],
[0.2105, 0.2368, 0.2632, 0.2895],
[0.2222, 0.2407, 0.2593, 0.2778],
[0.2286, 0.2429, 0.2571, 0.2714]])
A = torch.arange(20, dtype=torch.float32).reshape(5, 4)
A/A.sum(axis=1)
output:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-59-86c8759e15d3> in <module>()
1 A = torch.arange(20, dtype=torch.float32).reshape(5, 4)
----> 2 A/A.sum(axis=1)
RuntimeError: The size of tensor a (4) must match the size of tensor b (5) at non-singleton dimension 1
7.考虑一个具有形状 ( 2 , 3 , 4 ) (2,3,4) (2,3,4)的张量,在轴0、1、2上的求和输出是什么形状?
H=torch.arange(24).reshape(2,3,4)
H
output:
tensor([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
H0 = H.sum(axis=0)
H1 = H.sum(axis=1)
H2 = H.sum(axis=2)
H0, H1, H2
output:
(tensor([[12, 14, 16, 18],
[20, 22, 24, 26],
[28, 30, 32, 34]]), tensor([[12, 15, 18, 21],
[48, 51, 54, 57]]), tensor([[ 6, 22, 38],
[54, 70, 86]]))
8.为linalg.norm
函数提供3个或更多轴的张量,并观察其输出。对于任意形状的张量这个函数计算得到什么?
答:其实就是求范数的操作(默认为2范数)
Z=torch.ones(2,3,4)
W=torch.ones(2,2,3,4)
torch.norm(Z)*torch.norm(Z),torch.norm(W)*torch.norm(W)
output:
(tensor(24.0000), tensor(48.))