当前位置:网站首页>Tensor and numpy convert "suggested collection" to each other
Tensor and numpy convert "suggested collection" to each other
2022-07-24 11:22:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
Tensor and NumPy transformation
We're easy to use
numpy()andfrom_numpy()takeTensor andNumPyArrays in convert to each other . But one thing to note is : Generated by these two functionsTensorandNumPyArrays in share the same memory ( So the transition between them is very fast ), When you change one of them, the other will change !
1. Tensor turn NumPy
a = torch.ones(6)
b = a.numpy()
print(a, b)
a += 1
print(a, b)
b += 1
print(a, b)tensor([1., 1., 1., 1., 1., 1.]) [1. 1. 1. 1. 1. 1.]
tensor([2., 2., 2., 2., 2., 2.]) [2. 2. 2. 2. 2. 2.]
tensor([3., 3., 3., 3., 3., 3.]) [3. 3. 3. 3. 3. 3.]2. NumPy Array rotation Tensor
import numpy as np
a = np.ones(7)
b = torch.from_numpy(a)
print(a, b)
a += 1
print(a, b)
b += 1
print(a, b)[1. 1. 1. 1. 1. 1. 1.] tensor([1., 1., 1., 1., 1., 1., 1.], dtype=torch.float64)
[2. 2. 2. 2. 2. 2. 2.] tensor([2., 2., 2., 2., 2., 2., 2.], dtype=torch.float64)
[3. 3. 3. 3. 3. 3. 3.] tensor([3., 3., 3., 3., 3., 3., 3.], dtype=torch.float64)3. torch.tensor() take NumPy Array to Tensor
Direct use torch.tensor() take NumPy Array to Tensor, This method always copies the data , Back to Tensor No longer share memory with the original data .
import numpy as np
a = np.ones((2,3))
c = torch.tensor(a)
a += 1
print('a:',a)
print('c:',c)
print(id(a)==id(c))a: [[2. 2. 2.]
[2. 2. 2.]]
c: tensor([[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
FalsePublisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/125276.html Link to the original text :https://javaforall.cn
边栏推荐
- Ctfshow ThinkPHP topic 1
- 简单使用 MySQL 索引
- How to go from functional testing to automated testing?
- Why can't memset initialize array elements to 1?
- 只会“点点点”,凭什么让开发看得起你?
- Installing Oracle Xe with Linux
- Robot Framework官方教程(一)入门
- [golang] golang implements the string interception function substr
- Fastcgi operation principle and PHP FPM parameter configuration
- 系统管理员需知的 16 个 iptables 使用技巧
猜你喜欢
![[deserialization vulnerability-02] principle test and magic method summary of PHP deserialization vulnerability](/img/03/f80c82d009d21a938911a155dddf6b.png)
[deserialization vulnerability-02] principle test and magic method summary of PHP deserialization vulnerability

【C】 Understanding C language variable scope and life cycle from memory

Jmeter-Runtime控制器

性能测试总结(一)---基础理论篇

网络爬虫之短信验证
![08 [AIO programming]](/img/a6/156cb97e653190c76f22c88b758fef.png)
08 [AIO programming]

关于【软件测试-自动化测试之面试技巧和注意事项】——侃侃而谈

Redis 100 million level data storage scheme hash slot partition

cgo+gSoap+onvif学习总结:9、go和c进行socket通信进行onvif协议处理

Docker builds MySQL master-slave replication
随机推荐
Ask n! How many zeros are there behind
In idea, system.getproperty ("user.dir") identifies the path of the module: the setting of the working directory
Imeta view | is short reading long amplicon sequencing applicable to the prediction of microbiome function?
Publish local images to Alibaba cloud
Use Modelsim to independently simulate Altera and Xilinx IP cores
Simply understand MODBUS function code and partition
How to convert word to markdown text
Introduction to kubernetes Basics
[golang] golang implements sha256 encryption function
Sorting out the ideas of data processing received by TCP server, and the note of select: invalid argument error
关于【软件测试-自动化测试之面试技巧和注意事项】——侃侃而谈
RetinaNet:Focal Loss for Dense Object Detection
Ctfshow ThinkPHP topic 1
UNIX C language POSIX mutex thread synchronization
Video playback | how to become an excellent reviewer of international journals in the field of Geoscience and ecology?
16 tips for system administrators to use iptables
系统管理员需知的 16 个 iptables 使用技巧
CSDN会员的魅力何在?我要他有什么用?
This is the right way for developers to open artifact!
FastCGI运行原理及php-fpm参数配置