pytorch: tensor类型的构建与相互转换实例


Posted in Python onJuly 26, 2018

Summary

主要包括以下三种途径:

使用独立的函数;

使用torch.type()函数;

使用type_as(tesnor)将张量转换为给定类型的张量。

使用独立函数

import torch

tensor = torch.randn(3, 5)
print(tensor)

# torch.long() 将tensor投射为long类型
long_tensor = tensor.long()
print(long_tensor)

# torch.half()将tensor投射为半精度浮点类型
half_tensor = tensor.half()
print(half_tensor)

# torch.int()将该tensor投射为int类型
int_tensor = tensor.int()
print(int_tensor)

# torch.double()将该tensor投射为double类型
double_tensor = tensor.double()
print(double_tensor)

# torch.float()将该tensor投射为float类型
float_tensor = tensor.float()
print(float_tensor)

# torch.char()将该tensor投射为char类型
char_tensor = tensor.char()
print(char_tensor)

# torch.byte()将该tensor投射为byte类型
byte_tensor = tensor.byte()
print(byte_tensor)

# torch.short()将该tensor投射为short类型
short_tensor = tensor.short()
print(short_tensor)
-0.5841 -1.6370 0.1353 0.6334 -3.0761
-0.2628 0.1245 0.8626 0.4095 -0.3633
 1.3605 0.5055 -2.0090 0.8933 -0.6267
[torch.FloatTensor of size 3x5]


 0 -1 0 0 -3
 0 0 0 0 0
 1 0 -2 0 0
[torch.LongTensor of size 3x5]


-0.5840 -1.6367 0.1353 0.6333 -3.0762
-0.2627 0.1245 0.8628 0.4094 -0.3633
 1.3604 0.5054 -2.0098 0.8936 -0.6265
[torch.HalfTensor of size 3x5]


 0 -1 0 0 -3
 0 0 0 0 0
 1 0 -2 0 0
[torch.IntTensor of size 3x5]


-0.5841 -1.6370 0.1353 0.6334 -3.0761
-0.2628 0.1245 0.8626 0.4095 -0.3633
 1.3605 0.5055 -2.0090 0.8933 -0.6267
[torch.DoubleTensor of size 3x5]


-0.5841 -1.6370 0.1353 0.6334 -3.0761
-0.2628 0.1245 0.8626 0.4095 -0.3633
 1.3605 0.5055 -2.0090 0.8933 -0.6267
[torch.FloatTensor of size 3x5]


 0 -1 0 0 -3
 0 0 0 0 0
 1 0 -2 0 0
[torch.CharTensor of size 3x5]


 0 255 0 0 253
 0 0 0 0 0
 1 0 254 0 0
[torch.ByteTensor of size 3x5]


 0 -1 0 0 -3
 0 0 0 0 0
 1 0 -2 0 0
[torch.ShortTensor of size 3x5]

其中,torch.Tensor、torch.rand、torch.randn 均默认生成 torch.FloatTensor型 :

import torch

tensor = torch.Tensor(3, 5)
assert isinstance(tensor, torch.FloatTensor)

tensor = torch.rand(3, 5)
assert isinstance(tensor, torch.FloatTensor)

tensor = torch.randn(3, 5)
assert isinstance(tensor, torch.FloatTensor)

使用torch.type()函数

type(new_type=None, async=False)
import torch

tensor = torch.randn(3, 5)
print(tensor)

int_tensor = tensor.type(torch.IntTensor)
print(int_tensor)
-0.4449 0.0332 0.5187 0.1271 2.2303
 1.3961 -0.1542 0.8498 -0.3438 -0.2834
-0.5554 0.1684 1.5216 2.4527 0.0379
[torch.FloatTensor of size 3x5]


 0 0 0 0 2
 1 0 0 0 0
 0 0 1 2 0
[torch.IntTensor of size 3x5]

使用type_as(tesnor)将张量转换为给定类型的张量

import torch

tensor_1 = torch.FloatTensor(5)

tensor_2 = torch.IntTensor([10, 20])
tensor_1 = tensor_1.type_as(tensor_2)
assert isinstance(tensor_1, torch.IntTensor)

