当前位置:网站首页>即看即用 && 其他操作(Other Operations) && Pytorch官方文档总结 && 笔记 (八)
即看即用 && 其他操作(Other Operations) && Pytorch官方文档总结 && 笔记 (八)
2022-07-21 20:29:00 【Flying Bulldog】
(1)本文涉及函数的列表(注释在代码中)
- torch.cross # 向量积(叉积)
- torch.diag # 返回指定对角线元素(方阵/张量)
- torch.histc # 直方图
- torch.ones # 全 1 张量
- torch.renorm # 返回规范化后的各个子张量
- torch.trace # 迹
- torch.tril # 二维张量的下三角部分
- torch.triu # 二维张量的上三角部分
注意:
figure 1. torch.cross()
迹,是线性代数中的概念。
矩阵的迹:主对角线(左上至右下的那一条)上所有元素之和。记作tr(A),其中A为方阵。
规范化,信息学术语,理论正是用来改造关系模式,通过分解关系模式来消除其中不合适的数据依赖,以解决插入异常、删除异常、更新异常和数据冗余问题。
数据规范化是将原来的度量值转换为无量纲的值。通过将属性数据按比例缩放,通过一个函数将给定属性的整个值域映射到一个新的值域中,即每个旧的值都被一个新的值替代。
figure 2. 冗余百科
(2)代码示例(含注释)
"""
其他操作 Other Operations
"""
import torch
# # 返回沿着维度 dim 上,两个张量 input 和 other 的向量积(叉积)。
# # input 和 other 必须有相同的形状,且指定的 dim 维上 size 必须为 3。
# 向量积公式见图像:img.png
obj1 = torch.cross(torch.tensor([1, 2, 3]), torch.tensor([4, 5, 6]))
# a * b = [[0, -3, 2],
# [3, 0 ,-1],
# [-2, 1, 0]] * [4, 5, 6] = [-3, 6, -3]
# # 如果输入是一个向量(1D 张量),则返回一个以 input 为对角线元素的 2D 方阵
# # 如果输入是一个矩阵(2D 张量),则返回一个包含 input 对角线元素的 1D 张量
# 参数 diagonal 指定对角线:
# diagonal = 0, 主对角线
# diagonal > 0, 主对角线之上
# diagonal < 0, 主对角线之下
obj2 = torch.diag(torch.tensor([1, 2, 3]))
obj3 = torch.diag(torch. tensor([[1, 0, 8],
[0, 2, 0],
[6, 0, 3]]), diagonal=-1) # 对角线之下的第一个斜线
obj4 = torch.diag(torch. tensor([[1, 0, 8],
[0, 2, 0],
[6, 0, 3]]), diagonal=0) # 对角线
obj5 = torch.diag(torch. tensor([[1, 0, 8],
[0, 2, 0],
[6, 0, 3]]), diagonal=2) # 对角线之上的第二个斜线
# # 计算输入张量的直方图。
# # torch.histc(input, bins=100, min=0, max=0, out=None) → Tensor
# # 以 min 和 max 为 range 边界,将其均分成 bins 个直条,然后将排序好的数据划分到各个直条(bins)中。
# # 如果 min 和 max 都为 0, 则利用数据中的最大最小值作为边界。
obj6 = torch.histc(torch.FloatTensor([1, 2, 1, 1, 1, 2]), bins=4, min=0, max=3)
# # 返回一个张量,包含规范化后的各个子张量,使得沿着 dim 维划分的各子张量的 p 范数小于 maxnorm。
# # 注意: 如果 p 范数的值小于 maxnorm,则当前子张量不需要修改。
# # torch.renorm(input, p, dim, maxnorm, out=None) → Tensor
x = torch.ones(3, 3)
x[1].fill_(2)
x[2].fill_(3)
obj7 = torch.renorm(x, p=1, dim=0, maxnorm=5)
obj8 = torch.renorm(x, p=1, dim=0, maxnorm=2)
# # 返回输入 2 维矩阵对角线元素的和(迹)
obj9 = torch.trace(x)
# # 返回一个张量 out,包含输入矩阵(2D 张量)的下三角部分,out 其余部分被设为 0。
obj10 = torch.tril(x, diagonal=0)
obj11 = torch.tril(x, diagonal=1)
obj12 = torch.tril(x, diagonal=-1)
# # 返回一个张量,包含输入矩阵(2D 张量)的上三角部分,其余部分被设为 0。
obj13 = torch.triu(x, diagonal=0)
obj14 = torch.triu(x, diagonal=1)
obj15 = torch.triu(x, diagonal=-1)
print("*"*20, "obj1", "*"*20, "\n", obj1, "\n")
print("*"*20, "obj2", "*"*20, "\n", obj2, "\n", obj3, "\n", obj4, "\n", obj5, "\n")
print("*"*20, "obj6", "*"*20, "\n", obj6, "\n")
print("*"*20, "obj8", "*"*20, "\n", x, "\n", obj7, "\n", obj8, "\n")
print("*"*20, "obj9", "*"*20, "\n", obj9, "\n")
print("*"*20, "obj10", "*"*20, "\n", obj10, "\n", obj11, "\n", obj12, "\n")
>>>output
******************** obj1 ********************
tensor([-3, 6, -3])******************** obj2 ********************
tensor([[1, 0, 0],
[0, 2, 0],
[0, 0, 3]])
tensor([0, 0])
tensor([1, 2, 3])
tensor([8])******************** obj6 ********************
tensor([0., 4., 2., 0.])******************** obj8 ********************
tensor([[1., 1., 1.],
[2., 2., 2.],
[3., 3., 3.]])
tensor([[1.0000, 1.0000, 1.0000],
[1.6667, 1.6667, 1.6667],
[1.6667, 1.6667, 1.6667]])
tensor([[0.6667, 0.6667, 0.6667],
[0.6667, 0.6667, 0.6667],
[0.6667, 0.6667, 0.6667]])******************** obj9 ********************
tensor(6.)******************** obj10 ********************
tensor([[1., 0., 0.],
[2., 2., 0.],
[3., 3., 3.]])
tensor([[1., 1., 0.],
[2., 2., 2.],
[3., 3., 3.]])
tensor([[0., 0., 0.],
[2., 0., 0.],
[3., 3., 0.]])
专栏链接直达:
https://blog.csdn.net/qq_54185421/category_11794260.html?spm=1001.2014.3001.5482https://blog.csdn.net/qq_54185421/category_11794260.html?spm=1001.2014.3001.5482 >>>如有疑问,欢迎评论区一起探讨
边栏推荐
- TCP 通信并发服务器详解(附有案例代码)
- 学生管理系统(文件版)
- TCP 通信流程详解(附有案例代码)
- 软件测试工程师 | 不拼学历,还能进大厂吗?
- Is the flush platform safe? Huatai Securities Account Opening app
- AcWing 1184. 欧拉回路 题解(欧拉回路)
- 你真的会使用搜索引擎吗?
- 【学习笔记】带你从0开始学习 01Trie
- Where is gold trading safe
- Robot modeling and 3D simulation based on ROS [physical / mechanical significance]
猜你喜欢
云呐|动环监控系统巡检,动环监控系统功能大致介绍
nodes 简介
SYSTEMd management process exporter
GL_ TRIANGLE_ Fan and GL_ TRIANGLE_ STRIP
Robot modeling and 3D simulation based on ROS [physical / mechanical significance]
X书app数美-sid分析
Value and technical thinking of vectorization engine for HTAP
(pc+wap) dream weaving template emotional information website
向量化引擎对HTAP的价值与技术思考
[解决]vscode代码没有输入完成就自动换行bug
随机推荐
: empty pseudo class replaces JS to realize the prompt when it is empty
After the clothing ERP goes online, these problems must be paid attention to
Where is gold trading safe
(pc+wap) dream weaving template emotional information website
nodes 简介
Application of character sets and comparison rules
[解决]vscode代码没有输入完成就自动换行bug
Example interview - Zeng yuluo: gain experience from lectures
基于oracle数据库存储过程的创建及调用
AcWing 1124. Solution to the problem of repairing fences on horseback (Euler circuit)
软件测试工程师 | 不拼学历,还能进大厂吗?
nodes 簡介
同花顺平台安全吗 华泰证券开户app
B2B企业数字化转型,CIO如何避免踩坑
Start jar package shell script
NASA「史上最强超算」投入使用,碾压老超算霸主Pleiades
Restful URL design specification
(PC+WAP)织梦模板防护口罩类网站
Intelligent operation and maintenance scenario analysis: how to detect abnormal business system status through exception detection
Network security competition C module batch value taking script