线性代数 -- pytorch
import torch x = torch.tensor([3.0]) y = torch.tensor([2.0]) print(x + y, x * y, x/y, x**y)
import torch x = torch.arange(4) print(x) # 可以将向量视为标量值组成的列表 print(x[3]) # 通过张量的索引来访问任一元素 print(len(x)) # 访问张量的长度 print(x.shape) # 只有一个轴的张量,形状只有一个元素
# 轴对称翻转示意图
import torch A = torch.arange(20).reshape(5,4) # 20个元素,5行4列 print(A) # 矩阵转置(轴对称的翻转,参考示意图--个人理解感觉像是行转列,列转行) print(A.T)
import torch B = torch.tensor([[1, 2, 3], [2, 0, 4], [3, 4, 5]]) print(B) print(B.T) print(B == B.T)
import torch x = torch.arange(24).reshape(2, 3, 4) # 24个元素, 三矩阵(张量),2维,3行,4列 print(x)
import torch A = torch.arange(20, dtype=torch.float32).reshape(5, 4) B = A.clone() print(A, "\n", A + B) # 两个矩阵的按元素乘法称为 哈达玛积(Hadamard product)(数学符号⊙) print(A * B) print("-------------------") a = 2 X = torch.arange(24).reshape(2, 3, 4) print(X) print(a + X, (a * X).shape)
import torch # 计算其元素的和 x = torch.arange(4, dtype=torch.float32) print(x, x.sum()) # 表示任意形状张量的元素和 A = torch.arange(20, dtype=torch.float32).reshape(5, 4) print(A.shape, A.sum())
import torch A = torch.arange(20*2, dtype=torch.float32).reshape(2, 5, 4) print(A) # 使用2轴求和,降低维度(1维+2维张量的总和,降低成5行,4列) A_sum_axis0 = A.sum(axis=0) print('axis=0:\n', A_sum_axis0, A_sum_axis0.shape) # 使用5轴求和,降低维度(每一列总和,降低维度变成2行,4列) A_sum_axis1 = A.sum(axis=1) print('axis=1:\n', A_sum_axis1, A_sum_axis1.shape) # 使用4轴求和,降低维度(每一行总和,降低维度变成2行,5列) A_sum_axis2 = A.sum(axis=2) print('axis=2', A_sum_axis2, A_sum_axis2.shape) print("axis=0,1,2 SUM:\n", A.sum(axis=[0, 1, 2]))
import torch A = torch.arange(20, dtype=torch.float32).reshape(5, 4) print(A) print(A.numel()) # numel() 返回矩阵元素个数,这里是20 print(A.sum(axis=0)) # 自己表总每一列总和生成一个1行,4列的张量 print(A.mean(), A.sum() / A.numel()) # mean()是求平均数,后面的是验证平均数结果的一种方式 print(A.mean(axis=0), A.sum(axis=0) / A.shape[0]) # 求维度的均值,后面是验证
import torch A = torch.arange(20, dtype=torch.float32).reshape(5, 4) print(A) sum_A = A.sum(axis=1, keepdims=True) # keepdism=True 保持轴数不变 print(sum_A) sum_B = A.sum(axis=1) # 默认指定按照某个维度求和,这个维度会被丢掉 print(sum_B) print("A: \n", A / sum_A) # 因为设置了保持轴数不变,可以进行乘除操作;可以通过广播机制将A除以sum_A print("B: \n", A / sum_B) # 因为没有设置,将会报错“RuntimeError: The size of tensor a (4) must match the size of tensor b (5) at non-singleton dimension 1”
import torch A = torch.arange(20, dtype=torch.float32).reshape(5, 4) print(A, "\n") print(A.cumsum(axis=0)) # 从上往下累加
import torch x = torch.arange(4, dtype=torch.float32) y = torch.ones(4, dtype=torch.float32) print(x, y, torch.dot(x, y)) # 我们可以通过执行按元素乘法,然后进行求和来表示两个向量的点积 print(torch.sum(x * y))
import torch A = torch.arange(20, dtype=torch.float32).reshape(5,4) x = torch.arange(4, dtype=torch.float32) y = torch.ones(4, dtype=torch.float32) print(A) print(x, x.shape) print(y, y.shape) print(A.shape, x.shape, torch.mv(A, x)) print(A.shape, y.shape, torch.mv(A, y))
import torch A = torch.arange(20, dtype=torch.float32).reshape(5,4) B = torch.ones(4, 3) # 我们可以将矩阵-矩阵乘法AB看作时简单地执行m次矩阵-向量积,并将结果拼在一起,形成一个n X m矩阵 print(torch.mm(A, B)) # 个人理解:torch.mm 把4,3的矩阵B,变成了5,3的矩阵,并且把矩阵A的每行之和*矩阵B的向量,拼了一个新的矩阵
import torch u = torch.tensor([3.0, -4.0]) print("L2 norm: ", torch.norm(u)) # torch.norm(); L2范数,向量元素平方和的平方根 print("L1 norm: ", torch.abs(u).sum()) # torch.abs().sum(); L1 范数,向量元素的绝对值求和 print("F norm: ", torch.norm(torch.ones((4, 9)))) # torch.norm(torch.noes()); 佛罗贝尼乌斯范数(Frobenius norm),矩阵元素平方和的平方根
# 按特定轴求和,示意图
PS:主要注意是否使用keepdims = True,分清楚axis设置的值和剩下的值
对哪一维求和,就是消除哪一维数据
标签:
Ai
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
2020-03-30 (转)主从同步常遇见问题处理-线上MYSQL同步报错故障处理总结
2019-03-30 English trip EM2-LP-4B At school Teacher:Will