深度学习基础知识(三)-线性代数的实现

1.标量使用

标量由只有一个元素的张量表示,标量可以做最简单的计算。

python 复制代码
import torch 
x=torch.tensor([3.0])
y=torch.tensor([2.0])

print(x+y)
print(x*y)
print(x/y)
print(x**y)

结果:

bash 复制代码
tensor([5.])
tensor([6.])
tensor([1.5000])
tensor([9.])

2.向量使用

向量:将标量值组成的列表就是向量

python 复制代码
x=torch.arange(4)
print(x)
# 取向量中的元素
print(x[3])

结果:

bash 复制代码
tensor([0, 1, 2, 3])
tensor(3)

访问张量的长度

python 复制代码
print(len(x))
# 结果为4

只有一个轴的张量,形状只有一个元素

python 复制代码
print(x.shape)
# 结果为:torch.Size([4])

创建一个二维矩阵5行4列,然后将矩阵做转置,轴对称的一个转置

python 复制代码
a=torch.arange(20).reshape(5,4)
print(a)

# 矩阵的转置
print(a.T)

结果:其实就是把每一列转换成行

bash 复制代码
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11],
        [12, 13, 14, 15],
        [16, 17, 18, 19]])
tensor([[ 0,  4,  8, 12, 16],
        [ 1,  5,  9, 13, 17],
        [ 2,  6, 10, 14, 18],
        [ 3,  7, 11, 15, 19]])

3.矩阵使用

对称矩阵:它的转置等于自己,你可以注意数据里数值,就可以明白为什么是对称的了

python 复制代码
b=torch.tensor([[1,2,3],[2,0,4],[3,4,5]])
print(b)
print("b的转置:",b.T)
print("转置后元素是否相等:",b==b.T)

结果:

bash 复制代码
tensor([[1, 2, 3],
        [2, 0, 4],
        [3, 4, 5]])
b的转置: tensor([[1, 2, 3],
        [2, 0, 4],
        [3, 4, 5]])
转置后元素是否相等: tensor([[True, True, True],
        [True, True, True],
        [True, True, True]])

就像向量是标量的推广,矩阵是向量的推广一样,我们可以构建具有更多轴的数据结构。

python 复制代码
x=torch.arange(24).reshape(2,3,4)
print(x)

结果:分别有两个二维,三行,四列的维度

bash 复制代码
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]]])

两个矩阵按元素乘法称为哈达玛积(数学符号⊙)。

python 复制代码
a=torch.arange(20).reshape(5,4)
b=a.clone()# 重新分配内存,将A的副本分配给b
print("a:",a)
print("b:",b)

# 按元素乘法,哈达玛积
print("哈达玛积:",a*b)

# 也可以标量乘以或与矩阵相加
h=2
x=torch.arange(24).reshape(2,3,4)
print("h+x:",h+x)
print("x*a.shape:",(h*x).shape)

结果:

bash 复制代码
a: tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11],
        [12, 13, 14, 15],
        [16, 17, 18, 19]])
b: tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11],
        [12, 13, 14, 15],
        [16, 17, 18, 19]])
哈达玛积: tensor([[  0,   1,   4,   9],
        [ 16,  25,  36,  49],
        [ 64,  81, 100, 121],
        [144, 169, 196, 225],
        [256, 289, 324, 361]])
h+x: tensor([[[ 2,  3,  4,  5],
         [ 6,  7,  8,  9],
         [10, 11, 12, 13]],

        [[14, 15, 16, 17],
         [18, 19, 20, 21],
         [22, 23, 24, 25]]])
x*a.shape: torch.Size([2, 3, 4])

指定求和汇总张量的轴

python 复制代码
# 按维度的列求和
a_sumaxis0=a.sum(axis=0)
print(a_sumaxis0)

# 按维度的行求和
a_sumaxis1=a.sum(axis=1)
print(a_sumaxis1)

结果:

bash 复制代码
a原来的值: tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11],
        [12, 13, 14, 15],
        [16, 17, 18, 19]])

tensor([40, 45, 50, 55])  # 按列求的和
tensor([ 6, 22, 38, 54, 70]) # 按行求的和

一个与求和相关的量是平均值(mean或average),也可以指定按某个维度处理

bash 复制代码
print(a.sum())
# 满足条件的元素个数
print(a.numel())

# 求平均值,也等于a.mean()
print(a.sum()/a.numel())

