python线性代数

线性代数

import torch

A = torch.arange(20, dtype=torch.float32).reshape(5, 4)
print(A)
print(A.T)  # 转置


# 点积是相同位置的按元素乘积的和
x = torch.arange(4, dtype=torch.float32)
y = torch.ones(4, dtype=torch.float32)
print(x)
print(y)
print(torch.dot(x, y))  # 等同与torch.sum(x * y)

print(torch.mv(A, x))  # 矩阵向量积

B = torch.ones(4, 3)
print(torch.mm(A, B))  # 矩阵-矩阵相乘

# L2范数是向量元素平方和的平方根
u = torch.tensor([3.0, -4.0])
print(torch.norm(u))

# L1范数表示为向量元素的绝对值之和
print(torch.abs(u).sum())

# 通常使用L2范数

# 矩阵的佛罗贝尼乌斯范数是矩阵元素的平方和的平方根
print(torch.norm(torch.ones((4, 9))))


输出

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.]])
tensor([0., 1., 2., 3.])
tensor([1., 1., 1., 1.])
tensor(6.)
tensor([ 14.,  38.,  62.,  86., 110.])
tensor([[ 6.,  6.,  6.],
        [22., 22., 22.],
        [38., 38., 38.],
        [54., 54., 54.],
        [70., 70., 70.]])
tensor(5.)
tensor(7.)
tensor(6.)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值