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中操作字典之fromkeys()方法的使用
May 21 Python
多版本Python共存的配置方法
May 22 Python
十分钟利用Python制作属于你自己的个性logo
May 07 Python
Python实现手写一个类似django的web框架示例
Jul 20 Python
Selenium元素的常用操作方法分析
Aug 10 Python
Python3 pip3 list 出现 DEPRECATION 警告的解决方法
Feb 16 Python
Python基于scipy实现信号滤波功能
May 08 Python
利用Python库Scapy解析pcap文件的方法
Jul 23 Python
Python的pygame安装教程详解
Feb 10 Python
python百行代码自制电脑端网速悬浮窗的实现
May 12 Python
python urllib库的使用详解
Apr 13 Python
python 网络编程要点总结
Jun 18 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
《魔兽争霸3:重制版》翻车了?你想要的我们都没有
2019/11/07 魔兽争霸
php流量统计功能的实现代码
2012/09/29 PHP
对PHP语言认识上需要避免的10大误区
2014/06/12 PHP
PHP模板引擎Smarty内建函数foreach,foreachelse用法分析
2016/04/11 PHP
PHP实现新型冠状病毒疫情实时图的实例
2020/02/04 PHP
浅谈JavaScript中面向对象技术的模拟
2006/09/25 Javascript
ajaxControlToolkit AutoCompleteExtender的用法
2008/10/30 Javascript
JavaScript中的apply()方法和call()方法使用介绍
2012/07/25 Javascript
jquery Moblie入门—hello world的示例代码学习
2013/01/08 Javascript
js兼容pc端浏览器并有多种弹出小提示的手机端浮层控件实例
2015/04/29 Javascript
Javascript的表单验证-揭开正则表达式的面纱
2016/03/18 Javascript
基于HTML5+JS实现本地图片裁剪并上传功能
2017/03/24 Javascript
JavaScript中闭包的详解
2017/04/01 Javascript
详解vue-router 初始化时做了什么
2018/06/11 Javascript
详解Vue取消eslint语法限制
2018/08/04 Javascript
小程序转发探索示例
2019/02/19 Javascript
[02:44]完美大师赛主赛事淘汰赛第二日观众采访
2017/11/24 DOTA
python高并发异步服务器核心库forkcore使用方法
2013/11/26 Python
Python中pygame安装方法图文详解
2015/11/11 Python
剖析Python的Twisted框架的核心特性
2016/05/25 Python
Python中用psycopg2模块操作PostgreSQL方法
2017/11/28 Python
有关Python的22个编程技巧
2018/08/29 Python
Python FFT合成波形的实例
2019/12/04 Python
浅谈python中频繁的print到底能浪费多长时间
2020/02/21 Python
深入了解canvas在移动端绘制模糊的问题解决
2019/04/30 HTML / CSS
VICHY薇姿英国官网:全球专业敏感肌护肤领先品牌
2017/07/04 全球购物
学校安全责任书
2014/04/14 职场文书
求职信范文大全
2014/05/26 职场文书
承诺书格式
2014/06/03 职场文书
旅游饭店管理专业自荐书
2014/06/28 职场文书
2014年度安全生产目标管理责任书
2014/07/25 职场文书
大一新生期末自我评价
2014/09/12 职场文书
2014统计局民主生活会对照检查材料思想汇报
2014/10/02 职场文书
临时工聘用合同协议书
2014/10/29 职场文书
tensorflow+k-means聚类简单实现猫狗图像分类的方法
2021/04/28 Python
react国际化react-intl的使用
2021/05/06 Javascript