pytorch中tensor张量数据类型的转化方式

作者:|晴天| 时间:2022-03-19 20:07:24 

1.tensor张量与numpy相互转换


tensor ----->numpy

import torch
a=torch.ones([2,5])

tensor([[1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1.]])
# **********************************    
b=a.numpy()

array([[1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1.]], dtype=float32)

numpy ----->tensor

import numpy as np
a=np.ones([2,5])

array([[1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1.]])
# **********************************    
b=torch.from_numpy(a)

tensor([[1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1.]], dtype=torch.float64)

2.tensor张量与list相互转换


tensor—>list

a=torch.ones([1,5])

tensor([[1., 1., 1., 1., 1.]])
# ***********************************
b=a.tolist()

[[1.0, 1.0, 1.0, 1.0, 1.0]]

list—>tensor

a=list(range(1,6))

[1, 2, 3, 4, 5]
# **********************************
b=torch.tensor(a)

tensor([1, 2, 3, 4, 5])

3.tensor张量见类型转换

构建一个新的张量,你要转变成不同的类型只需要根据自己的需求选择即可


tensor = torch.Tensor(3, 5)

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

# torch.half()将tensor投射为半精度浮点类型
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()

4.type_as() 将张量转换成指定类型张量


>>> a=torch.Tensor(2,5)
>>> a
tensor([[1.9431e-19, 4.8613e+30, 1.4603e-19, 2.0704e-19, 4.7429e+30],
   [1.6530e+19, 1.8254e+31, 1.4607e-19, 6.8801e+16, 1.8370e+25]])
>>> b=torch.IntTensor(1,2)
>>> b
tensor([[16843009,    1]], dtype=torch.int32)
>>> a.type_as(b)
tensor([[     0, -2147483648,      0,      0, -2147483648],
   [-2147483648, -2147483648,      0, -2147483648, -2147483648]],
   dtype=torch.int32)
>>> a
tensor([[1.9431e-19, 4.8613e+30, 1.4603e-19, 2.0704e-19, 4.7429e+30],
   [1.6530e+19, 1.8254e+31, 1.4607e-19, 6.8801e+16, 1.8370e+25]])

来源:https://blog.csdn.net/qq_40357974/article/details/101697721

标签:pytorch,tensor,张量,类型转化
0
投稿

猜你喜欢

  • Python中的条件判断语句基础学习教程

    2021-06-19 11:51:36
  • 怎样设计网站首页?(解答)

    2007-11-04 18:56:00
  • 详解MySQL 数据库优化方法

    2010-08-12 14:50:00
  • Python流程控制 while循环实现解析

    2023-02-07 04:16:33
  • ASP中不用模板生成HTML静态页面的方法

    2011-03-06 10:49:00
  • win10下opencv-python特定版本手动安装与pip自动安装教程

    2022-09-29 14:03:02
  • 用ASP在线创建Word与Excel文档

    2008-07-20 19:17:00
  • Python scipy的二维图像卷积运算与图像模糊处理操作示例

    2022-12-13 11:56:41
  • 用 XSLT 把 XML 数据生成柱状图

    2009-05-19 12:46:00
  • Python 利用Entrez库筛选下载PubMed文献摘要的示例

    2021-05-27 11:35:01
  • python pip特殊用法之pip install -v -e .命令详解

    2022-07-02 09:56:43
  • Python代码模拟CPU工作原理

    2023-08-04 15:23:49
  • PHP asXML()函数讲解

    2023-06-08 14:04:37
  • python 计算方位角实例(根据两点的坐标计算)

    2023-08-01 09:30:54
  • 如何实现论坛的树状记录表展开技术?

    2010-05-19 21:37:00
  • Pycharm常用快捷键总结及配置方法

    2023-09-24 11:15:44
  • python3 requests库实现多图片爬取教程

    2023-11-22 15:37:40
  • 利用pyecharts读取csv并进行数据统计可视化的实现

    2023-07-15 07:18:03
  • Django 创建新App及其常用命令的实现方法

    2023-05-20 09:04:47
  • 视觉设计常见误解

    2008-11-13 13:09:00
  • asp之家 网络编程 m.aspxhome.com