Pytorch的tensor数据类型
基本类型
torch.Tensor是一种包含单一数据类型元素的多维矩阵。
Torch定义了七种CPU tensor类型和八种GPU tensor类型:
| Data tyoe | CPU tensor | GPU tensor | 
|---|---|---|
| 32-bit floating point | torch.FloatTensor | torch.cuda.FloatTensor | 
| 64-bit floating point | torch.DoubleTensor | torch.cuda.DoubleTensor | 
| 16-bit floating point | N/A | torch.cuda.HalfTensor | 
| 8-bit integer (unsigned) | torch.ByteTensor | torch.cuda.ByteTensor | 
| 8-bit integer (signed) | torch.CharTensor | torch.cuda.CharTensor | 
| 16-bit integer (signed) | torch.ShortTensor | torch.cuda.ShortTensor | 
| 32-bit integer (signed) | torch.IntTensor | torch.cuda.IntTensor | 
| 64-bit integer (signed) | torch.LongTensor | torch.cuda.LongTensor | 
torch.DoubleTensor(2, 2) 构建一个22 Double类型的张量
torch.ByteTensor(2, 2) 构建一个22 Byte类型的张量
torch.CharTensor(2, 2) 构建一个22 Char类型的张量
torch.ShortTensor(2, 2) 构建一个22 Short类型的张量
torch.IntTensor(2, 2) 构建一个22 Int类型的张量
torch.LongTensor(2, 2) 构建一个22 Long类型的张量
类型转换
2.1 CPU和GPU的Tensor之间转换
从cpu –> gpu,使用data.cuda()即可。
若从gpu –> cpu,则使用data.cpu()。
2.2 Tensor与Numpy Array之间的转换
Tensor –> Numpy.ndarray 可以使用 data.numpy(),其中data的类型为torch.Tensor。
Numpy.ndarray –> Tensor 可以使用torch.from_numpy(data),其中data的类型为numpy.ndarray。
2.3 Tensor的基本类型转换(也就是float转double,转byte这种。)
为了方便测试,我们构建一个新的张量,你要转变成不同的类型只需要根据自己的需求选择即可
- tensor = torch.Tensor(2, 5) 
- torch.long() 将tensor投射为long类型 
 newtensor = tensor.long()
- torch.half()将tensor投射为半精度浮点(16位浮点)类型 
 newtensor = tensor.half()
- torch.int()将该tensor投射为int类型 
 newtensor = tensor.int()
- torch.double()将该tensor投射为double类型
 newtensor = tensor.double()
- torch.float()将该tensor投射为float类型 
 newtensor = tensor.float()
- torch.char()将该tensor投射为char类型 
 newtensor = tensor.char()
- torch.byte()将该tensor投射为byte类型 
 newtensor = tensor.byte()
- torch.short()将该tensor投射为short类型 
 newtensor = tensor.short()
如果当你需要提高精度,比如说想把模型从float变为double。那么可以将要训练的模型设置为model = model.double()。此外,还要对所有的张量进行设置:pytorch.set_default_tensor_type('torch.DoubleTensor'),不过double比float要慢很多,要结合实际情况进行思考。
Pytorch的tensor数据类型的更多相关文章
- pytorch  中的数据类型,tensor的创建
		pytorch中的数据类型 import torch a=torch.randn(2,3) b=a.type() print(b) #检验是否是该数据类型 print(isinstance(a,tor ... 
- 对pytorch中Tensor的剖析
		不是python层面Tensor的剖析,是C层面的剖析. 看pytorch下lib库中的TH好一阵子了,TH也是torch7下面的一个重要的库. 可以在torch的github上看到相关文档.看了半天 ... 
- [Pytorch]Pytorch的tensor变量类型转换
		原文:https://blog.csdn.net/hustchenze/article/details/79154139 Pytorch的数据类型为各式各样的Tensor,Tensor可以理解为高维矩 ... 
