沐神~动手学深度学习课后练习题

2.3. 线性代数

1.证明一个矩阵 A \mathbf{A} A的转置的转置是 A \mathbf{A} A,即 ( A ⊤ ) ⊤ = A (\mathbf{A}^\top)^\top = \mathbf{A} (A)=A

哈哈,该问题过于基础就不再叙述啦!我们可以通过以下代码来进行下简单认证~

	import torch
	A = torch.arange(24).reshape(6,4)
	print(A == A.T.T)

结果如下:

tensor([[True, True, True, True],
        [True, True, True, True],
        [True, True, True, True],
        [True, True, True, True],
        [True, True, True, True],
        [True, True, True, True]])


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)

同样通过以下代码来进行下简单认证~

import torch
A = torch.arange(24).reshape(6,4)
B = torch.ones(24).reshape(6,4)
print((A.T+B.T)==(A+B).T)

结果如下:

tensor([[True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True],
        [True, True, True, True, True, True]])

3.给定任意方阵 A \mathbf{A} A A + A ⊤ \mathbf{A} + \mathbf{A}^\top A+A总是对称的吗?为什么?

A并不总是对称的, A + A ⊤ A+A^\top A+A总是对称的~

4.本节中定义了形状 ( 2 , 3 , 4 ) (2,3,4) (2,3,4)的张量Xlen(X)的输出结果是什么?

	len(X)=2

5.对于任意形状的张量X,len(X)是否总是对应于X特定轴的长度?这个轴是什么?

	len(X)的输出总是对应第0轴的长度。

6.运行A/A.sum(axis=1),看看会发生什么。请分析一下原因?

import torch
A = torch.arange(20, dtype=torch.float32).reshape(5, 4)
A/A.sum(axis=1)
#The size of tensor a (4) must match the size of tensor b (5) at non-singleton dimension 1
#输出结果报错,因为A是5*4矩阵A.sum(axis=1)是一个1*5向量无法触发广播机制。若改为axis=0,则可行。

输出结果如下:

RuntimeError          Traceback (most recent call last)
Cell In[76], line 2
      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

#输出结果报错,因为A是5 * 4矩阵,A.sum(axis=1)是一个1 * 5向量无法触发广播机制。若改为axis=0,则可行。

7.考虑一个具有形状 (2,3,4) 的张量,在轴0、1、2上的求和输出是什么形状?

import torch
#0维度 3*4
#1维度 2*4
#2维度 2*3
R = torch.arange(24).reshape(2,3,4)
R,R.sum(axis=0),R.sum(axis=1),R.sum(axis=2)

输出结果如下:

(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]]]),
 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个或更多轴的张量,并观察其输出。对于任意形状的张量这个函数计算得到什么?

这里定义一个3维张量A来进行观察~

import torch
A=torch.ones(12).reshape(2,2,3)
torch.norm(A)##所有值的L2范数

结果如下:

tensor(3.4641)

对更多轴的张量求范数时,仍然是将所有数据作为一个整体来进行计算。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值