以上这篇pytorch: tensor类型的构建与相互转换实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python使用xlrd实现检索excel中某列含有指定字符串记录的方法
May 09 Python
python多进程共享变量
Apr 06 Python
浅谈Python浅拷贝、深拷贝及引用机制
Dec 15 Python
使用Python对Excel进行读写操作
Mar 30 Python
git使用.gitignore设置不生效或不起作用问题的解决方法
Jun 01 Python
python opencv 直方图反向投影的方法
Feb 24 Python
使用Python写一个量化股票提醒系统
Aug 22 Python
利用Python正则表达式过滤敏感词的方法
Jan 21 Python
Python3开发实例之非关系型图数据库Neo4j安装方法及Python3连接操作Neo4j方法实例
Mar 18 Python
Windows下PyCharm配置Anaconda环境(超详细教程)
Jul 31 Python
Jupyter Notebook安装及使用方法解析
Nov 12 Python
浅析Python的命名空间与作用域
Nov 25 Python
pytorch中tensor的合并与截取方法
Jul 26 #Python
Python爬虫框架Scrapy常用命令总结
Jul 26 #Python
Python退火算法在高次方程的应用
Jul 26 #Python
Python爬虫框架Scrapy基本用法入门教程
Jul 26 #Python
Tensorflow 合并通道及加载子模型的方法
Jul 26 #Python
解决tensorflow测试模型时NotFoundError错误的问题
Jul 26 #Python
tensorflow 恢复指定层与不同层指定不同学习率的方法
Jul 26 #Python
You might like
用PHP制作静态网站的模板框架(四)
2006/10/09 PHP
php基于base64解码图片与加密图片还原实例
2014/11/03 PHP
php 流程控制switch的简单实例
2016/06/07 PHP
Js 获取Gridview选中行的内容操作步骤
2013/02/05 Javascript
jquery 元素控制(追加元素/追加内容)介绍及应用
2013/04/21 Javascript
jquery分页插件jquery.pagination.js使用方法解析
2016/04/01 Javascript
jQuery实现查找最近父节点的方法
2016/06/23 Javascript
清空元素html("") innerHTML="" 与 empty()的区别和应用(推荐)
2017/08/14 Javascript
JavaScript实现快速排序的方法分析
2018/01/10 Javascript
JS实现匀速与减速缓慢运动的动画效果封装示例
2018/08/27 Javascript
angular4强制刷新视图的方法
2018/10/09 Javascript
vue vantUI tab切换时 list组件不触发load事件的问题及解决方法
2020/02/14 Javascript
[15:57]教你分分钟做大人:斧王
2014/10/30 DOTA
[53:10]2018DOTA2亚洲邀请赛 4.6 淘汰赛 VP vs VG 第一场
2018/04/11 DOTA
[03:17]DOTA2-DPC中国联赛1月29日Recap集锦
2021/03/11 DOTA
python设置windows桌面壁纸的实现代码
2013/01/28 Python
python的paramiko模块实现远程控制和传输示例
2017/10/13 Python
详解Python3.6的py文件打包生成exe
2018/07/13 Python
Python unittest 简单实现参数化的方法
2018/11/30 Python
Python3标准库总结
2019/02/19 Python
django的model操作汇整详解
2019/07/26 Python
10 套华丽的CSS3 按钮小结
2012/10/03 HTML / CSS
解决H5的a标签的download属性下载service上的文件出现跨域问题
2019/07/16 HTML / CSS
请写一个C函数,若处理器是Big_endian的,则返回0;若是Little_endian的,则返回1
2015/07/16 面试题
opencv实现图像平移效果
2021/03/24 Python
员工培训心得体会
2013/12/30 职场文书
追悼会子女答谢词
2014/01/28 职场文书
构建和谐校园倡议书
2015/01/19 职场文书
2015年计生工作总结范文
2015/04/24 职场文书
安全承诺书格式范本
2015/04/28 职场文书
2015年“我们的节日·重阳节”活动总结
2015/07/29 职场文书
2016新春团拜会致辞
2015/08/01 职场文书
教师学期述职自我鉴定
2019/08/16 职场文书
QT连接MYSQL数据库的详细步骤
2021/07/07 MySQL
win10系统计算机图标怎么调出来?win10调出计算机图标的方法
2022/08/14 数码科技