当前位置:网站首页>[pytorch] 1.7 pytorch and numpy, tensor and array conversion
[pytorch] 1.7 pytorch and numpy, tensor and array conversion
2022-07-31 16:25:00 【Enzo wants to smash the computer】
1、pytorch与numpy 函数对比
2、tensor与ndarray的转换
torch.Tensor(array)torch.tensor(array)torch.as_tensor(array)torch.from_array(array)
import torch
import numpy as np
array = np.array([1,2,3,4])
print(array.dtype) #int64
# 用4种方式 将numpy array转换为torch tensor
tensor = torch.tensor(array)
Tensor = torch.Tensor(array)
as_tensor = torch.as_tensor(array)
from_array = torch.from_numpy(array)
# ====== 对比几种不同方法之间的异同 =========
print(tensor.dtype) #torch.int64
print(Tensor.dtype) #torch.float32
print(as_tensor.dtype) #torch.int64
print(from_array.dtype) #torch.int64
# ========== Determine whether to share memory ===========
#修改数组的值
array[0] = 10
print(tensor) #tensor([1, 2, 3, 4])
print(Tensor) #tensor([1., 2., 3., 4.])
print(as_tensor) #tensor([10, 2, 3, 4])
print(from_array) #tensor([10, 2, 3, 4])
结论:
1)torch.Tensor() 和 torch.tensor() 在转换的时候,Is to open up a new memory space to storetensor的.
- torch.Tensor 在生成tensor的时候,默认生成 float数据类型
- torch.tensor It is based on the situation to infer what kind of data type for longevity.
2)torch.as_tensor() 和 torch.from_array() 转换的tensor,和 numpy array 是共享内存的;So the data must be the same.
边栏推荐
- .NET 20th Anniversary Interview - Zhang Shanyou: How .NET technology empowers and changes the world
- Implement anti-shake and throttling functions
- The use of border controls
- Browser's built-in color picker
- 软件实现AT命令操作过程
- 6. 使用 Postman 工具高效管理和测试 SAP ABAP OData 服务
- i.MX6ULL driver development | 33 - NXP original network device driver reading (LAN8720 PHY)
- 01 Encounter typescript, build environment
- 牛客网刷题(三)
- LeetCode_733_图像渲染
猜你喜欢

Qt实战案例(54)——利用QPixmap设计图片透明度

Graham's Scan method for solving convex hull problems

C language "the third is" upgrade (mode selection + AI chess)

宁波大学NBU IT项目管理期末考试知识点整理

gerrit中如何切换远程服务器

【7.29】Code Source - 【Arrangement】【Stone Game II】【Cow and Snacks】【Minimum Number of Spawns】【Sequence】

6-22 Vulnerability exploit - postgresql database password cracking

2022年整理LeetCode最新刷题攻略分享(附中文详细题解)

复杂高维医学数据挖掘与疾病风险分类研究
How Redis handles concurrent access
随机推荐
网站漏洞修复服务商关于越权漏洞分析
Qt practical cases (54) - using transparency QPixmap design pictures
The principle of hough transform detection of straight lines (opencv hough straight line detection)
字符串反转的实现方法总结「建议收藏」
复制延迟案例(1)-最终一致性
贪吃蛇项目(简单)
牛客 HJ20 密码验证合格程序
智能垃圾桶(八)——红外对管传感器(树莓派pico)
ASP.NET Core generates continuous Guid
"Autumn Recruitment Series" MySQL Interview Core 25 Questions (with answers)
Foreign media right, apple on May be true in inventory
Summary of the implementation method of string inversion "recommended collection"
使用互相关进行音频对齐
mysql black window ~ build database and build table
How Redis handles concurrent access
Handling write conflicts under multi-master replication (4) - multi-master replication topology
The 2nd China PWA Developer Day
全新宝马3系上市,安全、舒适一个不落
数据表插入数据insert into
update data table update