学习目标:
动手深度学习V2(进度:6/73)
学习内容:
- 标量、向量、矩阵和张量是线性代数中的基本数学对象。
- 向量泛化自标量,矩阵泛化自向量。
- 标量、向量、矩阵和张量分别具有零、一、二和任意数量的轴。
- 一个张量可以通过sum和mean沿指定的轴降低维度。
- 两个矩阵的按元素乘法被称为他们的哈达玛积。它与矩阵乘法不同。
- 在深度学习中,我们经常使用范数,如 L1 范数、 L2 范数和弗罗贝尼乌斯范数。
- 我们可以对标量、向量、矩阵和张量执行各种操作。
学习时间:
2021.9.4( 10:40-11:20am)
学习产出:
本文2333
练习题:
1.我们在本节中定义了形状(2,3,4)的张量X。len(X)的输出结果是什么?
X = torch.arange(24).reshape(2, 3, 4)
print(X, '\n', len(X))
运行结果:
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]]])
2
说明3维张量中,len表示第一个维度的值。
2.运行A/A.sum(axis=1),看看会发生什么。你能分析原因吗?
A = torch.arange(20).reshape(5, 4)
A
运行结果:
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19]])
A/A.sum(aixs=1)
运行结果:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-66827c98fc68> in <module>()
----> 1 A/A.sum(aixs=1)
TypeError: sum() received an invalid combination of arguments - got (aixs=int, ), but expected one of:
* (*, torch.dtype dtype)
* (tuple of ints dim, bool keepdim, *, torch.dtype dtype)
* (tuple of names dim, bool keepdim, *, torch.dtype dtype)
原因应该是A/A中有元素为nan(0为被除数时,结果为nan)
A/A
运行结果:
tensor([[nan, 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
3.考虑一个具有形状(2,3,4)的张量,在轴0,1,2上的求和输出是什么形状?
X = torch.arange(24).reshape(2, 3, 4)
X.sum(axis=[0, 1, 2]).shape
运行结果:
torch.Size([])
就是一个标量,列表
4.向linalg.norm函数提供3个或更多轴的张量,并观察其输出。对于任意形状的张量这个函数计算得到什么?
import numpy as np
X = torch.arange(24).reshape(2, 3, 4)
Y = torch.arange(36).reshape(1, 3, 3, 4)
Z = torch.arange(36).reshape(3, 1, 3, 4)
print(np.linalg.norm(X), np.linalg.norm(Y), np.linalg.norm(Z))```
linalg.norm函数默认求张量的整体元素平方和开根号,不保留矩阵二维特性

本文介绍了深度学习中的基本数学概念,包括标量、向量、矩阵和张量,以及它们在维度降低、哈达玛积和范数计算中的应用。通过实例展示了张量的形状、按元素除法可能导致的错误以及张量求和的结果。此外,还探讨了linalg.norm函数如何计算张量的范数。
1045

被折叠的 条评论
为什么被折叠?