# 按维度进行求平均值
print(a.mean(axis=0,dtype=torch.float32))
# 与上一个结果相等,方式不同
print(a.sum(axis=0)/ a.shape[0])

结果:

bash 复制代码
tensor(190)
20
tensor(9.5000)
tensor([ 8.,  9., 10., 11.])
tensor([ 8.,  9., 10., 11.])

计算总和和均值时保持轴数不变

python 复制代码
# 按行求和
sum_a=a.sum(axis=1,keepdims=True)
print(sum_a)
print(a)

# 通过广播将a/sum_a
print(a/sum_a)

某个轴计算a元素的累积总和

python 复制代码
print(a.cumsum(axis=0))

结果:

bash 复制代码
tensor([[ 0,  1,  2,  3],
        [ 4,  6,  8, 10],
        [12, 15, 18, 21],
        [24, 28, 32, 36],
        [40, 45, 50, 55]])

点积:只能支持两个一维向量,是相同位置的按元素乘积的和

python 复制代码
y=torch.ones(4,dtype=torch.float32)
x=torch.arange(4,dtype=torch.float32)
print(y)

print(x)

# 点积也等价于torch.sum(x*y)
print(torch.dot(x,y))

结果:

bash 复制代码
tensor([1., 1., 1., 1.])
tensor([0., 1., 2., 3.])
tensor(6.)

矩阵向量乘法:只支持矩阵向量乘法,如果input为 n × m n\times m n×m的,vec向量的长度为m,那么输出为 n × 1的向量。

python 复制代码
from numpy.core.multiarray import dtype
print(a)
print(a.shape)
x=torch.arange(4)
print(x)
print(x.shape)
# 矩阵向量乘法,
print(torch.mv(a,x))

结果:

bash 复制代码
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11],
        [12, 13, 14, 15],
        [16, 17, 18, 19]])
torch.Size([5, 4])
tensor([0, 1, 2, 3])
torch.Size([4])
tensor([ 14,  38,  62,  86, 110])

两个矩阵做乘法:对矩阵input 和mat2进行相乘。 如果input 是一个n×m张量,mat2 是一个 m×p张量,将会输出一个 n×p张量out。
torch.mm()不支持广播机制

python 复制代码
mat1=torch.arange(12).reshape(3,4)
mat2=torch.arange(12).reshape(4,3)
print(mat1)
print(mat2)
print(torch.mm(mat1,mat2))

结果:

bash 复制代码
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]])
tensor([[ 42,  48,  54],
        [114, 136, 158],
        [186, 224, 262]])

L2范数:是向量元素平方和的平方根。

bash 复制代码
u=torch.tensor([3.0,-4.0])
print(torch.norm(u))

结果:

bash 复制代码
tensor(5.)

L1范数:表示为向量元素的绝对值之和:

python 复制代码
u=torch.tensor([3.0,-4.0])
print(torch.norm(u))

结果:

bash 复制代码
tensor(7.)

f范数矩阵:是矩阵元素的平方和的平方根。

python 复制代码
print(torch.norm(torch.ones(4,9)))
#结果为:tensor(6.)

李沐老师的深度学习课学习笔记内容!希望对你有帮助

相关推荐
deflag1 分钟前
第P10周-Pytorch实现车牌号识别
人工智能·pytorch·yolo
pzx_0016 分钟前
【机器学习】K折交叉验证(K-Fold Cross-Validation)
人工智能·深度学习·算法·机器学习
海域云赵从友17 分钟前
助力DeepSeek私有化部署服务:让企业AI落地更简单、更安全
人工智能·安全
伊一大数据&人工智能学习日志31 分钟前
自然语言处理NLP 04案例——苏宁易购优质评论与差评分析
人工智能·python·机器学习·自然语言处理·数据挖掘
刀客12336 分钟前
python3+TensorFlow 2.x(六)自编码器
人工智能·python·tensorflow
大模型之路1 小时前
Grok-3:人工智能领域的新突破
人工智能·llm·grok-3
闻道且行之1 小时前
LLaMA-Factory|微调大语言模型初探索(4),64G显存微调13b模型
人工智能·语言模型·llama·qlora·fsdp
造夢先森1 小时前
Transformer & LLaMA
深度学习·transformer·llama
喝不完一杯咖啡1 小时前
【AI时代】可视化训练模型工具LLaMA-Factory安装与使用
人工智能·llm·sft·llama·llama-factory
huaqianzkh2 小时前
理解构件的3种分类方法
人工智能·分类·数据挖掘