Pytorch数据类型转换

载入模块生成数据

import torch
import numpy as np
a_numpy = np.array([1,2,3])

Numpy转换为Tensor

a_tensor = torch.from_numpy(a_numpy)
print(a_tensor)

Tensor转换为Numpy

a_numpy = a_tensor.numpy()
print(a_numpy)

Int, float 转换为tensor

c = torch.tensor(2)
print(c)

tensor 转换为int

c = c.item()
print(c)

Numpy转换为Variable

a_variable = Variable(torch.from_numpy(a_numpy))
print(a_variable)

Variable转换为Numpy

a_numpy = a_variable.data.numpy()
print(a_numpy)

最新文章

  1. 将字母变为其下个字母(abc变为bcd)
  2. 笔记本_Lenovo_G480
  3. oracle初次使用连接不上
  4. 【打表】HDOJ-2089-不要62
  5. 通过javascript实现页面的横竖屏固定
  6. C#对数字添加逗号,千分位
  7. PAT (Advanced Level) 1073. Scientific Notation (20)
  8. 24、手把手教你Extjs5(二十四)模块Form的自定义的设计[3]
  9. Zabbix Agent for Windows部署(五)
  10. halcon开发必读
  11. 异步任务神器 Celery-入门
  12. Java 实现异步调用
  13. Linux 安装 ffmpeg
  14. aruba 802.11ac协议
  15. windows下搭建permeate漏洞测试系统实战
  16. Java开发新手经常遇到的一些问题
  17. 3PC
  18. 通过管道传输快速将MySQL的数据导入Redis
  19. Hadoop(hadoop,HBase)组件import到eclipse
  20. 1.1(Spring学习笔记)Spring-事务基础

热门文章

  1. Java13 闪亮来袭,你是否还停留在 Java8
  2. 当接口请求体里的日期格式跟web页面日期格式不一致时,该如何处理呢?
  3. Node 之 Express 4x 骨架详解
  4. WEB引入Google思源黑体
  5. java基础(17):包装类、System、Math、Arrays、大数据运算
  6. java基础(7):自定义类、ArrayList集合
  7. DevExpress的下拉框控件ComboBoxEdit控件的使用
  8. 敏捷软件开发_设计原则<三>
  9. js 时分秒转化为秒
  10. EGit(Git Eclipse Plugin)使用