当前位置:网站首页>pytoch总结—TENSOR 和NUMPY相互转换
pytoch总结—TENSOR 和NUMPY相互转换
2022-06-29 08:31:00 【TJMtaotao】
我们很容易用 numpy() 和 from_numpy() 将 Tensor 和NumPy中的数组相互转换。但是需要注意的⼀一
点是: 这两个函数所产⽣生的的 Tensor 和NumPy中的数组共享相同的内存(所以他们之间的转换很
快),改变其中一个时另⼀个也会改变!!!
还有一个常用的将NumPy中的array转换成 Tensor 的方法就是 torch.tensor() , 需要注意的
是,此方法总是会进行行数据拷贝(就会消耗更多的时间和空间),所以返回的 Tensor 和原来的数
据不再共享内存。
Tensor 转NumPy
a = torch.ones(5)
b = a.numpy()
print(a, b)
NumPy数组转 Tensor
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
print(a, b)
所有在CPU上的 Tensor (除了 CharTensor )都⽀持与NumPy数组相互转换。
此外上面提到还有一个常⽤的方法就是直接用 torch.tensor() 将NumPy数组转换成 Tensor ,需要
注意的是该方法总是会进行行数据拷贝,返回的 Tensor 和原来的数据不再共享内存。
边栏推荐
猜你喜欢

2022年7月(软考高级)信息系统项目管理师认证招生简章

微信小程序wx.navigateBack返回上一页携带参数

mysql insert 时出现Deadlock死锁场景分析

什么是超融合?与传统架构有什么区别?.

Cdga | what is the core of digital transformation in the transportation industry?

Unity C# 网络学习(十二)——Protobuf生成协议

一般乘法器设计,verilog code

记一些笔试题

Detecting and counting tiny faces

First electric shock, so you are such a dragon lizard community | dragon lizard developer said that issue 8
随机推荐
Open3D 隐藏点移除
微信小程序最新canvas2d手写签名
Debug H5 page -vconsole
Working for many years, recalling life -- three years in high school
Member inner class, static inner class, local inner class
Using scan in redistemplate
成员内部类、静态内部类、局部内部类
Official reply on issues related to the change of children's names after parents' divorce
Handwriting Redux thunk
uni-app获取当前页面路由url
MT-yolov6训练及测试
Wechat applet jump to official account image and text content
Training kernel switching using GPU
Open3D 最远点采样(FPS)
jar包和war包
宏,函数和内联函数
使用GPU训练kernel切换
Multiplier design (pipeline) Verilog code
Wechat applet project: tab navigation bar
Verilog equivalent operator