当前位置:网站首页>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
边栏推荐
- Docker installs 3 master and 3 slave redis clusters
- 运算放大器 —— 快速复苏笔记[贰](应用篇)
- 【反序列化漏洞-02】PHP反序列化漏洞原理测试及魔术方法总结
- MySQL paging
- FastCGI运行原理及php-fpm参数配置
- 这个应该是全网最全的接口测试工具之postman
- Fastcgi operation principle and PHP FPM parameter configuration
- PIP update command
- Blue Bridge Cup provincial match training camp - Calculation of date
- Paging query of employee information of black maredge takeout
猜你喜欢

Xilinx FPGA Microblaze AXI_ IIC usage and experience

Fiddler抓包工具总结

JMeter if controller

Reprint of illustrations in nature, issue 3 - area map (part2-100)

如何从功能测试到自动化测试?

Docker installs 3 master and 3 slave redis clusters

Conversion between hexadecimal string and byte array

Stm32+esp8266+mqtt protocol connects Alibaba cloud Internet of things platform

Redis 100 million level data storage scheme hash slot partition

JMeter接口测试步骤-安装教程-脚本录制-并发测试
随机推荐
Idea hidden Idea folder hides.Iml files
Druid encryption command
Nodejs CTF Foundation
[golang] golang realizes sending wechat service number template messages
Xilinx FPGA Microblaze AXI_ IIC usage and experience
Online customer service chat system source code_ Beautiful and powerful golang kernel development_ Binary operation fool installation_ Construction tutorial attached
视频回放 | 如何成为一名优秀的地学和生态学领域的国际期刊审稿人?
Idea background image set
Leetcode 112. path sum
【Markdown语法高级】让你的博客更精彩(四:设置字体样式以及颜色对照表)
Ldr6028 charging OTG live line live sound card audio adapter is the most cost-effective solution
How to use SSH and SFTP protocols at home
Use Modelsim to independently simulate Altera and Xilinx IP cores
RetinaNet:Focal Loss for Dense Object Detection
This is the right way for developers to open artifact!
《Nature》论文插图复刻第3期—面积图(Part2-100)
Simply understand MODBUS function code and partition
Zynq TTC usage
Jmeter-Runtime控制器
【10】团队协作和跨团队协作