- pytorch之Tensor
		#tensor和numpy import torch import numpy as np numpy_tensor = np.random.randn(3,4) print(numpy_tensor ... 
- Pytorch的基础数据类型
		引言 本篇介绍Pytorch的基础数据类型,判断方式以及常用向量 基础数据类型 torch.Tensor是一种包含单一数据类型元素的多维矩阵. 目前在1.2版本中有9种类型. 同python相比,py ... 
- pytorch中tensor张量数据基础入门
		pytorch张量数据类型入门1.对于pytorch的深度学习框架,其基本的数据类型属于张量数据类型,即Tensor数据类型,对于python里面的int,float,int array,flaot ... 
- Tensor数据类型
		目录 Tensor数据类型 属性 数据类型判断 数据类型转换 tensor转numpy Tensor数据类型 list: [1,1.2,'hello'] ,存储图片占用内存非常大 np.array, ... 
- pytorch中tensor数据和numpy数据转换中注意的一个问题
		转载自:(pytorch中tensor数据和numpy数据转换中注意的一个问题)[https://blog.csdn.net/nihate/article/details/82791277] 在pyt ... 
- [Pytorch]Pytorch中tensor常用语法
		原文地址:https://zhuanlan.zhihu.com/p/31494491 上次我总结了在PyTorch中建立随机数Tensor的多种方法的区别. 这次我把常用的Tensor的数学运算总结到 ... 
随机推荐
- 微信小程序——表单验证插件WxValidate的二次封装(终极版)
			微信小程序表单验证前面的两篇文章做的效果总感觉都有点不太友好,第一篇里的效果是将错误信息通过对话框形式弹出来,这种形式在web形式下早已经淘汰了:第二篇是一次性全部显示所有的错误,然后3秒后自动消失, ... 
- deducmsV5.7 在{dede:datalist}标签中runphp无效的解决办法
			问题: 后台数据是dede:datalist标签展示中,中间有isshow - 是否展示的字段,数据库里存的是0/1:我本来想用{dede:field.isshow runphp='yes'}来着,可 ... 
- 使用动态SQL处理table_name作为输入参数的存储过程(MySQL)
			关于mysql如何创建和使用存储过程,参考笔记<MySQL存储过程和函数创建>以及官网:https://dev.mysql.com/doc/refman/5.7/en/create-pro ... 
- bayaim——达梦数据库 导入导出
			导出: E:\dmdbms\bin\dexp.exe """SYSDBA"""/"""******" ... 
- 2019年全国高校计算机能力挑战赛初赛C语言解答
			http://www.ncccu.org.cn 2019年全国高校计算机能力挑战赛分设大数据算法赛,人工智能算法赛,Office高级应用赛,程序设计赛4大赛项 C语言初赛解答 1:编程1 16.现有一 ... 
- Microsemi Libero系列教程(一)——Libero开发环境介绍、下载、安装与注册
			前言 相比与Xilinx和Altera在国内的市场,Microsemi的FPGA在国内应用很少很少,网上几乎没有详细的教程,刚开始使用时,遇到了各种问题,自己也走了不少弯路.本系列教程以Libero ... 
- PAT 1008  Elevator 数学
			The highest building in our city has only one elevator. A request list is made up with N positive nu ... 
- Linux系统彻底卸载MySQL数据库
			一.首先查询系统是否安装了MySQL rpm -qa | grep -i mysql 输出结果表示,我安装的MySQL Server,Client都是5.6.44的,因为我系统支持的版本是要5.7+的 ... 
- Spring Boot 2 快速教程:WebFlux 快速入门(二)
			摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 02:WebFlux 快速入门实践 文章工程: JDK 1.8 ... 
- JS基础语法---函数作为参数使用---回调函数
			1. 函数可以作为参数使用, 如果一个函数作为参数, 那么我们说这个参数(函数)可以叫回调函数 2. 只要是看到一个函数作为参数使用了, 那就是回调函数 function sayHi(fn) { co ... 
