技术标签: # Pytorch笔记
1.只分配空间
x1 = torch.Tensor(5, 3) # 数据是随机的,只是分配了可见,没有初始化,所及数据是对应空间里的数据
print(x1)
输出:
tensor([[7.3470e-39, 8.9082e-39, 8.9082e-39],
[1.0194e-38, 9.1837e-39, 4.6837e-39],
[9.2755e-39, 1.0837e-38, 8.4490e-39],
[9.9184e-39, 9.9184e-39, 9.0000e-39],
[1.0561e-38, 1.0653e-38, 4.1327e-39]])
2.0-1分布
x2 = torch.rand(5, 3) # 使用[0,1]分布(默认的)随机初始化数据,构建(5, 3)shape的数组
print(x2)
输出:
tensor([[0.0786, 0.0817, 0.5277],
[0.3758, 0.9402, 0.2716],
[0.0723, 0.3258, 0.7880],
[0.9141, 0.7395, 0.1126],
[0.9843, 0.5128, 0.9107]])
3.直接创建
x3 = torch.tensor([ [1,2,3,4],
[5,6,7,8]])
print(x3)
输出:
tensor([[1, 2, 3, 4],
[5, 6, 7, 8]])
4.同一个数值
x4 = torch.full((5, 3), 6)
print(x4)
输出:
tensor([[6., 6., 6.],
[6., 6., 6.],
[6., 6., 6.],
[6., 6., 6.],
[6., 6., 6.]])
5.在一个区间内按一定步长
x5 = torch.arange(0, 100, 10) # 从0到99,按步长为10进行生成
print(x5)
输出:
tensor([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])
或者:
在一定区间内均匀生成多少份,步长自动计算
x6 = torch.linspace(0, 100, 10) # 在0到100均匀生成10份
print(x6)
输出:
tensor([ 0.0000, 11.1111, 22.2222, 33.3333, 44.4444, 55.5556, 66.6667,
77.7778, 88.8889, 100.0000])
这里可见看出来使用torch.linspace包含end,0到100分成10个数,相对于在0到100的线段上均匀定9个点,100/9 = 11.111111······,所以,步长为11.11111.
如果生成11份,步长就为10了。
输出:
tensor([ 0., 10., 20., 30., 40., 50., 60., 70., 80., 90., 100.])
6.特殊矩阵
# 特殊矩阵
x7 = torch.zeros(5, 3) # 全0矩阵
print(x7)
x8 = torch.ones(5, 3) # 全1矩阵
print(x8)
x9 = torch.eye(3, 3) # 单位阵
print(x9)
x10 = torch.eye(5, 3) # 当输入shape不是方阵时,一部分是单位阵,多出来的部分为0
print(x10)
输出:
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
[0., 0., 0.],
[0., 0., 0.]])
7.生成一个与已知tensor的shape相同的tensor
x11 = torch.rand_like(x10)
print(x11)
输出:
tensor([[0.2761, 0.7960, 0.0800],
[0.3035, 0.3525, 0.5574],
[0.3570, 0.5426, 0.4790],
[0.3504, 0.3996, 0.1984],
[0.5939, 0.3260, 0.6721]])
8.从numpy转
x12 = np.random.rand(5, 3)
print(x12)
x12 = torch.from_numpy(x12)
print(x12)
输出:
[[0.14858865 0.18512316 0.97759539]
[0.96559993 0.75191884 0.1561388 ]
[0.71575248 0.88542421 0.29086326]
[0.67362585 0.00512253 0.34022816]
[0.69759491 0.25110932 0.71962754]]
tensor([[0.1486, 0.1851, 0.9776],
[0.9656, 0.7519, 0.1561],
[0.7158, 0.8854, 0.2909],
[0.6736, 0.0051, 0.3402],
[0.6976, 0.2511, 0.7196]], dtype=torch.float64)
x2 = torch.rand(5, 3) # 使用[0,1]分布(默认的)随机初始化数据,构建(5, 3)shape的数组
print(x2)
print(x2.size()) # (列,行)
print(x2.shape) # (列, 行)
print(x2.dtype) # 数据类型
输出:
tensor([[0.6725, 0.6270, 0.0352],
[0.0420, 0.4865, 0.7263],
[0.9950, 0.3957, 0.3868],
[0.3802, 0.3337, 0.0465],
[0.0089, 0.7211, 0.1279]])
torch.Size([5, 3])
torch.Size([5, 3])
torch.float32
import torch
x1 = torch.randint(0, 10, (5, 3), dtype=torch.float)
print(x1)
x2 = torch.eye(5, 3)
print(x2)
# 1
print(x1 + x2)
# 2
print(torch.add(x1, x2))
# 3
print(x1.add(x2))
# 4
sum = torch.Tensor(5, 3) # 预先分配空间
torch.add(x1, x2, out=sum) # 加的结果保存在sum里
print(sum)
输出:
tensor([[5., 5., 8.],
[9., 3., 8.],
[9., 8., 3.],
[5., 5., 6.],
[6., 9., 5.]])
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.],
[0., 0., 0.],
[0., 0., 0.]])
tensor([[6., 5., 8.],
[9., 4., 8.],
[9., 8., 4.],
[5., 5., 6.],
[6., 9., 5.]])
tensor([[6., 5., 8.],
[9., 4., 8.],
[9., 8., 4.],
[5., 5., 6.],
[6., 9., 5.]])
tensor([[6., 5., 8.],
[9., 4., 8.],
[9., 8., 4.],
[5., 5., 6.],
[6., 9., 5.]])
tensor([[6., 5., 8.],
[9., 4., 8.],
[9., 8., 4.],
[5., 5., 6.],
[6., 9., 5.]])
这个算是和python的特性一致,不加下划线的第一种就是普通的加法,不会改变tensor1的内容。而加了下划线,即第二种,加的结果会赋值给tensor1。
例子:
print(x1.add(x2))
print(x1)
print(x1.add_(x2))
print(x1)
输出:
tensor([[5., 8., 9.],
[2., 4., 3.],
[6., 2., 9.],
[3., 1., 3.],
[8., 5., 9.]])
tensor([[4., 8., 9.],
[2., 3., 3.],
[6., 2., 8.],
[3., 1., 3.],
[8., 5., 9.]])
tensor([[5., 8., 9.],
[2., 4., 3.],
[6., 2., 9.],
[3., 1., 3.],
[8., 5., 9.]])
tensor([[5., 8., 9.],
[2., 4., 3.],
[6., 2., 9.],
[3., 1., 3.],
[8., 5., 9.]])
其他的减法,乘法,除法均可使用:-,*,/。同时也可以使用
torch.sub()
减法torch.mul()
乘法,只是简单的对应位置相乘,不是矩阵的乘法torch.div()
除法,对应位置相除或者
tensor1.sub(tensor2)
tensor1.mul(tensor2)
tensor.div(tensor2)
其中,如果被除数为0,会出现这样的结果:
tensor([[9., inf, inf],
[inf, 6., inf],
[inf, inf, 0.],
[nan, inf, inf],
[inf, inf, inf]])
上面的三种运算中torch.sub()
这种也都可以添加out=
.
此外,还有很多其他的运算,基本都比较简单。
RuntimeError: expected backend CPU and dtype Float but got backend CPU and dtype Long
这种情况,根据提示信息就是数据的类型不对。
一般可以在创建tensor的时候添加dtype=torch.float
类似的语句来改。也可以使用tensor1.float()
来修改
查看tensor的数据类型时:使用tensor1.dtype
文章浏览阅读5.2k次,点赞2次,收藏24次。HTML网页自动刷新方法在进行HTML网页编写的过程中,往往需要及时的编写反馈,反复的在编辑器和浏览器窗之间进行切换是一个很繁琐的事情,如果在编辑器上使用插件,我也不太愿意,繁琐而且不安全。下面介绍三种不需要插件的自动刷新方法。第一种使用meta标签在html的文档头中,在<head>标签中添加meta属性,像下面“ <meta http-equiv="refresh" content="10">其中前面是刷新所需的meta标识,后面的content是刷新的秒数。第二_html多线程刷新网页
文章浏览阅读726次。背景:当在写一个Spring项目时,配置了事件监听器,在项目启动的时候会自己启动一次,之后如果再调用Controller接口的时候,他还会自己再启动,这个时候我们的业务场景只需要启动一次该怎么办呢?解答: @EventListener public void listener1(ApplicationEvent event) { //解决controller调用接口的时候多次启动event实例 ContextRefreshedEvent event1 = _@eventlistener 被启动了2次
文章浏览阅读5k次。用python可以很小巧轻便的实时监控日志文件增量刷新,根据某些关键字进行匹配,方便做运维异常告警代码import timeimport re# specify log file pathlog_path = "my.log"# open file and monitor newst linenumber = 0position = 0with open(log_path,..._linux实时监控日志 python
文章浏览阅读1.4w次,点赞12次,收藏61次。svn的分支管理_svn 分支
文章浏览阅读4.2w次,点赞329次,收藏2.7k次。Python 入门的60个基础练习_练习python基础语法
文章浏览阅读1w次。用Xcode5运行一下应用,第一个看到的就是status bar的变化。在iOS6中,status bar是系统在处理,应用_ios7 statusbar
文章浏览阅读309次。1.关闭防火墙systemctlstopfirewalld(暂时关闭,重启后恢复)修改前------将index.html改为hello.html。2.设置宽容模式setenforce0(暂时性设置)配置同一ip不同端口的虚拟主机访问apache服务器。(1)安装httpd(先挂载,用yum安装)(4)修改配置文件httpd.html。(3)创建文件hello.html。(2)启动httpd服务。(5)重启httpd服务。(5)重启httpd服务。(5)重启httpd服务。..._虚拟机安装开启httpd服务
文章浏览阅读73次。记录一些软件测试工作中的想法。1)软件测试岗位价值在IT行业里是比较低的,为了更好的体现自身价值,我认为软件测试从业者应该比业务更懂技术,比技术更懂业务。同时具备很好的沟通协调能力。让自己变成万能胶。2)软件测试体现价值的点:1.发现bug2.提供信心3.提供信息4.预防缺陷3)软件测试一定要考虑测试的覆盖率4)测试工作要体现测试技术,而不是测试工..._软件测试随笔
文章浏览阅读1.6w次,点赞17次,收藏47次。VB程序设计教程(第四版) 龚沛曾 课后习题答案目录第一章课后答案第二章课后答案第三章课后答案第四章课后答案第五章课后答案第六章课后答案第七章课后答案第八章课后答案....第一章课后答案1、简述Visual Basic的特点。答:(1)具有基于对象的可视化设计工具(2)事件驱动的编程机制(3)提供易学易用的应用程序集成开发环境(4..._龚沛曾vb.pdf
文章浏览阅读452次。曰髯?分类号:论文编号:2丛坦丝旦生丛密级:公开贵州大学2009届硕士研究生学位论文基于即神经网络的语音增强研究学科专业:电路与系统研究方向:模式识别导师:刘宇红教授研究生:周元芬中国贵州’贵阳2009年5月摘要-5Abstract-6第一章绪言11.1引言11.2语音增强的意义与研究现状21.2.1语音增强的目的21.2.2语音增强的意义21.2.3国内外语音增强研究现状41.2.4语音增强的算..._基于神经网络的语音增强
文章浏览阅读1.7w次。问题描述: 在使用maven依赖项spring MVC、Hibernate和Struts2构建REST web项目之后,我将项目进行打包 “ProcjectName.war”,并通过tomcat manager html页面将其上传到 Linux 的tomcat 服务器,它可以正常工作。但是当我尝试在本地tomcat服务器 (Windows平台)上部署war时,我从tomcat catali..._org.springframework.core.io.resource
文章浏览阅读9.7k次。1.问题展示2.解决方案1.任意窗口, 打开git bash2.命令行界面, 输入cd C:3.cat ~/.ssh/id_rsa.pub正常下面应该显示一大串公钥如果没有,显示如下图, 则进行下一步, 创建公钥4.创建公钥, 输入 ssh-keygen5.然后一直下一步, 直到出现6.再次输入cat ~/.ssh/id_rsa.pub下面一大串数字便是公钥,复制这些字符串, 打开github, 点击头像, 打开settings, 打开SSH and GPG Keys_cmd warning: permanently added